|
| 1 | +import '@testing-library/jest-dom'; |
| 2 | +import { renderHook, waitFor } from '@testing-library/react'; |
| 3 | +import useProductInfo from '../src/hooks/useProduct'; |
| 4 | +import { transformResultItem } from '../src/utils/transformers'; |
| 5 | +import mockItem from './local_examples/item.json'; |
| 6 | +import { renderHookWithCioPlp } from './test-utils'; |
| 7 | + |
| 8 | +describe('Testing Hook: useProductInfo', () => { |
| 9 | + beforeEach(() => { |
| 10 | + // Mock console error to de-clutter the console for expected errors |
| 11 | + const spy = jest.spyOn(console, 'error'); |
| 12 | + spy.mockImplementation(() => {}); |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + jest.restoreAllMocks(); // This will reset all mocks after each test |
| 17 | + }); |
| 18 | + |
| 19 | + it('Should throw error if called outside of PlpContext', () => { |
| 20 | + expect(() => renderHook(() => useProductInfo())).toThrow(); |
| 21 | + }); |
| 22 | + |
| 23 | + const transformedItem = transformResultItem(mockItem); |
| 24 | + |
| 25 | + it('Should return productSwatch, itemName, itemImageUrl, itemUrl, itemPrice', async () => { |
| 26 | + const { result } = renderHookWithCioPlp(() => useProductInfo({ item: transformedItem })); |
| 27 | + |
| 28 | + await waitFor(() => { |
| 29 | + const { |
| 30 | + current: { productSwatch, itemName, itemImageUrl, itemUrl, itemPrice }, |
| 31 | + } = result; |
| 32 | + |
| 33 | + expect(productSwatch).not.toBeNull(); |
| 34 | + expect(itemName).toEqual(transformedItem.itemName); |
| 35 | + expect(itemImageUrl).toEqual(transformedItem.imageUrl); |
| 36 | + expect(itemUrl).toEqual(transformedItem.url); |
| 37 | + expect(itemPrice).toEqual(transformedItem.data.price); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('Should return nothing properly with getters that return nothing', async () => { |
| 42 | + const { result } = renderHookWithCioPlp(() => useProductInfo({ item: transformedItem }), { |
| 43 | + initialProps: { |
| 44 | + itemFieldGetters: { |
| 45 | + getPrice: () => {}, |
| 46 | + getSwatches: () => {}, |
| 47 | + getSwatchPreview: () => {}, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }); |
| 51 | + |
| 52 | + await waitFor(() => { |
| 53 | + const { |
| 54 | + current: { productSwatch, itemName, itemImageUrl, itemUrl, itemPrice }, |
| 55 | + } = result; |
| 56 | + |
| 57 | + expect(productSwatch).not.toBeNull(); |
| 58 | + expect(itemName).toEqual(transformedItem.itemName); |
| 59 | + expect(itemImageUrl).toEqual(transformedItem.imageUrl); |
| 60 | + expect(itemUrl).toEqual(transformedItem.url); |
| 61 | + expect(itemPrice).toBeUndefined(); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('Should return correctly after different variation is selected', async () => { |
| 66 | + const { result } = renderHookWithCioPlp(() => useProductInfo({ item: transformedItem })); |
| 67 | + |
| 68 | + await waitFor(() => { |
| 69 | + const { |
| 70 | + current: { productSwatch, itemName, itemImageUrl, itemUrl, itemPrice }, |
| 71 | + } = result; |
| 72 | + const { selectVariation, swatchList } = productSwatch; |
| 73 | + selectVariation(swatchList[1]); |
| 74 | + |
| 75 | + expect(itemName).toEqual(swatchList[1].itemName); |
| 76 | + expect(itemImageUrl).toEqual(swatchList[1].imageUrl || transformedItem.imageUrl); |
| 77 | + expect(itemUrl).toEqual(swatchList[1].url || transformedItem.url); |
| 78 | + expect(itemPrice).toEqual(swatchList[1].price || transformedItem.data.price); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('Should return nothing properly with getters that throw errors', async () => { |
| 83 | + const { result } = renderHookWithCioPlp(() => useProductInfo({ item: transformedItem }), { |
| 84 | + initialProps: { |
| 85 | + itemFieldGetters: { |
| 86 | + getPrice: () => { |
| 87 | + throw new Error(); |
| 88 | + }, |
| 89 | + getSwatches: () => { |
| 90 | + throw new Error(); |
| 91 | + }, |
| 92 | + getSwatchPreview: () => { |
| 93 | + throw new Error(); |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }); |
| 98 | + |
| 99 | + await waitFor(() => { |
| 100 | + const { |
| 101 | + current: { productSwatch, itemName, itemImageUrl, itemUrl, itemPrice }, |
| 102 | + } = result; |
| 103 | + |
| 104 | + expect(productSwatch).not.toBeNull(); |
| 105 | + expect(itemName).toEqual(transformedItem.itemName); |
| 106 | + expect(itemImageUrl).toEqual(transformedItem.imageUrl); |
| 107 | + expect(itemUrl).toEqual(transformedItem.url); |
| 108 | + expect(itemPrice).toBeUndefined(); |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments