-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathitem-attributes.test.js
More file actions
152 lines (129 loc) · 5.43 KB
/
item-attributes.test.js
File metadata and controls
152 lines (129 loc) · 5.43 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
/*
* 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 ItemVariantProvider from '@salesforce/retail-react-app/app/components/item-variant'
import {renderWithProviders} from '@salesforce/retail-react-app/app/utils/test-utils'
import {screen, waitFor} from '@testing-library/react'
import ItemAttributes from '@salesforce/retail-react-app/app/components/item-variant/item-attributes'
import {getDisplayVariationValues} from '@salesforce/retail-react-app/app/utils/product-utils'
import PropTypes from 'prop-types'
import {rest} from 'msw'
import {
mockBundledProductItemsVariant,
mockProductBundleWithVariants,
mockProductBundle
} from '@salesforce/retail-react-app/app/mocks/product-bundle'
const MockedComponent = ({variant}) => {
return (
<ItemVariantProvider variant={variant}>
<ItemAttributes />
</ItemVariantProvider>
)
}
MockedComponent.propTypes = {
variant: PropTypes.object
}
beforeEach(() => {
global.server.use(
rest.get('*/products', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json(mockProductBundleWithVariants))
}),
rest.get('*/products/:productId', (req, res, ctx) => {
return res(ctx.delay(0), ctx.status(200), ctx.json(mockProductBundle))
})
)
})
test('component renders product bundles with variant data', async () => {
renderWithProviders(<MockedComponent variant={mockBundledProductItemsVariant} />)
await waitFor(() => {
expect(screen.getByText(/selected options:/i)).toBeInTheDocument()
})
mockBundledProductItemsVariant.bundledProductItems.forEach((item) => {
expect(screen.getByText(item.productName)).toBeInTheDocument()
expect(screen.getAllByText(`Qty: ${item.quantity}`)[0]).toBeInTheDocument()
})
mockProductBundleWithVariants.data.forEach((item) => {
const variationValues = getDisplayVariationValues(
item.variationAttributes,
item.variationValues
)
Object.keys(variationValues).forEach((key) => {
expect(screen.getAllByText(`${key}: ${variationValues[key]}`)[0]).toBeInTheDocument()
})
})
})
test('component renders product bundles without variant data', async () => {
const mockWithoutBundledProductItems = {...mockBundledProductItemsVariant}
delete mockWithoutBundledProductItems.bundledProductItems
renderWithProviders(<MockedComponent variant={mockWithoutBundledProductItems} />)
await waitFor(() => {
mockProductBundle.bundledProducts.forEach(({product, quantity}) => {
expect(screen.getByText(product.name)).toBeInTheDocument()
expect(screen.getAllByText(`Qty: ${quantity}`)[0]).toBeInTheDocument()
})
})
})
// Helper function to render the component with a given variant and props
const renderComponent = (variant, props = {}) => {
renderWithProviders(
<ItemVariantProvider variant={variant}>
<ItemAttributes {...props} />
</ItemVariantProvider>
)
}
test('renders Bonus Product when bonusProductLineItem is true', async () => {
const mockVariantWithBonusProduct = {
...mockBundledProductItemsVariant,
bonusProductLineItem: true // Simulate the bonus product flag
}
// Case 1: excludeBonusLabel is true
renderComponent(mockVariantWithBonusProduct, {excludeBonusLabel: true})
await waitFor(() => {
expect(screen.queryByText(/Bonus Product/i)).not.toBeInTheDocument() // Fixed assertion
})
// Case 3: excludeBonusLabel is false
renderComponent(mockVariantWithBonusProduct, {excludeBonusLabel: false})
await waitFor(() => {
expect(screen.getByText(/Bonus Product/i)).toBeInTheDocument()
})
})
test('renders Bonus Product when excludeBonusLabel is not set', async () => {
// Case 1: Variant has no bonus product
const mockVariantWithOutBonusProduct = {
...mockBundledProductItemsVariant,
bonusProductLineItem: false // Simulate the bonus product flag
}
renderComponent(mockVariantWithOutBonusProduct)
await waitFor(() => {
expect(screen.queryByText(/Bonus Product/i)).not.toBeInTheDocument() // Fixed assertion
})
// Case 2: Variant has bonus product
const mockVariantWithBonusProduct = {
...mockBundledProductItemsVariant,
bonusProductLineItem: true // Simulate the bonus product flag
}
renderComponent(mockVariantWithBonusProduct)
await waitFor(() => {
expect(screen.queryByText(/Bonus Product/i)).toBeInTheDocument() // Fixed assertion
})
})
test('does not render Bonus Product when bonusProductLineItem is false', async () => {
const mockVariantWithoutBonusProduct = {
...mockBundledProductItemsVariant,
bonusProductLineItem: false // Simulate the absence of the bonus product flag
}
// Case 1: excludeBonusLabel is true
renderComponent(mockVariantWithoutBonusProduct, {excludeBonusLabel: true})
await waitFor(() => {
expect(screen.queryByText(/Bonus Product/i)).not.toBeInTheDocument()
})
// Case 2: excludeBonusLabel is false
renderComponent(mockVariantWithoutBonusProduct, {excludeBonusLabel: false})
await waitFor(() => {
expect(screen.queryByText(/Bonus Product/i)).not.toBeInTheDocument()
})
})