-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathmutation.test.ts
More file actions
644 lines (605 loc) · 31.8 KB
/
mutation.test.ts
File metadata and controls
644 lines (605 loc) · 31.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/*
* Copyright (c) 2023, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import {act} from '@testing-library/react'
import {ShopperCustomersTypes} from 'commerce-sdk-isomorphic'
import nock from 'nock'
import {
assertRemoveQuery,
assertUpdateQuery,
mockMutationEndpoints,
mockQueryEndpoint,
renderHookWithProviders,
waitAndExpectSuccess
} from '../../test-utils'
import {ShopperCustomersMutation, useShopperCustomersMutation} from '../ShopperCustomers'
import {ApiClients, Argument, DataType, RequireKeys} from '../types'
import * as queries from './query'
import {CLIENT_KEYS} from '../../constant'
jest.mock('../../auth/index.ts', () => {
const {default: mockAuth} = jest.requireActual('../../auth/index.ts')
mockAuth.prototype.ready = jest.fn().mockResolvedValue({access_token: 'access_token'})
return mockAuth
})
const CLIENT_KEY = CLIENT_KEYS.SHOPPER_CUSTOMERS
type Client = NonNullable<ApiClients[typeof CLIENT_KEY]>
const customersEndpoint = '/customer/shopper-customers/'
/** All Shopper Customers parameters. Can be used for all endpoints, as unused params are ignored. */
const PARAMETERS = {
addressName: 'address', // Not 'addressName' because sometimes it's used as `addressId`
customerId: 'customerId',
itemId: 'itemId',
listId: 'listId',
paymentInstrumentId: 'paymentInstrumentId'
} as const
/** Options object that can be used for all query endpoints. */
const queryOptions = {parameters: PARAMETERS}
const oldAddress: ShopperCustomersTypes.CustomerAddress = {
addressId: 'address',
countryCode: 'CA',
lastName: 'Doofenschmirtz'
}
const oldProductListItem: ShopperCustomersTypes.CustomerProductListItem = {
id: 'itemId', // MUST match parameters
priority: 0,
public: false,
quantity: 0
}
const basePaymentInstrument: ShopperCustomersTypes.CustomerPaymentInstrument = {
paymentBankAccount: {},
paymentCard: {cardType: 'fake'},
paymentInstrumentId: 'paymentInstrumentId',
paymentMethodId: 'paymentMethodId',
default: false
}
const baseCustomer: RequireKeys<
ShopperCustomersTypes.Customer,
'addresses' | 'paymentInstruments'
> = {
customerId: 'customerId',
addresses: [oldAddress],
paymentInstruments: [basePaymentInstrument]
}
const baseProductList = {
id: 'listId', // MUST match parameters used
customerProductListItems: [oldProductListItem]
}
const emptyListResult: ShopperCustomersTypes.CustomerProductListResult = {
data: [],
limit: 0,
total: 0
}
const baseListResult: ShopperCustomersTypes.CustomerProductListResult = {
data: [baseProductList],
limit: 1,
total: 1
}
const createOptions = <Mut extends ShopperCustomersMutation>(
body: Argument<Client[Mut]> extends {body: infer B} ? B : undefined
): Argument<Client[Mut]> => ({...queryOptions, body})
describe('ShopperCustomers mutations', () => {
beforeEach(() => nock.cleanAll())
describe('modify a customer', () => {
test('`createCustomerAddress` updates cache on success', async () => {
const customer: ShopperCustomersTypes.Customer = {...baseCustomer, addresses: []}
const options = createOptions<'createCustomerAddress'>(oldAddress)
const data: DataType<Client['createCustomerAddress']> = oldAddress
mockQueryEndpoint(customersEndpoint, customer) // getCustomer
mockMutationEndpoints(customersEndpoint, data) // this mutation
mockQueryEndpoint(customersEndpoint, {test: 'this should not get used'}) // getCustomer refetch
const {result} = renderHookWithProviders(
(props: {enabled: boolean} = {enabled: false}) => ({
customer: queries.useCustomer(queryOptions),
mutation: useShopperCustomersMutation('createCustomerAddress'),
// Initially disabled; not needed until after the creation mutation
query: queries.useCustomerAddress(queryOptions, props)
})
)
// 1. Populate cache with initial data
await waitAndExpectSuccess(() => result.current.customer)
expect(result.current.customer.data).toEqual(customer)
expect(result.current.query.data).toBeUndefined()
// 2. Do creation mutation
act(() => result.current.mutation.mutate(options))
await waitAndExpectSuccess(() => result.current.mutation)
expect(result.current.mutation.data).toEqual(data)
assertUpdateQuery(result.current.query, data)
})
test('`createCustomerPaymentInstrument` updates cache on success', async () => {
// 0. Setup
const customer: ShopperCustomersTypes.Customer = {
...baseCustomer,
paymentInstruments: []
}
const options = createOptions<'createCustomerPaymentInstrument'>({
bankRoutingNumber: 'bank',
giftCertificateCode: 'code',
// Type assertion so we don't need the full type
paymentCard: {} as ShopperCustomersTypes.CustomerPaymentCardRequest,
paymentMethodId: 'paymentMethodId'
})
const data: DataType<Client['createCustomerPaymentInstrument']> = basePaymentInstrument
mockQueryEndpoint(customersEndpoint, customer) // getCustomer
mockMutationEndpoints(customersEndpoint, data) // this mutation
mockQueryEndpoint(customersEndpoint, {test: 'this should not get used'}) // getCustomer refetch
const {result} = renderHookWithProviders(
(props: {enabled: boolean} = {enabled: false}) => ({
customer: queries.useCustomer(queryOptions),
mutation: useShopperCustomersMutation('createCustomerPaymentInstrument'),
// Initially disabled; we don't want to trigger it until after the creation mutation
query: queries.useCustomerPaymentInstrument(queryOptions, props)
})
)
// 1. Populate cache with initial data
await waitAndExpectSuccess(() => result.current.customer)
expect(result.current.customer.data).toEqual(customer)
expect(result.current.query.data).toBeUndefined()
// 2. Do creation mutation
act(() => result.current.mutation.mutate(options))
await waitAndExpectSuccess(() => result.current.mutation)
expect(result.current.mutation.data).toEqual(data)
assertUpdateQuery(result.current.query, data)
})
test('`deleteCustomerPaymentInstrument` updates cache on success', async () => {
// 0. Setup
const customer = baseCustomer
const options = queryOptions // Can be used for this mutation as it has no body
const data = basePaymentInstrument
mockQueryEndpoint(customersEndpoint, customer) // getCustomer
mockQueryEndpoint(customersEndpoint, data) // query
mockMutationEndpoints(customersEndpoint, {}) // this mutation
mockQueryEndpoint(customersEndpoint, {test: 'this should not get used'}) // getCustomer refetch
const {result} = renderHookWithProviders(() => ({
customer: queries.useCustomer(queryOptions),
mutation: useShopperCustomersMutation('deleteCustomerPaymentInstrument'),
query: queries.useCustomerPaymentInstrument(queryOptions)
}))
// 1. Populate cache with initial data
await waitAndExpectSuccess(() => result.current.customer)
expect(result.current.customer.data).toEqual(customer)
expect(result.current.query.data).toEqual(data)
// 2. Do deletion mutation
act(() => result.current.mutation.mutate(options))
await waitAndExpectSuccess(() => result.current.mutation)
expect(result.current.mutation.data).toBeUndefined()
assertRemoveQuery(result.current.query)
})
test('`updateCustomerPaymentInstrument` updates cache on success', async () => {
// 0. Setup
const customer = baseCustomer
const oldData = basePaymentInstrument
const newData: ShopperCustomersTypes.CustomerPaymentInstrument = {
...basePaymentInstrument,
default: true
}
const options = createOptions<'updateCustomerPaymentInstrument'>({
// Only updating default flag for this test
default: true as any
})
mockQueryEndpoint(customersEndpoint, customer) // getCustomer
mockQueryEndpoint(customersEndpoint, oldData) // getCustomerPaymentInstrument
mockMutationEndpoints(customersEndpoint, newData) // this mutation
mockQueryEndpoint(customersEndpoint, {test: 'this should not get used'}) // getCustomer refetch
mockQueryEndpoint(customersEndpoint, {test: 'this should not get used'}) // getCustomerPaymentInstrument refetch
const {result} = renderHookWithProviders(() => ({
customer: queries.useCustomer(queryOptions),
mutation: useShopperCustomersMutation('updateCustomerPaymentInstrument'),
query: queries.useCustomerPaymentInstrument(queryOptions)
}))
// 1. Populate cache with initial data
await waitAndExpectSuccess(() => result.current.customer)
await waitAndExpectSuccess(() => result.current.query)
expect(result.current.customer.data).toEqual(customer)
expect(result.current.query.data).toEqual(oldData)
// 2. Do update mutation
act(() => result.current.mutation.mutate(options))
await waitAndExpectSuccess(() => result.current.mutation)
expect(result.current.mutation.data).toEqual(newData)
// query updated
assertUpdateQuery(result.current.query, newData)
// customer cache updated (instrument replaced)
const expectedCustomer = {
...customer,
paymentInstruments: [newData]
}
assertUpdateQuery(result.current.customer, expectedCustomer as any)
})
test('`removeCustomerAddress` updates cache on success', async () => {
// 0. Setup
const customer = baseCustomer
const options = queryOptions // can be used for this mutation as it has no body
const queryData = oldAddress
mockQueryEndpoint(customersEndpoint, customer) // getCustomer
mockQueryEndpoint(customersEndpoint, queryData) // query
mockMutationEndpoints(customersEndpoint, {}) // this mutation
mockQueryEndpoint(customersEndpoint, {test: 'this should not get used'}) // getCustomer refetch
const {result} = renderHookWithProviders(() => ({
customer: queries.useCustomer(queryOptions),
mutation: useShopperCustomersMutation('removeCustomerAddress'),
query: queries.useCustomerAddress(queryOptions)
}))
// 1. Populate cache with initial data
await waitAndExpectSuccess(() => result.current.customer)
expect(result.current.customer.data).toEqual(customer)
expect(result.current.query.data).toEqual(queryData)
// 2. Do deletion mutation
act(() => result.current.mutation.mutate(options))
await waitAndExpectSuccess(() => result.current.mutation)
expect(result.current.mutation.data).toBeUndefined()
assertRemoveQuery(result.current.query)
})
test('`updateCustomer` updates cache on success', async () => {
// 0. Setup
const oldCustomer = baseCustomer
const body: ShopperCustomersTypes.Customer = {email: 'new@email'}
const newCustomer = {...oldCustomer, ...body}
const options = {body, parameters: PARAMETERS}
mockQueryEndpoint(customersEndpoint, oldCustomer) // getCustomer
mockMutationEndpoints(customersEndpoint, newCustomer) // this mutation
mockQueryEndpoint(customersEndpoint, oldAddress) // query
mockQueryEndpoint(customersEndpoint, {test: 'this should not get used'}) // getCustomer refetch
mockQueryEndpoint(customersEndpoint, {test: 'this should not get used'}) // query refetch
const {result} = renderHookWithProviders(() => ({
customer: queries.useCustomer(queryOptions),
mutation: useShopperCustomersMutation('updateCustomer'),
query: queries.useCustomerAddress(queryOptions)
}))
// 1. Populate cache with initial data
await waitAndExpectSuccess(() => result.current.customer)
expect(result.current.customer.data).toEqual(oldCustomer)
// 2. Do update mutation
act(() => result.current.mutation.mutate(options))
await waitAndExpectSuccess(() => result.current.mutation)
expect(result.current.mutation.data).toEqual(newCustomer)
assertUpdateQuery(result.current.customer, newCustomer)
})
test('`updateCustomerAddress` updates cache on success', async () => {
const customer = baseCustomer
const oldData = oldAddress
const newData: ShopperCustomersTypes.CustomerAddress = {
addressId: 'address',
countryCode: 'US',
lastName: 'Doofenschmirtz'
}
const options = createOptions<'updateCustomerAddress'>(newData)
mockQueryEndpoint(customersEndpoint, customer) // getCustomer
mockQueryEndpoint(customersEndpoint, oldData) // getCustomerAddress
mockMutationEndpoints(customersEndpoint, newData) // this mutation
mockQueryEndpoint(customersEndpoint, {test: 'this should not get used'}) // getCustomer refetch
mockQueryEndpoint(customersEndpoint, {test: 'this should not get used'}) // getCustomerAddress refetch
const {result} = renderHookWithProviders(() => ({
customer: queries.useCustomer(queryOptions),
mutation: useShopperCustomersMutation('updateCustomerAddress'),
query: queries.useCustomerAddress(queryOptions)
}))
// 1. Populate cache with initial data
await waitAndExpectSuccess(() => result.current.customer)
expect(result.current.customer.data).toEqual(customer)
expect(result.current.query.data).toEqual(oldData)
// 2. Do mutation
act(() => result.current.mutation.mutate(options))
await waitAndExpectSuccess(() => result.current.mutation)
expect(result.current.mutation.data).toEqual(newData)
assertUpdateQuery(result.current.query, newData)
})
})
describe('modify a customer product list', () => {
test('`createCustomerProductList` updates cache on success', async () => {
const listResult = emptyListResult
const data: ShopperCustomersTypes.CustomerProductList = {
id: 'listId', // MUST match parameters used
customerProductListItems: []
}
const newlistResult = {
...baseListResult,
data: [
{
...baseProductList,
customerProductListItems: []
}
]
}
const options = createOptions<'createCustomerProductList'>(data)
mockQueryEndpoint(customersEndpoint, listResult) // getCustomerProductLists
mockMutationEndpoints(customersEndpoint, data) // this mutation
mockQueryEndpoint(customersEndpoint, {test: 'this should not get used'}) // getCustomerProductList refetch
const {result} = renderHookWithProviders(
(props: {enabled: boolean} = {enabled: false}) => ({
lists: queries.useCustomerProductLists(queryOptions),
mutation: useShopperCustomersMutation('createCustomerProductList'),
// Initially disabled; not needed until after the creation mutation
query: queries.useCustomerProductList(queryOptions, props)
})
)
// 1. Populate cache with initial data
await waitAndExpectSuccess(() => result.current.lists)
expect(result.current.lists.data).toEqual(listResult)
expect(result.current.query.data).toBeUndefined()
// 2. Do creation mutation
act(() => result.current.mutation.mutate(options))
await waitAndExpectSuccess(() => result.current.mutation)
expect(result.current.mutation.data).toEqual(data)
expect(result.current.lists.data).toEqual(newlistResult)
assertUpdateQuery(result.current.query, data)
})
// We want to make sure our code can handle the case
// where API does not include data key from getProductLists where it should be (API bug)
test('`createCustomerProductList` updates cache on success when there is not data key in fetched product list', async () => {
const listResult = {
limit: 0,
total: 0
}
const data: ShopperCustomersTypes.CustomerProductList = {
id: 'listId', // MUST match parameters used
customerProductListItems: []
}
const newlistResult = {
...baseListResult,
data: [
{
...baseProductList,
customerProductListItems: []
}
]
}
const options = createOptions<'createCustomerProductList'>(data)
mockQueryEndpoint(customersEndpoint, listResult) // getCustomerProductLists
mockMutationEndpoints(customersEndpoint, data) // this mutation
mockQueryEndpoint(customersEndpoint, {test: 'this should not get used'}) // getCustomerProductList refetch
const {result} = renderHookWithProviders(
(props: {enabled: boolean} = {enabled: false}) => ({
lists: queries.useCustomerProductLists(queryOptions),
mutation: useShopperCustomersMutation('createCustomerProductList'),
// Initially disabled; not needed until after the creation mutation
query: queries.useCustomerProductList(queryOptions, props)
})
)
// 1. Populate cache with initial data
await waitAndExpectSuccess(() => result.current.lists)
expect(result.current.lists.data).toEqual(listResult)
expect(result.current.query.data).toBeUndefined()
// 2. Do creation mutation
act(() => result.current.mutation.mutate(options))
await waitAndExpectSuccess(() => result.current.mutation)
expect(result.current.mutation.data).toEqual(data)
expect(result.current.lists.data).toEqual(newlistResult)
assertUpdateQuery(result.current.query, data)
})
test('`updateCustomerProductList` updates cache on success', async () => {
const listResult = baseListResult
const oldList = baseProductList
const body: ShopperCustomersTypes.CustomerProductList = {description: 'new description'}
const newList = {...oldList, ...body}
const data = {body, parameters: PARAMETERS}
const options = createOptions<'updateCustomerProductList'>(data)
const newListResult = {
...listResult,
data: [newList]
}
mockQueryEndpoint(customersEndpoint, listResult) // getCustomerProductLists
mockMutationEndpoints(customersEndpoint, newList) // this mutation
mockQueryEndpoint(customersEndpoint, oldList) // getCustomerProductList
mockQueryEndpoint(customersEndpoint, {test: 'this should not get used'}) // getCustomerProductList refetch
const {result} = renderHookWithProviders(
(props: {enabled: boolean} = {enabled: false}) => ({
lists: queries.useCustomerProductLists(queryOptions),
mutation: useShopperCustomersMutation('updateCustomerProductList'),
// Initially disabled; not needed until after the creation mutation
query: queries.useCustomerProductList(queryOptions, props)
})
)
// 1. Populate cache with initial data
await waitAndExpectSuccess(() => result.current.lists)
expect(result.current.lists.data).toEqual(listResult)
expect(result.current.query.data).toBeUndefined()
// 2. Do creation mutation
act(() => result.current.mutation.mutate(options))
await waitAndExpectSuccess(() => result.current.mutation)
expect(result.current.mutation.data).toEqual(newList)
expect(result.current.lists.data).toEqual(newListResult)
})
test('`deleteCustomerProductList` updates cache on success', async () => {
const listResult = baseListResult
const options = queryOptions // Can be used for this mutation as it has no body
const data = undefined
mockQueryEndpoint(customersEndpoint, listResult) // getCustomerProductLists
mockMutationEndpoints(customersEndpoint, data) // this mutation
mockQueryEndpoint(customersEndpoint, {test: 'this should not get used'}) // getCustomerProductList refetch
const {result} = renderHookWithProviders(
(props: {enabled: boolean} = {enabled: false}) => ({
lists: queries.useCustomerProductLists(queryOptions),
mutation: useShopperCustomersMutation('deleteCustomerProductList'),
// Initially disabled; not needed until after the creation mutation
query: queries.useCustomerProductList(queryOptions, props)
})
)
// 1. Populate cache with initial data
await waitAndExpectSuccess(() => result.current.lists)
expect(result.current.lists.data).toEqual(listResult)
expect(result.current.query.data).toBeUndefined()
// 2. Do creation mutation
act(() => result.current.mutation.mutate(options))
await waitAndExpectSuccess(() => result.current.mutation)
expect(result.current.mutation.data).toEqual(data)
expect(result.current.lists.data).toEqual(emptyListResult)
})
test('`createCustomerProductListItem` updates cache on success', async () => {
const data = oldProductListItem
const list = baseProductList
const listResult = baseListResult
const newList = {
...list,
customerProductListItems: [...list.customerProductListItems, data]
}
const newListResult = {
...listResult,
data: [newList]
}
const options = createOptions<'createCustomerProductListItem'>(data)
mockQueryEndpoint(customersEndpoint, list) // getCustomerProductList
mockQueryEndpoint(customersEndpoint, listResult) // getCustomerProductLists
mockMutationEndpoints(customersEndpoint, data) // this mutation
mockQueryEndpoint(customersEndpoint, {test: 'this should not get used'}) // getCustomerProductList refetch
mockQueryEndpoint(customersEndpoint, {test: 'use this? should not be!'}) // getCustomerProductLists refetch
const {result} = renderHookWithProviders(
(props: {enabled: boolean} = {enabled: false}) => ({
list: queries.useCustomerProductList(queryOptions),
lists: queries.useCustomerProductLists(queryOptions),
mutation: useShopperCustomersMutation('createCustomerProductListItem'),
// Initially disabled; not needed until after the creation mutation
query: queries.useCustomerProductListItem(queryOptions, props)
})
)
// 1. Populate cache with initial data
await waitAndExpectSuccess(() => result.current.lists)
expect(result.current.list.data).toEqual(list)
expect(result.current.lists.data).toEqual(listResult)
expect(result.current.query.data).toBeUndefined()
// 2. Do creation mutation
act(() => result.current.mutation.mutate(options))
await waitAndExpectSuccess(() => result.current.mutation)
expect(result.current.mutation.data).toEqual(data)
expect(result.current.list.data).toEqual(newList)
expect(result.current.lists.data).toEqual(newListResult)
assertUpdateQuery(result.current.query, data)
})
test('`deleteCustomerProductListItem` updates cache on success', async () => {
const data = oldProductListItem
const list = baseProductList
const listResult = baseListResult
const options = queryOptions // Can be used for this mutation as it has no body
const newList = {
...list,
customerProductListItems: []
}
const newListResult = {
...listResult,
data: [newList]
}
mockQueryEndpoint(customersEndpoint, list) // getCustomerProductList
mockQueryEndpoint(customersEndpoint, listResult) // getCustomerProductLists
mockQueryEndpoint(customersEndpoint, data) // getCustomerProductListItem
mockMutationEndpoints(customersEndpoint, {}) // this mutation
mockQueryEndpoint(customersEndpoint, {test: 'this should not get used'}) // getCustomerProductList refetch
mockQueryEndpoint(customersEndpoint, {test: 'use this? should not be!'}) // getCustomerProductLists refetch
const {result} = renderHookWithProviders(() => ({
list: queries.useCustomerProductList(queryOptions),
lists: queries.useCustomerProductLists(queryOptions),
mutation: useShopperCustomersMutation('deleteCustomerProductListItem'),
query: queries.useCustomerProductListItem(queryOptions)
}))
// 1. Populate cache with initial data
await waitAndExpectSuccess(() => result.current.lists)
expect(result.current.list.data).toEqual(list)
expect(result.current.lists.data).toEqual(listResult)
expect(result.current.query.data).toEqual(data)
// 2. Do deletion mutation
act(() => result.current.mutation.mutate(options))
await waitAndExpectSuccess(() => result.current.mutation)
expect(result.current.mutation.data).toBeUndefined()
expect(result.current.list.data).toEqual(newList)
expect(result.current.lists.data).toEqual(newListResult)
assertRemoveQuery(result.current.query)
})
test('`updateCustomerProductListItem` updates cache on success', async () => {
const oldData = oldProductListItem
const newData: ShopperCustomersTypes.CustomerProductListItem = {
id: 'itemId', // MUST match parameters
priority: 1,
public: false,
quantity: 1
}
const list = baseProductList
const listResult = baseListResult
const newList = {
...list,
customerProductListItems: [newData]
}
const newListResult = {
...listResult,
data: [newList]
}
const options = createOptions<'updateCustomerProductListItem'>(newData)
mockQueryEndpoint(customersEndpoint, list) // getCustomerProductList
mockQueryEndpoint(customersEndpoint, listResult) // getCustomerProductLists
mockQueryEndpoint(customersEndpoint, oldData) // getCustomerProductListItem
mockMutationEndpoints(customersEndpoint, newData) // this mutation
mockQueryEndpoint(customersEndpoint, {test: 'this should not get used'}) // getCustomerProductList refetch
mockQueryEndpoint(customersEndpoint, {test: 'use this? should not be!'}) // getCustomerProductLists refetch
const {result} = renderHookWithProviders(() => ({
list: queries.useCustomerProductList(queryOptions),
lists: queries.useCustomerProductLists(queryOptions),
mutation: useShopperCustomersMutation('updateCustomerProductListItem'),
query: queries.useCustomerProductListItem(queryOptions)
}))
// 1. Populate cache with initial data
await waitAndExpectSuccess(() => result.current.lists)
expect(result.current.list.data).toEqual(list)
expect(result.current.lists.data).toEqual(listResult)
expect(result.current.query.data).toEqual(oldData)
// 2. Do update mutation
act(() => result.current.mutation.mutate(options))
await waitAndExpectSuccess(() => result.current.mutation)
expect(result.current.mutation.data).toEqual(newData)
expect(result.current.list.data).toEqual(newList)
expect(result.current.lists.data).toEqual(newListResult)
assertUpdateQuery(result.current.query, newData)
})
})
describe('simple mutations (no cache updates)', () => {
/** `void` doesn't play nice as a value, so just replace it with `undefined`. */
type FixVoid<T> = void extends T ? undefined : T
type TestMap = {
[Mut in ShopperCustomersMutation]?: [
Argument<Client[Mut]>,
FixVoid<DataType<Client[Mut]>>
]
}
// Using an object, rather than array, to more easily manage data values
const testMap: TestMap = {
getResetPasswordToken: [
createOptions<'getResetPasswordToken'>({login: 'login'}),
{email: 'customer@email', expiresInMinutes: 10, login: 'login', resetToken: 'token'}
],
registerCustomer: [
createOptions<'registerCustomer'>({customer: {}, password: 'hunter2'}),
{customerId: 'customerId'}
],
resetPassword: [
createOptions<'resetPassword'>({
resetToken: 'token',
newPassword: 'hunter3',
login: 'login'
}),
undefined
],
updateCustomerPassword: [
createOptions<'updateCustomerPassword'>({
currentPassword: 'hunter2',
password: 'hunter3'
}),
undefined
]
}
// Type assertion because `Object.entries` is limited :\
const testCases = Object.entries(testMap) as [
ShopperCustomersMutation,
NonNullable<TestMap[ShopperCustomersMutation]>
][]
test.each(testCases)(
'`%s` returns data on success',
async (mutationName, [options, data]) => {
mockMutationEndpoints(customersEndpoint, data ?? {}) // Fallback for `void` endpoints
const {result} = renderHookWithProviders(() => {
return useShopperCustomersMutation(mutationName)
})
act(() => result.current.mutate(options))
await waitAndExpectSuccess(() => result.current)
expect(result.current.data).toEqual(data)
}
)
})
})