@@ -8,7 +8,6 @@ import {act} from '@testing-library/react'
8
8
import { ShopperCustomersTypes } from 'commerce-sdk-isomorphic'
9
9
import nock from 'nock'
10
10
import {
11
- assertInvalidateQuery ,
12
11
assertRemoveQuery ,
13
12
assertUpdateQuery ,
14
13
mockMutationEndpoints ,
@@ -18,7 +17,6 @@ import {
18
17
} from '../../test-utils'
19
18
import { ShopperCustomersMutation , useShopperCustomersMutation } from '../ShopperCustomers'
20
19
import { ApiClients , Argument , DataType , RequireKeys } from '../types'
21
- import { NotImplementedError } from '../utils'
22
20
import * as queries from './query'
23
21
24
22
jest . mock ( '../../auth/index.ts' , ( ) => {
@@ -83,20 +81,8 @@ const createOptions = <Mut extends ShopperCustomersMutation>(
83
81
body : Argument < Client [ Mut ] > extends { body : infer B } ? B : undefined
84
82
) : Argument < Client [ Mut ] > => ( { ...queryOptions , body} )
85
83
86
- // Not implemented checks are temporary to make sure we don't forget to add tests when adding
87
- // implentations. When all mutations are added, the "not implemented" tests can be removed,
88
- // and the `TestMap` type can be changed from optional keys to required keys. Doing so will
89
- // leverage TypeScript to enforce having tests for all mutations.
90
- const notImplTestCases : ShopperCustomersMutation [ ] [ ] = [
91
- [ 'deleteCustomerProductList' ] ,
92
- [ 'updateCustomerProductList' ]
93
- ]
94
-
95
84
describe ( 'ShopperCustomers mutations' , ( ) => {
96
85
beforeEach ( ( ) => nock . cleanAll ( ) )
97
- test . each ( notImplTestCases ) ( '`%s` is not yet implemented' , async ( mutationName ) => {
98
- expect ( ( ) => useShopperCustomersMutation ( mutationName ) ) . toThrow ( NotImplementedError )
99
- } )
100
86
describe ( 'modify a customer' , ( ) => {
101
87
test ( '`createCustomerAddress` updates cache on success' , async ( ) => {
102
88
const customer : ShopperCustomersTypes . Customer = { ...baseCustomer , addresses : [ ] }
@@ -123,11 +109,6 @@ describe('ShopperCustomers mutations', () => {
123
109
act ( ( ) => result . current . mutation . mutate ( options ) )
124
110
await waitAndExpectSuccess ( waitForValueToChange , ( ) => result . current . mutation )
125
111
expect ( result . current . mutation . data ) . toEqual ( data )
126
- assertInvalidateQuery ( result . current . customer , customer )
127
-
128
- // 3. Re-render to validate the created resource was added to cache
129
- await rerender ( { enabled : true } )
130
- await waitForValueToChange ( ( ) => result . current . query )
131
112
assertUpdateQuery ( result . current . query , data )
132
113
} )
133
114
test ( '`createCustomerPaymentInstrument` updates cache on success' , async ( ) => {
@@ -165,11 +146,6 @@ describe('ShopperCustomers mutations', () => {
165
146
act ( ( ) => result . current . mutation . mutate ( options ) )
166
147
await waitAndExpectSuccess ( waitForValueToChange , ( ) => result . current . mutation )
167
148
expect ( result . current . mutation . data ) . toEqual ( data )
168
- assertInvalidateQuery ( result . current . customer , customer )
169
-
170
- // 3. Re-render to validate the created resource was added to cache
171
- await rerender ( { enabled : true } )
172
- await waitForValueToChange ( ( ) => result . current . query )
173
149
assertUpdateQuery ( result . current . query , data )
174
150
} )
175
151
test ( '`deleteCustomerPaymentInstrument` updates cache on success' , async ( ) => {
@@ -196,7 +172,6 @@ describe('ShopperCustomers mutations', () => {
196
172
act ( ( ) => result . current . mutation . mutate ( options ) )
197
173
await waitAndExpectSuccess ( waitForValueToChange , ( ) => result . current . mutation )
198
174
expect ( result . current . mutation . data ) . toBeUndefined ( )
199
- assertInvalidateQuery ( result . current . customer , customer )
200
175
assertRemoveQuery ( result . current . query )
201
176
} )
202
177
test ( '`removeCustomerAddress` updates cache on success' , async ( ) => {
@@ -223,7 +198,6 @@ describe('ShopperCustomers mutations', () => {
223
198
act ( ( ) => result . current . mutation . mutate ( options ) )
224
199
await waitAndExpectSuccess ( waitForValueToChange , ( ) => result . current . mutation )
225
200
expect ( result . current . mutation . data ) . toBeUndefined ( )
226
- assertInvalidateQuery ( result . current . customer , customer )
227
201
assertRemoveQuery ( result . current . query )
228
202
} )
229
203
test ( '`updateCustomer` updates cache on success' , async ( ) => {
@@ -251,9 +225,6 @@ describe('ShopperCustomers mutations', () => {
251
225
act ( ( ) => result . current . mutation . mutate ( options ) )
252
226
await waitAndExpectSuccess ( waitForValueToChange , ( ) => result . current . mutation )
253
227
expect ( result . current . mutation . data ) . toEqual ( newCustomer )
254
- // `updateCustomer` invalidates all customer endpoints, which one we check isn't important
255
- // assertInvalidateQuery(result.current.query, oldAddress)
256
- // expect(result.current.customer.data).toEqual(newCustomer)
257
228
assertUpdateQuery ( result . current . customer , newCustomer )
258
229
} )
259
230
test ( '`updateCustomerAddress` updates cache on success' , async ( ) => {
@@ -285,7 +256,6 @@ describe('ShopperCustomers mutations', () => {
285
256
act ( ( ) => result . current . mutation . mutate ( options ) )
286
257
await waitAndExpectSuccess ( waitForValueToChange , ( ) => result . current . mutation )
287
258
expect ( result . current . mutation . data ) . toEqual ( newData )
288
- assertInvalidateQuery ( result . current . customer , customer )
289
259
assertUpdateQuery ( result . current . query , newData )
290
260
} )
291
261
} )
@@ -296,6 +266,15 @@ describe('ShopperCustomers mutations', () => {
296
266
id : 'listId' , // MUST match parameters used
297
267
customerProductListItems : [ ]
298
268
}
269
+ const newlistResult = {
270
+ ...baseListResult ,
271
+ data : [
272
+ {
273
+ ...baseProductList ,
274
+ customerProductListItems : [ ]
275
+ }
276
+ ]
277
+ }
299
278
const options = createOptions < 'createCustomerProductList' > ( data )
300
279
mockQueryEndpoint ( customersEndpoint , listResult ) // getCustomerProductLists
301
280
mockMutationEndpoints ( customersEndpoint , data ) // this mutation
@@ -318,17 +297,87 @@ describe('ShopperCustomers mutations', () => {
318
297
act ( ( ) => result . current . mutation . mutate ( options ) )
319
298
await waitAndExpectSuccess ( waitForValueToChange , ( ) => result . current . mutation )
320
299
expect ( result . current . mutation . data ) . toEqual ( data )
321
- assertInvalidateQuery ( result . current . lists , listResult )
322
-
323
- // 3. Re-render to validate the created resource was added to cache
324
- await rerender ( { enabled : true } )
325
- await waitForValueToChange ( ( ) => result . current . query )
300
+ expect ( result . current . lists . data ) . toEqual ( newlistResult )
326
301
assertUpdateQuery ( result . current . query , data )
327
302
} )
303
+ test ( '`updateCustomerProductList` updates cache on success' , async ( ) => {
304
+ const listResult = baseListResult
305
+ const oldList = baseProductList
306
+ const body : ShopperCustomersTypes . CustomerProductList = { description : 'new description' }
307
+ const newList = { ...oldList , ...body }
308
+ const data = { body, parameters : PARAMETERS }
309
+ const options = createOptions < 'updateCustomerProductList' > ( data )
310
+
311
+ const newListResult = {
312
+ ...listResult ,
313
+ data : [ newList ]
314
+ }
315
+
316
+ mockQueryEndpoint ( customersEndpoint , listResult ) // getCustomerProductLists
317
+ mockMutationEndpoints ( customersEndpoint , newList ) // this mutation
318
+ mockQueryEndpoint ( customersEndpoint , oldList ) // getCustomerProductList
319
+
320
+ mockQueryEndpoint ( customersEndpoint , { test : 'this should not get used' } ) // getCustomerProductList refetch
321
+ const { result, rerender, waitForValueToChange} = renderHookWithProviders (
322
+ ( props : { enabled : boolean } = { enabled : false } ) => ( {
323
+ lists : queries . useCustomerProductLists ( queryOptions ) ,
324
+ mutation : useShopperCustomersMutation ( 'updateCustomerProductList' ) ,
325
+ // Initially disabled; not needed until after the creation mutation
326
+ query : queries . useCustomerProductList ( queryOptions , props )
327
+ } )
328
+ )
329
+
330
+ // 1. Populate cache with initial data
331
+ await waitAndExpectSuccess ( waitForValueToChange , ( ) => result . current . lists )
332
+ expect ( result . current . lists . data ) . toEqual ( listResult )
333
+ expect ( result . current . query . data ) . toBeUndefined ( )
334
+
335
+ // 2. Do creation mutation
336
+ act ( ( ) => result . current . mutation . mutate ( options ) )
337
+ await waitAndExpectSuccess ( waitForValueToChange , ( ) => result . current . mutation )
338
+ expect ( result . current . mutation . data ) . toEqual ( newList )
339
+ expect ( result . current . lists . data ) . toEqual ( newListResult )
340
+ } )
341
+ test ( '`deleteCustomerProductList` updates cache on success' , async ( ) => {
342
+ const listResult = baseListResult
343
+ const options = queryOptions // Can be used for this mutation as it has no body
344
+ const data = undefined
345
+
346
+ mockQueryEndpoint ( customersEndpoint , listResult ) // getCustomerProductLists
347
+ mockMutationEndpoints ( customersEndpoint , data ) // this mutation
348
+ mockQueryEndpoint ( customersEndpoint , { test : 'this should not get used' } ) // getCustomerProductList refetch
349
+ const { result, rerender, waitForValueToChange} = renderHookWithProviders (
350
+ ( props : { enabled : boolean } = { enabled : false } ) => ( {
351
+ lists : queries . useCustomerProductLists ( queryOptions ) ,
352
+ mutation : useShopperCustomersMutation ( 'deleteCustomerProductList' ) ,
353
+ // Initially disabled; not needed until after the creation mutation
354
+ query : queries . useCustomerProductList ( queryOptions , props )
355
+ } )
356
+ )
357
+
358
+ // 1. Populate cache with initial data
359
+ await waitAndExpectSuccess ( waitForValueToChange , ( ) => result . current . lists )
360
+ expect ( result . current . lists . data ) . toEqual ( listResult )
361
+ expect ( result . current . query . data ) . toBeUndefined ( )
362
+
363
+ // 2. Do creation mutation
364
+ act ( ( ) => result . current . mutation . mutate ( options ) )
365
+ await waitAndExpectSuccess ( waitForValueToChange , ( ) => result . current . mutation )
366
+ expect ( result . current . mutation . data ) . toEqual ( data )
367
+ expect ( result . current . lists . data ) . toEqual ( emptyListResult )
368
+ } )
328
369
test ( '`createCustomerProductListItem` updates cache on success' , async ( ) => {
329
370
const data = oldProductListItem
330
- const list = emptyListResult
331
- const listResult = emptyListResult
371
+ const list = baseProductList
372
+ const listResult = baseListResult
373
+ const newList = {
374
+ ...list ,
375
+ customerProductListItems : [ ...list . customerProductListItems , data ]
376
+ }
377
+ const newListResult = {
378
+ ...listResult ,
379
+ data : [ newList ]
380
+ }
332
381
const options = createOptions < 'createCustomerProductListItem' > ( data )
333
382
mockQueryEndpoint ( customersEndpoint , list ) // getCustomerProductList
334
383
mockQueryEndpoint ( customersEndpoint , listResult ) // getCustomerProductLists
@@ -355,19 +404,23 @@ describe('ShopperCustomers mutations', () => {
355
404
act ( ( ) => result . current . mutation . mutate ( options ) )
356
405
await waitAndExpectSuccess ( waitForValueToChange , ( ) => result . current . mutation )
357
406
expect ( result . current . mutation . data ) . toEqual ( data )
358
- assertInvalidateQuery ( result . current . list , list )
359
- assertInvalidateQuery ( result . current . lists , listResult )
360
-
361
- // 3. Re-render to validate the created resource was added to cache
362
- await rerender ( { enabled : true } )
363
- await waitForValueToChange ( ( ) => result . current . query )
407
+ expect ( result . current . list . data ) . toEqual ( newList )
408
+ expect ( result . current . lists . data ) . toEqual ( newListResult )
364
409
assertUpdateQuery ( result . current . query , data )
365
410
} )
366
- test . only ( '`deleteCustomerProductListItem` updates cache on success' , async ( ) => {
411
+ test ( '`deleteCustomerProductListItem` updates cache on success' , async ( ) => {
367
412
const data = oldProductListItem
368
413
const list = baseProductList
369
414
const listResult = baseListResult
370
415
const options = queryOptions // Can be used for this mutation as it has no body
416
+ const newList = {
417
+ ...list ,
418
+ customerProductListItems : [ ]
419
+ }
420
+ const newListResult = {
421
+ ...listResult ,
422
+ data : [ newList ]
423
+ }
371
424
mockQueryEndpoint ( customersEndpoint , list ) // getCustomerProductList
372
425
mockQueryEndpoint ( customersEndpoint , listResult ) // getCustomerProductLists
373
426
mockQueryEndpoint ( customersEndpoint , data ) // getCustomerProductListItem
@@ -391,8 +444,8 @@ describe('ShopperCustomers mutations', () => {
391
444
act ( ( ) => result . current . mutation . mutate ( options ) )
392
445
await waitAndExpectSuccess ( waitForValueToChange , ( ) => result . current . mutation )
393
446
expect ( result . current . mutation . data ) . toBeUndefined ( )
394
- assertInvalidateQuery ( result . current . list , list )
395
- assertInvalidateQuery ( result . current . lists , listResult )
447
+ expect ( result . current . list . data ) . toEqual ( newList )
448
+ expect ( result . current . lists . data ) . toEqual ( newListResult )
396
449
assertRemoveQuery ( result . current . query )
397
450
} )
398
451
test ( '`updateCustomerProductListItem` updates cache on success' , async ( ) => {
@@ -405,6 +458,14 @@ describe('ShopperCustomers mutations', () => {
405
458
}
406
459
const list = baseProductList
407
460
const listResult = baseListResult
461
+ const newList = {
462
+ ...list ,
463
+ customerProductListItems : [ newData ]
464
+ }
465
+ const newListResult = {
466
+ ...listResult ,
467
+ data : [ newList ]
468
+ }
408
469
const options = createOptions < 'updateCustomerProductListItem' > ( newData )
409
470
mockQueryEndpoint ( customersEndpoint , list ) // getCustomerProductList
410
471
mockQueryEndpoint ( customersEndpoint , listResult ) // getCustomerProductLists
@@ -429,8 +490,8 @@ describe('ShopperCustomers mutations', () => {
429
490
act ( ( ) => result . current . mutation . mutate ( options ) )
430
491
await waitAndExpectSuccess ( waitForValueToChange , ( ) => result . current . mutation )
431
492
expect ( result . current . mutation . data ) . toEqual ( newData )
432
- assertInvalidateQuery ( result . current . list , list )
433
- assertInvalidateQuery ( result . current . lists , listResult )
493
+ expect ( result . current . list . data ) . toEqual ( newList )
494
+ expect ( result . current . lists . data ) . toEqual ( newListResult )
434
495
assertUpdateQuery ( result . current . query , newData )
435
496
} )
436
497
} )
0 commit comments