generated from RealDevSquad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathuserMigrations.test.js
More file actions
58 lines (55 loc) · 2.5 KB
/
userMigrations.test.js
File metadata and controls
58 lines (55 loc) · 2.5 KB
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
import { MAX_TRANSACTION_WRITES } from "../../../models/userMigrations";
const chai = require("chai");
const { expect } = chai;
const firestore = require("../../../utils/firestore");
const userModel = firestore.collection("users");
const cleanDb = require("../../utils/cleanDb");
const userMigrationModel = require("../../../models/userMigrations");
const userData = require("../../fixtures/user/user")();
const addUser = require("../../utils/addUser");
describe("userColorMigrations", function () {
beforeEach(async function () {
await addUser(userData[0]);
await addUser(userData[1]);
await addUser(userData[2]);
await addUser(userData[3]);
await addUser(userData[4]);
await addUser(userData[5]);
});
afterEach(async function () {
await cleanDb();
});
it("should throw an error on passing invalid max transactions", async function () {
try {
await userMigrationModel.addDefaultColors(MAX_TRANSACTION_WRITES + 1);
} catch (err) {
expect(err).to.be.a("error");
expect(err.message).to.be.equal("Error cannot add more than 499 users at once .");
}
});
it("should add color property to added users which dont have a color property", async function () {
const response = await userMigrationModel.addDefaultColors(MAX_TRANSACTION_WRITES);
expect(response.totalUsersFetched).to.equal(6);
expect(response.totalUsersUpdated).to.equal(5);
expect(response.totalUsersUnaffected).to.equal(1);
});
it("should make sure that batch updates are working properly by passing smaller batch size", async function () {
const SMALL_BATCH_SIZE = 2;
const response = await userMigrationModel.addDefaultColors(SMALL_BATCH_SIZE);
expect(response.totalUsersFetched).to.equal(6);
expect(response.totalUsersUpdated).to.equal(5);
expect(response.totalUsersUnaffected).to.equal(1);
});
it("should not affect users already having color property", async function () {
// Manually add a color property to a user
const userId = await addUser(userData[0]);
await userModel.doc(userId).update({ colors: { color_id: 3 } });
const response = await userMigrationModel.addDefaultColors(MAX_TRANSACTION_WRITES);
expect(response.totalUsersFetched).to.equal(6);
expect(response.totalUsersUpdated).to.equal(4);
expect(response.totalUsersUnaffected).to.equal(2);
// Check that the user with a color property was unaffected
const updatedUser = await userModel.doc(userId).get();
expect(updatedUser.data().colors.color_id).to.equal(3);
});
});