|
| 1 | +import React from 'react'; |
| 2 | +import { formSetup } from '../__mocks__/setup'; |
| 3 | +import { FFInputEmail } from '../elements/email/email'; |
| 4 | +import { axe } from 'jest-axe'; |
| 5 | +import userEvent from '@testing-library/user-event'; |
| 6 | + |
| 7 | +describe('Email input', () => { |
| 8 | + test('is accessible', async () => { |
| 9 | + const { container } = formSetup({ |
| 10 | + render: <FFInputEmail label="Email" testId="email-input" name="email" />, |
| 11 | + }); |
| 12 | + const results = await axe(container); |
| 13 | + expect(results).toHaveNoViolations(); |
| 14 | + }); |
| 15 | + |
| 16 | + test('values get captured correctly', async () => { |
| 17 | + const testId = 'email-input'; |
| 18 | + const handleSubmit = jest.fn(); |
| 19 | + const { getByText, getByTestId } = formSetup({ |
| 20 | + render: <FFInputEmail label="Email" testId={testId} name="email" />, |
| 21 | + onSubmit: handleSubmit, |
| 22 | + }); |
| 23 | + |
| 24 | + userEvent.type(getByTestId(testId), 'david.alekna@tpr.gov.uk'); |
| 25 | + getByText('Submit').click(); |
| 26 | + |
| 27 | + expect(getByTestId(testId)).toHaveValue('david.alekna@tpr.gov.uk'); |
| 28 | + }); |
| 29 | + |
| 30 | + test('should not accept invalid email addresses', async () => { |
| 31 | + const testId = 'email-input'; |
| 32 | + const handleSubmit = jest.fn(); |
| 33 | + const { getByText, getByTestId, form } = formSetup({ |
| 34 | + render: <FFInputEmail label="Email" testId={testId} name="email" />, |
| 35 | + onSubmit: handleSubmit, |
| 36 | + }); |
| 37 | + |
| 38 | + userEvent.type(getByTestId(testId), 'this is not an email address'); |
| 39 | + getByText('Submit').click(); |
| 40 | + |
| 41 | + expect(form.getState().valid).toBeFalsy(); |
| 42 | + }); |
| 43 | + |
| 44 | + test('accepts only valid emails', async () => { |
| 45 | + const testId = 'email-input'; |
| 46 | + const handleSubmit = jest.fn(); |
| 47 | + const { getByText, getByTestId, form } = formSetup({ |
| 48 | + render: <FFInputEmail label="Email" testId={testId} name="email" />, |
| 49 | + onSubmit: handleSubmit, |
| 50 | + }); |
| 51 | + |
| 52 | + userEvent.type(getByTestId(testId), 'david.alekna@tpr.gov.uk'); |
| 53 | + getByText('Submit').click(); |
| 54 | + |
| 55 | + expect(form.getState().valid).toBeTruthy(); |
| 56 | + }); |
| 57 | + |
| 58 | + test('composes custom validation function', async () => { |
| 59 | + const testId = 'email-input'; |
| 60 | + const errorMessage = 'Must be a TPR email'; |
| 61 | + const handleSubmit = jest.fn(); |
| 62 | + const { getByText, getByTestId, queryByText, form } = formSetup({ |
| 63 | + render: ( |
| 64 | + <FFInputEmail |
| 65 | + label="Email" |
| 66 | + testId={testId} |
| 67 | + name="email" |
| 68 | + validate={(email) => |
| 69 | + email && email.includes('tpr.gov.uk') ? undefined : errorMessage |
| 70 | + } |
| 71 | + /> |
| 72 | + ), |
| 73 | + onSubmit: handleSubmit, |
| 74 | + }); |
| 75 | + |
| 76 | + userEvent.type(getByTestId(testId), 'david.alekna@gmail.com'); |
| 77 | + getByText('Submit').click(); |
| 78 | + |
| 79 | + expect(queryByText(errorMessage)).toBeInTheDocument(); |
| 80 | + expect(form.getState().valid).toBeFalsy(); |
| 81 | + }); |
| 82 | +}); |
0 commit comments