|
| 1 | +import { configure, screen, within } from '@testing-library/react'; |
| 2 | +import { beforeAll, describe, expect, it, onTestFinished } from 'vitest'; |
| 3 | +import { page } from 'vitest/browser'; |
| 4 | + |
| 5 | +import { serveFapi } from '../../../__tests__/feature/fake-fapi'; |
| 6 | +import { fapiClient, fapiEnvironment, fapiSession, fapiUser } from '../../../__tests__/feature/fapi'; |
| 7 | +import { renderWithClerk } from '../../../__tests__/feature/render'; |
| 8 | + |
| 9 | +beforeAll(() => page.viewport(1280, 800)); |
| 10 | + |
| 11 | +function serve() { |
| 12 | + serveFapi({ |
| 13 | + environment: fapiEnvironment(), |
| 14 | + client: fapiClient([fapiSession({ id: 'sess_1', user: fapiUser({ id: 'user_1' }) })]), |
| 15 | + }); |
| 16 | +} |
| 17 | + |
| 18 | +function visit(search: string) { |
| 19 | + const original = window.location.href; |
| 20 | + window.history.replaceState(null, '', `${window.location.pathname}${search}#accounts`); |
| 21 | + onTestFinished(() => window.history.replaceState(null, '', original)); |
| 22 | +} |
| 23 | + |
| 24 | +function strictMode() { |
| 25 | + configure({ reactStrictMode: true }); |
| 26 | + onTestFinished(() => configure({ reactStrictMode: false })); |
| 27 | +} |
| 28 | + |
| 29 | +describe('UserProfile modal return', () => { |
| 30 | + it.each([ |
| 31 | + { name: 'renders normally', strict: false }, |
| 32 | + { name: 'renders in StrictMode', strict: true }, |
| 33 | + ])('reopens the profile on the account page and clears the param when it $name', async ({ strict }) => { |
| 34 | + if (strict) { |
| 35 | + strictMode(); |
| 36 | + } |
| 37 | + serve(); |
| 38 | + visit('?__clerk_mosaic_modal=user-profile%2Faccount&tab=connections'); |
| 39 | + |
| 40 | + await renderWithClerk(<p>Page</p>); |
| 41 | + |
| 42 | + const dialog = await screen.findByRole('dialog'); |
| 43 | + expect(await within(dialog).findByRole('tab', { name: 'Account' })).toHaveAttribute('aria-selected', 'true'); |
| 44 | + expect(window.location.search).toBe('?tab=connections'); |
| 45 | + expect(window.location.hash).toBe('#accounts'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('ignores an unknown value', async () => { |
| 49 | + serve(); |
| 50 | + visit('?__clerk_mosaic_modal=organization-profile%2Fgeneral&tab=connections'); |
| 51 | + |
| 52 | + await renderWithClerk(<p>Page</p>); |
| 53 | + |
| 54 | + expect(await screen.findByText('Page')).toBeVisible(); |
| 55 | + expect(screen.queryByRole('dialog')).toBeNull(); |
| 56 | + expect(window.location.search).toBe('?__clerk_mosaic_modal=organization-profile%2Fgeneral&tab=connections'); |
| 57 | + expect(window.location.hash).toBe('#accounts'); |
| 58 | + }); |
| 59 | +}); |
0 commit comments