-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathindex.test.js
More file actions
480 lines (415 loc) · 18.6 KB
/
index.test.js
File metadata and controls
480 lines (415 loc) · 18.6 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
/*
* 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 React from 'react'
import {fireEvent, screen, waitFor, within} from '@testing-library/react'
import {mockCustomerBaskets, mockedCustomerProductLists} from '../../mocks/mock-data'
import {Route, Switch} from 'react-router-dom'
import {rest} from 'msw'
import ProductDetail from '.'
import {renderWithProviders} from '../../utils/test-utils'
import {
basketWithProductSet,
mockWishlistWithItem,
einsteinRecommendation,
masterProduct,
productsForEinstein
} from '../../pages/product-detail/index.mock'
import mockedProductSet from '../../mocks/product-set-winter-lookM'
import {
mockProductBundle,
basketWithProductBundle,
bundleProductItemsForPDP
} from '../../mocks/product-bundle'
jest.setTimeout(60000)
jest.useFakeTimers()
const mockAddToWishlist = jest.fn()
jest.mock('@salesforce/commerce-sdk-react', () => {
const originalModule = jest.requireActual('@salesforce/commerce-sdk-react')
return {
...originalModule,
useShopperCustomersMutation: (mutation) => {
if (mutation === 'createCustomerProductListItem') {
return {mutate: mockAddToWishlist}
} else {
return originalModule.useShopperCustomersMutation(mutation)
}
}
}
})
const MockedComponent = () => {
return (
<Switch>
<Route
path="/uk/en-GB/product/:productId"
render={(props) => <ProductDetail {...props} />}
/>
</Switch>
)
}
beforeEach(() => {
jest.resetModules()
global.server.use(
// By default, the page will be rendered with a product set
rest.get('*/products/:productId', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json(mockedProductSet))
}),
rest.get('*/customers/:customerId/baskets', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json(mockCustomerBaskets))
}),
rest.get('*/customers/:customerId/product-lists', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json(mockedCustomerProductLists))
}),
rest.get('*/products', (req, res, ctx) => {
return res(ctx.json(productsForEinstein))
}),
rest.post('*/v3/personalization/recs/EinsteinTestSite/*', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json(einsteinRecommendation))
})
)
// Since we're testing some navigation logic, we are using a simple Router
// around our component. We need to initialize the default route/path here.
window.history.pushState({}, 'ProductDetail', '/uk/en-GB/product/25517823M')
})
afterEach(() => {
jest.resetModules()
jest.clearAllMocks()
})
test('should render product details page', async () => {
global.server.use(
// Use a single product (and not a product set)
rest.get('*/products/:productId', (req, res, ctx) => {
return res(ctx.json(masterProduct))
})
)
renderWithProviders(<MockedComponent />)
expect(await screen.findByTestId('product-details-page')).toBeInTheDocument()
await waitFor(() => {
expect(screen.getByRole('link', {name: /mens/i})).toBeInTheDocument()
expect(screen.getAllByText(/Long Sleeve Crew Neck/)).toHaveLength(2)
expect(screen.getAllByText(/from £9\.59/i)).toHaveLength(2)
expect(screen.getAllByText(/£15\.36/i)).toHaveLength(4)
expect(screen.getAllByText(/Add to Cart/)).toHaveLength(2)
expect(screen.getAllByText(/Add to Wishlist/)).toHaveLength(2)
expect(screen.getAllByTestId('product-view')).toHaveLength(1)
expect(screen.getByText(/You might also like/i)).toBeInTheDocument()
})
})
test('should add to wishlist', async () => {
global.server.use(
// Use a single product (and not a product set)
rest.get('*/products/:productId', (req, res, ctx) => {
return res(ctx.json(masterProduct))
})
)
renderWithProviders(<MockedComponent />)
expect(await screen.findByTestId('product-details-page')).toBeInTheDocument()
// wait for data to fully loaded before taking any action
await waitFor(() => {
expect(screen.getByRole('link', {name: /mens/i})).toBeInTheDocument()
expect(screen.getAllByText(/Long Sleeve Crew Neck/)).toHaveLength(2)
expect(screen.getByText(/You might also like/i)).toBeInTheDocument()
})
const wishlistButton = await screen.findByRole('button', {name: 'Add to Wishlist'})
fireEvent.click(wishlistButton)
await waitFor(() => {
expect(mockAddToWishlist).toHaveBeenCalledTimes(1)
})
})
test('should not add to wishlist if item is already in wishlist', async () => {
global.server.use(
// Use a product that is already in the wishlist
rest.get('*/products/:productId', (req, res, ctx) => {
return res(ctx.json(masterProduct))
}),
rest.get('*/customers/:customerId/product-lists', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json(mockWishlistWithItem))
})
)
renderWithProviders(<MockedComponent />)
// wait for data to fully loaded before taking any action
await waitFor(() => {
expect(screen.getByRole('link', {name: /mens/i})).toBeInTheDocument()
expect(screen.getAllByText(/Long Sleeve Crew Neck/)).toHaveLength(2)
expect(screen.getByText(/You might also like/i)).toBeInTheDocument()
})
const wishlistButton = await screen.findByRole('button', {name: 'Add to Wishlist'})
fireEvent.click(wishlistButton)
await waitFor(() => {
expect(mockAddToWishlist).toHaveBeenCalledTimes(0)
})
})
describe('product set', () => {
beforeEach(() => {
global.server.use(
// For adding items to basket
rest.post('*/baskets/:basketId/items', (req, res, ctx) => {
return res(ctx.json(basketWithProductSet))
})
)
})
test('render multi-product layout', async () => {
renderWithProviders(<MockedComponent />)
await waitFor(() => {
expect(screen.getByRole('link', {name: /mens/i})).toBeInTheDocument()
expect(screen.getAllByTestId('product-view')).toHaveLength(4) // 1 parent + 3 children
})
})
test('add the set to cart successfully', async () => {
const urlPathAfterSelectingAllVariants = `/uk/en-GB/product/winter-lookM?${new URLSearchParams(
{
'25518447M': 'color=JJ5FUXX&size=9MD',
'25518704M': 'color=JJ2XNXX&size=9MD',
'25772717M': 'color=TAUPETX&size=070&width=M'
}
)}`
window.history.pushState({}, 'ProductDetail', urlPathAfterSelectingAllVariants)
// Initial basket is necessary to add items to it
const initialBasket = {basketId: 'valid_id'}
renderWithProviders(<MockedComponent />, {wrapperProps: {initialBasket}})
await waitFor(
() => {
expect(screen.getByRole('link', {name: /mens/i})).toBeInTheDocument()
expect(screen.getAllByText('Winter Look')[0]).toBeInTheDocument()
expect(screen.getAllByText('Quilted Jacket')[0]).toBeInTheDocument()
expect(screen.getAllByText('Pull On Pant')[0]).toBeInTheDocument()
expect(screen.getAllByText('Zerrick')[0]).toBeInTheDocument()
expect(
screen.getByRole('heading', {name: /you might also like/i})
).toBeInTheDocument()
},
{timeout: 5000}
)
const buttons = await screen.findAllByText(/add set to cart/i)
fireEvent.click(buttons[0])
await waitFor(
() => {
const modal = screen.getByTestId('add-to-cart-modal')
expect(within(modal).getByText(/items added to cart/i)).toBeInTheDocument()
expect(within(modal).getByText(/Quilted Jacket/i)).toBeInTheDocument()
expect(within(modal).getByText(/Pull On Pant/i)).toBeInTheDocument()
expect(within(modal).getByText(/Zerrick/i)).toBeInTheDocument()
},
// Seems like rendering the modal takes a bit more time
{timeout: 10000}
)
})
test('add the set to cart with error messages', async () => {
renderWithProviders(<MockedComponent />)
await waitFor(
() => {
expect(screen.getByRole('link', {name: /mens/i})).toBeInTheDocument()
expect(screen.getAllByText('Winter Look')[0]).toBeInTheDocument()
},
{timeout: 5000}
)
const buttons = await screen.findAllByText(/add set to cart/i)
fireEvent.click(buttons[0])
await waitFor(() => {
// Show error when users have not selected all the variants yet
// 1 error for each child product
const errorMessages = screen.getAllByText(/Please select all your options above/i)
expect(errorMessages).toHaveLength(3)
})
})
test("child products' images are lazy loaded", async () => {
renderWithProviders(<MockedComponent />)
await waitFor(() => {
expect(screen.getByRole('link', {name: /mens/i})).toBeInTheDocument()
})
const childProducts = await screen.findAllByTestId('child-product')
childProducts.forEach((child) => {
const heroImage = within(child).getAllByRole('img')[0]
expect(heroImage.getAttribute('loading')).toBe('lazy')
})
})
})
describe('Recommended Products', () => {
test('Recently Viewed gets updated when navigating between products', async () => {
global.server.use(
// Use a single product (and not a product set)
rest.get('*/products/:productId', (req, res, ctx) => {
return res(ctx.json(masterProduct))
})
)
// const user = userEvent.setup({advanceTimers: jest.advanceTimersByTime})
renderWithProviders(<MockedComponent />)
// If we poll for updates immediately, the test output is flooded with errors:
// "Warning: An update to WrappedComponent inside a test was not wrapped in act(...)."
// If we wait to poll until the component is updated, then the errors disappear. Using a
// timeout is clearly a suboptimal solution, but I don't know the "correct" way to fix it.
// let done = false
// setTimeout(() => (done = true), 200)
// await waitFor(() => expect(done).toBeTruthy())
await waitFor(() => {
expect(screen.getByRole('link', {name: /mens/i})).toBeInTheDocument()
expect(screen.getByText(/You might also like/i)).toBeInTheDocument()
expect(screen.getAllByText(/Long Sleeve Crew Neck/)).toHaveLength(2)
expect(screen.getAllByText(/Summer Bomber Jacket/)).toHaveLength(3)
})
})
})
describe('product bundles', () => {
let hasUpdatedBundleChildren = false
beforeEach(() => {
hasUpdatedBundleChildren = false
global.server.use(
// Use product bundle instead of product set
rest.get('*/products/:productId', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json(mockProductBundle))
}),
rest.get('*/products', (req, res, ctx) => {
let inventoryLevel = 0
let bundleChildVariantId = '701643473915M'
if (req.url.toString().includes('701643473908M')) {
bundleChildVariantId = '701643473908M'
inventoryLevel = 3
}
const bundleChildVariantData = {
data: [
{
id: bundleChildVariantId,
inventory: {
ats: inventoryLevel,
backorderable: false,
id: 'inventory_m',
orderable: false,
preorderable: false,
stockLevel: inventoryLevel
},
master: {
masterId: '25565139M',
orderable: true
}
}
]
}
return res(ctx.delay(0), ctx.status(200), ctx.json(bundleChildVariantData))
}),
// For adding items to basket
rest.post('*/baskets/:basketId/items', (req, res, ctx) => {
const basketWithBundle = {
...basketWithProductBundle,
productItems: bundleProductItemsForPDP
}
return res(ctx.json(basketWithBundle))
}),
// Follow up call to update child bundle variant selections
rest.patch('*/baskets/:basketId/items', (req, res, ctx) => {
hasUpdatedBundleChildren = true
return res(ctx.json(basketWithProductBundle))
})
)
})
test('renders multi-product layout', async () => {
renderWithProviders(<MockedComponent />)
await waitFor(() => {
expect(screen.getAllByTestId('product-view')).toHaveLength(4) // 1 parent + 3 children
})
})
test('add the bundle to cart successfully', async () => {
const urlPathAfterSelectingAllVariants = `uk/en-GB/product/test-bundle?${new URLSearchParams(
{
'25592770M': 'color=JJGN9A0&size=006',
'25565139M': 'color=JJ169XX&size=9SM',
'25565094M': 'color=JJ0CZXX&size=9XS'
}
)}`
window.history.pushState({}, 'ProductDetail', urlPathAfterSelectingAllVariants)
// Initial basket is necessary to add items to it
const initialBasket = {basketId: 'valid_id'}
renderWithProviders(<MockedComponent />, {wrapperProps: {initialBasket}})
await waitFor(() => {
expect(screen.getAllByText("Women's clothing test bundle")[0]).toBeInTheDocument()
expect(hasUpdatedBundleChildren).toBe(false)
})
const buttons = await screen.findAllByText(/add bundle to cart/i)
fireEvent.click(buttons[0])
await waitFor(
() => {
const modal = screen.getByTestId('add-to-cart-modal')
expect(within(modal).getByText(/1 item added to cart/i)).toBeInTheDocument()
expect(hasUpdatedBundleChildren).toBe(true)
},
// Seems like rendering the modal takes a bit more time
{timeout: 10000}
)
})
test('add the bundle to cart with error messages', async () => {
renderWithProviders(<MockedComponent />)
await waitFor(() => {
expect(screen.getAllByText("Women's clothing test bundle")[0]).toBeInTheDocument()
})
const buttons = await screen.findAllByText(/add bundle to cart/i)
fireEvent.click(buttons[0])
await waitFor(() => {
// Show error when users have not selected all the variants yet
// 1 error for each child product
const errorMessages = screen.getAllByText(/Please select all your options above/i)
expect(errorMessages).toHaveLength(3)
})
})
test('child product images are lazy loaded', async () => {
renderWithProviders(<MockedComponent />)
const childProducts = await screen.findAllByTestId('child-product')
childProducts.forEach((child) => {
const heroImage = within(child).getAllByRole('img')[0]
expect(heroImage.getAttribute('loading')).toBe('lazy')
})
})
test('add to cart button is disabled when child is out of stock', async () => {
renderWithProviders(<MockedComponent />)
await waitFor(() => {
expect(screen.getAllByText("Women's clothing test bundle")[0]).toBeInTheDocument()
})
const childProducts = screen.getAllByTestId('child-product')
expect(childProducts).toHaveLength(3)
const swingTankProduct = childProducts[1]
const colorSelectionBtn = within(swingTankProduct).getByLabelText('Black')
const sizeSelectionBtn = within(swingTankProduct).getByLabelText('M')
expect(swingTankProduct).toBeInTheDocument()
expect(colorSelectionBtn).toBeInTheDocument()
expect(sizeSelectionBtn).toBeInTheDocument()
fireEvent.click(colorSelectionBtn)
fireEvent.click(sizeSelectionBtn)
await waitFor(() => {
expect(screen.getByText('Out of stock')).toBeInTheDocument()
const addBundleToCartBtn = screen.getByRole('button', {name: /add bundle to cart/i})
expect(addBundleToCartBtn).toBeInTheDocument()
expect(addBundleToCartBtn).toBeDisabled()
})
})
test('add to cart button is disabled when quantity exceeds child stock level', async () => {
renderWithProviders(<MockedComponent />)
await waitFor(() => {
expect(screen.getAllByText("Women's clothing test bundle")[0]).toBeInTheDocument()
})
const childProducts = screen.getAllByTestId('child-product')
expect(childProducts).toHaveLength(3)
const swingTankProduct = childProducts[1]
const colorSelectionBtn = within(swingTankProduct).getByLabelText('Black')
const sizeSelectionBtn = within(swingTankProduct).getByLabelText('L')
expect(swingTankProduct).toBeInTheDocument()
expect(colorSelectionBtn).toBeInTheDocument()
expect(sizeSelectionBtn).toBeInTheDocument()
fireEvent.click(colorSelectionBtn)
fireEvent.click(sizeSelectionBtn)
const addBundleToCartBtn = screen.getByRole('button', {name: /add bundle to cart/i})
await waitFor(() => {
expect(addBundleToCartBtn).toBeInTheDocument()
expect(addBundleToCartBtn).toBeEnabled()
})
// Set product bundle quantity selection to 4
const quantityInput = screen.getByRole('spinbutton', {name: /quantity/i})
quantityInput.focus()
fireEvent.change(quantityInput, {target: {value: '4'}})
await waitFor(() => {
expect(screen.getByRole('spinbutton', {name: /quantity/i})).toHaveValue('4')
expect(screen.getByText('Only 3 left!')).toBeInTheDocument()
expect(addBundleToCartBtn).toBeDisabled()
})
})
})