|
| 1 | +import { it, expect, beforeEach, afterEach, vi } from 'vitest' |
| 2 | +import { renderHook, act } from '@testing-library/react' |
| 3 | +import { useForm, client } from '../src/index' |
| 4 | +import axios from 'axios' |
| 5 | +import { Config } from 'laravel-precognition' |
| 6 | + |
| 7 | +beforeEach(() => { |
| 8 | + vi.mock('axios') |
| 9 | + client.use(axios) |
| 10 | +}) |
| 11 | + |
| 12 | +afterEach(() => { |
| 13 | + vi.restoreAllMocks() |
| 14 | +}) |
| 15 | + |
| 16 | +it('can clear all errors via Inertia\'s clearErrors', () => { |
| 17 | + const { result: form } = renderHook(() => useForm('post', '/register', { |
| 18 | + name: '', |
| 19 | + })) |
| 20 | + |
| 21 | + act(() => form.current.setErrors({ |
| 22 | + name: 'xxxx', |
| 23 | + other: 'xxxx', |
| 24 | + })) |
| 25 | + |
| 26 | + expect(form.current.errors).toEqual({ |
| 27 | + name: 'xxxx', |
| 28 | + other: 'xxxx', |
| 29 | + }) |
| 30 | + |
| 31 | + act(() => form.current.clearErrors()) |
| 32 | + |
| 33 | + expect(form.current.errors).toEqual({}) |
| 34 | + expect(form.current.validator().errors()).toEqual({}) |
| 35 | +}) |
| 36 | + |
| 37 | +it('can clear specific errors via Inertia\'s clearErrors', () => { |
| 38 | + const { result: form } = renderHook(() => useForm('post', '/register', { |
| 39 | + name: '', |
| 40 | + })) |
| 41 | + |
| 42 | + act(() => form.current.setErrors({ |
| 43 | + name: 'xxxx', |
| 44 | + email: 'xxxx', |
| 45 | + other: 'xxxx', |
| 46 | + })) |
| 47 | + |
| 48 | + expect(form.current.errors).toEqual({ |
| 49 | + name: 'xxxx', |
| 50 | + email: 'xxxx', |
| 51 | + other: 'xxxx', |
| 52 | + }) |
| 53 | + |
| 54 | + act(() => form.current.clearErrors('name', 'email')) |
| 55 | + |
| 56 | + expect(form.current.errors).toEqual({ |
| 57 | + other: 'xxxx', |
| 58 | + }) |
| 59 | + expect(form.current.validator().errors()).toEqual({ |
| 60 | + other: ['xxxx'], |
| 61 | + }) |
| 62 | +}) |
| 63 | + |
| 64 | +it('provides default data for validation requests', () => { |
| 65 | + const response = { headers: { precognition: 'true', 'precognition-success': 'true' }, status: 204, data: 'data' } |
| 66 | + |
| 67 | + let config: Config |
| 68 | + axios.request.mockImplementation(async (c: Config) => { |
| 69 | + config = c |
| 70 | + |
| 71 | + return response |
| 72 | + }) |
| 73 | + |
| 74 | + const { result: form } = renderHook(() => useForm('post', '/register', { |
| 75 | + emails: '', |
| 76 | + })) |
| 77 | + |
| 78 | + act(() => form.current.setData('emails', '[email protected], [email protected]')) |
| 79 | + act(() => form.current.validate('emails')) |
| 80 | + |
| 81 | + expect(config!.data.emails).toEqual('[email protected], [email protected]') |
| 82 | + expect(form.current.data.emails).toBe('[email protected], [email protected]') |
| 83 | +}) |
| 84 | + |
| 85 | +it('transforms data for validation requests', () => { |
| 86 | + const response = { headers: { precognition: 'true', 'precognition-success': 'true' }, status: 204, data: 'data' } |
| 87 | + |
| 88 | + let config: Config |
| 89 | + axios.request.mockImplementation(async (c: Config) => { |
| 90 | + config = c |
| 91 | + |
| 92 | + return response |
| 93 | + }) |
| 94 | + |
| 95 | + const { result: form } = renderHook(() => useForm('post', '/register', { |
| 96 | + emails: '', |
| 97 | + })) |
| 98 | + |
| 99 | + act(() => form.current.transform((data) => ({ |
| 100 | + emails: data.emails.split(',').map((email: string) => email.trim()), |
| 101 | + }))) |
| 102 | + |
| 103 | + act(() => form.current.setData('emails', '[email protected], [email protected]')) |
| 104 | + act(() => form.current.validate('emails')) |
| 105 | + |
| 106 | + expect(config!.data.emails).toEqual(['[email protected]', '[email protected]']) |
| 107 | + expect(form.current.data.emails).toBe('[email protected], [email protected]') |
| 108 | +}) |
| 109 | + |
| 110 | +it('can set individual errors', function () { |
| 111 | + const { result: form } = renderHook(() => useForm('post', '/register', { |
| 112 | + name: '', |
| 113 | + })) |
| 114 | + |
| 115 | + act(() => form.current.setError('name', 'The name is required.')) |
| 116 | + |
| 117 | + expect(form.current.errors.name).toBe('The name is required.') |
| 118 | +}) |
| 119 | + |
| 120 | +it('can check that specific fields have been touched', () => { |
| 121 | + const { result: form } = renderHook(() => useForm('post', '/register', { |
| 122 | + name: '', |
| 123 | + email: '', |
| 124 | + })) |
| 125 | + |
| 126 | + expect(form.current.touched('name')).toBe(false) |
| 127 | + expect(form.current.touched('email')).toBe(false) |
| 128 | + |
| 129 | + act(() => form.current.touch('name')) |
| 130 | + |
| 131 | + expect(form.current.touched('name')).toBe(true) |
| 132 | + expect(form.current.touched('email')).toBe(false) |
| 133 | +}) |
| 134 | + |
| 135 | +it('can check it any fields have been touched', () => { |
| 136 | + const { result: form } = renderHook(() => useForm('post', '/register', { |
| 137 | + name: '', |
| 138 | + email: '', |
| 139 | + })) |
| 140 | + |
| 141 | + expect(form.current.touched()).toBe(false) |
| 142 | + |
| 143 | + act(() => form.current.touch('name')) |
| 144 | + |
| 145 | + expect(form.current.touched()).toBe(true) |
| 146 | +}) |
| 147 | + |
| 148 | +it('can set defaults with no arguments', () => { |
| 149 | + let requests = 0 |
| 150 | + axios.request.mockImplementation(async () => { |
| 151 | + requests++ |
| 152 | + }) |
| 153 | + |
| 154 | + const { result: form } = renderHook(() => useForm('post', '/register', { |
| 155 | + name: 'John', |
| 156 | + })) |
| 157 | + |
| 158 | + act(() => form.current.setData('name', 'Jane')) |
| 159 | + act(() => form.current.setDefaults()) |
| 160 | + |
| 161 | + act(() => form.current.setData('name', 'John')) |
| 162 | + act(() => form.current.reset()) |
| 163 | + expect(form.current.data.name).toBe('Jane') |
| 164 | + |
| 165 | + act(() => form.current.validate('name')) |
| 166 | + expect(requests).toBe(0) |
| 167 | +}) |
| 168 | + |
| 169 | +it('can set defaults with an object', () => { |
| 170 | + let requests = 0 |
| 171 | + axios.request.mockImplementation(async () => { |
| 172 | + requests++ |
| 173 | + }) |
| 174 | + |
| 175 | + const { result: form } = renderHook(() => useForm('post', '/register', { |
| 176 | + name: 'John', |
| 177 | + })) |
| 178 | + |
| 179 | + act(() => form.current.setDefaults({ name: 'Jane' })) |
| 180 | + |
| 181 | + act(() => form.current.setData('name', 'John')) |
| 182 | + act(() => form.current.reset()) |
| 183 | + expect(form.current.data.name).toBe('Jane') |
| 184 | + |
| 185 | + act(() => form.current.validate('name')) |
| 186 | + expect(requests).toBe(0) |
| 187 | +}) |
| 188 | + |
| 189 | +it('can set defaults with a function', () => { |
| 190 | + let requests = 0 |
| 191 | + axios.request.mockImplementation(async () => { |
| 192 | + requests++ |
| 193 | + }) |
| 194 | + |
| 195 | + const { result: form } = renderHook(() => useForm('post', '/register', { |
| 196 | + name: 'John', |
| 197 | + })) |
| 198 | + |
| 199 | + act(() => form.current.setDefaults((prevData: any) => { |
| 200 | + expect(prevData).toEqual({ |
| 201 | + name: 'John', |
| 202 | + }) |
| 203 | + |
| 204 | + return { |
| 205 | + name: 'Jane', |
| 206 | + } |
| 207 | + })) |
| 208 | + |
| 209 | + act(() => form.current.setData('name', 'John')) |
| 210 | + act(() => form.current.reset()) |
| 211 | + expect(form.current.data.name).toBe('Jane') |
| 212 | + |
| 213 | + act(() => form.current.validate('name')) |
| 214 | + expect(requests).toBe(0) |
| 215 | +}) |
| 216 | + |
| 217 | +it('can set defaults with a field and value', () => { |
| 218 | + let requests = 0 |
| 219 | + axios.request.mockImplementation(async () => { |
| 220 | + requests++ |
| 221 | + }) |
| 222 | + |
| 223 | + const { result: form } = renderHook(() => useForm('post', '/register', { |
| 224 | + name: 'John', |
| 225 | + })) |
| 226 | + |
| 227 | + act(() => form.current.setDefaults('name', 'Jane')) |
| 228 | + |
| 229 | + act(() => form.current.setData('name', 'John')) |
| 230 | + act(() => form.current.reset()) |
| 231 | + expect(form.current.data.name).toBe('Jane') |
| 232 | + |
| 233 | + act(() => form.current.validate('name')) |
| 234 | + expect(requests).toBe(0) |
| 235 | +}) |
0 commit comments