forked from firebase/firebase-tools
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchWebSetup.spec.ts
116 lines (91 loc) · 3.24 KB
/
fetchWebSetup.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { expect } from "chai";
import * as nock from "nock";
import * as sinon from "sinon";
import { configstore } from "./configstore";
import { fetchWebSetup, getCachedWebSetup } from "./fetchWebSetup";
import { firebaseApiOrigin } from "./api";
import { FirebaseError } from "./error";
describe("fetchWebSetup module", () => {
before(() => {
nock.disableNetConnect();
});
after(() => {
nock.enableNetConnect();
});
afterEach(() => {
expect(nock.isDone()).to.be.true;
});
describe("fetchWebSetup", () => {
let configSetStub: sinon.SinonStub;
beforeEach(() => {
sinon.stub(configstore, "get");
configSetStub = sinon.stub(configstore, "set").returns();
});
afterEach(() => {
sinon.restore();
});
it("should fetch the web app config", async () => {
const projectId = "foo";
nock(firebaseApiOrigin())
.get(`/v1beta1/projects/${projectId}/webApps/-/config`)
.reply(200, { some: "config" });
const config = await fetchWebSetup({ project: projectId });
expect(config).to.deep.equal({ some: "config" });
});
it("should store the fetched config", async () => {
const projectId = "projectId";
nock(firebaseApiOrigin())
.get(`/v1beta1/projects/${projectId}/webApps/-/config`)
.reply(200, { projectId, some: "config" });
await fetchWebSetup({ project: projectId });
expect(configSetStub).to.have.been.calledOnceWith("webconfig", {
[projectId]: {
projectId,
some: "config",
},
});
expect(nock.isDone()).to.be.true;
});
it("should throw an error if the request fails", async () => {
const projectId = "foo";
nock(firebaseApiOrigin())
.get(`/v1beta1/projects/${projectId}/webApps/-/config`)
.reply(404, { error: "Not Found" });
await expect(fetchWebSetup({ project: projectId })).to.eventually.be.rejectedWith(
FirebaseError,
"Not Found",
);
});
it("should return a fake config for a demo project id", async () => {
const projectId = "demo-project-1234";
await expect(fetchWebSetup({ project: projectId })).to.eventually.deep.equal({
projectId: "demo-project-1234",
databaseURL: "https://demo-project-1234.firebaseio.com",
storageBucket: "demo-project-1234.appspot.com",
apiKey: "fake-api-key",
authDomain: "demo-project-1234.firebaseapp.com",
});
});
});
describe("getCachedWebSetup", () => {
let configGetStub: sinon.SinonStub;
beforeEach(() => {
sinon.stub(configstore, "set").returns();
configGetStub = sinon.stub(configstore, "get");
});
afterEach(() => {
sinon.restore();
});
it("should return no config if none is cached", () => {
configGetStub.returns(undefined);
const config = getCachedWebSetup({ project: "foo" });
expect(config).to.be.undefined;
});
it("should return a stored config", () => {
const projectId = "projectId";
configGetStub.returns({ [projectId]: { project: projectId, some: "config" } });
const config = getCachedWebSetup({ project: projectId });
expect(config).to.be.deep.equal({ project: projectId, some: "config" });
});
});
});