-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathuse-bonus-product-search.test.js
More file actions
139 lines (115 loc) · 4.66 KB
/
use-bonus-product-search.test.js
File metadata and controls
139 lines (115 loc) · 4.66 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
/*
* 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 {renderHook} from '@testing-library/react'
import {useProductSearch} from '@salesforce/commerce-sdk-react'
import {useBonusProductSearch} from '@salesforce/retail-react-app/../../app/hooks/use-bonus-product-search'
import {HOME_SHOP_PRODUCTS_LIMIT} from '@salesforce/retail-react-app/app/constants'
// Mock the commerce SDK hook
jest.mock('@salesforce/commerce-sdk-react')
describe('useBonusProductSearch', () => {
let mockUseProductSearch
beforeEach(() => {
mockUseProductSearch = {
data: null,
isLoading: false,
error: null
}
useProductSearch.mockReturnValue(mockUseProductSearch)
})
afterEach(() => {
jest.clearAllMocks()
})
test('should call useProductSearch with correct parameters when promotionId is provided', () => {
const promotionId = 'test-promotion-id'
renderHook(() => useBonusProductSearch(promotionId))
expect(useProductSearch).toHaveBeenCalledWith({
parameters: {
allImages: true,
allVariationProperties: true,
expand: ['promotions', 'variations', 'prices', 'images', 'custom_properties'],
limit: HOME_SHOP_PRODUCTS_LIMIT,
perPricebook: true,
refine: [`pmid=${promotionId}`, 'htype=master']
}
})
})
test('should call useProductSearch with null promotionId', () => {
renderHook(() => useBonusProductSearch(null))
expect(useProductSearch).toHaveBeenCalledWith({
parameters: {
allImages: true,
allVariationProperties: true,
expand: ['promotions', 'variations', 'prices', 'images', 'custom_properties'],
limit: HOME_SHOP_PRODUCTS_LIMIT,
perPricebook: true,
refine: ['pmid=null', 'htype=master']
}
})
})
test('should call useProductSearch with undefined promotionId', () => {
renderHook(() => useBonusProductSearch(undefined))
expect(useProductSearch).toHaveBeenCalledWith({
parameters: {
allImages: true,
allVariationProperties: true,
expand: ['promotions', 'variations', 'prices', 'images', 'custom_properties'],
limit: HOME_SHOP_PRODUCTS_LIMIT,
perPricebook: true,
refine: ['pmid=undefined', 'htype=master']
}
})
})
test('should return data from useProductSearch', () => {
const mockData = {
hits: [
{
productId: 'test-product-1',
productName: 'Test Product 1'
},
{
productId: 'test-product-2',
productName: 'Test Product 2'
}
]
}
mockUseProductSearch.data = mockData
const {result} = renderHook(() => useBonusProductSearch('test-promotion'))
expect(result.current.data).toBe(mockData)
})
test('should return null data when useProductSearch returns null', () => {
mockUseProductSearch.data = null
const {result} = renderHook(() => useBonusProductSearch('test-promotion'))
expect(result.current.data).toBeNull()
})
test('should handle empty string promotionId', () => {
renderHook(() => useBonusProductSearch(''))
expect(useProductSearch).toHaveBeenCalledWith({
parameters: {
allImages: true,
allVariationProperties: true,
expand: ['promotions', 'variations', 'prices', 'images', 'custom_properties'],
limit: HOME_SHOP_PRODUCTS_LIMIT,
perPricebook: true,
refine: ['pmid=', 'htype=master']
}
})
})
test('should use correct refine parameters for promotion search', () => {
const promotionId = 'ChoiceOfBonusProdect-ProductLevel-ruleBased'
renderHook(() => useBonusProductSearch(promotionId))
expect(useProductSearch).toHaveBeenCalledWith({
parameters: {
allImages: true,
allVariationProperties: true,
expand: ['promotions', 'variations', 'prices', 'images', 'custom_properties'],
limit: HOME_SHOP_PRODUCTS_LIMIT,
perPricebook: true,
refine: [`pmid=${promotionId}`, 'htype=master']
}
})
})
})