|
| 1 | +import type {InternalFormState, MutableState} from 'final-form'; |
| 2 | + |
| 3 | +import {mockServiceFieldName, mockTools} from '../../../../__tests__/helpers.test'; |
| 4 | +import {JsonSchemaType} from '../../../constants'; |
| 5 | +import {setValidationCache, setValidationWaiters} from '../async-validation'; |
| 6 | +import type {ValidationCache, ValidationWaiter} from '../types'; |
| 7 | + |
| 8 | +const createMockWaiterAndCache = (prefix = '') => { |
| 9 | + const fieldName = `fieldName${prefix}`; |
| 10 | + const waiter: ValidationWaiter = { |
| 11 | + schema: {type: JsonSchemaType.String}, |
| 12 | + validator: jest.fn(), |
| 13 | + value: `value${prefix}`, |
| 14 | + }; |
| 15 | + const cache: ValidationCache = {...waiter, result: `result${prefix}`}; |
| 16 | + |
| 17 | + return {cache, fieldName, waiter}; |
| 18 | +}; |
| 19 | + |
| 20 | +const createMockMutableState = (fields: Record<string, any> = {}): MutableState<{}, {}> => { |
| 21 | + const mockFormState = {} as InternalFormState; |
| 22 | + |
| 23 | + return { |
| 24 | + fields, |
| 25 | + formState: mockFormState, |
| 26 | + fieldSubscribers: {}, |
| 27 | + }; |
| 28 | +}; |
| 29 | + |
| 30 | +describe('async-validation', () => { |
| 31 | + describe('setValidationWaiters', () => { |
| 32 | + it('should not modify state if service field does not exist', () => { |
| 33 | + const {fieldName, waiter} = createMockWaiterAndCache(); |
| 34 | + const mutableState = createMockMutableState(); |
| 35 | + |
| 36 | + setValidationWaiters( |
| 37 | + [{serviceFieldName: mockServiceFieldName, waiters: {[fieldName]: waiter}}], |
| 38 | + mutableState, |
| 39 | + mockTools, |
| 40 | + ); |
| 41 | + |
| 42 | + expect(mutableState.fields).toEqual({}); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should not modify state if waiters are not provided', () => { |
| 46 | + const mutableState = createMockMutableState({ |
| 47 | + [mockServiceFieldName]: {data: {}}, |
| 48 | + }); |
| 49 | + |
| 50 | + setValidationWaiters( |
| 51 | + [{serviceFieldName: mockServiceFieldName, waiters: undefined as any}], |
| 52 | + mutableState, |
| 53 | + mockTools, |
| 54 | + ); |
| 55 | + |
| 56 | + expect(mutableState.fields[mockServiceFieldName].data).toEqual({}); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should add waiter to the validation state', () => { |
| 60 | + const {fieldName, waiter} = createMockWaiterAndCache(); |
| 61 | + const mutableState = createMockMutableState({ |
| 62 | + [mockServiceFieldName]: {data: {}}, |
| 63 | + [fieldName]: {}, |
| 64 | + }); |
| 65 | + |
| 66 | + setValidationWaiters( |
| 67 | + [{serviceFieldName: mockServiceFieldName, waiters: {[fieldName]: waiter}}], |
| 68 | + mutableState, |
| 69 | + mockTools, |
| 70 | + ); |
| 71 | + |
| 72 | + expect(mutableState.fields[mockServiceFieldName].data.waiters).toEqual({ |
| 73 | + [fieldName]: waiter, |
| 74 | + }); |
| 75 | + expect(mutableState.fields[fieldName].validating).toBe(true); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should add waiters to the validation state', () => { |
| 79 | + const waiterAndCacheKit = createMockWaiterAndCache(); |
| 80 | + const waiterAndCacheKit2 = createMockWaiterAndCache('2'); |
| 81 | + |
| 82 | + const mutableState = createMockMutableState({ |
| 83 | + [mockServiceFieldName]: {data: {}}, |
| 84 | + [waiterAndCacheKit.fieldName]: {validating: false}, |
| 85 | + [waiterAndCacheKit2.fieldName]: {validating: false}, |
| 86 | + }); |
| 87 | + |
| 88 | + setValidationWaiters( |
| 89 | + [ |
| 90 | + { |
| 91 | + serviceFieldName: mockServiceFieldName, |
| 92 | + waiters: { |
| 93 | + [waiterAndCacheKit.fieldName]: waiterAndCacheKit.waiter, |
| 94 | + [waiterAndCacheKit2.fieldName]: waiterAndCacheKit2.waiter, |
| 95 | + }, |
| 96 | + }, |
| 97 | + ], |
| 98 | + mutableState, |
| 99 | + mockTools, |
| 100 | + ); |
| 101 | + |
| 102 | + expect(mutableState.fields[mockServiceFieldName].data.waiters).toEqual({ |
| 103 | + [waiterAndCacheKit.fieldName]: waiterAndCacheKit.waiter, |
| 104 | + [waiterAndCacheKit2.fieldName]: waiterAndCacheKit2.waiter, |
| 105 | + }); |
| 106 | + expect(mutableState.fields[waiterAndCacheKit.fieldName].validating).toBe(true); |
| 107 | + expect(mutableState.fields[waiterAndCacheKit2.fieldName].validating).toBe(true); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should merge waiters with existing waiters', () => { |
| 111 | + const waiterAndCacheKit = createMockWaiterAndCache(); |
| 112 | + const existingWaiterAndCacheKit = createMockWaiterAndCache('existing'); |
| 113 | + const mutableState = createMockMutableState({ |
| 114 | + [mockServiceFieldName]: { |
| 115 | + data: { |
| 116 | + waiters: { |
| 117 | + [existingWaiterAndCacheKit.fieldName]: existingWaiterAndCacheKit.waiter, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + [waiterAndCacheKit.fieldName]: {}, |
| 122 | + }); |
| 123 | + |
| 124 | + setValidationWaiters( |
| 125 | + [ |
| 126 | + { |
| 127 | + serviceFieldName: mockServiceFieldName, |
| 128 | + waiters: {[waiterAndCacheKit.fieldName]: waiterAndCacheKit.waiter}, |
| 129 | + }, |
| 130 | + ], |
| 131 | + mutableState, |
| 132 | + mockTools, |
| 133 | + ); |
| 134 | + |
| 135 | + expect(mutableState.fields[mockServiceFieldName].data.waiters).toEqual({ |
| 136 | + [existingWaiterAndCacheKit.fieldName]: existingWaiterAndCacheKit.waiter, |
| 137 | + [waiterAndCacheKit.fieldName]: waiterAndCacheKit.waiter, |
| 138 | + }); |
| 139 | + expect(mutableState.fields[waiterAndCacheKit.fieldName].validating).toBe(true); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + describe('setValidationCache', () => { |
| 144 | + it('should not modify state if field does not exist', () => { |
| 145 | + const {cache, fieldName} = createMockWaiterAndCache(); |
| 146 | + const mutableState = createMockMutableState(); |
| 147 | + |
| 148 | + setValidationCache( |
| 149 | + [{serviceFieldName: mockServiceFieldName, cache: {[fieldName]: cache}}], |
| 150 | + mutableState, |
| 151 | + mockTools, |
| 152 | + ); |
| 153 | + |
| 154 | + expect(mutableState.fields).toEqual({}); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should not modify state if cache is not provided', () => { |
| 158 | + const mutableState = createMockMutableState({ |
| 159 | + [mockServiceFieldName]: {data: {}}, |
| 160 | + }); |
| 161 | + |
| 162 | + setValidationCache( |
| 163 | + [{serviceFieldName: mockServiceFieldName, cache: undefined as any}], |
| 164 | + mutableState, |
| 165 | + mockTools, |
| 166 | + ); |
| 167 | + |
| 168 | + expect(mutableState.fields[mockServiceFieldName].data).toEqual({}); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should add cache to the validation state', () => { |
| 172 | + const {cache, fieldName} = createMockWaiterAndCache(); |
| 173 | + const mutableState = createMockMutableState({ |
| 174 | + [mockServiceFieldName]: {data: {}}, |
| 175 | + [fieldName]: {}, |
| 176 | + }); |
| 177 | + |
| 178 | + setValidationCache( |
| 179 | + [{serviceFieldName: mockServiceFieldName, cache: {[fieldName]: cache}}], |
| 180 | + mutableState, |
| 181 | + mockTools, |
| 182 | + ); |
| 183 | + |
| 184 | + expect(mutableState.fields[mockServiceFieldName].data.cache).toEqual({ |
| 185 | + [fieldName]: [cache], |
| 186 | + }); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should append to existing cache', () => { |
| 190 | + const fieldName = 'fieldName'; |
| 191 | + const existingWaiterAndCacheKit = createMockWaiterAndCache('existing'); |
| 192 | + const waiterAndCacheKit = createMockWaiterAndCache(); |
| 193 | + const mutableState = createMockMutableState({ |
| 194 | + [mockServiceFieldName]: { |
| 195 | + data: { |
| 196 | + cache: { |
| 197 | + [fieldName]: [existingWaiterAndCacheKit.cache], |
| 198 | + }, |
| 199 | + }, |
| 200 | + }, |
| 201 | + [fieldName]: {}, |
| 202 | + }); |
| 203 | + |
| 204 | + setValidationCache( |
| 205 | + [ |
| 206 | + { |
| 207 | + serviceFieldName: mockServiceFieldName, |
| 208 | + cache: {[fieldName]: waiterAndCacheKit.cache}, |
| 209 | + }, |
| 210 | + ], |
| 211 | + mutableState, |
| 212 | + mockTools, |
| 213 | + ); |
| 214 | + |
| 215 | + expect(mutableState.fields[mockServiceFieldName].data.cache).toEqual({ |
| 216 | + [fieldName]: [existingWaiterAndCacheKit.cache, waiterAndCacheKit.cache], |
| 217 | + }); |
| 218 | + }); |
| 219 | + |
| 220 | + it('should clear waiter and set validating to false when waiter matches cache', () => { |
| 221 | + const fieldName = 'fieldName'; |
| 222 | + const {cache, waiter} = createMockWaiterAndCache(); |
| 223 | + const mutableState = createMockMutableState({ |
| 224 | + [mockServiceFieldName]: {data: {waiters: {[fieldName]: waiter}}}, |
| 225 | + [fieldName]: {validating: true}, |
| 226 | + }); |
| 227 | + |
| 228 | + setValidationCache( |
| 229 | + [{serviceFieldName: mockServiceFieldName, cache: {[fieldName]: cache}}], |
| 230 | + mutableState, |
| 231 | + mockTools, |
| 232 | + ); |
| 233 | + |
| 234 | + expect(mutableState.fields[mockServiceFieldName].data.waiters).toEqual({}); |
| 235 | + expect(mutableState.fields[fieldName].validating).toBe(false); |
| 236 | + }); |
| 237 | + |
| 238 | + it('should not clear waiter when waiter does not match cache', () => { |
| 239 | + const fieldName = 'fieldName'; |
| 240 | + const {waiter} = createMockWaiterAndCache('1'); |
| 241 | + const {cache} = createMockWaiterAndCache('2'); |
| 242 | + |
| 243 | + const mutableState = createMockMutableState({ |
| 244 | + [mockServiceFieldName]: {data: {waiters: {[fieldName]: waiter}}}, |
| 245 | + [fieldName]: {validating: true}, |
| 246 | + }); |
| 247 | + |
| 248 | + setValidationCache( |
| 249 | + [{serviceFieldName: mockServiceFieldName, cache: {[fieldName]: cache}}], |
| 250 | + mutableState, |
| 251 | + mockTools, |
| 252 | + ); |
| 253 | + |
| 254 | + expect(mutableState.fields[mockServiceFieldName].data.waiters).toEqual({ |
| 255 | + [fieldName]: waiter, |
| 256 | + }); |
| 257 | + expect(mutableState.fields[fieldName].validating).toBe(true); |
| 258 | + }); |
| 259 | + |
| 260 | + it('should handle multiple cache entries', () => { |
| 261 | + const fieldName = 'fieldName'; |
| 262 | + const waiterAndCacheKit = createMockWaiterAndCache(fieldName); |
| 263 | + |
| 264 | + const fieldName2 = 'fieldName2'; |
| 265 | + const waiterAndCacheKit2 = createMockWaiterAndCache(fieldName2); |
| 266 | + |
| 267 | + const mutableState = createMockMutableState({ |
| 268 | + [mockServiceFieldName]: { |
| 269 | + data: { |
| 270 | + waiters: { |
| 271 | + [fieldName]: waiterAndCacheKit.waiter, |
| 272 | + [fieldName2]: waiterAndCacheKit2.waiter, |
| 273 | + }, |
| 274 | + }, |
| 275 | + }, |
| 276 | + [fieldName]: {validating: true}, |
| 277 | + [fieldName2]: {validating: true}, |
| 278 | + }); |
| 279 | + |
| 280 | + setValidationCache( |
| 281 | + [ |
| 282 | + { |
| 283 | + serviceFieldName: mockServiceFieldName, |
| 284 | + cache: { |
| 285 | + [fieldName]: waiterAndCacheKit.cache, |
| 286 | + [fieldName2]: waiterAndCacheKit2.cache, |
| 287 | + }, |
| 288 | + }, |
| 289 | + ], |
| 290 | + mutableState, |
| 291 | + mockTools, |
| 292 | + ); |
| 293 | + |
| 294 | + expect(mutableState.fields[mockServiceFieldName].data.waiters).toEqual({}); |
| 295 | + expect(mutableState.fields[fieldName].validating).toBe(false); |
| 296 | + expect(mutableState.fields[fieldName2].validating).toBe(false); |
| 297 | + expect(mutableState.fields[mockServiceFieldName].data.cache).toEqual({ |
| 298 | + [fieldName]: [waiterAndCacheKit.cache], |
| 299 | + [fieldName2]: [waiterAndCacheKit2.cache], |
| 300 | + }); |
| 301 | + }); |
| 302 | + }); |
| 303 | +}); |
0 commit comments