|
| 1 | +import '@testing-library/jest-dom/vitest'; |
| 2 | + |
| 3 | +import { Dialog } from '@clerk/mosaic/components/dialog'; |
| 4 | +import { UserProfileView } from '@clerk/mosaic/features/user-profile/user-profile.view'; |
| 5 | +import { UserProfileMfaSectionView } from '@clerk/mosaic/features/user-profile/user-profile-mfa-section.view'; |
| 6 | +import { UserProfileSecurityPanelView } from '@clerk/mosaic/features/user-profile/user-profile-security-panel.view'; |
| 7 | +import { MosaicProvider } from '@clerk/mosaic/MosaicProvider'; |
| 8 | +import { render, screen, waitFor, within } from '@testing-library/react'; |
| 9 | +import userEvent from '@testing-library/user-event'; |
| 10 | +import { describe, expect, it, vi } from 'vitest'; |
| 11 | + |
| 12 | +import { useUserProfileFixture } from './user-profile'; |
| 13 | +import { useUserProfileMfaExample } from './user-profile-mfa-example'; |
| 14 | + |
| 15 | +describe('shared MFA examples', () => { |
| 16 | + it('finishes authenticator setup inside the Profile overlay without closing the Profile', async () => { |
| 17 | + const user = userEvent.setup(); |
| 18 | + function Example() { |
| 19 | + const { pages } = useUserProfileFixture(); |
| 20 | + return ( |
| 21 | + <MosaicProvider> |
| 22 | + <Dialog.Root defaultOpen> |
| 23 | + <Dialog.Popup variant='profile'> |
| 24 | + <UserProfileView |
| 25 | + activePage='security' |
| 26 | + onPageChange={vi.fn()} |
| 27 | + pages={pages} |
| 28 | + /> |
| 29 | + </Dialog.Popup> |
| 30 | + </Dialog.Root> |
| 31 | + </MosaicProvider> |
| 32 | + ); |
| 33 | + } |
| 34 | + render(<Example />); |
| 35 | + const profile = screen.getByRole('dialog'); |
| 36 | + const add = within(profile).getByRole('button', { name: 'Add verification method' }); |
| 37 | + await user.click(add); |
| 38 | + const setup = screen.getByRole('dialog', { name: 'Add 2-step verification' }); |
| 39 | + await user.click(within(setup).getByRole('button', { name: /Authenticator app/ })); |
| 40 | + await user.type(await within(setup).findByRole('textbox', { name: 'Verification code' }), '123456'); |
| 41 | + await within(setup).findByRole('list', { name: 'Backup codes' }); |
| 42 | + await user.click(within(setup).getByRole('button', { name: 'Copy and close' })); |
| 43 | + await waitFor(() => expect(setup).not.toBeInTheDocument()); |
| 44 | + expect(screen.getByRole('dialog')).toBe(profile); |
| 45 | + expect(add).toHaveFocus(); |
| 46 | + expect(within(profile).getByRole('button', { name: 'Manage Authenticator app' })).toBeVisible(); |
| 47 | + }); |
| 48 | + |
| 49 | + it.each(['section', 'security', 'profile'] as const)( |
| 50 | + 'uses the enrollment and backup-code flow in %s', |
| 51 | + async surface => { |
| 52 | + const user = userEvent.setup(); |
| 53 | + const copy = vi.spyOn(navigator.clipboard, 'writeText').mockResolvedValue(); |
| 54 | + function Example() { |
| 55 | + const mfa = useUserProfileMfaExample(); |
| 56 | + const { pages } = useUserProfileFixture(); |
| 57 | + return ( |
| 58 | + <MosaicProvider> |
| 59 | + {surface === 'section' ? ( |
| 60 | + <UserProfileMfaSectionView {...mfa.section} /> |
| 61 | + ) : surface === 'security' ? ( |
| 62 | + <UserProfileSecurityPanelView {...pages.security} /> |
| 63 | + ) : ( |
| 64 | + <UserProfileView |
| 65 | + activePage='security' |
| 66 | + onPageChange={vi.fn()} |
| 67 | + pages={pages} |
| 68 | + /> |
| 69 | + )} |
| 70 | + </MosaicProvider> |
| 71 | + ); |
| 72 | + } |
| 73 | + render(<Example />); |
| 74 | + const add = screen.getByRole('button', { name: 'Add verification method' }); |
| 75 | + await user.click(add); |
| 76 | + const dialog = screen.getByRole('dialog', { name: 'Add 2-step verification' }); |
| 77 | + await user.click(within(dialog).getByRole('button', { name: /SMS verification/ })); |
| 78 | + await user.click(within(dialog).getByRole('button', { name: 'Continue' })); |
| 79 | + const code = await screen.findByRole('textbox', { name: 'Verification code' }); |
| 80 | + await user.type(code, '000000'); |
| 81 | + expect(await screen.findByText('That code is incorrect. Try again.')).toBeVisible(); |
| 82 | + expect(screen.getByRole('dialog')).toBe(dialog); |
| 83 | + await user.clear(code); |
| 84 | + await user.type(code, '123456'); |
| 85 | + await screen.findByRole('list', { name: 'Backup codes' }); |
| 86 | + expect(screen.getByRole('dialog', { name: 'Save your backup codes' })).toBe(dialog); |
| 87 | + await user.click(screen.getByRole('button', { name: 'Copy and close' })); |
| 88 | + await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument()); |
| 89 | + expect(copy).toHaveBeenCalledOnce(); |
| 90 | + expect(add).toHaveFocus(); |
| 91 | + await user.click(screen.getByRole('button', { name: 'Manage Backup codes' })); |
| 92 | + expect(screen.getAllByRole('menuitem')).toHaveLength(1); |
| 93 | + await user.click(screen.getByRole('menuitem', { name: 'Regenerate' })); |
| 94 | + expect(await screen.findByText('demo-new-01')).toBeVisible(); |
| 95 | + }, |
| 96 | + ); |
| 97 | +}); |
0 commit comments