forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponents.test.ts
More file actions
484 lines (389 loc) · 20.1 KB
/
Copy pathcomponents.test.ts
File metadata and controls
484 lines (389 loc) · 20.1 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import { expect, test } from '@playwright/test';
import type { FakeOrganization, FakeUser } from '../../testUtils';
import { createTestUtils, testAgainstRunningApps } from '../../testUtils';
testAgainstRunningApps({ withPattern: ['astro.node.withCustomRoles'] })('basic flows for @astro', ({ app }) => {
test.describe.configure({ mode: 'serial' });
let fakeAdmin: FakeUser;
let fakeOrganization: FakeOrganization;
let fakeAdmin2: FakeUser;
let fakeOrganization2: FakeOrganization;
test.beforeAll(async () => {
const m = createTestUtils({ app });
fakeAdmin = m.services.users.createFakeUser();
const admin = await m.services.users.createBapiUser(fakeAdmin);
fakeOrganization = await m.services.users.createFakeOrganization(admin.id);
fakeAdmin2 = m.services.users.createFakeUser();
const admin2 = await m.services.users.createBapiUser(fakeAdmin2);
fakeOrganization2 = await m.services.users.createFakeOrganization(admin2.id);
});
test.afterAll(async () => {
await fakeOrganization.delete();
await fakeAdmin.deleteIfExists();
await fakeOrganization2.delete();
await fakeAdmin2.deleteIfExists();
await app.teardown();
});
test('Clerk client loads on first visit and Sign In button renders', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToAppHome();
await u.page.waitForClerkJsLoaded();
await u.po.expect.toBeSignedOut();
await expect(u.page.getByRole('link', { name: /Login/i })).toBeVisible();
});
test('sign in with hash routing', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/sign-in');
await u.po.signIn.waitForMounted();
await u.po.signIn.setIdentifier(fakeAdmin.email);
await u.po.signIn.continue();
await u.page.waitForURL(`${app.serverUrl}/sign-in#/factor-one`);
await u.po.signIn.setPassword(fakeAdmin.password);
await u.po.signIn.continue();
await u.po.expect.toBeSignedIn();
});
test('renders user button', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/sign-in');
await u.po.signIn.waitForMounted();
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin.email, password: fakeAdmin.password });
await u.page.waitForAppUrl('/');
await u.po.expect.toBeSignedIn();
await u.po.userButton.waitForMounted();
await u.po.userButton.toggleTrigger();
await u.po.userButton.waitForPopover();
await u.po.userButton.toHaveVisibleMenuItems([/Manage account/i, /Sign out$/i]);
await u.po.userButton.triggerManageAccount();
await u.po.userProfile.waitForUserProfileModal();
await expect(u.page.getByText(/profile details/i)).toBeVisible();
});
test('renders user button with custom menu items', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/sign-in');
await u.po.signIn.waitForMounted();
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin.email, password: fakeAdmin.password });
await u.page.waitForAppUrl('/');
await u.po.expect.toBeSignedIn();
await u.po.userButton.waitForMounted();
await u.po.userButton.toggleTrigger();
await u.po.userButton.waitForPopover();
// Check if custom menu items are visible
await u.po.userButton.toHaveVisibleMenuItems([/Custom link/i, /Custom action/i, /Custom click/i]);
// Click custom action and check for custom page availbility
await u.page.getByRole('menuitem', { name: /Custom action/i }).click();
await u.po.userProfile.waitForUserProfileModal();
await expect(u.page.getByRole('heading', { name: 'Custom Terms Page' })).toBeVisible();
// Close the modal and trigger the popover again
await u.page.locator('.cl-modalCloseButton').click();
await u.po.userButton.toggleTrigger();
await u.po.userButton.waitForPopover();
// Click custom action with click handler
const eventPromise = u.page.evaluate(() => {
return new Promise<string>(resolve => {
document.addEventListener(
'clerk:menu-item-click',
(e: CustomEvent<string>) => {
resolve(e.detail);
},
{ once: true },
);
});
});
await u.page.getByRole('menuitem', { name: /Custom click/i }).click();
expect(await eventPromise).toBe('custom_click');
// Trigger the popover again
await u.po.userButton.toggleTrigger();
await u.po.userButton.waitForPopover();
// Click custom link and check navigation
await u.page.getByRole('menuitem', { name: /Custom link/i }).click();
await u.page.waitForAppUrl('/user');
});
test('reorders default user button menu items and functions as expected', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/sign-in');
await u.po.signIn.waitForMounted();
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin.email, password: fakeAdmin.password });
await u.page.waitForAppUrl('/');
await u.po.expect.toBeSignedIn();
await u.po.userButton.waitForMounted();
await u.po.userButton.toggleTrigger();
await u.po.userButton.waitForPopover();
// First item should now be the sign out button
await u.page.getByRole('menuitem').first().click();
await u.po.expect.toBeSignedOut();
});
test('render user profile with streamed data', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/sign-in');
await u.po.signIn.waitForMounted();
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin.email, password: fakeAdmin.password });
await u.po.expect.toBeSignedIn();
await u.po.userButton.waitForMounted();
await u.page.goToRelative('/user');
await u.po.userProfile.waitForMounted();
// Streams data from Astro.locals.currentUser()
await expect(u.page.getByText(`"firstName":"${fakeAdmin.firstName}"`)).toBeVisible();
});
test('render user profile with custom pages and links', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/sign-in');
await u.po.signIn.waitForMounted();
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin.email, password: fakeAdmin.password });
await u.po.expect.toBeSignedIn();
await u.page.goToRelative('/custom-pages/user-profile');
await u.po.userProfile.waitForMounted();
// Check if custom pages and links are visible
await expect(u.page.getByRole('button', { name: /Terms/i })).toBeVisible();
await expect(u.page.getByRole('button', { name: /Homepage/i })).toBeVisible();
// Navigate to custom page
await u.page.getByRole('button', { name: /Terms/i }).click();
await expect(u.page.getByRole('heading', { name: 'Custom Terms Page' })).toBeVisible();
// Check reordered default label. Security tab is now the last item.
await u.page.locator('.cl-navbarButton').last().click();
await expect(u.page.getByRole('heading', { name: 'Security' })).toBeVisible();
// Click custom link and check navigation
await u.page.getByRole('button', { name: /Homepage/i }).click();
await u.page.waitForAppUrl('/');
});
test('redirects to sign-in when unauthenticated', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/user');
await u.page.waitForURL(`${app.serverUrl}/sign-in?redirect_url=${encodeURIComponent(`${app.serverUrl}/user`)}`);
await u.po.signIn.waitForMounted();
});
test('SignedIn, SignedOut SSR', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToAppHome();
await expect(u.page.getByText('Go to this page to log in')).toBeVisible();
await u.page.goToRelative('/sign-in');
await u.po.signIn.waitForMounted();
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin.email, password: fakeAdmin.password });
await u.po.expect.toBeSignedIn();
await expect(u.page.getByText('Go to this page to see your profile')).toBeVisible();
await expect(u.page.getByText('Sign out!')).toBeVisible();
});
test('SignInButton renders and respects props', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/buttons');
await u.po.expect.toBeSignedOut();
await u.page.waitForClerkJsLoaded();
await u.page.getByRole('button', { name: /Sign in/i }).click();
await u.po.signIn.waitForMounted();
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin.email, password: fakeAdmin.password });
await u.page.waitForAppUrl('/user');
await u.po.expect.toBeSignedIn();
});
test('SignUpButton renders and respects props', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
const fakeAdmin = u.services.users.createFakeUser({
fictionalEmail: true,
withPhoneNumber: true,
withUsername: true,
});
await u.page.goToRelative('/buttons');
await u.page.waitForClerkJsLoaded();
await u.page.getByRole('button', { name: /Sign up/i }).click();
await u.po.signUp.waitForMounted();
// Fill in sign up form
await u.po.signUp.signUpWithEmailAndPassword({
email: fakeAdmin.email,
password: fakeAdmin.password,
});
// Verify email
await u.po.signUp.enterTestOtpCode();
await u.page.waitForAppUrl('/user');
// Check if user is signed in
await u.po.expect.toBeSignedIn();
await fakeAdmin.deleteIfExists();
});
test('updateClerkOptions by changing localization on the fly', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.po.signIn.goTo();
await u.po.signIn.waitForMounted();
const selectElement = await u.page.$('select');
const selectedOption = await selectElement.evaluate(el => (el as any).options[(el as any).selectedIndex].text);
if (selectedOption !== 'English') {
throw new Error('English option is not selected by default');
}
await expect(u.page.getByText('Welcome back! Please sign in to continue')).toBeVisible();
await selectElement.selectOption({ label: 'French' });
await expect(u.page.getByText('pour continuer vers')).toBeVisible();
});
test('render organization profile with custom pages and links in dedicated page', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/sign-in');
await u.po.signIn.waitForMounted();
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin.email, password: fakeAdmin.password });
await u.po.expect.toBeSignedIn();
await u.page.goToRelative('/custom-pages/organization-profile?dedicatedPage=true');
await u.po.organizationSwitcher.waitForMounted();
await u.po.organizationSwitcher.waitForAnOrganizationToSelected();
// Check if custom pages and links are visible
await expect(u.page.getByRole('button', { name: /Terms/i })).toBeVisible();
await expect(u.page.getByRole('button', { name: /Homepage/i })).toBeVisible();
// Navigate to custom page
await u.page.getByRole('button', { name: /Terms/i }).click();
await expect(u.page.getByRole('heading', { name: 'Custom Terms Page' })).toBeVisible();
// Check reordered default label. General tab is now the last item.
await u.page.locator('.cl-navbarButton').last().click();
await expect(u.page.getByRole('heading', { name: 'General' })).toBeVisible();
// Click custom link and check navigation
await u.page.getByRole('button', { name: /Homepage/i }).click();
await u.page.waitForAppUrl('/');
});
test('render organization profile with custom pages and links inside organization switcher', async ({
page,
context,
}) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/sign-in');
await u.po.signIn.waitForMounted();
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin.email, password: fakeAdmin.password });
await u.po.expect.toBeSignedIn();
await u.page.goToRelative('/custom-pages/organization-profile?dedicatedPage=false');
await u.po.organizationSwitcher.waitForMounted();
await u.po.organizationSwitcher.waitForAnOrganizationToSelected();
// Open organization profile inside organization switcher
await u.po.organizationSwitcher.toggleTrigger();
await u.page.waitForSelector('.cl-organizationSwitcherPopoverCard', { state: 'visible' });
await u.page.locator('.cl-button__manageOrganization').click();
// Check if custom pages and links are visible
await expect(u.page.getByRole('button', { name: /Terms/i })).toBeVisible();
await expect(u.page.getByRole('button', { name: /Homepage/i })).toBeVisible();
// Navigate to custom page
await u.page.getByRole('button', { name: /Terms/i }).click();
await expect(u.page.getByRole('heading', { name: 'Custom Terms Page' })).toBeVisible();
// Check reordered default label. Members tab is now the last item.
await u.page.locator('.cl-navbarButton').last().click();
await expect(u.page.getByRole('heading', { name: 'Members' })).toBeVisible();
// Click custom link and check navigation
await u.page.getByRole('button', { name: /Homepage/i }).click();
await u.page.waitForAppUrl('/');
});
// ---- react/protect
test('only admin react', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/react/sign-in#/?redirect_url=/react');
await u.po.signIn.waitForMounted();
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin.email, password: fakeAdmin.password });
await u.po.expect.toBeSignedIn();
await u.po.organizationSwitcher.waitForMounted();
await u.po.organizationSwitcher.waitForAnOrganizationToSelected();
await u.page.goToRelative('/react/only-admins');
await expect(u.page.getByText("I'm an admin")).toBeVisible();
});
test('only member react', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/react/sign-in#/?redirect_url=/react/only-members');
await u.po.signIn.waitForMounted();
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin.email, password: fakeAdmin.password });
await u.po.expect.toBeSignedIn();
await expect(u.page.getByText('Not a member')).toBeVisible();
});
// --- react/components
test('react/ render user button', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/react/sign-in#/?redirect_url=/react');
await u.po.signIn.waitForMounted();
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin.email, password: fakeAdmin.password });
await u.page.waitForAppUrl('/react');
await u.po.expect.toBeSignedIn();
await u.po.userButton.waitForMounted();
await u.po.userButton.toggleTrigger();
await u.po.userButton.waitForPopover();
await u.po.userButton.toHaveVisibleMenuItems([/Manage account/i, /Sign out$/i]);
await u.po.userButton.triggerManageAccount();
await u.po.userProfile.waitForUserProfileModal();
await expect(u.page.getByText(/profile details/i)).toBeVisible();
});
test('react/ render user profile with streamed data', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/react/sign-in#/?redirect_url=/react');
await u.po.signIn.waitForMounted();
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin.email, password: fakeAdmin.password });
await u.po.expect.toBeSignedIn();
await u.po.userButton.waitForMounted();
await u.page.goToRelative('/react/user');
await u.po.userProfile.waitForMounted();
await expect(u.page.getByText(`My name is: ${fakeAdmin.firstName}`)).toBeVisible();
// Streams data from Astro.locals.currentUser()
await expect(u.page.getByText(`"firstName":"${fakeAdmin.firstName}"`)).toBeVisible();
});
test('react/ SignedIn, SignedOut SSR', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/react');
await expect(u.page.getByText('Go to this page to log in')).toBeVisible();
await u.page.goToRelative('/react/sign-in#/?redirect_url=/react');
await u.po.signIn.waitForMounted();
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin.email, password: fakeAdmin.password });
await u.po.expect.toBeSignedIn();
await expect(u.page.getByText('Go to this page to see your profile')).toBeVisible();
await expect(u.page.getByText('Sign out!')).toBeVisible();
});
test('react/ render content based on Clerk loaded status', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/utility');
await expect(u.page.getByText('Clerk is loading')).toBeVisible();
await expect(u.page.getByText('Clerk is loaded')).toBeHidden();
await u.page.waitForClerkJsLoaded();
await expect(u.page.getByText('Clerk is loaded')).toBeVisible();
await expect(u.page.getByText('Clerk is loading')).toBeHidden();
});
// ----- redirect
test('redirects to sign-in when unauthenticated (middleware)', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToAppHome();
await u.page.getByRole('link', { name: 'User', exact: true }).click();
await u.page.waitForURL(`${app.serverUrl}/sign-in?redirect_url=${encodeURIComponent(`${app.serverUrl}/user`)}`);
});
test('redirects to sign-in when unauthenticated (page)', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToAppHome();
await u.page.getByRole('link', { name: 'Organization', exact: true }).click();
await u.page.waitForURL(
`${app.serverUrl}/sign-in?redirect_url=${encodeURIComponent(`${app.serverUrl}/organization`)}`,
);
});
// ---- protect
test('only admin', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/sign-in');
await u.po.signIn.waitForMounted();
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin2.email, password: fakeAdmin2.password });
await u.po.expect.toBeSignedIn();
await u.po.organizationSwitcher.waitForMounted();
await u.po.organizationSwitcher.waitForAnOrganizationToSelected();
await u.page.goToRelative('/only-admins');
await expect(u.page.getByText("I'm an admin")).toBeVisible();
});
test('only member', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/sign-in#/?redirect_url=/only-members');
await u.po.signIn.waitForMounted();
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin2.email, password: fakeAdmin2.password });
await u.po.expect.toBeSignedIn();
await expect(u.page.getByText('Not a member')).toBeVisible();
});
test('renders components and keep internal routing behavior when view transitions is enabled', async ({
page,
context,
}) => {
const u = createTestUtils({ app, page, context });
await u.page.goToRelative('/transitions');
// Navigate to sign-in page using link to simulate transition
await u.page.getByRole('link', { name: /Sign in/i }).click();
// Components should be mounted on the new document
// when navigating through links
await u.page.waitForURL(`${app.serverUrl}/transitions/sign-in`);
await u.po.signIn.waitForMounted();
await u.po.signIn.setIdentifier(fakeAdmin.email);
await u.po.signIn.continue();
await u.page.waitForURL(`${app.serverUrl}/transitions/sign-in#/factor-one`);
await u.po.signIn.setPassword(fakeAdmin.password);
await u.po.signIn.continue();
await u.po.expect.toBeSignedIn();
// Internal Clerk routing should still work
await u.page.waitForURL(`${app.serverUrl}/transitions`);
// Components should be rendered on hard reload
await u.po.userButton.waitForMounted();
});
});