-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha-testjs.test.ts
223 lines (193 loc) · 6.57 KB
/
a-testjs.test.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// #############################
// A quick typescript/jest translation of functions/test.js
// #############################
//
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const fs = require("fs");
const path = require("path");
// const { PROJECT_ID } = require('./firebase-info');
import { PROJECT_ID } from "./firebase-info";
const TEST_FIREBASE_PROJECT_ID = "test-firestore-rules-project";
// TODO: Change this to your real Firebase Project ID
const REAL_FIREBASE_PROJECT_ID = PROJECT_ID;
const firebase = require("@firebase/testing");
const seedItems: { [index: string]: number } = {
chocolate: 4.99,
"coffee beans": 12.99,
milk: 5.99,
};
const aliceAuth = {
uid: "alice",
email: "alice@example.com",
};
// before(async () => {
beforeAll(async () => {
// Load the content of the "firestore.rules" file into the emulator before running the
// test suite. This is necessary because we are using a fake Project ID in the tests,
// so the rules "hot reloading" behavior which works in the Web App does not apply here.
const rulesContent = fs.readFileSync(
path.resolve(__dirname, "../firestore.rules"),
"utf8"
);
await firebase.loadFirestoreRules({
projectId: TEST_FIREBASE_PROJECT_ID,
rules: rulesContent,
});
});
// after(() => {
afterAll(() => {
firebase.apps().forEach((app: any) => app.delete());
});
// Unit test the security rules
describe("shopping carts", () => {
const db = firebase
.initializeTestApp({
projectId: TEST_FIREBASE_PROJECT_ID,
auth: aliceAuth,
})
.firestore();
// after(() => {
afterEach(() => {
// Clear data from the emulator
firebase.clearFirestoreData({ projectId: TEST_FIREBASE_PROJECT_ID });
});
it("can be created by the cart owner", async () => {
await firebase.assertSucceeds(
db.doc("carts/alicesCart").set({
ownerUID: "alice",
total: 0,
})
);
});
});
// describe("shopping carts", async () => {
describe("shopping carts", () => {
const db = firebase
.initializeTestApp({
projectId: TEST_FIREBASE_PROJECT_ID,
auth: aliceAuth,
})
.firestore();
// before(async () => {
beforeEach(async () => {
const admin = firebase
.initializeAdminApp({
projectId: TEST_FIREBASE_PROJECT_ID,
})
.firestore();
// Create Alice's cart
await admin.doc("carts/alicesCart").set({
ownerUID: "alice",
total: 0,
});
});
// after(() => {
afterEach(() => {
// Clear data from the emulator
firebase.clearFirestoreData({ projectId: TEST_FIREBASE_PROJECT_ID });
});
it("can be read, updated, and deleted by the cart owner", async () => {
await firebase.assertSucceeds(db.doc("carts/alicesCart").get());
});
});
//describe("shopping cart items", async () => {
describe("shopping cart items", () => {
const db = firebase
.initializeTestApp({
projectId: TEST_FIREBASE_PROJECT_ID,
auth: aliceAuth,
})
.firestore();
// before(async () => {
beforeEach(async () => {
const admin = firebase
.initializeAdminApp({
projectId: TEST_FIREBASE_PROJECT_ID,
})
.firestore();
// Create Alice's cart
await admin.doc("carts/alicesCart").set({
ownerUID: "alice",
total: 0,
});
// Create Items Subcollection in Alice's Cart
const alicesItemsRef = admin.doc("carts/alicesCart").collection("items");
for (const name of Object.keys(seedItems)) {
await alicesItemsRef.doc(name).set({ value: seedItems[name] });
}
});
// after(() => {
afterEach(() => {
// Clear data from the emulator
firebase.clearFirestoreData({ projectId: TEST_FIREBASE_PROJECT_ID });
});
it("can be read by the cart owner", async () => {
await firebase.assertSucceeds(db.doc("carts/alicesCart/items/milk").get());
});
it("can be added by the cart owner", async () => {
await firebase.assertSucceeds(
db.doc("carts/alicesCart/items/lemon").set({
name: "lemon",
price: 0.99,
})
);
});
});
describe("adding an item to the cart recalculates the cart total. ", () => {
let unsubscribe: any;
// after(() => {
afterEach(() => {
// Clear data from the emulator
firebase.clearFirestoreData({ projectId: REAL_FIREBASE_PROJECT_ID });
// Call the function returned by `onSnapshot` to unsubscribe from updates
if (unsubscribe) {
unsubscribe();
}
});
it("should sum the cost of their items", (done) => {
/*
if (REAL_FIREBASE_PROJECT_ID == "changeme") {
throw new Error("Please change the REAL_FIREBASE_PROJECT_ID at the top of the test file");
}
*/
const db = firebase
.initializeAdminApp({ projectId: REAL_FIREBASE_PROJECT_ID })
.firestore();
// Setup: Initialize cart
const aliceCartRef = db.doc("carts/alice");
aliceCartRef.set({ ownerUID: "alice", totalPrice: 0 });
// Trigger `calculateCart` by adding items to the cart
const aliceItemsRef = aliceCartRef.collection("items");
aliceItemsRef.doc("doc1").set({ name: "nectarine", price: 2.99 });
aliceItemsRef.doc("doc2").set({ name: "grapefruit", price: 6.99 });
// Listen for every update to the cart. Every time an item is added to
// the cart's subcollection of items, the function updates `totalPrice`
// and `itemCount` attributes on the cart.
// Returns a function that can be called to unsubscribe the listener.
unsubscribe = aliceCartRef.onSnapshot((snap: any) => {
// If the function worked, these will be cart's final attributes.
const expectedCount = 2;
const expectedTotal = 9.98;
// When the `itemCount`and `totalPrice` match the expectations for the
// two items added, the promise resolves, and the test passes.
if (
snap.data().itemCount === expectedCount &&
snap.data().totalPrice == expectedTotal
) {
done();
}
});
});
});