|
| 1 | +import { createRef } from 'react'; |
| 2 | + |
| 3 | +import { PasswordInput } from '..'; |
| 4 | + |
| 5 | +import { renderClient, renderServer } from '#util/components'; |
| 6 | +import { type InputWidth } from '#util/types'; |
| 7 | + |
| 8 | +describe('PasswordInput', () => { |
| 9 | + afterEach(() => { |
| 10 | + jest.restoreAllMocks(); |
| 11 | + }); |
| 12 | + |
| 13 | + it('matches snapshot', async () => { |
| 14 | + const { container } = await renderClient( |
| 15 | + <PasswordInput label="What is your NHS number?" labelProps={{ size: 'l' }} id="nhs-number" />, |
| 16 | + { className: 'nhsuk-input' }, |
| 17 | + ); |
| 18 | + |
| 19 | + expect(container).toMatchSnapshot('PasswordInput'); |
| 20 | + }); |
| 21 | + |
| 22 | + it('matches snapshot with HTML in props', async () => { |
| 23 | + const { container } = await renderClient( |
| 24 | + <PasswordInput |
| 25 | + label={ |
| 26 | + <> |
| 27 | + <span className="nhsuk-caption-l">Example</span> Label text |
| 28 | + </> |
| 29 | + } |
| 30 | + labelProps={{ |
| 31 | + isPageHeading: true, |
| 32 | + size: 'l', |
| 33 | + }} |
| 34 | + hint={ |
| 35 | + <> |
| 36 | + Hint text <em>with HTML</em> |
| 37 | + </> |
| 38 | + } |
| 39 | + error={ |
| 40 | + <> |
| 41 | + Error text <em>with HTML</em> |
| 42 | + </> |
| 43 | + } |
| 44 | + id="nhs-number" |
| 45 | + />, |
| 46 | + { className: 'nhsuk-input' }, |
| 47 | + ); |
| 48 | + |
| 49 | + expect(container).toMatchSnapshot(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('matches snapshot (via server)', async () => { |
| 53 | + const { container, element } = await renderServer( |
| 54 | + <PasswordInput label="What is your NHS number?" labelProps={{ size: 'l' }} id="nhs-number" />, |
| 55 | + { className: 'nhsuk-input' }, |
| 56 | + ); |
| 57 | + |
| 58 | + expect(container).toMatchSnapshot('server'); |
| 59 | + |
| 60 | + await renderClient(element, { |
| 61 | + className: 'nhsuk-input', |
| 62 | + hydrate: true, |
| 63 | + container, |
| 64 | + }); |
| 65 | + |
| 66 | + expect(container).toMatchSnapshot('client'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('forwards refs', async () => { |
| 70 | + const groupRef = createRef<HTMLDivElement>(); |
| 71 | + const fieldRef = createRef<HTMLInputElement>(); |
| 72 | + const buttonRef = createRef<HTMLButtonElement>(); |
| 73 | + |
| 74 | + const { container } = await renderClient( |
| 75 | + <PasswordInput |
| 76 | + formGroupProps={{ ref: groupRef }} |
| 77 | + buttonProps={{ ref: buttonRef }} |
| 78 | + ref={fieldRef} |
| 79 | + />, |
| 80 | + { className: 'nhsuk-password-input' }, |
| 81 | + ); |
| 82 | + |
| 83 | + const groupEl = container.querySelector('div'); |
| 84 | + const inputEl = container.querySelector('input'); |
| 85 | + const buttonEl = container.querySelector('button'); |
| 86 | + |
| 87 | + expect(groupRef.current).toBe(groupEl); |
| 88 | + expect(groupRef.current).toHaveClass('nhsuk-form-group'); |
| 89 | + |
| 90 | + expect(fieldRef.current).toBe(inputEl); |
| 91 | + expect(fieldRef.current).toHaveClass('nhsuk-input'); |
| 92 | + |
| 93 | + expect(buttonRef.current).toBe(buttonEl); |
| 94 | + expect(buttonRef.current).toHaveClass('nhsuk-password-input__toggle'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should handle DOM events where ref exists', async () => { |
| 98 | + const ref = createRef<HTMLInputElement>(); |
| 99 | + const mock = jest.fn(); |
| 100 | + |
| 101 | + const handleClick = () => { |
| 102 | + if (!ref.current) return; |
| 103 | + mock(); |
| 104 | + }; |
| 105 | + |
| 106 | + const { modules } = await renderClient(<PasswordInput onClick={handleClick} ref={ref} />, { |
| 107 | + className: 'nhsuk-input', |
| 108 | + }); |
| 109 | + |
| 110 | + const [inputEl] = modules; |
| 111 | + inputEl.click(); |
| 112 | + |
| 113 | + expect(mock).toHaveBeenCalledTimes(1); |
| 114 | + }); |
| 115 | + |
| 116 | + it.each<InputWidth | undefined>([undefined, '5', '10'])( |
| 117 | + 'Sets the provided input width if specified with %s', |
| 118 | + async (width) => { |
| 119 | + const { modules } = await renderClient(<PasswordInput width={width} />, { |
| 120 | + className: 'nhsuk-input', |
| 121 | + }); |
| 122 | + |
| 123 | + const [inputEl] = modules; |
| 124 | + |
| 125 | + if (width) { |
| 126 | + expect(inputEl).toHaveClass(`nhsuk-input--width-${width}`); |
| 127 | + } else { |
| 128 | + expect(inputEl.className.indexOf('nhsuk-input--width')).toBe(-1); |
| 129 | + } |
| 130 | + }, |
| 131 | + ); |
| 132 | + |
| 133 | + it('sets the error class when error message is provided', async () => { |
| 134 | + const { modules } = await renderClient( |
| 135 | + <> |
| 136 | + <PasswordInput error={undefined} /> |
| 137 | + <PasswordInput error="Enter password" /> |
| 138 | + </>, |
| 139 | + { className: 'nhsuk-input' }, |
| 140 | + ); |
| 141 | + |
| 142 | + const [inputEl1, inputEl2] = modules; |
| 143 | + |
| 144 | + expect(inputEl1).not.toHaveClass('nhsuk-input--error'); |
| 145 | + expect(inputEl2).toHaveClass('nhsuk-input--error'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('sets the provided input width if specified', async () => { |
| 149 | + const { modules } = await renderClient(<PasswordInput width="5" />, { |
| 150 | + className: 'nhsuk-input', |
| 151 | + }); |
| 152 | + |
| 153 | + const [inputEl] = modules; |
| 154 | + |
| 155 | + expect(inputEl).toHaveClass('nhsuk-input--width-5'); |
| 156 | + }); |
| 157 | +}); |
0 commit comments