|
| 1 | +import { ThemeProvider } from '@emotion/react' |
| 2 | +import { theme as lightTheme } from '@scaleway/ui' |
| 3 | +import { renderHook } from '@testing-library/react' |
| 4 | +import type { ReactElement } from 'react' |
| 5 | +import { CheckboxField, Form, TextBoxField } from '../../components' |
| 6 | +import { mockErrors } from '../../mocks' |
| 7 | +import { useOnFieldChange } from '../useOnFieldChange' |
| 8 | + |
| 9 | +type FormValues = { |
| 10 | + textBoxName: string |
| 11 | + check: boolean |
| 12 | +} |
| 13 | + |
| 14 | +type Wrapers = { |
| 15 | + children: ReactElement |
| 16 | + initialValues: FormValues |
| 17 | +} |
| 18 | + |
| 19 | +const initial = { |
| 20 | + textBoxName: 'test', |
| 21 | + check: true, |
| 22 | +} |
| 23 | + |
| 24 | +const updated = { |
| 25 | + textBoxName: 'updated', |
| 26 | + check: false, |
| 27 | +} |
| 28 | + |
| 29 | +const Wrapper = ({ children, initialValues }: Wrapers) => ( |
| 30 | + <ThemeProvider theme={lightTheme}> |
| 31 | + <Form<FormValues> |
| 32 | + initialValues={initialValues} |
| 33 | + errors={mockErrors} |
| 34 | + onRawSubmit={() => {}} |
| 35 | + > |
| 36 | + {children} |
| 37 | + <CheckboxField name="check" /> |
| 38 | + <TextBoxField name="textBoxName" type="text" /> |
| 39 | + </Form> |
| 40 | + </ThemeProvider> |
| 41 | +) |
| 42 | + |
| 43 | +describe('useOnFieldChange', () => { |
| 44 | + test('should render correctly', () => { |
| 45 | + const callback = jest.fn((value, values) => { |
| 46 | + expect(value).toBe(updated.textBoxName) |
| 47 | + expect(values).toBe(updated) |
| 48 | + }) |
| 49 | + |
| 50 | + let initialValues = initial |
| 51 | + |
| 52 | + const { result, rerender } = renderHook( |
| 53 | + () => |
| 54 | + useOnFieldChange<FormValues['textBoxName'], FormValues>( |
| 55 | + 'textBoxName', |
| 56 | + // Condition always true, just need to change a value inside the form to trigger this hook |
| 57 | + true, |
| 58 | + callback, |
| 59 | + ), |
| 60 | + { |
| 61 | + wrapper: ({ children }) => ( |
| 62 | + <Wrapper initialValues={initialValues}>{children}</Wrapper> |
| 63 | + ), |
| 64 | + }, |
| 65 | + ) |
| 66 | + |
| 67 | + expect(result.current).toBeUndefined() |
| 68 | + |
| 69 | + expect(callback).toHaveBeenCalledTimes(0) |
| 70 | + |
| 71 | + initialValues = updated |
| 72 | + |
| 73 | + rerender() |
| 74 | + |
| 75 | + expect(callback).toHaveBeenCalledTimes(1) |
| 76 | + }) |
| 77 | + |
| 78 | + test('should render when condition change', () => { |
| 79 | + const callback = jest.fn() |
| 80 | + |
| 81 | + let initialValues = initial |
| 82 | + |
| 83 | + const { result, rerender } = renderHook( |
| 84 | + ({ condition }) => { |
| 85 | + useOnFieldChange<FormValues['textBoxName'], FormValues>( |
| 86 | + 'textBoxName', |
| 87 | + // Condition will depends of rerender({ condition: '' }) |
| 88 | + condition, |
| 89 | + callback, |
| 90 | + ) |
| 91 | + }, |
| 92 | + { |
| 93 | + wrapper: ({ children }) => ( |
| 94 | + <Wrapper initialValues={initialValues}>{children}</Wrapper> |
| 95 | + ), |
| 96 | + |
| 97 | + initialProps: { |
| 98 | + condition: false, |
| 99 | + }, |
| 100 | + }, |
| 101 | + ) |
| 102 | + |
| 103 | + expect(result.current).toBeUndefined() |
| 104 | + |
| 105 | + expect(callback).toHaveBeenCalledTimes(0) |
| 106 | + |
| 107 | + initialValues = updated |
| 108 | + |
| 109 | + rerender({ condition: true }) |
| 110 | + |
| 111 | + expect(callback).toHaveBeenCalledTimes(1) |
| 112 | + }) |
| 113 | +}) |
0 commit comments