Skip to content

Commit

Permalink
tests and helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
wizardlyhel committed Nov 29, 2024
1 parent 256cd25 commit 68dd48f
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 93 deletions.
197 changes: 139 additions & 58 deletions packages/hydrogen-react/src/getProductOptions.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,68 @@
import {afterEach, describe, expect, it, vi} from 'vitest';
import {checkProductParam} from './getProductOptions.js';
import {checkProductParam, getAdjacentAndFirstAvailableVariants} from './getProductOptions.js';

Check failure on line 2 in packages/hydrogen-react/src/getProductOptions.test.ts

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Replace `checkProductParam,·getAdjacentAndFirstAvailableVariants` with `⏎··checkProductParam,⏎··getAdjacentAndFirstAvailableVariants,⏎`
import {Product} from './storefront-api-types.js';

const ERROR_MSG_START = '[h2:warn:getProductOptions] product.';
const ERROR_MSG_START = '[h2:error:getProductOptions] product.';
const ERROR_MSG_END = ' is missing. Make sure you query for this field from the Storefront API.';

Check failure on line 6 in packages/hydrogen-react/src/getProductOptions.test.ts

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Insert `⏎·`

describe('getAdjacentAndFirstAvailableVariants', () => {
it('returns the correct number of variants found', () => {
const variants = getAdjacentAndFirstAvailableVariants({
options: [
{
optionValues: [
{
firstSelectableVariant: {
id: "snowboard",

Check failure on line 16 in packages/hydrogen-react/src/getProductOptions.test.ts

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Replace `"snowboard"` with `'snowboard'`
selectedOptions: [
{
name: "Color",

Check failure on line 19 in packages/hydrogen-react/src/getProductOptions.test.ts

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Replace `"Color"` with `'Color'`
value: "Turquoise"

Check failure on line 20 in packages/hydrogen-react/src/getProductOptions.test.ts

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Replace `"Turquoise"` with `'Turquoise',`
},
],
},
}

Check failure on line 24 in packages/hydrogen-react/src/getProductOptions.test.ts

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Insert `,`
],
}

Check failure on line 26 in packages/hydrogen-react/src/getProductOptions.test.ts

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Insert `,`
],
selectedOrFirstAvailableVariant: {
id: "snowboard",

Check failure on line 29 in packages/hydrogen-react/src/getProductOptions.test.ts

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Replace `"snowboard"` with `'snowboard'`
selectedOptions: [
{
name: "Color",

Check failure on line 32 in packages/hydrogen-react/src/getProductOptions.test.ts

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Replace `"Color"` with `'Color'`
value: "Turquoise"

Check failure on line 33 in packages/hydrogen-react/src/getProductOptions.test.ts

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Replace `"Turquoise"` with `'Turquoise',`
},
],
},
adjacentVariants: [{
id: "snowboard-2",
selectedOptions: [
{
name: "Color",
value: "Ember"
},
],
}],
} as unknown as Product);

expect(variants.length).toBe(2);
expect(variants[0]).toMatchInlineSnapshot(`
{
"id": "snowboard",
"selectedOptions": [
{
"name": "Color",
"value": "Turquoise",
},
],
}
`);
});
});

describe('checkProductParam', () => {
beforeEach(() => {
vi.spyOn(console, 'warn').mockImplementation(() => {});
vi.spyOn(console, 'error').mockImplementation(() => {});
})

afterEach(() => {
Expand Down Expand Up @@ -46,23 +101,49 @@ describe('checkProductParam', () => {
encodedVariantAvailability: '',
selectedOrFirstAvailableVariant: null,
adjacentVariants: [],
} as unknown as Product, true);

expect(console.error).toHaveBeenCalledTimes(0);
});

it('logs nothing when provided a valid product input without checkAll flag', () => {
checkProductParam({
options: [
{
optionValues: [
{
firstSelectableVariant: {
id: "snowboard",
selectedOptions: [
{
name: "Color",
value: "Turquoise"
},
],
},
}
],
}
],
selectedOrFirstAvailableVariant: null,
adjacentVariants: [],
} as unknown as Product);

expect(console.warn).toHaveBeenCalledTimes(0);
expect(console.error).toHaveBeenCalledTimes(0);
});

it('logs warnings for each missing field when provided an invalid product input', () => {
checkProductParam({
id: "snowboard",
} as unknown as Product)

expect(console.warn).toHaveBeenCalledTimes(6);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}handle${ERROR_MSG_END}`);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}options${ERROR_MSG_END}`);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}encodedVariantExistence${ERROR_MSG_END}`);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}encodedVariantAvailability${ERROR_MSG_END}`);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}selectedOrFirstAvailableVariant${ERROR_MSG_END}`);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}adjacentVariants${ERROR_MSG_END}`);
} as unknown as Product, true)

expect(console.error).toHaveBeenCalledTimes(6);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}handle${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}options${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}encodedVariantExistence${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}encodedVariantAvailability${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}selectedOrFirstAvailableVariant${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}adjacentVariants${ERROR_MSG_END}`);
});

it('logs warnings when provided an invalid options input - missing optionValues', () => {
Expand All @@ -78,10 +159,10 @@ describe('checkProductParam', () => {
encodedVariantAvailability: '',
selectedOrFirstAvailableVariant: null,
adjacentVariants: [],
} as unknown as Product)
} as unknown as Product, true)

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}options.optionValues${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}options.optionValues${ERROR_MSG_END}`);
});

it('logs warnings when provided an invalid options input - missing optionValues.name', () => {
Expand Down Expand Up @@ -111,10 +192,10 @@ describe('checkProductParam', () => {
encodedVariantAvailability: '',
selectedOrFirstAvailableVariant: null,
adjacentVariants: [],
} as unknown as Product)
} as unknown as Product, true)

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}options.optionValues.name${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}options.optionValues.name${ERROR_MSG_END}`);
});

it('logs warnings when provided an invalid options input - missing optionValues.firstSelectableVariant', () => {
Expand All @@ -139,10 +220,10 @@ describe('checkProductParam', () => {
encodedVariantAvailability: '',
selectedOrFirstAvailableVariant: null,
adjacentVariants: [],
} as unknown as Product)
} as unknown as Product, true)

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}options.optionValues.firstSelectableVariant${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}options.optionValues.firstSelectableVariant${ERROR_MSG_END}`);
});

it('logs warnings when provided an invalid options input - missing optionValues.firstSelectableVariant.product.handle', () => {
Expand Down Expand Up @@ -171,10 +252,10 @@ describe('checkProductParam', () => {
encodedVariantAvailability: '',
selectedOrFirstAvailableVariant: null,
adjacentVariants: [],
} as unknown as Product)
} as unknown as Product, true)

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}options.optionValues.firstSelectableVariant.product.handle${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}options.optionValues.firstSelectableVariant.product.handle${ERROR_MSG_END}`);
});

it('logs warnings when provided an invalid options input - missing optionValues.firstSelectableVariant.product.selectedOptions', () => {
Expand All @@ -200,10 +281,10 @@ describe('checkProductParam', () => {
encodedVariantAvailability: '',
selectedOrFirstAvailableVariant: null,
adjacentVariants: [],
} as unknown as Product)
} as unknown as Product, true)

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}options.optionValues.firstSelectableVariant.selectedOptions${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}options.optionValues.firstSelectableVariant.selectedOptions${ERROR_MSG_END}`);
});

it('logs warnings when provided an invalid options input - missing optionValues.firstSelectableVariant.product.selectedOptions.name', () => {
Expand Down Expand Up @@ -234,10 +315,10 @@ describe('checkProductParam', () => {
encodedVariantAvailability: '',
selectedOrFirstAvailableVariant: null,
adjacentVariants: [],
} as unknown as Product)
} as unknown as Product, true)

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}options.optionValues.firstSelectableVariant.selectedOptions.name${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}options.optionValues.firstSelectableVariant.selectedOptions.name${ERROR_MSG_END}`);
});

it('logs warnings when provided an invalid options input - missing optionValues.firstSelectableVariant.product.selectedOptions.value', () => {
Expand Down Expand Up @@ -268,10 +349,10 @@ describe('checkProductParam', () => {
encodedVariantAvailability: '',
selectedOrFirstAvailableVariant: null,
adjacentVariants: [],
} as unknown as Product)
} as unknown as Product, true)

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}options.optionValues.firstSelectableVariant.selectedOptions.value${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}options.optionValues.firstSelectableVariant.selectedOptions.value${ERROR_MSG_END}`);
});

it('logs warnings when product.selectedOrFirstAvailableVariant is available but is invalid - missing selectedOrFirstAvailableVariant.product.handle', () => {
Expand Down Expand Up @@ -313,10 +394,10 @@ describe('checkProductParam', () => {
]
},
adjacentVariants: [],
} as unknown as Product);
} as unknown as Product, true);

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}selectedOrFirstAvailableVariant.product.handle${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}selectedOrFirstAvailableVariant.product.handle${ERROR_MSG_END}`);
});

it('logs warnings when product.selectedOrFirstAvailableVariant is available but is invalid - missing selectedOrFirstAvailableVariant.selectedOptions', () => {
Expand Down Expand Up @@ -355,10 +436,10 @@ describe('checkProductParam', () => {
},
},
adjacentVariants: [],
} as unknown as Product);
} as unknown as Product, true);

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}selectedOrFirstAvailableVariant.selectedOptions${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}selectedOrFirstAvailableVariant.selectedOptions${ERROR_MSG_END}`);
});

it('logs warnings when product.selectedOrFirstAvailableVariant is available but is invalid - missing selectedOrFirstAvailableVariant.selectedOptions.name', () => {
Expand Down Expand Up @@ -402,10 +483,10 @@ describe('checkProductParam', () => {
]
},
adjacentVariants: [],
} as unknown as Product);
} as unknown as Product, true);

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}selectedOrFirstAvailableVariant.selectedOptions.name${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}selectedOrFirstAvailableVariant.selectedOptions.name${ERROR_MSG_END}`);
});

it('logs warnings when product.selectedOrFirstAvailableVariant is available but is invalid - missing selectedOrFirstAvailableVariant.selectedOptions.value', () => {
Expand Down Expand Up @@ -449,10 +530,10 @@ describe('checkProductParam', () => {
]
},
adjacentVariants: [],
} as unknown as Product);
} as unknown as Product, true);

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}selectedOrFirstAvailableVariant.selectedOptions.value${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}selectedOrFirstAvailableVariant.selectedOptions.value${ERROR_MSG_END}`);
});

it('logs warnings when product.adjacentVariants is available but is invalid - missing adjacentVariants.product.handle', () => {
Expand Down Expand Up @@ -494,10 +575,10 @@ describe('checkProductParam', () => {
},
]
}],
} as unknown as Product);
} as unknown as Product, true);

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}adjacentVariants.product.handle${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}adjacentVariants.product.handle${ERROR_MSG_END}`);
});

it('logs warnings when product.adjacentVariants is available but is invalid - missing adjacentVariants.selectedOptions', () => {
Expand Down Expand Up @@ -536,10 +617,10 @@ describe('checkProductParam', () => {
handle: "snowboard"
},
}],
} as unknown as Product);
} as unknown as Product, true);

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}adjacentVariants.selectedOptions${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}adjacentVariants.selectedOptions${ERROR_MSG_END}`);
});

it('logs warnings when product.adjacentVariants is available but is invalid - missing adjacentVariants.selectedOptions.name', () => {
Expand Down Expand Up @@ -583,10 +664,10 @@ describe('checkProductParam', () => {
},
]
}],
} as unknown as Product);
} as unknown as Product, true);

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}adjacentVariants.selectedOptions.name${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}adjacentVariants.selectedOptions.name${ERROR_MSG_END}`);
});

it('logs warnings when product.adjacentVariants is available but is invalid - missing adjacentVariants.selectedOptions.value', () => {
Expand Down Expand Up @@ -630,9 +711,9 @@ describe('checkProductParam', () => {
},
]
}],
} as unknown as Product);
} as unknown as Product, true);

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(`${ERROR_MSG_START}adjacentVariants.selectedOptions.value${ERROR_MSG_END}`);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(`${ERROR_MSG_START}adjacentVariants.selectedOptions.value${ERROR_MSG_END}`);
});
});
Loading

0 comments on commit 68dd48f

Please sign in to comment.