-
Notifications
You must be signed in to change notification settings - Fork 214
@W-18771949 Get rule based bonus products from API #2641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
d35d40f
37abf08
36006e4
0c0350f
d1136a0
b2d0d0a
b02f97e
037675e
cd91851
77c9779
814c9ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,7 +35,7 @@ export const useBonusState = (basket) => { | |
| const [state, setState] = useState({ | ||
| isOpen: false, | ||
| data: {}, | ||
| bonusProducts: basket?.bonusDiscountLineItems || [] | ||
| existingBonusProducts: basket?.bonusDiscountLineItems || [] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why changing it to existingBonusProducts? What made it different from |
||
| }) | ||
| const {pathname} = useLocation() | ||
| const {onOpen: onAddToCartModalOpen} = useAddToCartModalContext() | ||
|
|
@@ -54,10 +54,10 @@ export const useBonusState = (basket) => { | |
| const currentBonusItems = basket?.bonusDiscountLineItems || [] | ||
| setState((prev) => { | ||
| // Only update if the bonus items have actually changed | ||
| if (JSON.stringify(prev.bonusProducts) !== JSON.stringify(currentBonusItems)) { | ||
| if (JSON.stringify(prev.existingBonusProducts) !== JSON.stringify(currentBonusItems)) { | ||
| return { | ||
| ...prev, | ||
| bonusProducts: currentBonusItems | ||
| existingBonusProducts: currentBonusItems | ||
| } | ||
| } | ||
| return prev | ||
|
|
@@ -66,18 +66,18 @@ export const useBonusState = (basket) => { | |
|
|
||
| const addBonusProducts = (newBonusItems) => { | ||
| setState((prev) => { | ||
| const updatedBonusProducts = [...prev.bonusProducts, ...newBonusItems] | ||
| const updatedBonusProducts = [...prev.existingBonusProducts, ...newBonusItems] | ||
| return { | ||
| ...prev, | ||
| bonusProducts: updatedBonusProducts | ||
| existingBonusProducts: updatedBonusProducts | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| return { | ||
| isOpen: state.isOpen, | ||
| data: state.data, | ||
| bonusProducts: state.bonusProducts, | ||
| bonusProducts: state.existingBonusProducts, | ||
| addBonusProducts, | ||
| onClose: () => { | ||
| setState((prev) => ({ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /* | ||
| * 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 {HOME_SHOP_PRODUCTS_LIMIT} from '@salesforce/retail-react-app/app/constants' | ||
| import {useProductSearch} from '@salesforce/commerce-sdk-react' | ||
|
|
||
| export const useBonusProductSearch = (promotionId) => { | ||
sf-madhuri-uppu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const {data: productSearchResult} = useProductSearch({ | ||
| parameters: { | ||
| allImages: true, | ||
| allVariationProperties: true, | ||
| expand: ['promotions', 'variations', 'prices', 'images', 'custom_properties'], | ||
| limit: HOME_SHOP_PRODUCTS_LIMIT, | ||
| perPricebook: true, | ||
| refine: [`pmid=${promotionId}`, 'htype=master'] | ||
| } | ||
| }) | ||
| return { | ||
| data: productSearchResult | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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'] | ||
| } | ||
| }) | ||
| }) | ||
| }) |
Uh oh!
There was an error while loading. Please reload this page.