Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/getDefaultHostingSite.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { expect } from "chai";
import * as sinon from "sinon";

import { getDefaultHostingSite, errNoDefaultSite } from "./getDefaultHostingSite";
import * as projectUtils from "./projectUtils";
import * as projects from "./management/projects";
import * as hostingApi from "./hosting/api";
import { SiteType } from "./hosting/api";

const PROJECT_ID = "test-project-id";

describe("getDefaultHostingSite", () => {
let sandbox: sinon.SinonSandbox;
let getFirebaseProjectStub: sinon.SinonStub;
let listSitesStub: sinon.SinonStub;

beforeEach(() => {
sandbox = sinon.createSandbox();
sandbox.stub(projectUtils, "needProjectId").returns(PROJECT_ID);
getFirebaseProjectStub = sandbox.stub(projects, "getFirebaseProject");
listSitesStub = sandbox.stub(hostingApi, "listSites");
});

afterEach(() => {
sandbox.restore();
});

it("should return the default hosting site from project resources", async () => {
const defaultSite = "my-default-site";
getFirebaseProjectStub.resolves({
resources: { hostingSite: defaultSite },
});

const site = await getDefaultHostingSite({ projectId: PROJECT_ID });

expect(site).to.equal(defaultSite);
expect(getFirebaseProjectStub).to.have.been.calledWith(PROJECT_ID);
expect(listSitesStub).to.not.have.been.called;
});

it("should return the default hosting site from listSites if not in project resources", async () => {
const defaultSite = "another-default-site";
getFirebaseProjectStub.resolves({ resources: {} });
listSitesStub.resolves([
{ name: `projects/${PROJECT_ID}/sites/other-site`, type: SiteType.USER_SITE },
{ name: `projects/${PROJECT_ID}/sites/${defaultSite}`, type: SiteType.DEFAULT_SITE },
]);

const site = await getDefaultHostingSite({ projectId: PROJECT_ID });

expect(site).to.equal(defaultSite);
expect(getFirebaseProjectStub).to.have.been.calledWith(PROJECT_ID);
expect(listSitesStub).to.have.been.calledWith(PROJECT_ID);
});

it("should throw an error if no default site is found", async () => {
getFirebaseProjectStub.resolves({ resources: {} });
listSitesStub.resolves([
{ name: `projects/${PROJECT_ID}/sites/other-site`, type: SiteType.USER_SITE },
]);

await expect(getDefaultHostingSite({ projectId: PROJECT_ID })).to.be.rejectedWith(
errNoDefaultSite,
);

expect(getFirebaseProjectStub).to.have.been.calledWith(PROJECT_ID);
expect(listSitesStub).to.have.been.calledWith(PROJECT_ID);
});

it("should throw an error if listSites returns no sites", async () => {
getFirebaseProjectStub.resolves({ resources: {} });
listSitesStub.resolves([]);

await expect(getDefaultHostingSite({ projectId: PROJECT_ID })).to.be.rejectedWith(
errNoDefaultSite,
);
});
Comment on lines +70 to +77
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency with other tests in this suite, it's good practice to also assert that the mocked functions (getFirebaseProjectStub and listSitesStub) were called as expected. This makes the test more thorough by verifying not just the outcome (the thrown error) but also that the function followed the correct execution path to get there.

  it("should throw an error if listSites returns no sites", async () => {
    getFirebaseProjectStub.resolves({ resources: {} });
    listSitesStub.resolves([]);

    await expect(getDefaultHostingSite({ projectId: PROJECT_ID })).to.be.rejectedWith(
      errNoDefaultSite,
    );

    expect(getFirebaseProjectStub).to.have.been.calledWith(PROJECT_ID);
    expect(listSitesStub).to.have.been.calledWith(PROJECT_ID);
  });

});
46 changes: 46 additions & 0 deletions src/getProjectNumber.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { expect } from "chai";
import * as sinon from "sinon";

import { getProjectNumber } from "./getProjectNumber";
import * as projectUtils from "./projectUtils";
import * as projects from "./management/projects";

const PROJECT_ID = "test-project-id";
const PROJECT_NUMBER = "123456789";

describe("getProjectNumber", () => {
let sandbox: sinon.SinonSandbox;
let needProjectIdStub: sinon.SinonStub;
let getProjectStub: sinon.SinonStub;

beforeEach(() => {
sandbox = sinon.createSandbox();
needProjectIdStub = sandbox.stub(projectUtils, "needProjectId").returns(PROJECT_ID);
getProjectStub = sandbox.stub(projects, "getProject");
});

afterEach(() => {
sandbox.restore();
});

it("should return project number from options if it exists", async () => {
const options = { projectNumber: PROJECT_NUMBER };
const projectNumber = await getProjectNumber(options);

expect(projectNumber).to.equal(PROJECT_NUMBER);
expect(needProjectIdStub).to.not.have.been.called;
expect(getProjectStub).to.not.have.been.called;
});

it("should fetch project number if not in options", async () => {
const options: any = { projectId: PROJECT_ID };

Check warning on line 36 in src/getProjectNumber.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
getProjectStub.resolves({ projectNumber: PROJECT_NUMBER });

const projectNumber = await getProjectNumber(options);

expect(projectNumber).to.equal(PROJECT_NUMBER);
expect(needProjectIdStub).to.have.been.calledWith(options);
expect(getProjectStub).to.have.been.calledWith(PROJECT_ID);
expect(options.projectNumber).to.equal(PROJECT_NUMBER);

Check warning on line 44 in src/getProjectNumber.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .projectNumber on an `any` value
});
});
Loading