Skip to content

Commit 27ec7b0

Browse files
committed
test(e2e): keep the mosaic user button e2e to what only a real app can prove
1 parent e16c658 commit 27ec7b0

2 files changed

Lines changed: 17 additions & 80 deletions

File tree

‎integration/templates/next-app-router-mosaic/src/app/custom/page.tsx‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
'use client';
22

33
import { MosaicProvider, UserButton } from '@clerk/mosaic';
4-
import { useState } from 'react';
54

65
export default function Page() {
7-
const [actionCount, setActionCount] = useState(0);
8-
96
return (
107
<MosaicProvider>
11-
<p>custom-action-count-{actionCount}</p>
128
<UserButton
13-
customMenuItems={[
14-
{ id: 'custom-link', label: 'Custom link', href: '/custom/link-target' },
15-
{ id: 'custom-action', label: 'Custom action', onClick: () => setActionCount(count => count + 1) },
16-
]}
9+
customMenuItems={[{ id: 'custom-link', label: 'Custom link', href: '/custom/link-target' }]}
1710
userProfileProps={{
1811
customPages: [{ label: 'Custom page', path: 'custom-page', content: <p>custom-page-content</p> }],
1912
}}

‎integration/tests/mosaic/user-button.test.ts‎

Lines changed: 16 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,23 @@ testAgainstRunningApps({ withPattern: ['next.appRouterMosaic.*'] })('Mosaic User
99
test.describe.configure({ mode: 'serial' });
1010

1111
let fakeUser: FakeUser;
12-
let otherFakeUser: FakeUser;
13-
let organizations: Organization[] = [];
12+
let organization: Organization;
1413

1514
test.beforeAll(async () => {
1615
const u = createTestUtils({ app });
1716
fakeUser = u.services.users.createFakeUser(test);
18-
otherFakeUser = u.services.users.createFakeUser(test);
19-
const [user] = await Promise.all([
20-
u.services.users.createBapiUser(fakeUser),
21-
u.services.users.createBapiUser(otherFakeUser),
22-
]);
23-
const suffix = Date.now();
24-
organizations = await Promise.all(
25-
['Alpha', 'Beta'].map(name =>
26-
u.services.clerk.organizations.createOrganization({ name: `Mosaic ${name} ${suffix}`, createdBy: user.id }),
27-
),
28-
);
17+
const user = await u.services.users.createBapiUser(fakeUser);
18+
organization = await u.services.clerk.organizations.createOrganization({
19+
name: `Mosaic ${Date.now()}`,
20+
createdBy: user.id,
21+
});
2922
});
3023

3124
test.afterAll(async () => {
3225
const u = createTestUtils({ app });
3326
const results = await Promise.allSettled([
34-
...organizations.map(({ id }) => u.services.clerk.organizations.deleteOrganization(id)),
27+
u.services.clerk.organizations.deleteOrganization(organization.id),
3528
fakeUser.deleteIfExists(),
36-
otherFakeUser.deleteIfExists(),
3729
]);
3830
await app.teardown();
3931
const failures = results.flatMap(result => (result.status === 'rejected' ? [result.reason] : []));
@@ -62,37 +54,17 @@ testAgainstRunningApps({ withPattern: ['next.appRouterMosaic.*'] })('Mosaic User
6254
await page.getByRole('menuitem', { name: label }).click();
6355
}
6456

65-
test('renders nothing while signed out', async ({ page, context }) => {
66-
const u = createTestUtils({ app, page, context });
67-
await u.page.goToAppHome();
68-
await u.page.waitForClerkJsLoaded();
69-
await u.po.expect.toBeSignedOut();
70-
71-
await expect(page.getByText('signed-out-state')).toBeVisible();
72-
await expect(trigger(page)).toHaveCount(0);
73-
});
74-
75-
test('switches the active organization', async ({ page, context }) => {
76-
await signIn({ page, context });
57+
test('switches the active organization and keeps it across a reload', async ({ page, context }) => {
58+
const u = await signIn({ page, context });
59+
const { id, name } = organization;
7760

7861
await trigger(page).click();
79-
await popup(page).getByRole('button', { name: 'Personal account' }).click();
80-
await page.waitForFunction(() => window.Clerk?.organization === null);
81-
await page.keyboard.press('Escape');
82-
await expect(popup(page)).toBeHidden();
83-
84-
for (const { id, name } of organizations) {
85-
await trigger(page).click();
86-
await popup(page).getByRole('button', { name }).click();
87-
88-
await page.waitForFunction(orgId => window.Clerk?.organization?.id === orgId, id);
89-
await page.keyboard.press('Escape');
90-
await expect(popup(page)).toBeHidden();
91-
}
62+
await popup(page).getByRole('button', { name }).click();
63+
await page.waitForFunction(orgId => window.Clerk?.organization?.id === orgId, id);
9264

93-
await trigger(page).click();
94-
await popup(page).getByRole('button', { name: 'Personal account' }).click();
95-
await page.waitForFunction(() => window.Clerk?.organization === null);
65+
await u.page.reload();
66+
await u.page.waitForClerkJsLoaded();
67+
await page.waitForFunction(orgId => window.Clerk?.organization?.id === orgId, id);
9668
});
9769

9870
test('signs out', async ({ page, context }) => {
@@ -114,14 +86,9 @@ testAgainstRunningApps({ withPattern: ['next.appRouterMosaic.*'] })('Mosaic User
11486
await page.waitForURL(url => url.pathname.startsWith('/sign-in'));
11587
});
11688

117-
test('runs custom menu items', async ({ page, context }) => {
89+
test('follows a custom menu link', async ({ page, context }) => {
11890
await signIn({ page, context }, '/custom');
11991

120-
await trigger(page).click();
121-
await popup(page).getByRole('button', { name: 'Custom action' }).click();
122-
await expect(page.getByText('custom-action-count-1')).toBeVisible();
123-
await expect(popup(page)).toBeHidden();
124-
12592
await trigger(page).click();
12693
await popup(page).getByRole('link', { name: 'Custom link' }).click();
12794
await expect(page.getByText('custom-link-target')).toBeVisible();
@@ -137,27 +104,4 @@ testAgainstRunningApps({ withPattern: ['next.appRouterMosaic.*'] })('Mosaic User
137104
await page.locator('.cl-userProfile-root').getByText('Custom page').click();
138105
await expect(page.getByText('custom-page-content')).toBeVisible();
139106
});
140-
141-
test('switches to another signed-in account', async ({ page, context }) => {
142-
const u = await signIn({ page, context });
143-
await u.po.signIn.goTo();
144-
await u.po.signIn.setIdentifier(otherFakeUser.email);
145-
await u.po.signIn.continue();
146-
await u.po.signIn.setPassword(otherFakeUser.password);
147-
await u.po.signIn.continue();
148-
await page.waitForFunction(
149-
email => window.Clerk?.user?.primaryEmailAddress?.emailAddress === email,
150-
otherFakeUser.email,
151-
);
152-
153-
await u.page.goToAppHome();
154-
await trigger(page).click();
155-
await popup(page).getByRole('button', { name: 'Switch account' }).click();
156-
await page.getByRole('menuitem', { name: fakeUser.email }).click();
157-
158-
await page.waitForFunction(
159-
email => window.Clerk?.user?.primaryEmailAddress?.emailAddress === email,
160-
fakeUser.email,
161-
);
162-
});
163107
});

0 commit comments

Comments
 (0)