-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathmutation.test.ts
More file actions
269 lines (247 loc) · 10.7 KB
/
mutation.test.ts
File metadata and controls
269 lines (247 loc) · 10.7 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
/*
* Copyright (c) 2025, 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 {ShopperConsentsTypes} from 'commerce-sdk-isomorphic'
import nock from 'nock'
import {
assertInvalidateQuery,
mockMutationEndpoints,
mockQueryEndpoint,
renderHookWithProviders,
waitAndExpectSuccess
} from '../../test-utils'
import {ShopperConsentsMutation, useShopperConsentsMutation} from '../ShopperConsents'
import {ApiClients, Argument} 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_CONSENTS
type Client = NonNullable<ApiClients[typeof CLIENT_KEY]>
const consentsEndpoint = '/shopper/shopper-consents/'
/** All Shopper Consents parameters. Can be used for all endpoints, as unused params are ignored. */
const PARAMETERS = {
siteId: 'RefArch'
} as const
/** Options object that can be used for all query endpoints. */
const queryOptions = {parameters: PARAMETERS}
const baseSubscriptionResponse: ShopperConsentsTypes.ConsentSubscriptionResponse = {
data: [
{
subscriptionId: 'test-subscription',
channels: ['email' as ShopperConsentsTypes.ChannelType],
contactPointValue: 'test@example.com'
}
]
}
type Mutation = ShopperConsentsMutation
/** Map of mutation method name to test options. */
type TestMap = {[Mut in Mutation]?: Argument<Client[Mut]>}
const testMap: TestMap = {
updateSubscription: {
parameters: PARAMETERS,
body: {
subscriptionId: 'test-subscription',
channel: 'email',
status: 'opt_in',
contactPointValue: 'test@example.com'
}
},
updateSubscriptions: {
parameters: PARAMETERS,
body: {
subscriptions: [
{
subscriptionId: 'test-subscription',
channel: 'email' as ShopperConsentsTypes.ChannelType,
status: 'opt_in' as ShopperConsentsTypes.ConsentStatus,
contactPointValue: 'test@example.com'
}
]
}
}
}
// Type assertion is necessary because `Object.entries` is limited
type TestCase = [Mutation, NonNullable<TestMap[Mutation]>]
const testCases = Object.entries(testMap) as TestCase[]
describe('Shopper Consents mutations', () => {
beforeEach(() => nock.cleanAll())
test.each(testCases)('`%s` returns data on success', async (mutationName, options) => {
mockMutationEndpoints(consentsEndpoint, {})
const {result} = renderHookWithProviders(() => {
return useShopperConsentsMutation(mutationName)
})
act(() => result.current.mutate(options))
await waitAndExpectSuccess(() => result.current)
})
})
describe('Cache update behavior', () => {
describe('updateSubscription', () => {
beforeEach(() => nock.cleanAll())
test('invalidates `getSubscriptions` query', async () => {
mockQueryEndpoint(consentsEndpoint, baseSubscriptionResponse)
mockMutationEndpoints(consentsEndpoint, {})
const {result} = renderHookWithProviders(() => {
return {
query: queries.useSubscriptions(queryOptions),
mutation: useShopperConsentsMutation('updateSubscription')
}
})
await waitAndExpectSuccess(() => result.current.query)
const oldData = result.current.query.data
act(() =>
result.current.mutation.mutate({
parameters: PARAMETERS,
body: {
subscriptionId: 'test-subscription',
channel: 'email',
status: 'opt_out',
contactPointValue: 'test@example.com'
}
})
)
await waitAndExpectSuccess(() => result.current.mutation)
assertInvalidateQuery(result.current.query, oldData)
})
test('triggers refetch after cache invalidation', async () => {
const updatedResponse: ShopperConsentsTypes.ConsentSubscriptionResponse = {
data: [
{
subscriptionId: 'test-subscription',
channels: ['email' as ShopperConsentsTypes.ChannelType],
contactPointValue: 'test@example.com'
}
]
}
mockQueryEndpoint(consentsEndpoint, baseSubscriptionResponse)
mockMutationEndpoints(consentsEndpoint, {})
mockQueryEndpoint(consentsEndpoint, updatedResponse) // Refetch returns updated data
const {result} = renderHookWithProviders(() => {
return {
query: queries.useSubscriptions(queryOptions),
mutation: useShopperConsentsMutation('updateSubscription')
}
})
await waitAndExpectSuccess(() => result.current.query)
expect(result.current.query.data).toEqual(baseSubscriptionResponse)
act(() =>
result.current.mutation.mutate({
parameters: PARAMETERS,
body: {
subscriptionId: 'test-subscription',
channel: 'email',
status: 'opt_out',
contactPointValue: 'test@example.com'
}
})
)
await waitAndExpectSuccess(() => result.current.mutation)
// Wait for refetch to complete
await waitAndExpectSuccess(() => result.current.query)
// After refetch, data should be updated
expect(result.current.query.data).toEqual(updatedResponse)
})
})
describe('updateSubscriptions', () => {
beforeEach(() => nock.cleanAll())
test('invalidates `getSubscriptions` query', async () => {
mockQueryEndpoint(consentsEndpoint, baseSubscriptionResponse)
mockMutationEndpoints(consentsEndpoint, {})
const {result} = renderHookWithProviders(() => {
return {
query: queries.useSubscriptions(queryOptions),
mutation: useShopperConsentsMutation('updateSubscriptions')
}
})
await waitAndExpectSuccess(() => result.current.query)
const oldData = result.current.query.data
act(() =>
result.current.mutation.mutate({
parameters: PARAMETERS,
body: {
subscriptions: [
{
subscriptionId: 'test-subscription',
channel: 'email' as ShopperConsentsTypes.ChannelType,
status: 'opt_out' as ShopperConsentsTypes.ConsentStatus,
contactPointValue: 'test@example.com'
},
{
subscriptionId: 'test-subscription-2',
channel: 'sms' as ShopperConsentsTypes.ChannelType,
status: 'opt_in' as ShopperConsentsTypes.ConsentStatus,
contactPointValue: '+15551234567'
}
]
}
})
)
await waitAndExpectSuccess(() => result.current.mutation)
assertInvalidateQuery(result.current.query, oldData)
})
test('ensures stale cache is not served after mutation', async () => {
const staleResponse = baseSubscriptionResponse
const freshResponse: ShopperConsentsTypes.ConsentSubscriptionResponse = {
data: [
{
subscriptionId: 'test-subscription',
channels: ['email' as ShopperConsentsTypes.ChannelType],
contactPointValue: 'test@example.com'
},
{
subscriptionId: 'test-subscription-2',
channels: ['sms' as ShopperConsentsTypes.ChannelType],
contactPointValue: '+15551234567'
}
]
}
mockQueryEndpoint(consentsEndpoint, staleResponse)
mockMutationEndpoints(consentsEndpoint, {})
mockQueryEndpoint(consentsEndpoint, freshResponse) // Refetch returns fresh data
const {result} = renderHookWithProviders(() => {
return {
query: queries.useSubscriptions(queryOptions),
mutation: useShopperConsentsMutation('updateSubscriptions')
}
})
// Initial fetch - get stale data
await waitAndExpectSuccess(() => result.current.query)
expect(result.current.query.data?.data).toHaveLength(1)
// Perform mutation
act(() =>
result.current.mutation.mutate({
parameters: PARAMETERS,
body: {
subscriptions: [
{
subscriptionId: 'test-subscription',
channel: 'email' as ShopperConsentsTypes.ChannelType,
status: 'opt_out' as ShopperConsentsTypes.ConsentStatus,
contactPointValue: 'test@example.com'
},
{
subscriptionId: 'test-subscription-2',
channel: 'sms' as ShopperConsentsTypes.ChannelType,
status: 'opt_in' as ShopperConsentsTypes.ConsentStatus,
contactPointValue: '+15551234567'
}
]
}
})
)
await waitAndExpectSuccess(() => result.current.mutation)
// Wait for automatic refetch
await waitAndExpectSuccess(() => result.current.query)
// Should have fresh data, not stale
expect(result.current.query.data?.data).toHaveLength(2)
expect(result.current.query.data).toEqual(freshResponse)
})
})
})