Skip to content

Commit a129664

Browse files
fix(swingset): reuse MFA flows in Security and Profile examples
1 parent e99c4f8 commit a129664

6 files changed

Lines changed: 123 additions & 102 deletions

File tree

‎packages/swingset/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"dev": "next dev --port 6006",
99
"format": "node ../../scripts/format-package.mjs",
1010
"format:check": "node ../../scripts/format-package.mjs --check",
11-
"test": "vitest run --config ../mosaic/vitest.config.mts --root ."
11+
"test": "vitest run"
1212
},
1313
"dependencies": {
1414
"@base-ui/react": "^1.5.0",
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
});

‎packages/swingset/src/stories/fixtures/user-profile.ts‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import type {
1010
} from '@clerk/mosaic/features/user-profile/user-profile-profile-panel.view';
1111
import type {
1212
UserProfileDevice,
13-
UserProfileMfaMethod,
1413
UserProfilePasskey,
1514
} from '@clerk/mosaic/features/user-profile/user-profile-security-panel.view';
1615
import { useMemo, useState } from 'react';
@@ -22,6 +21,7 @@ import { useConnectedAccountsFixture } from './user-profile-connected-accounts';
2221
import { useUserProfileEditNameFixture } from './user-profile-edit-name';
2322
import { useUserProfileEditPasswordFixture } from './user-profile-edit-password';
2423
import { useUserProfileEditUsernameFixture } from './user-profile-edit-username';
24+
import { useUserProfileMfaExample } from './user-profile-mfa-example';
2525
import { useWeb3WalletsFixture } from './user-profile-web3-wallets';
2626

2727
export interface UserProfileFixtureOptions {
@@ -57,6 +57,7 @@ export function useUserProfileFixture({ onAddEmail }: UserProfileFixtureOptions
5757
const editName = useUserProfileEditNameFixture();
5858
const editUsername = useUserProfileEditUsernameFixture();
5959
const editPassword = useUserProfileEditPasswordFixture();
60+
const mfa = useUserProfileMfaExample();
6061
const [activePage, setActivePage] = useState<UserProfileViewProps['activePage']>('account');
6162
const [emails, setEmails] = useState<UserProfileEmail[]>([
6263
{ id: 'email_1', value: 'preston@clerk.dev', isDefault: true, isVerified: true },
@@ -73,10 +74,6 @@ export function useUserProfileFixture({ onAddEmail }: UserProfileFixtureOptions
7374
lastUsedAtLabel: 'Last used 1h ago',
7475
},
7576
]);
76-
const [mfaMethods, setMfaMethods] = useState<UserProfileMfaMethod[]>([
77-
{ id: 'sms', type: 'sms', description: '+1 801-888-8181' },
78-
{ id: 'backup', type: 'backup-codes' },
79-
]);
8077
const [devices, setDevices] = useState<UserProfileDevice[]>([
8178
{
8279
id: 'current',
@@ -167,13 +164,8 @@ export function useUserProfileFixture({ onAddEmail }: UserProfileFixtureOptions
167164
security: {
168165
...editPassword,
169166
passkeys,
170-
mfaMethods,
167+
...mfa.security,
171168
devices,
172-
onAddMfaMethod: type =>
173-
setMfaMethods(current => [
174-
...current,
175-
{ id: `${type}-${Date.now()}`, type, description: type === 'sms' ? '+1 801-555-0100' : undefined },
176-
]),
177169
onAddPasskey: () =>
178170
setPasskeys(current => [
179171
...current,
@@ -182,8 +174,6 @@ export function useUserProfileFixture({ onAddEmail }: UserProfileFixtureOptions
182174
onDeleteAccount: () => Promise.resolve(),
183175
onManageDevice: () => undefined,
184176
onManagePasskey: () => undefined,
185-
onRegenerateBackupCodes: () => undefined,
186-
onRemoveMfaMethod: id => setMfaMethods(current => current.filter(method => method.id !== id)),
187177
onRemovePasskey: id => setPasskeys(current => current.filter(passkey => passkey.id !== id)),
188178
onSignOutAllOtherDevices: () => setDevices(current => current.filter(device => device.isCurrent)),
189179
onSignOutDevice: id => setDevices(current => current.filter(device => device.id !== id)),
Lines changed: 3 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
import type {
2-
UserProfileDevice,
3-
UserProfileMfaMethod,
4-
UserProfilePasskey,
5-
} from '@clerk/mosaic/features/user-profile/user-profile-security-panel.view';
61
import { UserProfileSecurityPanelView } from '@clerk/mosaic/features/user-profile/user-profile-security-panel.view';
7-
import { useState } from 'react';
82

93
import type { StoryMeta } from '@/lib/types';
104

11-
import { useUserProfileEditPasswordFixture } from './fixtures/user-profile-edit-password';
5+
import { useUserProfileFixture } from './fixtures/user-profile';
126

137
export { default as __source } from './user-profile-security-panel.stories?raw';
148

@@ -22,84 +16,6 @@ export const meta: StoryMeta = {
2216
};
2317

2418
export function Default() {
25-
const editPassword = useUserProfileEditPasswordFixture();
26-
const [passkeys, setPasskeys] = useState<UserProfilePasskey[]>([
27-
{
28-
id: 'passkey',
29-
name: 'Passkey',
30-
createdAtLabel: 'Created today at 10:12 PM',
31-
lastUsedAtLabel: 'Last used 1h ago',
32-
},
33-
]);
34-
const [mfaMethods, setMfaMethods] = useState<UserProfileMfaMethod[]>([
35-
{ id: 'sms', type: 'sms', description: '+1 801-888-8181' },
36-
{ id: 'backup', type: 'backup-codes' },
37-
]);
38-
const [devices, setDevices] = useState<UserProfileDevice[]>([
39-
{
40-
id: 'current',
41-
name: 'Safari on macOS',
42-
description: 'Salt Lake City, UT, United States',
43-
type: 'desktop',
44-
isCurrent: true,
45-
},
46-
{
47-
id: 'mobile',
48-
name: 'Safari on iOS',
49-
description: 'Last seen 2 weeks ago · Orem, UT, United States',
50-
type: 'mobile',
51-
},
52-
{
53-
id: 'desktop',
54-
name: 'Clerk App on macOS',
55-
description: 'Last seen May 14th, 2026 · San Francisco, CA, United States',
56-
type: 'desktop',
57-
},
58-
]);
59-
60-
return (
61-
<UserProfileSecurityPanelView
62-
{...editPassword}
63-
devices={devices}
64-
mfaMethods={mfaMethods}
65-
addableMfaMethods={
66-
mfaMethods.some(method => method.type === 'authenticator') ? ['sms'] : ['sms', 'authenticator']
67-
}
68-
passkeys={passkeys}
69-
onAddMfaMethod={type =>
70-
setMfaMethods(current => {
71-
const timestamp = Date.now();
72-
return [
73-
...current,
74-
{
75-
id: `${type}-${timestamp}`,
76-
type,
77-
description: type === 'sms' ? '+1 801-555-0100' : undefined,
78-
},
79-
...(current.some(method => method.type === 'backup-codes')
80-
? []
81-
: [{ id: `backup-${timestamp}`, type: 'backup-codes' as const }]),
82-
];
83-
})
84-
}
85-
onAddPasskey={() =>
86-
setPasskeys(current => [
87-
...current,
88-
{ id: `passkey-${Date.now()}`, name: `Passkey ${current.length + 1}`, createdAtLabel: 'Created just now' },
89-
])
90-
}
91-
onDeleteAccount={() => Promise.resolve()}
92-
onManageDevice={() => undefined}
93-
onManagePasskey={() => undefined}
94-
onRegenerateBackupCodes={() =>
95-
setMfaMethods(current =>
96-
current.map(method => (method.type === 'backup-codes' ? { ...method, description: 'Just now' } : method)),
97-
)
98-
}
99-
onRemoveMfaMethod={id => setMfaMethods(current => current.filter(method => method.id !== id))}
100-
onRemovePasskey={id => setPasskeys(current => current.filter(passkey => passkey.id !== id))}
101-
onSignOutAllOtherDevices={() => setDevices(current => current.filter(device => device.isCurrent))}
102-
onSignOutDevice={id => setDevices(current => current.filter(device => device.id !== id))}
103-
/>
104-
);
19+
const { pages } = useUserProfileFixture();
20+
return <UserProfileSecurityPanelView {...pages.security} />;
10521
}

‎packages/swingset/tsconfig.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
}
2323
],
2424
"allowJs": true,
25+
"allowImportingTsExtensions": true,
2526
"noEmit": true,
2627
"incremental": true
2728
},
28-
"include": ["mdx-components.tsx", "next.config.mjs", "src", ".next/types/**/*.ts"],
29+
"include": ["mdx-components.tsx", "next.config.mjs", "vitest.config.mts", "src", ".next/types/**/*.ts"],
2930
"exclude": ["node_modules"]
3031
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { resolve } from 'node:path';
2+
3+
import { mergeConfig } from 'vitest/config';
4+
5+
import mosaicConfig from '../mosaic/vitest.config.mts';
6+
7+
export default mergeConfig(mosaicConfig, {
8+
oxc: {
9+
jsx: { runtime: 'automatic' },
10+
},
11+
resolve: {
12+
alias: {
13+
'@clerk/mosaic': resolve(import.meta.dirname, '../mosaic/src'),
14+
'@': resolve(import.meta.dirname, 'src'),
15+
},
16+
},
17+
});

0 commit comments

Comments
 (0)