Skip to content

Commit a68cea4

Browse files
committed
:bug fix(sign-up-import-gcal): update sync controller tests
1 parent e8c53ad commit a68cea4

File tree

7 files changed

+373
-80
lines changed

7 files changed

+373
-80
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { WithId } from "mongodb";
2+
import { faker } from "@faker-js/faker";
3+
import { Schema_User } from "@core/types/user.types";
4+
import mongoService from "@backend/common/services/mongo.service";
5+
import { Schema_Sync } from "../../../../core/src/types/sync.types";
6+
7+
export class SyncDriver {
8+
static async createSync(
9+
user: Pick<WithId<Schema_User>, "_id">,
10+
defaultUser = false,
11+
): Promise<WithId<Schema_Sync>> {
12+
const gCalendarId = defaultUser ? "test-calendar" : faker.string.uuid();
13+
const nextSyncToken = faker.internet.jwt();
14+
15+
const syncRecord: Schema_Sync = {
16+
user: user._id.toString(),
17+
google: {
18+
calendarlist: [
19+
{
20+
gCalendarId,
21+
nextSyncToken,
22+
lastSyncedAt: new Date(),
23+
},
24+
],
25+
events: [
26+
{
27+
gCalendarId,
28+
resourceId: faker.string.nanoid(),
29+
channelId: faker.string.uuid(),
30+
expiration: new Date(Date.now() + 3600000).toISOString(),
31+
nextSyncToken,
32+
},
33+
],
34+
},
35+
};
36+
37+
const created = await mongoService.sync.insertOne(syncRecord);
38+
39+
return { _id: created.insertedId, ...syncRecord };
40+
}
41+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { WithId } from "mongodb";
2+
import { faker } from "@faker-js/faker";
3+
import { Schema_User } from "@core/types/user.types";
4+
import mongoService from "@backend/common/services/mongo.service";
5+
6+
export class UserDriver {
7+
static async createUser(): Promise<WithId<Schema_User>> {
8+
const firstName = faker.person.firstName();
9+
const lastName = faker.person.lastName();
10+
11+
const user: Schema_User = {
12+
email: faker.internet.email(),
13+
firstName,
14+
lastName,
15+
name: `${firstName} ${lastName}`,
16+
locale: faker.location.language().alpha2,
17+
google: {
18+
googleId: faker.string.uuid(),
19+
picture: faker.internet.url({ protocol: "https" }),
20+
gRefreshToken: faker.internet.jwt(),
21+
},
22+
};
23+
24+
const created = await mongoService.user.insertOne(user);
25+
26+
return { _id: created.insertedId, ...user };
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { WithId } from "mongodb";
2+
import { Schema_User } from "@core/types/user.types";
3+
import { Schema_Waitlist } from "@core/types/waitlist/waitlist.types";
4+
import mongoService from "@backend/common/services/mongo.service";
5+
6+
export class WaitListDriver {
7+
static async createWaitListRecord(
8+
user: Pick<WithId<Schema_User>, "email" | "firstName" | "lastName">,
9+
): Promise<WithId<Schema_Waitlist>> {
10+
const waitListRecord: Schema_Waitlist = {
11+
email: user.email,
12+
schemaVersion: "0",
13+
source: "other",
14+
firstName: user.firstName,
15+
lastName: user.lastName,
16+
currentlyPayingFor: ["superhuman", "notion"],
17+
howClearAboutValues: "not-clear",
18+
workingTowardsMainGoal: "yes",
19+
isWillingToShare: false,
20+
status: "waitlisted",
21+
waitlistedAt: new Date().toISOString(),
22+
};
23+
24+
const created = await mongoService.waitlist.insertOne(waitListRecord);
25+
26+
return { _id: created.insertedId, ...waitListRecord };
27+
}
28+
}

packages/backend/src/__tests__/helpers/mock.db.setup.ts

Lines changed: 9 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Db, ObjectId } from "mongodb";
1+
import { Db } from "mongodb";
22
import { MongoMemoryServer } from "mongodb-memory-server";
3-
import { Schema_Sync } from "@core/types/sync.types";
4-
import { Schema_User } from "@core/types/user.types";
53
import { Collections } from "@backend/common/constants/collections";
64
import mongoService from "@backend/common/services/mongo.service";
7-
import { Schema_Waitlist } from "../../../../core/src/types/waitlist/waitlist.types";
5+
import { SyncDriver } from "../drivers/sync.driver";
6+
import { UserDriver } from "../drivers/user.driver";
7+
import { WaitListDriver } from "../drivers/waitlist.driver";
88

99
export interface TestSetup {
1010
mongoServer: MongoMemoryServer;
@@ -29,75 +29,19 @@ export async function setupTestDb(): Promise<TestSetup> {
2929
await mongoServer.start();
3030
await mongoService.start(mongoServer.getUri());
3131

32-
const userId = new ObjectId();
33-
const userIdStr = userId.toString();
34-
const email = "test@example.com";
35-
36-
// test user
37-
const user: Schema_User & { _id: ObjectId } = {
38-
_id: userId,
39-
email,
40-
firstName: "Test",
41-
lastName: "User",
42-
name: "Test User",
43-
locale: "en",
44-
google: {
45-
googleId: "test-google-id",
46-
picture: "test-picture",
47-
gRefreshToken: "fake-refresh-token",
48-
},
49-
};
50-
51-
// sync record for the user
52-
const syncRecord: Schema_Sync = {
53-
user: userIdStr,
54-
google: {
55-
calendarlist: [
56-
{
57-
gCalendarId: "test-calendar",
58-
nextSyncToken: "initial-sync-token",
59-
lastSyncedAt: new Date(),
60-
},
61-
],
62-
events: [
63-
{
64-
gCalendarId: "test-calendar",
65-
resourceId: "test-resource-id",
66-
channelId: "test-channel-id",
67-
expiration: new Date(Date.now() + 3600000).toISOString(),
68-
nextSyncToken: "initial-sync-token",
69-
},
70-
],
71-
},
72-
};
73-
74-
// Create waitlist user
75-
const waitlistRecord: Schema_Waitlist = {
76-
email,
77-
schemaVersion: "0",
78-
source: "other",
79-
firstName: "Test",
80-
lastName: "User",
81-
currentlyPayingFor: ["superhuman", "notion"],
82-
howClearAboutValues: "not-clear",
83-
workingTowardsMainGoal: "yes",
84-
isWillingToShare: false,
85-
status: "waitlisted",
86-
waitlistedAt: new Date().toISOString(),
87-
};
32+
const user = await UserDriver.createUser();
8833

8934
await Promise.all([
90-
mongoService.user.insertOne(user),
91-
mongoService.sync.insertOne(syncRecord),
92-
mongoService.waitlist.insertOne(waitlistRecord),
35+
SyncDriver.createSync(user, true),
36+
WaitListDriver.createWaitListRecord(user),
9337
]);
9438

9539
return {
9640
mongoServer,
9741
mongoClient: mongoService,
9842
db: mongoService.db,
99-
userId: userIdStr,
100-
email,
43+
userId: user._id.toString(),
44+
email: user.email,
10145
};
10246
} catch (err) {
10347
const error = err as Error;

0 commit comments

Comments
 (0)