Skip to content

Commit b2d0d0a

Browse files
Addressed pr comments
1 parent d1136a0 commit b2d0d0a

File tree

4 files changed

+110
-105
lines changed

4 files changed

+110
-105
lines changed

packages/template-retail-react-app/app/components/product-view/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ const ProductView = forwardRef(
319319
setPromotionIdToSearch(newBonusItems[0].promotionId)
320320
let ruleBasedBonusProducts = []
321321
if (bonusProductSearchResult?.hits?.length > 0) {
322-
bonusProductSearchResult.hits.forEach((bonusProduct, index) => {
322+
bonusProductSearchResult.hits.forEach((bonusProduct) => {
323323
ruleBasedBonusProducts.push({
324324
productId: bonusProduct.productId,
325325
productName: bonusProduct.productName,

packages/template-retail-react-app/app/components/product-view/index.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,40 @@
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77

8+
// Mock the current customer hook
9+
jest.mock('@salesforce/retail-react-app/app/hooks/use-current-customer', () => ({
10+
useCurrentCustomer: () => ({
11+
data: {
12+
authType: 'registered',
13+
isRegistered: true
14+
}
15+
})
16+
}))
17+
18+
// Mock the bonus product hooks
19+
jest.mock('@salesforce/retail-react-app/app/hooks/use-bonus-product-search', () => ({
20+
useBonusProductSearch: () => ({
21+
data: null
22+
})
23+
}))
24+
25+
jest.mock('@salesforce/retail-react-app/app/hooks/use-bonus-product-modal', () => {
26+
const MockProvider = ({children}) => {
27+
return children
28+
}
29+
30+
return {
31+
useBonusProductModalContext: () => ({
32+
isOpen: false,
33+
onOpen: jest.fn(),
34+
onClose: jest.fn(),
35+
bonusProducts: [],
36+
addBonusProducts: jest.fn()
37+
}),
38+
BonusProductModalProvider: MockProvider
39+
}
40+
})
41+
842
import React from 'react'
943
import PropTypes from 'prop-types'
1044
import {fireEvent, screen, waitFor} from '@testing-library/react'
@@ -16,6 +50,8 @@ import {renderWithProviders} from '@salesforce/retail-react-app/app/utils/test-u
1650
import userEvent from '@testing-library/user-event'
1751
import {useCurrentCustomer} from '@salesforce/retail-react-app/app/hooks/use-current-customer'
1852
import frMessages from '@salesforce/retail-react-app/app/static/translations/compiled/fr-FR.json'
53+
import {useBonusProductSearch} from '@salesforce/retail-react-app/app/hooks/use-bonus-product-search'
54+
import {useBonusProductModalContext} from '@salesforce/retail-react-app/app/hooks/use-bonus-product-modal'
1955

2056
const MockComponent = (props) => {
2157
const {data: customer} = useCurrentCustomer()
@@ -369,3 +405,15 @@ test('renders "Add to Cart" and "Add to Wishlist" buttons in French', async () =
369405
screen.getByRole('button', {name: /ajouter à la liste de souhaits/i})
370406
).toBeInTheDocument()
371407
})
408+
409+
describe('ProductView Bonus Product Integration', () => {
410+
test('should have useBonusProductSearch hook available', () => {
411+
expect(useBonusProductSearch).toBeDefined()
412+
expect(typeof useBonusProductSearch).toBe('function')
413+
})
414+
415+
test('should have useBonusProductModalContext hook available', () => {
416+
expect(useBonusProductModalContext).toBeDefined()
417+
expect(typeof useBonusProductModalContext).toBe('function')
418+
})
419+
})

packages/template-retail-react-app/app/hooks/use-bonus-product-search.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,28 @@
77
import {HOME_SHOP_PRODUCTS_LIMIT} from '@salesforce/retail-react-app/app/constants'
88
import {useProductSearch} from '@salesforce/commerce-sdk-react'
99

10+
/**
11+
* Fetch bonus products for rule based promotions when promotionId is provided.
12+
* @param {string} promotionId - The promotion ID to fetch bonus products for.
13+
* @returns {Object} - The product search result.
14+
*/
1015
export const useBonusProductSearch = (promotionId) => {
11-
const {data: productSearchResult} = useProductSearch({
12-
parameters: {
13-
allImages: true,
14-
allVariationProperties: true,
15-
expand: ['promotions', 'variations', 'prices', 'images', 'custom_properties'],
16-
limit: HOME_SHOP_PRODUCTS_LIMIT,
17-
perPricebook: true,
18-
refine: [`pmid=${promotionId}`, 'htype=master']
16+
const {data: productSearchResult} = useProductSearch(
17+
{
18+
parameters: {
19+
allImages: true,
20+
allVariationProperties: true,
21+
expand: ['promotions', 'variations', 'prices', 'images', 'custom_properties'],
22+
limit: HOME_SHOP_PRODUCTS_LIMIT,
23+
perPricebook: true,
24+
refine: [`pmid=${promotionId}`, 'htype=master']
25+
}
26+
},
27+
{
28+
enabled: !!promotionId
1929
}
20-
})
30+
)
31+
2132
return {
2233
data: productSearchResult
2334
}

packages/template-retail-react-app/app/hooks/use-bonus-product-search.test.js

Lines changed: 41 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -17,123 +17,69 @@ describe('useBonusProductSearch', () => {
1717
let mockUseProductSearch
1818

1919
beforeEach(() => {
20-
mockUseProductSearch = {
21-
data: null,
22-
isLoading: false,
23-
error: null
24-
}
25-
useProductSearch.mockReturnValue(mockUseProductSearch)
20+
mockUseProductSearch = jest.fn()
21+
useProductSearch.mockImplementation(mockUseProductSearch)
2622
})
2723

2824
afterEach(() => {
2925
jest.clearAllMocks()
3026
})
3127

32-
test('should call useProductSearch with correct parameters when promotionId is provided', () => {
28+
it('should call useProductSearch with correct parameters when promotionId is provided', () => {
3329
const promotionId = 'test-promotion-id'
30+
const mockData = {hits: []}
31+
mockUseProductSearch.mockReturnValue({data: mockData})
3432

3533
renderHook(() => useBonusProductSearch(promotionId))
3634

37-
expect(useProductSearch).toHaveBeenCalledWith({
38-
parameters: {
39-
allImages: true,
40-
allVariationProperties: true,
41-
expand: ['promotions', 'variations', 'prices', 'images', 'custom_properties'],
42-
limit: HOME_SHOP_PRODUCTS_LIMIT,
43-
perPricebook: true,
44-
refine: [`pmid=${promotionId}`, 'htype=master']
45-
}
46-
})
47-
})
48-
49-
test('should call useProductSearch with null promotionId', () => {
50-
renderHook(() => useBonusProductSearch(null))
51-
52-
expect(useProductSearch).toHaveBeenCalledWith({
53-
parameters: {
54-
allImages: true,
55-
allVariationProperties: true,
56-
expand: ['promotions', 'variations', 'prices', 'images', 'custom_properties'],
57-
limit: HOME_SHOP_PRODUCTS_LIMIT,
58-
perPricebook: true,
59-
refine: ['pmid=null', 'htype=master']
60-
}
61-
})
62-
})
63-
64-
test('should call useProductSearch with undefined promotionId', () => {
65-
renderHook(() => useBonusProductSearch(undefined))
66-
67-
expect(useProductSearch).toHaveBeenCalledWith({
68-
parameters: {
69-
allImages: true,
70-
allVariationProperties: true,
71-
expand: ['promotions', 'variations', 'prices', 'images', 'custom_properties'],
72-
limit: HOME_SHOP_PRODUCTS_LIMIT,
73-
perPricebook: true,
74-
refine: ['pmid=undefined', 'htype=master']
35+
expect(useProductSearch).toHaveBeenCalledWith(
36+
{
37+
parameters: {
38+
allImages: true,
39+
allVariationProperties: true,
40+
expand: ['promotions', 'variations', 'prices', 'images', 'custom_properties'],
41+
limit: HOME_SHOP_PRODUCTS_LIMIT,
42+
perPricebook: true,
43+
refine: [`pmid=${promotionId}`, 'htype=master']
44+
}
45+
},
46+
{
47+
enabled: true
7548
}
76-
})
49+
)
7750
})
7851

79-
test('should return data from useProductSearch', () => {
80-
const mockData = {
81-
hits: [
82-
{
83-
productId: 'test-product-1',
84-
productName: 'Test Product 1'
85-
},
86-
{
87-
productId: 'test-product-2',
88-
productName: 'Test Product 2'
89-
}
90-
]
91-
}
92-
93-
mockUseProductSearch.data = mockData
52+
it('should return data from useProductSearch when promotionId is provided', () => {
53+
const promotionId = 'test-promotion-id'
54+
const mockData = {hits: [{productId: '123', productName: 'Test Product'}]}
55+
mockUseProductSearch.mockReturnValue({data: mockData})
9456

95-
const {result} = renderHook(() => useBonusProductSearch('test-promotion'))
57+
const {result} = renderHook(() => useBonusProductSearch(promotionId))
9658

9759
expect(result.current.data).toBe(mockData)
9860
})
9961

100-
test('should return null data when useProductSearch returns null', () => {
101-
mockUseProductSearch.data = null
102-
103-
const {result} = renderHook(() => useBonusProductSearch('test-promotion'))
104-
105-
expect(result.current.data).toBeNull()
106-
})
107-
108-
test('should handle empty string promotionId', () => {
109-
renderHook(() => useBonusProductSearch(''))
110-
111-
expect(useProductSearch).toHaveBeenCalledWith({
112-
parameters: {
113-
allImages: true,
114-
allVariationProperties: true,
115-
expand: ['promotions', 'variations', 'prices', 'images', 'custom_properties'],
116-
limit: HOME_SHOP_PRODUCTS_LIMIT,
117-
perPricebook: true,
118-
refine: ['pmid=', 'htype=master']
119-
}
120-
})
121-
})
122-
123-
test('should use correct refine parameters for promotion search', () => {
62+
it('should use correct refine parameters for promotion search', () => {
12463
const promotionId = 'ChoiceOfBonusProdect-ProductLevel-ruleBased'
64+
const mockData = {hits: []}
65+
mockUseProductSearch.mockReturnValue({data: mockData})
12566

12667
renderHook(() => useBonusProductSearch(promotionId))
12768

128-
expect(useProductSearch).toHaveBeenCalledWith({
129-
parameters: {
130-
allImages: true,
131-
allVariationProperties: true,
132-
expand: ['promotions', 'variations', 'prices', 'images', 'custom_properties'],
133-
limit: HOME_SHOP_PRODUCTS_LIMIT,
134-
perPricebook: true,
135-
refine: [`pmid=${promotionId}`, 'htype=master']
69+
expect(useProductSearch).toHaveBeenCalledWith(
70+
{
71+
parameters: {
72+
allImages: true,
73+
allVariationProperties: true,
74+
expand: ['promotions', 'variations', 'prices', 'images', 'custom_properties'],
75+
limit: HOME_SHOP_PRODUCTS_LIMIT,
76+
perPricebook: true,
77+
refine: [`pmid=${promotionId}`, 'htype=master']
78+
}
79+
},
80+
{
81+
enabled: true
13682
}
137-
})
83+
)
13884
})
13985
})

0 commit comments

Comments
 (0)