Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 46 additions & 35 deletions spec/hooks/useProductInfo/useProductInfo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { renderHook, waitFor } from '@testing-library/react';
import useProductInfo from '../../../src/hooks/useProduct';
import { transformResultItem } from '../../../src/utils/transformers';
import mockItem from '../../local_examples/item.json';
import mockItemWithSalePrice from '../../local_examples/itemWithSalePrice.json';
import { renderHookWithCioPlp } from '../../test-utils';

describe('Testing Hook: useProductInfo', () => {
Expand All @@ -21,6 +22,7 @@ describe('Testing Hook: useProductInfo', () => {
});

const transformedItem = transformResultItem(mockItem);
const transformedItemWithSalePrice = transformResultItem(mockItemWithSalePrice);

it('Should return productSwatch, itemId, itemName, itemImageUrl, itemUrl, itemPrice', async () => {
const { result } = renderHookWithCioPlp(() => useProductInfo({ item: transformedItem }));
Expand All @@ -39,27 +41,28 @@ describe('Testing Hook: useProductInfo', () => {
});
});

it('Should return nothing properly with getters that return nothing', async () => {
const { result } = renderHookWithCioPlp(() => useProductInfo({ item: transformedItem }), {
initialProps: {
itemFieldGetters: {
getPrice: () => {},
getSwatches: () => {},
getSwatchPreview: () => {},
},
},
});

await waitFor(() => {
const {
current: { productSwatch, itemName, itemImageUrl, itemUrl, itemPrice },
} = result;

expect(productSwatch).not.toBeNull();
expect(itemName).toEqual(transformedItem.itemName);
expect(itemImageUrl).toEqual(transformedItem.imageUrl);
expect(itemUrl).toEqual(transformedItem.url);
expect(itemPrice).toBeUndefined();
describe.each([
{
item: transformedItem,
itemDescription: 'a standard item',
},
{
item: transformedItemWithSalePrice,
itemDescription: 'an item on sale',
},
])('With $itemDescription', ({ item }) => {
it.each([
['itemId', item.itemId],
['itemName', item.itemName],
['itemImageUrl', item.imageUrl],
['itemUrl', item.url],
['itemPrice', item.data.price],
['salePrice', item.data.sale_price],
])('Should return the correct value for "%s"', async (property, expectedValue) => {
const { result } = renderHookWithCioPlp(() => useProductInfo({ item }));
await waitFor(() => {
expect(result.current[property]).toEqual(expectedValue);
});
});
});

Expand All @@ -68,7 +71,7 @@ describe('Testing Hook: useProductInfo', () => {

await waitFor(() => {
const {
current: { productSwatch, itemName, itemImageUrl, itemUrl, itemPrice },
current: { productSwatch, itemName, itemImageUrl, itemUrl, itemPrice, itemSalePrice },
} = result;
const { selectVariation, swatchList } = productSwatch;
selectVariation(swatchList[1]);
Expand All @@ -77,36 +80,44 @@ describe('Testing Hook: useProductInfo', () => {
expect(itemImageUrl).toEqual(swatchList[1].imageUrl || transformedItem.imageUrl);
expect(itemUrl).toEqual(swatchList[1].url || transformedItem.url);
expect(itemPrice).toEqual(swatchList[1].price || transformedItem.data.price);
expect(itemSalePrice).toEqual(swatchList[1].salePrice || transformedItem.data.salePrice);
});
});

it('Should return nothing properly with getters that throw errors', async () => {
const { result } = renderHookWithCioPlp(() => useProductInfo({ item: transformedItem }), {
initialProps: {
itemFieldGetters: {
getPrice: () => {
throw new Error();
},
getSwatches: () => {
throw new Error();
},
getSwatchPreview: () => {
throw new Error();
const { result } = renderHookWithCioPlp(
() => useProductInfo({ item: transformedItemWithSalePrice }),
{
initialProps: {
itemFieldGetters: {
getPrice: () => {
throw new Error();
},
getSwatches: () => {
throw new Error();
},
getSwatchPreview: () => {
throw new Error();
},
getSalePrice: () => {
throw new Error();
},
},
},
},
});
);

await waitFor(() => {
const {
current: { productSwatch, itemName, itemImageUrl, itemUrl, itemPrice },
current: { productSwatch, itemName, itemImageUrl, itemUrl, itemPrice, itemSalePrice },
} = result;

expect(productSwatch).not.toBeNull();
expect(itemName).toEqual(transformedItem.itemName);
expect(itemImageUrl).toEqual(transformedItem.imageUrl);
expect(itemUrl).toEqual(transformedItem.url);
expect(itemPrice).toBeUndefined();
expect(itemSalePrice).toBeUndefined();
});
});
});
13 changes: 11 additions & 2 deletions spec/hooks/useProductSwatch/useProductSwatch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import useProductSwatch from '../../../src/hooks/useProductSwatch';
import { transformResultItem } from '../../../src/utils/transformers';
import mockItem from '../../local_examples/item.json';
import { renderHookWithCioPlp } from '../../test-utils';
import { getSwatchPreview, getPrice, getSwatches } from '../../../src/utils/itemFieldGetters';
import {
getSwatchPreview,
getPrice,
getSwatches,
getSalePrice,
} from '../../../src/utils/itemFieldGetters';

describe('Testing Hook: useProductSwatch', () => {
beforeEach(() => {
Expand All @@ -18,7 +23,7 @@ describe('Testing Hook: useProductSwatch', () => {
});

const transformedItem = transformResultItem(mockItem);
const expectedSwatch = getSwatches(transformedItem, getPrice, getSwatchPreview);
const expectedSwatch = getSwatches(transformedItem, getPrice, getSwatchPreview, getSalePrice);

it('Should throw error if called outside of PlpContext', () => {
expect(() => renderHook(() => useProductSwatch())).toThrow();
Expand Down Expand Up @@ -63,6 +68,7 @@ describe('Testing Hook: useProductSwatch', () => {
getPrice: () => {},
getSwatches: () => {},
getSwatchPreview: () => {},
getSalePrice: () => {},
},
},
});
Expand Down Expand Up @@ -91,6 +97,9 @@ describe('Testing Hook: useProductSwatch', () => {
getSwatchPreview: () => {
throw new Error();
},
getSalePrice: () => {
throw new Error();
},
},
},
});
Expand Down
1 change: 0 additions & 1 deletion spec/local_examples/item.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"url": "https://constructorio-integrations.s3.amazonaws.com/tikus-threads/2022-06-29/KNIT-CASUAL-SHIRT_BUTTON-DOWN-KNIT-SHIRT_BKT00110SG1733_3_category.jpg",
"price": 90,
"altPrice": 69,
"sale_price": 23,
"image_url": "https://constructorio-integrations.s3.amazonaws.com/tikus-threads/2022-06-29/KNIT-CASUAL-SHIRT_BUTTON-DOWN-KNIT-SHIRT_BKT00110SG1733_3_category.jpg",
"group_ids": [
"1111"
Expand Down
210 changes: 210 additions & 0 deletions spec/local_examples/itemWithSalePrice.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
{
"matched_terms": [
"shirts"
],
"data": {
"id": "KNITS00423-park bench dot",
"url": "https://constructorio-integrations.s3.amazonaws.com/tikus-threads/2022-06-29/KNIT-CASUAL-SHIRT_BUTTON-DOWN-KNIT-SHIRT_BKT00110SG1733_3_category.jpg",
"price": 90,
"altPrice": 69,
"sale_price": 21,
"image_url": "https://constructorio-integrations.s3.amazonaws.com/tikus-threads/2022-06-29/KNIT-CASUAL-SHIRT_BUTTON-DOWN-KNIT-SHIRT_BKT00110SG1733_3_category.jpg",
"group_ids": [
"1111"
],
"description": "Cotton jersey fabric that feels like a tee & looks like a button-down.",
"facets": [
{
"name": "Color",
"values": [
"Park Bench Dot"
]
}
],
"variation_id": "BKT00110DG1733LR"
},
"value": "Jersey Riviera Shirt (Red Park Bench Dot)",
"is_slotted": false,
"labels": {},
"variations": [
{
"data": {
"variation_id": "BKT00110DG1733LR",
"swatchPreview": "#e04062",
"price": 90,
"sale_price": 21,
"facets": [
{
"name": "Color",
"values": [
"Park Bench Dot"
]
}
]
},
"value": "Jersey Riviera Shirt (Red Park Bench Dot)"
},
{
"data": {
"variation_id": "BKT00110DG1733MR",
"swatchPreview": "#a3c43b",
"price": 99,
"sale_price": 22,
"facets": [
{
"name": "Color",
"values": [
"Red Park Bench Dot"
]
}
]
},
"value": "Jersey Riviera Shirt (Green Park Bench Dot)"
},
{
"data": {
"variation_id": "BKT00110DG1733SR",
"swatchPreview": "#253d37",
"price": 100,
"sale_price": 23,
"facets": [
{
"name": "Color",
"values": [
"Green Park Bench Dot"
]
}
]
},
"value": "Jersey Riviera Shirt (Black Park Bench Dot)"
},
{
"data": {
"variation_id": "BKT00110DG1733XLR",
"swatchPreview": "#bfc1bc",
"price": 105,
"facets": [
{
"name": "Color",
"values": [
"Black Park Bench Dot"
]
}
]
},
"value": "Jersey Riviera Shirt (Silver Park Bench Dot)"
},
{
"data": {
"variation_id": "BKT00110DG1733XSR",
"facets": [
{
"name": "Color",
"values": [
"Silver Park Bench Dot"
]
}
]
},
"value": "Jersey Riviera Shirt (Park Bench Dot)"
},
{
"data": {
"variation_id": "BKT00110DG1733XXLR",
"facets": [
{
"name": "Color",
"values": [
"Park Bench Dot"
]
}
]
},
"value": "Jersey Riviera Shirt (Park Bench Dot)"
},
{
"data": {
"variation_id": "BKT00110SG1733LR",
"facets": [
{
"name": "Color",
"values": [
"Park Bench Dot"
]
}
]
},
"value": "Jersey Riviera Shirt (Park Bench Dot)"
},
{
"data": {
"variation_id": "BKT00110SG1733MR",
"facets": [
{
"name": "Color",
"values": [
"Park Bench Dot"
]
}
]
},
"value": "Jersey Riviera Shirt (Park Bench Dot)"
},
{
"data": {
"variation_id": "BKT00110SG1733SR",
"facets": [
{
"name": "Color",
"values": [
"Park Bench Dot"
]
}
]
},
"value": "Jersey Riviera Shirt (Park Bench Dot)"
},
{
"data": {
"variation_id": "BKT00110SG1733XLR",
"facets": [
{
"name": "Color",
"values": [
"Park Bench Dot"
]
}
]
},
"value": "Jersey Riviera Shirt (Park Bench Dot)"
},
{
"data": {
"variation_id": "BKT00110SG1733XSR",
"facets": [
{
"name": "Color",
"values": [
"Park Bench Dot"
]
}
]
},
"value": "Jersey Riviera Shirt (Park Bench Dot)"
},
{
"data": {
"variation_id": "BKT00110SG1733XXLR",
"facets": [
{
"name": "Color",
"values": [
"Park Bench Dot"
]
}
]
},
"value": "Jersey Riviera Shirt (Park Bench Dot)"
}
]
}
Loading