-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathuse-derived-product.test.js
More file actions
166 lines (140 loc) · 5.3 KB
/
use-derived-product.test.js
File metadata and controls
166 lines (140 loc) · 5.3 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
/*
* 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 {screen} from '@testing-library/react'
import {useDerivedProduct} from '@salesforce/retail-react-app/app/hooks/use-derived-product'
import mockProductDetail from '@salesforce/retail-react-app/app/mocks/variant-750518699578M'
import {renderWithProviders} from '@salesforce/retail-react-app/app/utils/test-utils'
import {useVariant} from '@salesforce/retail-react-app/app/hooks/use-variant'
// Mock the useVariant hook
jest.mock('@salesforce/retail-react-app/app/hooks/use-variant', () => ({
useVariant: jest.fn()
}))
const MockComponent = ({product}) => {
const {inventoryMessage, quantity, variationParams, variant} = useDerivedProduct(product)
return (
<div>
<div>{`Quantity: ${quantity}`}</div>
<div>{inventoryMessage}</div>
<div>{JSON.stringify(variant)}</div>
<div>{JSON.stringify(variationParams)}</div>
</div>
)
}
MockComponent.propTypes = {
product: PropTypes.object
}
describe('useDerivedProduct hook', () => {
beforeEach(() => {
// Reset mock before each test
jest.clearAllMocks()
})
test('should not show out of stock message when stockLevel is greater then 0 and greater then asked quantity', () => {
// Mock useVariant to return a valid variant
useVariant.mockReturnValue({
orderable: true,
price: 299.99,
productId: '750518699578M',
variationValues: {color: 'BLACKFB', size: '038', width: 'V'}
})
renderWithProviders(<MockComponent product={mockProductDetail} />)
expect(screen.getByText(/Quantity: 1/)).toBeInTheDocument()
expect(
screen.getByText(
/{"orderable":true,"price":299.99,"productId":"750518699578M","variationValues":{"color":"BLACKFB","size":"038","width":"V"}}/
)
).toBeInTheDocument()
})
test('should show out of stock message when stockLevel is 0', () => {
// Mock useVariant to return a valid variant
useVariant.mockReturnValue({
orderable: true,
price: 299.99,
productId: '750518699578M',
variationValues: {color: 'BLACKFB', size: '038', width: 'V'}
})
const mockData = {
...mockProductDetail,
inventory: {
ats: 0,
backorderable: false,
id: 'inventory_m',
orderable: false,
preorderable: false,
stockLevel: 0
}
}
renderWithProviders(<MockComponent product={mockData} />)
expect(screen.getByText(/Out of stock/)).toBeInTheDocument()
})
test('should show unfulfillable messsage when stockLevel is less then asked quantity', () => {
// Mock useVariant to return a valid variant
useVariant.mockReturnValue({
orderable: true,
price: 299.99,
productId: '750518699578M',
variationValues: {color: 'BLACKFB', size: '038', width: 'V'}
})
const mockData = {
...mockProductDetail,
quantity: 10,
inventory: {
ats: 0,
backorderable: false,
id: 'inventory_m',
orderable: false,
preorderable: false,
stockLevel: 5
}
}
renderWithProviders(<MockComponent product={mockData} />)
expect(screen.getByText(/Only 5 left!/)).toBeInTheDocument()
})
test('should show of stock message for bundle products', () => {
// Mock useVariant to return null for bundle products (bundles don't have variants)
useVariant.mockReturnValue(null)
const mockBundleData = {
...mockProductDetail,
type: {
bundle: true
},
inventory: {
ats: 10,
backorderable: false,
id: 'inventory_m',
orderable: true,
preorderable: false,
stockLevel: 10
}
}
renderWithProviders(<MockComponent product={mockBundleData} />)
// Bundle products should not show out of stock message when inventory is available
expect(screen.queryByText(/Out of stock/)).not.toBeInTheDocument()
})
test('should show unfulfillable message for bundle products', () => {
// Mock useVariant to return null for bundle products (bundles don't have variants)
useVariant.mockReturnValue(null)
const mockBundleData = {
...mockProductDetail,
type: {
bundle: true
},
quantity: 15,
inventory: {
ats: 5,
backorderable: false,
id: 'inventory_m',
orderable: true,
preorderable: false,
stockLevel: 5
}
}
renderWithProviders(<MockComponent product={mockBundleData} />)
expect(screen.getByText(/Only 5 left!/)).toBeInTheDocument()
})
})