-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathhelpers.test.tsx
More file actions
154 lines (147 loc) · 5.26 KB
/
helpers.test.tsx
File metadata and controls
154 lines (147 loc) · 5.26 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
/*
* Copyright (c) 2024, salesforce.com, 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 React from 'react'
import {useShopperBasketsMutationHelper} from './helpers'
import {useCustomerBaskets} from '../ShopperCustomers'
import {renderWithProviders} from '../../test-utils'
import {screen, waitFor} from '@testing-library/react'
import {ShopperBasketsTypes} from 'commerce-sdk-isomorphic'
import jwt from 'jsonwebtoken'
const basketId = '10cf6aa40edba4fcfcc6915594'
const mockAsyncMutate = jest.fn()
jest.mock('../ShopperCustomers', () => {
const originalModule = jest.requireActual('../ShopperCustomers')
return {
...originalModule,
useCustomerBaskets: jest.fn()
}
})
jest.mock('../index', () => {
const originalModule = jest.requireActual('./index')
return {
...originalModule,
useCustomerId: jest.fn(() => 'customer-id')
}
})
jest.mock('./mutation', () => {
const originalModule = jest.requireActual('./mutation')
return {
...originalModule,
useShopperBasketsMutation: () => ({
mutateAsync: mockAsyncMutate
})
}
})
const MockComponent = () => {
const helpers = useShopperBasketsMutationHelper()
return (
<div>
<button
onClick={() => {
helpers
.addItemToNewOrExistingBasket([
{
productId: 'product-123',
price: 100,
quantity: 1
}
] as ShopperBasketsTypes.ProductItem[] & Record<`c_${string}`, any>)
.catch((e) => console.log('e', e))
}}
>
Add to cart
</button>
</div>
)
}
describe('useShopperBasketsMutationHelper.addItemToNewOrExistingBasket', function () {
afterEach(() => {
jest.resetModules()
})
test('should perform add to cart mutation when basket is already created', async () => {
mockAsyncMutate.mockImplementationOnce(() => ({
productItems: [{id: 'product-id-111', quantity: 20}],
basketId
}))
// @ts-expect-error ts complains because mockImplementation is not part of declared type from useCustomerBaskets queries
useCustomerBaskets.mockImplementation(() => {
return {
data: {total: 1, baskets: [{basketId}]},
isLoading: false
}
})
const fetchedToken = jwt.sign(
{
sub: `cc-slas::zzrf_001::scid:xxxxxx::usid:usidddddd`,
isb: `uido:ecom::upn:Guest::uidn:firstname lastname::gcid:customerId::rcid:registeredCid::chid:siteId`
},
'secret'
)
const {user} = renderWithProviders(<MockComponent />, {
fetchedToken
})
const addToCartBtn = screen.getByText(/add to cart/i)
await user.click(addToCartBtn)
await waitFor(() =>
expect(mockAsyncMutate.mock.calls[0][0]).toEqual({
parameters: {basketId},
body: [
{
productId: 'product-123',
price: 100,
quantity: 1
}
]
})
)
})
test('should call a basket mutation before calling add to cart mutation', async () => {
// order is important since mockAsyncMutate will represent createBasket and addToBasket mutation in the order of executions
mockAsyncMutate
.mockImplementationOnce(() => ({
productItems: [],
basketId
}))
.mockImplementationOnce(() => ({
productItems: [{id: 'product-id', quantity: 2}],
basketId
}))
// @ts-expect-error ts complains because mockImplementation is not part of declared type from useCustomerBaskets queries
useCustomerBaskets.mockImplementation(() => {
return {
data: {total: 0},
isLoading: false
}
})
const fetchedToken = jwt.sign(
{
sub: `cc-slas::zzrf_001::scid:xxxxxx::usid:usidddddd`,
isb: `uido:ecom::upn:Guest::uidn:firstname lastname::gcid:customerId::rcid:registeredCid::chid:siteId`
},
'secret'
)
const {user} = renderWithProviders(<MockComponent />, {
fetchedToken
})
const addToCartBtn = screen.getByText(/add to cart/i)
await user.click(addToCartBtn)
expect(mockAsyncMutate).toHaveBeenCalledTimes(2)
await waitFor(() => expect(mockAsyncMutate.mock.calls[0][0]).toEqual({body: {}}))
await waitFor(() =>
expect(mockAsyncMutate.mock.calls[1][0]).toEqual({
parameters: {basketId},
body: [
{
productId: 'product-123',
price: 100,
quantity: 1
}
]
})
)
})
})