Skip to content

Commit 5c6a8ed

Browse files
feat: Add unit tests for getDefaultHostingSite and getProjectNumber
Adds unit tests for the `getDefaultHostingSite` and `getProjectNumber` functions. The tests cover various scenarios, including: - Retrieving the default hosting site from project resources. - Falling back to `listSites` when the default site is not in project resources. - Handling cases where no default site is found. - Retrieving the project number from options. - Fetching the project number when it's not available in options.
1 parent 49bd4ef commit 5c6a8ed

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

src/getDefaultHostingSite.spec.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { expect } from "chai";
2+
import * as sinon from "sinon";
3+
4+
import { getDefaultHostingSite, errNoDefaultSite } from "./getDefaultHostingSite";
5+
import * as projectUtils from "./projectUtils";
6+
import * as projects from "./management/projects";
7+
import * as hostingApi from "./hosting/api";
8+
import { SiteType } from "./hosting/api";
9+
10+
const PROJECT_ID = "test-project-id";
11+
12+
describe("getDefaultHostingSite", () => {
13+
let sandbox: sinon.SinonSandbox;
14+
let getFirebaseProjectStub: sinon.SinonStub;
15+
let listSitesStub: sinon.SinonStub;
16+
17+
beforeEach(() => {
18+
sandbox = sinon.createSandbox();
19+
sandbox.stub(projectUtils, "needProjectId").returns(PROJECT_ID);
20+
getFirebaseProjectStub = sandbox.stub(projects, "getFirebaseProject");
21+
listSitesStub = sandbox.stub(hostingApi, "listSites");
22+
});
23+
24+
afterEach(() => {
25+
sandbox.restore();
26+
});
27+
28+
it("should return the default hosting site from project resources", async () => {
29+
const defaultSite = "my-default-site";
30+
getFirebaseProjectStub.resolves({
31+
resources: { hostingSite: defaultSite },
32+
});
33+
34+
const site = await getDefaultHostingSite({ projectId: PROJECT_ID });
35+
36+
expect(site).to.equal(defaultSite);
37+
expect(getFirebaseProjectStub).to.have.been.calledWith(PROJECT_ID);
38+
expect(listSitesStub).to.not.have.been.called;
39+
});
40+
41+
it("should return the default hosting site from listSites if not in project resources", async () => {
42+
const defaultSite = "another-default-site";
43+
getFirebaseProjectStub.resolves({ resources: {} });
44+
listSitesStub.resolves([
45+
{ name: `projects/${PROJECT_ID}/sites/other-site`, type: SiteType.USER_SITE },
46+
{ name: `projects/${PROJECT_ID}/sites/${defaultSite}`, type: SiteType.DEFAULT_SITE },
47+
]);
48+
49+
const site = await getDefaultHostingSite({ projectId: PROJECT_ID });
50+
51+
expect(site).to.equal(defaultSite);
52+
expect(getFirebaseProjectStub).to.have.been.calledWith(PROJECT_ID);
53+
expect(listSitesStub).to.have.been.calledWith(PROJECT_ID);
54+
});
55+
56+
it("should throw an error if no default site is found", async () => {
57+
getFirebaseProjectStub.resolves({ resources: {} });
58+
listSitesStub.resolves([
59+
{ name: `projects/${PROJECT_ID}/sites/other-site`, type: SiteType.USER_SITE },
60+
]);
61+
62+
await expect(getDefaultHostingSite({ projectId: PROJECT_ID })).to.be.rejectedWith(
63+
errNoDefaultSite,
64+
);
65+
66+
expect(getFirebaseProjectStub).to.have.been.calledWith(PROJECT_ID);
67+
expect(listSitesStub).to.have.been.calledWith(PROJECT_ID);
68+
});
69+
70+
it("should throw an error if listSites returns no sites", async () => {
71+
getFirebaseProjectStub.resolves({ resources: {} });
72+
listSitesStub.resolves([]);
73+
74+
await expect(getDefaultHostingSite({ projectId: PROJECT_ID })).to.be.rejectedWith(
75+
errNoDefaultSite,
76+
);
77+
});
78+
});

src/getProjectNumber.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { expect } from "chai";
2+
import * as sinon from "sinon";
3+
4+
import { getProjectNumber } from "./getProjectNumber";
5+
import * as projectUtils from "./projectUtils";
6+
import * as projects from "./management/projects";
7+
8+
const PROJECT_ID = "test-project-id";
9+
const PROJECT_NUMBER = "123456789";
10+
11+
describe("getProjectNumber", () => {
12+
let sandbox: sinon.SinonSandbox;
13+
let needProjectIdStub: sinon.SinonStub;
14+
let getProjectStub: sinon.SinonStub;
15+
16+
beforeEach(() => {
17+
sandbox = sinon.createSandbox();
18+
needProjectIdStub = sandbox.stub(projectUtils, "needProjectId").returns(PROJECT_ID);
19+
getProjectStub = sandbox.stub(projects, "getProject");
20+
});
21+
22+
afterEach(() => {
23+
sandbox.restore();
24+
});
25+
26+
it("should return project number from options if it exists", async () => {
27+
const options = { projectNumber: PROJECT_NUMBER };
28+
const projectNumber = await getProjectNumber(options);
29+
30+
expect(projectNumber).to.equal(PROJECT_NUMBER);
31+
expect(needProjectIdStub).to.not.have.been.called;
32+
expect(getProjectStub).to.not.have.been.called;
33+
});
34+
35+
it("should fetch project number if not in options", async () => {
36+
const options: any = { projectId: PROJECT_ID };
37+
getProjectStub.resolves({ projectNumber: PROJECT_NUMBER });
38+
39+
const projectNumber = await getProjectNumber(options);
40+
41+
expect(projectNumber).to.equal(PROJECT_NUMBER);
42+
expect(needProjectIdStub).to.have.been.calledWith(options);
43+
expect(getProjectStub).to.have.been.calledWith(PROJECT_ID);
44+
expect(options.projectNumber).to.equal(PROJECT_NUMBER);
45+
});
46+
});

0 commit comments

Comments
 (0)