Skip to content

Commit 5aafe72

Browse files
committed
adding unit tests'
1 parent c1c1c2c commit 5aafe72

File tree

2 files changed

+197
-2
lines changed

2 files changed

+197
-2
lines changed

src/management/studio.spec.ts

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
import * as chai from "chai";
2+
import * as sinon from "sinon";
3+
import * as studio from "./studio";
4+
import * as prompt from "../prompt";
5+
import { configstore } from "../configstore";
6+
import { Client } from "../apiv2";
7+
import * as utils from "../utils";
8+
import { Options } from "../options";
9+
import { Config } from "../config";
10+
import { RC } from "../rc";
11+
import { logger } from "../logger";
12+
13+
const expect = chai.expect;
14+
15+
describe("Studio Management", () => {
16+
let sandbox: sinon.SinonSandbox;
17+
let promptStub: sinon.SinonStub;
18+
let clientRequestStub: sinon.SinonStub;
19+
let utilsStub: sinon.SinonStub;
20+
21+
let testOptions: Options;
22+
23+
beforeEach(() => {
24+
sandbox = sinon.createSandbox();
25+
promptStub = sandbox.stub(prompt, "select");
26+
sandbox.stub(configstore, "get");
27+
sandbox.stub(configstore, "set");
28+
clientRequestStub = sandbox.stub(Client.prototype, "request");
29+
utilsStub = sandbox.stub(utils, "makeActiveProject");
30+
const emptyConfig = new Config("{}", {});
31+
testOptions = {
32+
cwd: "",
33+
configPath: "",
34+
only: "",
35+
except: "",
36+
filteredTargets: [],
37+
force: false,
38+
json: false,
39+
nonInteractive: false,
40+
interactive: false,
41+
debug: false,
42+
config: emptyConfig,
43+
rc: new RC(),
44+
};
45+
});
46+
47+
afterEach(() => {
48+
sandbox.restore();
49+
});
50+
51+
describe("reconcileStudioFirebaseProject", () => {
52+
it("should return active project from config if WORKSPACE_SLUG is not set", async () => {
53+
process.env.WORKSPACE_SLUG = "";
54+
const result = await studio.reconcileStudioFirebaseProject(testOptions, "cli-project");
55+
expect(result).to.equal("cli-project");
56+
expect(clientRequestStub).to.not.have.been.called;
57+
});
58+
59+
it("should return active project from config if getStudioWorkspace fails", async () => {
60+
process.env.WORKSPACE_SLUG = "test-workspace";
61+
clientRequestStub.rejects(new Error("API Error"));
62+
const result = await studio.reconcileStudioFirebaseProject(testOptions, "cli-project");
63+
expect(result).to.equal("cli-project");
64+
});
65+
66+
it("should update studio with CLI project if studio has no project", async () => {
67+
process.env.WORKSPACE_SLUG = "test-workspace";
68+
clientRequestStub
69+
.onFirstCall()
70+
.resolves({ body: { name: "test-workspace", firebaseProjectId: undefined } });
71+
clientRequestStub.onSecondCall().resolves({ body: {} });
72+
73+
const result = await studio.reconcileStudioFirebaseProject(testOptions, "cli-project");
74+
75+
expect(result).to.equal("cli-project");
76+
expect(clientRequestStub).to.have.been.calledTwice;
77+
expect(clientRequestStub.secondCall.args[0].body.firebaseProjectId).to.equal("cli-project");
78+
});
79+
80+
it("should update CLI with studio project if CLI has no project", async () => {
81+
process.env.WORKSPACE_SLUG = "test-workspace";
82+
clientRequestStub.resolves({
83+
body: { name: "test-workspace", firebaseProjectId: "studio-project" },
84+
});
85+
86+
const result = await studio.reconcileStudioFirebaseProject(
87+
{ ...testOptions, projectRoot: "/test" },
88+
undefined,
89+
);
90+
91+
expect(result).to.equal("studio-project");
92+
expect(utilsStub).to.have.been.calledOnceWith("/test", "studio-project");
93+
});
94+
95+
it("should prompt user and update studio if user chooses CLI project", async () => {
96+
process.env.WORKSPACE_SLUG = "test-workspace";
97+
clientRequestStub
98+
.onFirstCall()
99+
.resolves({ body: { name: "test-workspace", firebaseProjectId: "studio-project" } });
100+
clientRequestStub.onSecondCall().resolves({ body: {} });
101+
promptStub.resolves(true);
102+
103+
const result = await studio.reconcileStudioFirebaseProject(testOptions, "cli-project");
104+
105+
expect(result).to.equal("cli-project");
106+
expect(promptStub).to.have.been.calledOnce;
107+
expect(clientRequestStub).to.have.been.calledTwice;
108+
expect(clientRequestStub.secondCall.args[0].body.firebaseProjectId).to.equal("cli-project");
109+
});
110+
111+
it("should prompt user and update CLI if user chooses studio project", async () => {
112+
process.env.WORKSPACE_SLUG = "test-workspace";
113+
clientRequestStub.resolves({
114+
body: { name: "test-workspace", firebaseProjectId: "studio-project" },
115+
});
116+
promptStub.resolves(false);
117+
118+
const result = await studio.reconcileStudioFirebaseProject(
119+
{ ...testOptions, projectRoot: "/test" },
120+
"cli-project",
121+
);
122+
123+
expect(result).to.equal("studio-project");
124+
expect(promptStub).to.have.been.calledOnce;
125+
expect(utilsStub).to.have.been.calledOnceWith("/test", "studio-project");
126+
});
127+
128+
it("should do nothing if projects are the same", async () => {
129+
process.env.WORKSPACE_SLUG = "test-workspace";
130+
clientRequestStub.resolves({
131+
body: { name: "test-workspace", firebaseProjectId: "same-project" },
132+
});
133+
134+
const result = await studio.reconcileStudioFirebaseProject(testOptions, "same-project");
135+
136+
expect(result).to.equal("same-project");
137+
expect(promptStub).to.not.have.been.called;
138+
expect(utilsStub).to.not.have.been.called;
139+
});
140+
141+
it("should do nothing if in non-interactive mode", async () => {
142+
process.env.WORKSPACE_SLUG = "test-workspace";
143+
clientRequestStub.resolves({
144+
body: { name: "test-workspace", firebaseProjectId: "studio-project" },
145+
});
146+
147+
const result = await studio.reconcileStudioFirebaseProject(
148+
{ ...testOptions, nonInteractive: true },
149+
"cli-project",
150+
);
151+
152+
expect(result).to.equal("studio-project");
153+
expect(promptStub).to.not.have.been.called;
154+
expect(utilsStub).to.not.have.been.called;
155+
});
156+
});
157+
158+
describe("updateStudioFirebaseProject", () => {
159+
it("should not call api if WORKSPACE_SLUG is not set", async () => {
160+
process.env.WORKSPACE_SLUG = "";
161+
await studio.updateStudioFirebaseProject("new-project");
162+
expect(clientRequestStub).to.not.have.been.called;
163+
});
164+
165+
it("should call api to update project id", async () => {
166+
process.env.WORKSPACE_SLUG = "test-workspace";
167+
clientRequestStub.resolves({ body: {} });
168+
169+
await studio.updateStudioFirebaseProject("new-project");
170+
171+
expect(clientRequestStub).to.have.been.calledOnceWith({
172+
method: "PATCH",
173+
path: `/workspaces/test-workspace`,
174+
responseType: "json",
175+
body: {
176+
firebaseProjectId: "new-project",
177+
},
178+
queryParams: {
179+
updateMask: "firebaseProjectId",
180+
},
181+
timeout: 30000,
182+
});
183+
});
184+
185+
it("should log error if api call fails", async () => {
186+
process.env.WORKSPACE_SLUG = "test-workspace";
187+
clientRequestStub.rejects(new Error("API Error"));
188+
const errorLogSpy = sandbox.spy(logger, "warn");
189+
190+
await studio.updateStudioFirebaseProject("new-project");
191+
192+
expect(errorLogSpy).to.have.been.calledOnce;
193+
});
194+
});
195+
});

src/management/studio.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export async function updateStudioFirebaseProject(projectId: string): Promise<vo
142142
if (err.original) {
143143
message += ` (original: ${err.original.message})`;
144144
}
145-
logger.error(
145+
logger.warn(
146146
`Failed to update active Firebase Project for current Studio Workspace: ${message}`,
147147
);
148148
}
@@ -151,7 +151,7 @@ export async function updateStudioFirebaseProject(projectId: string): Promise<vo
151151

152152
/**
153153
* Records the last time we synced the Studio project in Configstore.
154-
* Conviently, this triggers the file watcher.
154+
* This is important to trigger a file watcher in Firebase Studio that keeps the UI in sync.
155155
*/
156156
function recordStudioProjectSyncTime() {
157157
configstore.set("firebaseStudioProjectLastSynced", Date.now());

0 commit comments

Comments
 (0)