-
Notifications
You must be signed in to change notification settings - Fork 331
/
Copy pathindex.ts
96 lines (84 loc) · 3.31 KB
/
index.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
import { Clerk } from '@clerk/backend';
import type { Browser, BrowserContext, Page } from '@playwright/test';
import type { Application } from '../models/application';
import { createAppPageObject } from './appPageObject';
import { createEmailService } from './emailService';
import type { EnchancedPage, TestArgs } from './signInPageObject';
import { createSignInComponentPageObject } from './signInPageObject';
import { createSignUpComponentPageObject } from './signUpPageObject';
import { createUserProfileComponentPageObject } from './userProfilePageObject';
import type { FakeUser } from './usersService';
import { createUserService } from './usersService';
export type { FakeUser };
const createClerkClient = (app: Application) => {
return Clerk({
secretKey: app.env.privateVariables.get('CLERK_SECRET_KEY'),
publishableKey: app.env.publicVariables.get('CLERK_PUBLISHABLE_KEY'),
});
};
const createExpectPageObject = ({ page }: TestArgs) => {
return {
toBeSignedOut: () => {
return page.waitForFunction(() => {
return !window.Clerk?.user;
});
},
toBeSignedIn: async () => {
return page.waitForFunction(() => {
return !!window.Clerk?.user;
});
},
};
};
type CreateAppPageObjectArgs = { page: Page; context: BrowserContext; browser: Browser };
export const createTestUtils = <
Params extends { app: Application } & Partial<CreateAppPageObjectArgs>,
Services = typeof services,
PO = typeof pageObjects,
BH = typeof browserHelpers,
FullReturn = { services: Services; po: PO; tabs: BH; page: EnchancedPage },
OnlyAppReturn = { services: Services },
>(
params: Params,
): Params extends Partial<CreateAppPageObjectArgs> ? FullReturn : OnlyAppReturn => {
const { app, context, browser } = params || {};
const clerkClient = createClerkClient(app);
const services = {
email: createEmailService(),
users: createUserService(clerkClient),
clerk: clerkClient,
};
if (!params.page) {
return { services } as any;
}
const page = createAppPageObject({ page: params.page }, app);
const testArgs = { page, context, browser };
const pageObjects = {
signUp: createSignUpComponentPageObject(testArgs),
signIn: createSignInComponentPageObject(testArgs),
userProfile: createUserProfileComponentPageObject(testArgs),
expect: createExpectPageObject(testArgs),
};
const browserHelpers = {
runInNewTab: async (
cb: (u: { services: Services; po: PO; page: EnchancedPage }, context: BrowserContext) => Promise<unknown>,
) => {
const u = createTestUtils({ app, page: createAppPageObject({ page: await context.newPage() }, app) });
await cb(u as any, context);
return u;
},
runInNewBrowser: async (
cb: (u: { services: Services; po: PO; page: EnchancedPage }, context: BrowserContext) => Promise<unknown>,
) => {
if (!browser) {
throw new Error('Browser is not defined. Did you forget to pass it to createPageObjects?');
}
const context = await browser.newContext();
const u = createTestUtils({ app, page: createAppPageObject({ page: await context.newPage() }, app) });
await cb(u as any, context);
return u;
},
};
return { page, services, po: pageObjects, tabs: browserHelpers } as any;
};
export { testAgainstRunningApps } from './testAgainstRunningApps';