|
| 1 | +import { useFormState } from '../src'; |
| 2 | +import * as TestUtils from './test-utils'; |
| 3 | + |
| 4 | +TestUtils.mockReactUseReducer(); |
| 5 | +TestUtils.mockReactUseCallback(); |
| 6 | +TestUtils.mockReactUseRef(); |
| 7 | + |
| 8 | +describe('Input IDs', () => { |
| 9 | + /** |
| 10 | + * Label only needs a htmlFor |
| 11 | + */ |
| 12 | + it('input method correct props from type "label"', () => { |
| 13 | + const [, input] = useFormState(null, { withIds: true }); |
| 14 | + expect(input.label('name')).toEqual({ |
| 15 | + htmlFor: expect.any(String), |
| 16 | + }); |
| 17 | + }); |
| 18 | + |
| 19 | + it('input method has an "id" prop', () => { |
| 20 | + const [, input] = useFormState(null, { withIds: true }); |
| 21 | + expect(input.text('name')).toHaveProperty('id', expect.any(String)); |
| 22 | + }); |
| 23 | + |
| 24 | + it('generates unique IDs for inputs with different names', () => { |
| 25 | + const [, input] = useFormState(null, { withIds: true }); |
| 26 | + const { id: firstId } = input.text('firstName'); |
| 27 | + const { id: lastId } = input.text('lastName'); |
| 28 | + expect(firstId).not.toBe(lastId); |
| 29 | + }); |
| 30 | + |
| 31 | + it('generates unique IDs for inputs with the same name and different values', () => { |
| 32 | + const [, input] = useFormState(null, { withIds: true }); |
| 33 | + const { id: freeId } = input.radio('plan', 'free'); |
| 34 | + const { id: premiumId } = input.radio('plan', 'premium'); |
| 35 | + expect(freeId).not.toBe(premiumId); |
| 36 | + }); |
| 37 | + |
| 38 | + it('sets matching IDs for inputs and labels', () => { |
| 39 | + const [, input] = useFormState(null, { withIds: true }); |
| 40 | + const { id: inputId } = input.text('name'); |
| 41 | + const { htmlFor: labelId } = input.label('name'); |
| 42 | + expect(labelId).toBe(inputId); |
| 43 | + }); |
| 44 | + |
| 45 | + it('sets matching IDs for inputs and labels with non string values', () => { |
| 46 | + const [, input] = useFormState(null, { withIds: true }); |
| 47 | + const { id: inputId } = input.checkbox('name', 0); |
| 48 | + const { htmlFor: labelId } = input.label('name', 0); |
| 49 | + expect(labelId).toBe(inputId); |
| 50 | + }); |
| 51 | + |
| 52 | + it('sets a custom id when formOptions.withIds is set to a function', () => { |
| 53 | + const customInputFormat = jest.fn((name, value) => |
| 54 | + value ? `form-${name}-${value}` : `form-${name}`, |
| 55 | + ); |
| 56 | + const [, input] = useFormState(null, { withIds: customInputFormat }); |
| 57 | + |
| 58 | + // inputs with own values (e.g. radio button) |
| 59 | + |
| 60 | + const radioProps = input.radio('option', 0); |
| 61 | + expect(radioProps.id).toEqual('form-option-0'); |
| 62 | + expect(customInputFormat).toHaveBeenCalledWith('option', '0'); |
| 63 | + |
| 64 | + const radioLabelProps = input.label('option', 0); |
| 65 | + expect(radioLabelProps.htmlFor).toEqual('form-option-0'); |
| 66 | + expect(customInputFormat).toHaveBeenNthCalledWith(2, 'option', '0'); |
| 67 | + |
| 68 | + // inputs with no own values (e.g. text input) |
| 69 | + |
| 70 | + const textProps = input.text('name'); |
| 71 | + expect(textProps.id).toEqual('form-name'); |
| 72 | + expect(customInputFormat).toHaveBeenLastCalledWith('name'); |
| 73 | + |
| 74 | + const textLabelProps = input.label('name'); |
| 75 | + expect(textLabelProps.htmlFor).toEqual('form-name'); |
| 76 | + expect(customInputFormat).toHaveBeenNthCalledWith(3, 'name'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('does not return IDs when formOptions.withIds is set to false', () => { |
| 80 | + const [, input] = useFormState(); |
| 81 | + const nameInputProps = input.checkbox('name', 0); |
| 82 | + const nameLabelProps = input.label('name', 0); |
| 83 | + expect(nameInputProps).not.toHaveProperty('id'); |
| 84 | + expect(nameLabelProps).not.toHaveProperty('htmlFor'); |
| 85 | + }); |
| 86 | +}); |
0 commit comments