-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathbundle.test.js
More file actions
259 lines (223 loc) · 9.29 KB
/
bundle.test.js
File metadata and controls
259 lines (223 loc) · 9.29 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
/*
* Copyright (c) 2021, 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 PropTypes from 'prop-types'
import BundleProductViewModal from '@salesforce/retail-react-app/app/components/product-view-modal/bundle'
import {renderWithProviders} from '@salesforce/retail-react-app/app/utils/test-utils'
import {fireEvent, screen, waitFor, within} from '@testing-library/react'
import {useDisclosure} from '@chakra-ui/react'
import {
mockBundledProductItemsVariant,
mockProductBundleWithVariants,
mockProductBundle
} from '@salesforce/retail-react-app/app/mocks/product-bundle'
import {rest} from 'msw'
import {mockStandardProductOrderable} from '@salesforce/retail-react-app/app/mocks/standard-product'
const MockComponent = ({updateCart}) => {
const {isOpen, onOpen, onClose} = useDisclosure()
return (
<div>
<button onClick={onOpen}>Open Modal</button>
<BundleProductViewModal
updateCart={updateCart}
onOpen={onOpen}
onClose={onClose}
isOpen={isOpen}
product={mockBundledProductItemsVariant}
/>
</div>
)
}
MockComponent.propTypes = {
updateCart: PropTypes.func
}
beforeEach(() => {
global.server.use(
rest.get('*/products/:productId', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json(mockProductBundle))
}),
rest.get('*/products', (req, res, ctx) => {
const swingTankBlackMediumVariantId = '701643473915M'
const swingTankBlackLargeVariantId = '701643473908M'
if (req.url.toString().includes(swingTankBlackMediumVariantId)) {
mockProductBundleWithVariants.data[1].inventory = {
...mockProductBundleWithVariants.data[1].inventory,
stockLevel: 0
}
} else if (req.url.toString().includes(swingTankBlackLargeVariantId)) {
mockProductBundleWithVariants.data[1].inventory = {
...mockProductBundleWithVariants.data[1].inventory,
stockLevel: 1
}
}
return res(ctx.json(mockProductBundleWithVariants))
})
)
})
afterEach(() => {
jest.resetModules()
jest.restoreAllMocks()
})
test('renders bundle product view modal', async () => {
renderWithProviders(<MockComponent />)
await waitFor(() => {
const trigger = screen.getByText(/open modal/i)
fireEvent.click(trigger)
})
await waitFor(() => {
const bundleTitleQuery = screen.getAllByText(/Women's clothing test bundle/i)
expect(bundleTitleQuery[0]).toBeInTheDocument()
expect(bundleTitleQuery).toHaveLength(2) // one for desktop and one for mobile
})
expect(screen.getByRole('button', {name: /update/i})).toBeInTheDocument()
mockProductBundleWithVariants.data.forEach((childProduct) => {
const childProductQuery = screen.getAllByText(childProduct.name)
expect(childProductQuery[0]).toBeInTheDocument()
expect(childProductQuery).toHaveLength(2) // one for desktop and one for mobile
})
})
test('renders bundle product view modal with handleUpdateCart handler', async () => {
const handleUpdateCart = jest.fn()
renderWithProviders(<MockComponent updateCart={handleUpdateCart} />)
// open the modal
await waitFor(() => {
const trigger = screen.getByText(/open modal/i)
fireEvent.click(trigger)
})
// click on update
await waitFor(() => {
const updateButton = screen.getAllByText(/Update/)[0]
fireEvent.click(updateButton)
})
expect(handleUpdateCart).toHaveBeenCalledTimes(1)
})
test('bundle product view modal disables update button when child is out of stock', async () => {
renderWithProviders(<MockComponent />)
await waitFor(() => {
const trigger = screen.getByText(/open modal/i)
fireEvent.click(trigger)
})
await waitFor(() => {
const bundleTitleQuery = screen.getAllByText(/Women's clothing test bundle/i)
expect(bundleTitleQuery[0]).toBeInTheDocument()
expect(bundleTitleQuery).toHaveLength(2) // one for desktop and one for mobile
})
const productViews = screen.getAllByTestId('product-view')
const swingTankProductView = productViews[2]
expect(productViews).toHaveLength(4)
expect(swingTankProductView).toBeInTheDocument()
const updateBtn = screen.getByRole('button', {name: /update/i})
expect(updateBtn).toBeEnabled()
let sizeSelectBtn = within(swingTankProductView).getByLabelText('M')
expect(sizeSelectBtn).toBeInTheDocument()
fireEvent.click(sizeSelectBtn)
await waitFor(() => {
expect(within(swingTankProductView).getAllByText('M')).toHaveLength(2)
expect(updateBtn).toBeInTheDocument()
expect(updateBtn).toBeDisabled()
expect(screen.getByText('Out of stock')).toBeInTheDocument()
})
})
test('bundle product view modal disables update button when quantity exceeds child inventory', async () => {
renderWithProviders(<MockComponent />)
await waitFor(() => {
const trigger = screen.getByText(/open modal/i)
fireEvent.click(trigger)
})
await waitFor(() => {
const bundleTitleQuery = screen.getAllByText(/Women's clothing test bundle/i)
expect(bundleTitleQuery[0]).toBeInTheDocument()
expect(bundleTitleQuery).toHaveLength(2) // one for desktop and one for mobile
})
const productViews = screen.getAllByTestId('product-view')
const swingTankProductView = productViews[2]
const updateBtn = screen.getByRole('button', {name: /update/i})
const quantityInput = screen.getByRole('spinbutton', {name: /quantity/i})
let sizeSelectBtn = within(swingTankProductView).getByLabelText('L')
await waitFor(() => {
expect(productViews).toHaveLength(4)
expect(swingTankProductView).toBeInTheDocument()
expect(sizeSelectBtn).toBeInTheDocument()
expect(quantityInput).toBeInTheDocument()
})
// Wait for all child products to be rendered
await waitFor(() => {
expect(screen.getAllByText('Sleeveless Pleated Floral Front Blouse')).toHaveLength(2)
expect(screen.getAllByText('Swing Tank')).toHaveLength(2)
expect(screen.getAllByText('Pull On Neutral Pant')).toHaveLength(2)
})
// Select a valid variant
fireEvent.click(sizeSelectBtn)
// Wait for the initial state to be fully loaded and the button to be enabled
await waitFor(() => {
expect(updateBtn).toBeInTheDocument()
})
// Set product bundle quantity selection to 4
fireEvent.change(quantityInput, {target: {value: '4'}})
fireEvent.keyDown(quantityInput, {key: 'Enter', code: 'Enter', charCode: 13})
// Wait for quantity change to be processed
await waitFor(() => {
expect(screen.getByRole('spinbutton', {name: /quantity/i})).toHaveValue('4')
})
// Now click the size selection
fireEvent.click(sizeSelectBtn)
await waitFor(() => {
expect(within(swingTankProductView).getAllByText('L')).toHaveLength(2)
expect(updateBtn).toBeDisabled()
expect(screen.getByText('Only 1 left!')).toBeInTheDocument()
})
})
test('modal displays standard product child name when bundle contains a standard product', async () => {
// Create a bundle with a standard product as the first child
const bundleWithStandardChild = {
...mockBundledProductItemsVariant,
bundledProductItems: [
{
...mockBundledProductItemsVariant.bundledProductItems[0],
productId: mockStandardProductOrderable.id,
productName: mockStandardProductOrderable.name,
itemText: mockStandardProductOrderable.name
},
...mockBundledProductItemsVariant.bundledProductItems.slice(1)
]
}
// Custom MSW handler for this test
global.server.use(
rest.get('*/products', (req, res, ctx) => {
const ids = req.url.searchParams.get('ids')
if (ids && ids.includes(mockStandardProductOrderable.id)) {
return res(ctx.json({data: [mockStandardProductOrderable], total: 1}))
}
return res(ctx.json(mockProductBundleWithVariants))
})
)
const MockComponent = ({updateCart}) => {
const {isOpen, onOpen, onClose} = useDisclosure()
return (
<div>
<button onClick={onOpen}>Open Modal</button>
<BundleProductViewModal
updateCart={updateCart}
onOpen={onOpen}
onClose={onClose}
isOpen={isOpen}
product={bundleWithStandardChild}
/>
</div>
)
}
MockComponent.propTypes = {updateCart: PropTypes.func}
renderWithProviders(<MockComponent />)
await waitFor(() => {
const trigger = screen.getByText(/open modal/i)
fireEvent.click(trigger)
})
// Assert the standard product child name is rendered
await waitFor(() => {
expect(screen.getAllByText(mockStandardProductOrderable.name)[0]).toBeInTheDocument()
})
})