|
| 1 | +import axios from 'axios' |
| 2 | +import { client } from '../src/index' |
| 3 | +import { createValidator } from '../src/validator' |
| 4 | + |
| 5 | +jest.mock('axios') |
| 6 | +client.use(axios) |
| 7 | +jest.useFakeTimers() |
| 8 | + |
| 9 | +test('revalidates data when validate is called', async () => { |
| 10 | + expect.assertions(4) |
| 11 | + |
| 12 | + let requests = 0 |
| 13 | + axios.request.mockImplementation(() => { |
| 14 | + requests++ |
| 15 | + |
| 16 | + return Promise.resolve({ headers: { precognition: 'true' } }) |
| 17 | + }) |
| 18 | + let data |
| 19 | + const validator = createValidator((client) => client.post('/foo', data)) |
| 20 | + |
| 21 | + expect(requests).toBe(0) |
| 22 | + |
| 23 | + data = { name: 'Tim' } |
| 24 | + validator.validate('name', 'Tim') |
| 25 | + expect(requests).toBe(1) |
| 26 | + jest.advanceTimersByTime(1500) |
| 27 | + |
| 28 | + data = { name: 'Jess' } |
| 29 | + validator.validate('name', 'Jess') |
| 30 | + expect(requests).toBe(2) |
| 31 | + jest.advanceTimersByTime(1500) |
| 32 | + |
| 33 | + data = { name: 'Taylor' } |
| 34 | + validator.validate('name', 'Taylor') |
| 35 | + expect(requests).toBe(3) |
| 36 | + jest.advanceTimersByTime(1500) |
| 37 | +}) |
| 38 | + |
| 39 | +test('does not revalidate data when data is unchanged', async () => { |
| 40 | + expect.assertions(4) |
| 41 | + |
| 42 | + let requests = 0 |
| 43 | + axios.request.mockImplementation(() => { |
| 44 | + requests++ |
| 45 | + |
| 46 | + return Promise.resolve({ headers: { precognition: 'true' } }) |
| 47 | + }) |
| 48 | + let data = {} |
| 49 | + const validator = createValidator((client) => client.post('/foo', data)) |
| 50 | + |
| 51 | + expect(requests).toBe(0) |
| 52 | + |
| 53 | + data = { first: true } |
| 54 | + validator.validate('name', true) |
| 55 | + expect(requests).toBe(1) |
| 56 | + jest.advanceTimersByTime(1500) |
| 57 | + |
| 58 | + data = { first: true } |
| 59 | + validator.validate('name', true) |
| 60 | + expect(requests).toBe(1) |
| 61 | + jest.advanceTimersByTime(1500) |
| 62 | + |
| 63 | + data = { second: true } |
| 64 | + validator.validate('name', true) |
| 65 | + expect(requests).toBe(2) |
| 66 | + jest.advanceTimersByTime(1500) |
| 67 | +}) |
| 68 | + |
| 69 | +test('it marks inputs as touched when they have an error', () => { |
| 70 | + expect.assertions(1) |
| 71 | + |
| 72 | + const validator = createValidator((client) => client.post('/foo', {}), { |
| 73 | + name: 'Tim' |
| 74 | + }) |
| 75 | + |
| 76 | + validator.setErrors({ |
| 77 | + name: 'xxxx' |
| 78 | + }) |
| 79 | + |
| 80 | + expect(validator.touched()).toEqual(['name']) |
| 81 | +}) |
| 82 | + |
| 83 | +test('inputs remain touched if they become valid', () => { |
| 84 | + expect.assertions(1) |
| 85 | + |
| 86 | + const validator = createValidator((client) => client.post('/foo', {}), { |
| 87 | + name: 'Tim' |
| 88 | + }) |
| 89 | + |
| 90 | + validator.setErrors({ |
| 91 | + name: 'xxxx' |
| 92 | + }) |
| 93 | + validator.setErrors({ |
| 94 | + // |
| 95 | + }) |
| 96 | + expect(validator.touched()).toEqual(['name']) |
| 97 | +}) |
| 98 | + |
| 99 | +test('setErrors accepts laravel formatted validation errors', () => { |
| 100 | + expect.assertions(1) |
| 101 | + |
| 102 | + const validator = createValidator((client) => client.post('/foo', {}), { |
| 103 | + name: 'Tim', |
| 104 | + location: 'Melbourne', |
| 105 | + }) |
| 106 | + |
| 107 | + validator.setErrors({ |
| 108 | + name: ['xxxx'], |
| 109 | + location: ['xxxx', 'yyyy'], |
| 110 | + }) |
| 111 | + expect(validator.errors()).toEqual({ |
| 112 | + name: ['xxxx'], |
| 113 | + location: ['xxxx', 'yyyy'], |
| 114 | + }) |
| 115 | +}) |
| 116 | + |
| 117 | +test('setErrors accepts inertia formatted validation errors', () => { |
| 118 | + expect.assertions(1) |
| 119 | + |
| 120 | + const validator = createValidator((client) => client.post('/foo', {}), { |
| 121 | + name: 'Tim', |
| 122 | + location: 'Melbourne', |
| 123 | + }) |
| 124 | + |
| 125 | + validator.setErrors({ |
| 126 | + name: 'xxxx', |
| 127 | + location: 'yyyy', |
| 128 | + }) |
| 129 | + expect(validator.errors()).toEqual({ |
| 130 | + name: ['xxxx'], |
| 131 | + location: ['yyyy'], |
| 132 | + }) |
| 133 | +}) |
| 134 | + |
| 135 | +test('it triggers errorsChanged event when setting errors', () => { |
| 136 | + expect.assertions(2) |
| 137 | + |
| 138 | + const validator = createValidator((client) => client.post('/foo', {}), { |
| 139 | + name: 'Tim', |
| 140 | + }) |
| 141 | + let triggered = 0; |
| 142 | + |
| 143 | + validator.on('errorsChanged', () => triggered++) |
| 144 | + |
| 145 | + validator.setErrors({ |
| 146 | + name: 'xxxx', |
| 147 | + }) |
| 148 | + expect(triggered).toEqual(1) |
| 149 | + |
| 150 | + validator.setErrors({ |
| 151 | + name: 'yyyy', |
| 152 | + }) |
| 153 | + expect(triggered).toEqual(2) |
| 154 | +}) |
| 155 | + |
| 156 | +test('it doesnt trigger errorsChanged event when errors are the same', () => { |
| 157 | + expect.assertions(2) |
| 158 | + |
| 159 | + const validator = createValidator((client) => client.post('/foo', {}), { |
| 160 | + name: 'Tim', |
| 161 | + }) |
| 162 | + let triggered = 0; |
| 163 | + |
| 164 | + validator.on('errorsChanged', () => triggered++) |
| 165 | + |
| 166 | + validator.setErrors({ |
| 167 | + name: 'xxxx', |
| 168 | + }) |
| 169 | + expect(triggered).toEqual(1) |
| 170 | + |
| 171 | + validator.setErrors({ |
| 172 | + name: 'xxxx', |
| 173 | + }) |
| 174 | + expect(triggered).toEqual(1) |
| 175 | +}) |
| 176 | + |
| 177 | +test('hasErrors function', () => { |
| 178 | + expect.assertions(3) |
| 179 | + |
| 180 | + const validator = createValidator((client) => client.post('/foo', {}), { |
| 181 | + name: 'Tim', |
| 182 | + }) |
| 183 | + |
| 184 | + expect(validator.hasErrors()).toBe(false) |
| 185 | + |
| 186 | + validator.setErrors({ |
| 187 | + name: 'xxxx', |
| 188 | + }) |
| 189 | + expect(validator.hasErrors()).toBe(true) |
| 190 | + |
| 191 | + validator.setErrors({}) |
| 192 | + expect(validator.hasErrors()).toBe(false) |
| 193 | +}) |
| 194 | + |
| 195 | +test('is not valid before it has been validated', async () => { |
| 196 | + expect.assertions(2) |
| 197 | + |
| 198 | + const validator = createValidator((client) => client.post('/foo', {}), { |
| 199 | + name: 'Tim', |
| 200 | + }) |
| 201 | + |
| 202 | + expect(validator.valid()).toEqual([]) |
| 203 | + |
| 204 | + validator.setErrors({ |
| 205 | + name: 'xxxx', |
| 206 | + }) |
| 207 | + |
| 208 | + expect(validator.valid()).toEqual([]) |
| 209 | +}) |
| 210 | + |
| 211 | +test('it does not validate if the field has not been changed', async () => { |
| 212 | + let requestMade = false |
| 213 | + axios.request.mockImplementation(() => { |
| 214 | + requestMade = true |
| 215 | + return Promise.resolve({ |
| 216 | + status: 201, |
| 217 | + headers: { precognition: 'true' }, |
| 218 | + data: {} |
| 219 | + }) |
| 220 | + }) |
| 221 | + const validator = createValidator((client) => client.post('/foo', {}), { |
| 222 | + name: 'Tim', |
| 223 | + }) |
| 224 | + |
| 225 | + validator.validate('name', 'Tim') |
| 226 | + |
| 227 | + expect(requestMade).toBe(false) |
| 228 | +}) |
| 229 | + |
| 230 | +test('is valid after field has changed and successful validation has triggered', async () => { |
| 231 | + let requestMade = false |
| 232 | + let promise |
| 233 | + axios.request.mockImplementation(() => { |
| 234 | + requestMade = true |
| 235 | + return promise = Promise.resolve({ |
| 236 | + status: 201, |
| 237 | + headers: { precognition: 'true' }, |
| 238 | + data: {} |
| 239 | + }) |
| 240 | + }) |
| 241 | + const validator = createValidator((client) => client.post('/foo', {}), { |
| 242 | + name: 'Tim', |
| 243 | + }) |
| 244 | + |
| 245 | + validator.validate('name', 'Taylor') |
| 246 | + await promise |
| 247 | + |
| 248 | + expect(requestMade).toBe(true) |
| 249 | + expect(validator.valid()).toEqual(['name']) |
| 250 | +}) |
| 251 | + |
| 252 | + |
0 commit comments