|
| 1 | +import type { Organization } from '@clerk/backend'; |
| 2 | +import type { BrowserContext, Page } from '@playwright/test'; |
| 3 | +import { expect, test } from '@playwright/test'; |
| 4 | + |
| 5 | +import type { FakeUser } from '../../testUtils'; |
| 6 | +import { createTestUtils, testAgainstRunningApps } from '../../testUtils'; |
| 7 | + |
| 8 | +testAgainstRunningApps({ withPattern: ['next.appRouterMosaic.*'] })('Mosaic OrganizationProfile @mosaic', ({ app }) => { |
| 9 | + test.describe.configure({ mode: 'serial' }); |
| 10 | + |
| 11 | + let fakeUser: FakeUser; |
| 12 | + let organization: Organization; |
| 13 | + const cleanups: Array<() => Promise<unknown>> = []; |
| 14 | + |
| 15 | + test.beforeAll(async () => { |
| 16 | + const u = await createTestUtils({ app }); |
| 17 | + fakeUser = u.services.users.createFakeUser(test); |
| 18 | + cleanups.push(() => fakeUser.deleteIfExists()); |
| 19 | + const user = await u.services.users.createBapiUser(fakeUser); |
| 20 | + organization = await u.services.clerk.organizations.createOrganization({ |
| 21 | + name: `Mosaic ${Date.now()}`, |
| 22 | + createdBy: user.id, |
| 23 | + }); |
| 24 | + cleanups.push(() => u.services.clerk.organizations.deleteOrganization(organization.id)); |
| 25 | + }); |
| 26 | + |
| 27 | + test.afterAll(async () => { |
| 28 | + const results = await Promise.allSettled(cleanups.map(cleanup => cleanup())); |
| 29 | + await app.teardown(); |
| 30 | + const failures = results.flatMap(result => (result.status === 'rejected' ? [result.reason] : [])); |
| 31 | + if (failures.length > 0) { |
| 32 | + throw new AggregateError(failures, 'Mosaic OrganizationProfile cleanup failed'); |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + const path = '/organization-profile'; |
| 37 | + const tab = (page: Page, name: string) => page.getByRole('navigation').getByRole('tab', { name, exact: true }); |
| 38 | + |
| 39 | + async function signIn({ page, context }: { page: Page; context: BrowserContext }) { |
| 40 | + const u = await createTestUtils({ app, page, context }); |
| 41 | + await page.setViewportSize({ width: 1280, height: 800 }); |
| 42 | + await u.po.signIn.goTo(); |
| 43 | + await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeUser.email, password: fakeUser.password }); |
| 44 | + await u.page.goToRelative(path); |
| 45 | + await u.po.expect.toBeSignedIn(); |
| 46 | + await page.evaluate(orgId => window.Clerk?.setActive({ organization: orgId }), organization.id); |
| 47 | + await page.waitForFunction(orgId => window.Clerk?.organization?.id === orgId, organization.id); |
| 48 | + await expect(tab(page, 'General')).toBeVisible(); |
| 49 | + return u; |
| 50 | + } |
| 51 | + |
| 52 | + test('renders nothing when signed out', async ({ page, context }) => { |
| 53 | + const u = await createTestUtils({ app, page, context }); |
| 54 | + await u.page.goToRelative(path); |
| 55 | + await u.page.waitForClerkJsLoaded(); |
| 56 | + |
| 57 | + await expect(tab(page, 'General')).toHaveCount(0); |
| 58 | + }); |
| 59 | + |
| 60 | + test('opens on the general page with the active organization', async ({ page, context }) => { |
| 61 | + await signIn({ page, context }); |
| 62 | + |
| 63 | + await expect(tab(page, 'General')).toHaveAttribute('aria-selected', 'true'); |
| 64 | + await expect(page.getByText(organization.name).first()).toBeVisible(); |
| 65 | + }); |
| 66 | + |
| 67 | + test('switches to the members page', async ({ page, context }) => { |
| 68 | + await signIn({ page, context }); |
| 69 | + |
| 70 | + await tab(page, 'Members').click(); |
| 71 | + |
| 72 | + await expect(tab(page, 'Members')).toHaveAttribute('aria-selected', 'true'); |
| 73 | + await expect(page.getByText(fakeUser.email)).toBeVisible(); |
| 74 | + }); |
| 75 | + |
| 76 | + test('renders a custom page', async ({ page, context }) => { |
| 77 | + await signIn({ page, context }); |
| 78 | + |
| 79 | + await tab(page, 'Custom page').click(); |
| 80 | + await expect(page.getByText('custom-page-content')).toBeVisible(); |
| 81 | + }); |
| 82 | +}); |
0 commit comments