|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import { renderWithLocalizationProvider } from 'fxa-react/lib/test-utils/localizationProvider'; |
| 6 | +import React from 'react'; |
| 7 | +import Subject from './mocks'; |
| 8 | +import userEvent from '@testing-library/user-event'; |
| 9 | +import { screen, waitFor } from '@testing-library/react'; |
| 10 | + |
| 11 | +describe('FormVerifyTotp component', () => { |
| 12 | + it('renders as expected with default props', async () => { |
| 13 | + renderWithLocalizationProvider(<Subject />); |
| 14 | + expect(screen.getByText('Enter 6-digit code')).toBeVisible(); |
| 15 | + expect(screen.getAllByRole('textbox')).toHaveLength(6); |
| 16 | + const button = screen.getByRole('button'); |
| 17 | + expect(button).toHaveTextContent('Submit'); |
| 18 | + }); |
| 19 | + |
| 20 | + describe('submit button', () => { |
| 21 | + it('is disabled on render', () => { |
| 22 | + renderWithLocalizationProvider(<Subject />); |
| 23 | + const button = screen.getByRole('button'); |
| 24 | + expect(button).toHaveTextContent('Submit'); |
| 25 | + expect(button).toBeDisabled(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('is enabled when numbers are typed into all inputs', async () => { |
| 29 | + const user = userEvent.setup(); |
| 30 | + renderWithLocalizationProvider(<Subject />); |
| 31 | + const button = screen.getByRole('button'); |
| 32 | + expect(button).toHaveTextContent('Submit'); |
| 33 | + expect(button).toBeDisabled(); |
| 34 | + |
| 35 | + expect( |
| 36 | + screen.getByRole('textbox', { name: 'Digit 1 of 6' }) |
| 37 | + ).toHaveFocus(); |
| 38 | + |
| 39 | + // type in each input |
| 40 | + for (let i = 1; i <= 6; i++) { |
| 41 | + await waitFor(() => |
| 42 | + user.type( |
| 43 | + screen.getByRole('textbox', { name: `Digit ${i} of 6` }), |
| 44 | + i.toString() |
| 45 | + ) |
| 46 | + ); |
| 47 | + } |
| 48 | + expect(button).toBeEnabled(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('is enabled when numbers are pasted into all inputs', async () => { |
| 52 | + const user = userEvent.setup(); |
| 53 | + renderWithLocalizationProvider(<Subject />); |
| 54 | + const button = screen.getByRole('button'); |
| 55 | + expect(button).toHaveTextContent('Submit'); |
| 56 | + expect(button).toBeDisabled(); |
| 57 | + |
| 58 | + await waitFor(() => { |
| 59 | + user.click(screen.getByRole('textbox', { name: 'Digit 1 of 6' })); |
| 60 | + user.paste('123456'); |
| 61 | + }); |
| 62 | + |
| 63 | + expect(button).toBeEnabled(); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('errors', () => { |
| 68 | + it('are cleared when typing in input', async () => { |
| 69 | + const user = userEvent.setup(); |
| 70 | + renderWithLocalizationProvider( |
| 71 | + <Subject initialErrorMessage="Something went wrong" /> |
| 72 | + ); |
| 73 | + |
| 74 | + expect(screen.getByText('Something went wrong')).toBeVisible(); |
| 75 | + |
| 76 | + await waitFor(() => |
| 77 | + user.type(screen.getByRole('textbox', { name: 'Digit 1 of 6' }), '1') |
| 78 | + ); |
| 79 | + |
| 80 | + expect( |
| 81 | + screen.queryByText('Something went wrong') |
| 82 | + ).not.toBeInTheDocument(); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments