|
1 | | -import { render, screen, waitFor } from '@testing-library/react'; |
| 1 | +import { render, screen } from '@testing-library/react'; |
2 | 2 | import userEvent from '@testing-library/user-event'; |
3 | 3 | import { useState } from 'react'; |
4 | 4 | import { describe, expect, it, vi } from 'vitest'; |
@@ -51,72 +51,6 @@ function VerificationExample({ onSubmit }: Pick<UserProfileAddAuthenticatorDialo |
51 | 51 | } |
52 | 52 |
|
53 | 53 | describe('UserProfileAddAuthenticatorDialog', () => { |
54 | | - it.each([undefined, 'Unable to prepare your authenticator.'])( |
55 | | - 'allows cancellation during preparation: %s', |
56 | | - async setupErrorMessage => { |
57 | | - const user = userEvent.setup(); |
58 | | - const { props } = renderView({ setup: undefined, setupErrorMessage }); |
59 | | - await user.click(screen.getByRole('button', { name: 'Cancel' })); |
60 | | - expect(props.onOpenChange).toHaveBeenCalledWith(false); |
61 | | - expect(props.onRetry).not.toHaveBeenCalled(); |
62 | | - expect(props.onSubmit).not.toHaveBeenCalled(); |
63 | | - }, |
64 | | - ); |
65 | | - |
66 | | - it('copies either manual credential through the caller and displays controlled copy feedback', async () => { |
67 | | - const user = userEvent.setup(); |
68 | | - const onCopy = vi.fn(); |
69 | | - const { props, rerender } = renderView({ onCopy }); |
70 | | - await user.click(screen.getByRole('button', { name: 'Can’t scan? View setup key' })); |
71 | | - const copyKey = screen.getByRole('button', { name: 'Copy setup key' }); |
72 | | - const copyUri = screen.getByRole('button', { name: 'Copy setup URI' }); |
73 | | - await user.click(copyKey); |
74 | | - expect(onCopy).toHaveBeenCalledExactlyOnceWith(setup.secret); |
75 | | - expect(props.onSubmit).not.toHaveBeenCalled(); |
76 | | - |
77 | | - rerender( |
78 | | - <MosaicProvider> |
79 | | - <UserProfileAddAuthenticatorDialog |
80 | | - {...props} |
81 | | - copyStatus='pending' |
82 | | - /> |
83 | | - </MosaicProvider>, |
84 | | - ); |
85 | | - expect(copyKey).toHaveFocus(); |
86 | | - expect(copyKey).toHaveAttribute('aria-disabled', 'true'); |
87 | | - expect(copyUri).toHaveAttribute('aria-disabled', 'true'); |
88 | | - expect(screen.getByRole('status', { name: 'Copy feedback' })).toHaveTextContent('Copying…'); |
89 | | - await user.click(copyUri); |
90 | | - await user.keyboard('{Enter}'); |
91 | | - expect(onCopy).toHaveBeenCalledOnce(); |
92 | | - |
93 | | - rerender( |
94 | | - <MosaicProvider> |
95 | | - <UserProfileAddAuthenticatorDialog |
96 | | - {...props} |
97 | | - copyErrorMessage='Could not copy. Please try again.' |
98 | | - /> |
99 | | - </MosaicProvider>, |
100 | | - ); |
101 | | - expect(screen.getByRole('alert')).toHaveTextContent('Could not copy. Please try again.'); |
102 | | - expect(screen.getByRole('textbox', { name: 'Setup key' })).toHaveValue(setup.secret); |
103 | | - await user.click(copyUri); |
104 | | - expect(onCopy).toHaveBeenLastCalledWith(setup.uri); |
105 | | - |
106 | | - rerender( |
107 | | - <MosaicProvider> |
108 | | - <UserProfileAddAuthenticatorDialog |
109 | | - {...props} |
110 | | - copyStatus='success' |
111 | | - /> |
112 | | - </MosaicProvider>, |
113 | | - ); |
114 | | - expect(screen.queryByRole('alert')).not.toBeInTheDocument(); |
115 | | - expect(screen.getByRole('status', { name: 'Copy feedback' })).toHaveTextContent('Copied'); |
116 | | - expect(screen.getByRole('dialog')).toBeVisible(); |
117 | | - expect(props.onOpenChange).not.toHaveBeenCalled(); |
118 | | - }); |
119 | | - |
120 | 54 | it('shows preparation, offers retry on failure, and waits for setup data before verification', async () => { |
121 | 55 | const user = userEvent.setup(); |
122 | 56 | const { props, rerender } = renderView({ setup: undefined }); |
@@ -177,47 +111,6 @@ describe('UserProfileAddAuthenticatorDialog', () => { |
177 | 111 | expect(props.onSubmit).toHaveBeenCalledExactlyOnceWith('123456'); |
178 | 112 | }); |
179 | 113 |
|
180 | | - it.each(['typing', 'pasting'] as const)('submits a complete authenticator code after %s', async method => { |
181 | | - const user = userEvent.setup(); |
182 | | - const onSubmit = vi.fn(); |
183 | | - render(<VerificationExample onSubmit={onSubmit} />); |
184 | | - const dialog = screen.getByRole('dialog', { name: 'Add an authenticator app' }); |
185 | | - expect(screen.getByRole('img', { name: 'Authenticator setup QR code' })).toBeVisible(); |
186 | | - expect(screen.queryByRole('button', { name: /Resend/ })).not.toBeInTheDocument(); |
187 | | - await waitFor(() => expect(screen.getByRole('button', { name: 'Close', exact: true })).toHaveFocus()); |
188 | | - await user.click(screen.getByRole('textbox', { name: 'Verification code' })); |
189 | | - |
190 | | - if (method === 'typing') { |
191 | | - await user.keyboard('12345'); |
192 | | - expect(onSubmit).not.toHaveBeenCalled(); |
193 | | - await user.keyboard('6'); |
194 | | - } else { |
195 | | - await user.paste('123456'); |
196 | | - } |
197 | | - |
198 | | - expect(onSubmit).toHaveBeenCalledExactlyOnceWith('123456'); |
199 | | - expect(screen.getByRole('dialog')).toBe(dialog); |
200 | | - }); |
201 | | - |
202 | | - it('submits the current code through Verify or the form and does not submit on Cancel', async () => { |
203 | | - const user = userEvent.setup(); |
204 | | - const { props } = renderView({ code: '654321' }); |
205 | | - await user.click(screen.getByRole('button', { name: 'Verify', exact: true })); |
206 | | - expect(props.onSubmit).toHaveBeenCalledExactlyOnceWith('654321'); |
207 | | - |
208 | | - const form = screen.getByRole('textbox', { name: 'Verification code' }).closest('form'); |
209 | | - if (!form) { |
210 | | - throw new Error('Verification form missing'); |
211 | | - } |
212 | | - form.requestSubmit(); |
213 | | - expect(props.onSubmit).toHaveBeenCalledTimes(2); |
214 | | - expect(props.onSubmit).toHaveBeenLastCalledWith('654321'); |
215 | | - |
216 | | - await user.click(screen.getByRole('button', { name: 'Cancel' })); |
217 | | - expect(props.onOpenChange).toHaveBeenCalledWith(false); |
218 | | - expect(props.onSubmit).toHaveBeenCalledTimes(2); |
219 | | - }); |
220 | | - |
221 | 114 | it('blocks incomplete and pending submissions, including native form submission', async () => { |
222 | 115 | const user = userEvent.setup(); |
223 | 116 | const { props, rerender } = renderView({ code: '123' }); |
@@ -251,42 +144,6 @@ describe('UserProfileAddAuthenticatorDialog', () => { |
251 | 144 | expect(props.onSubmit).not.toHaveBeenCalled(); |
252 | 145 | }); |
253 | 146 |
|
254 | | - it('preserves the setup mode and code on failure, then clears feedback when retry begins', async () => { |
255 | | - const user = userEvent.setup(); |
256 | | - const { props, rerender } = renderView({ code: '123456' }); |
257 | | - const dialog = screen.getByRole('dialog'); |
258 | | - await user.click(screen.getByRole('button', { name: 'Can’t scan? View setup key' })); |
259 | | - |
260 | | - rerender( |
261 | | - <MosaicProvider> |
262 | | - <UserProfileAddAuthenticatorDialog |
263 | | - {...props} |
264 | | - errorMessage='That code has expired. Please try again.' |
265 | | - /> |
266 | | - </MosaicProvider>, |
267 | | - ); |
268 | | - expect(screen.getByRole('dialog')).toBe(dialog); |
269 | | - expect(screen.getByRole('textbox', { name: 'Setup key' })).toHaveValue(setup.secret); |
270 | | - expect(screen.getByRole('textbox', { name: 'Setup URI' })).toHaveValue(setup.uri); |
271 | | - const group = screen.getByRole('group', { name: 'Verification code' }); |
272 | | - expect(group).toHaveAccessibleDescription('That code has expired. Please try again.'); |
273 | | - expect(screen.getByRole('textbox', { name: 'Verification code' })).toHaveAttribute('aria-invalid', 'true'); |
274 | | - await user.click(screen.getByRole('button', { name: 'Verify', exact: true })); |
275 | | - expect(props.onSubmit).toHaveBeenCalledExactlyOnceWith('123456'); |
276 | | - |
277 | | - rerender( |
278 | | - <MosaicProvider> |
279 | | - <UserProfileAddAuthenticatorDialog |
280 | | - {...props} |
281 | | - isPending |
282 | | - /> |
283 | | - </MosaicProvider>, |
284 | | - ); |
285 | | - expect(group).not.toHaveAccessibleDescription(); |
286 | | - expect(screen.getByRole('textbox', { name: 'Verification code' })).not.toHaveAttribute('aria-invalid'); |
287 | | - expect(screen.getByRole('textbox', { name: 'Setup key' })).toHaveValue(setup.secret); |
288 | | - }); |
289 | | - |
290 | 147 | it('keeps the entered code when switching between QR and manual setup', async () => { |
291 | 148 | const user = userEvent.setup(); |
292 | 149 | const onSubmit = vi.fn(); |
|
0 commit comments