Skip to content
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

Fail rendering when variant product data is null #14

Merged
merged 6 commits into from
Jan 16, 2025
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
2 changes: 1 addition & 1 deletion actions/pdp-renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async function main (params) {
const contentUrl = __ow_query?.contentUrl || HLX_CONTENT_URL;
const storeUrl = __ow_query?.storeUrl || HLX_STORE_URL || contentUrl;
const productsTemplate = __ow_query?.productsTemplate || HLX_PRODUCTS_TEMPLATE;
const context = { contentUrl, storeUrl, configName };
const context = { contentUrl, storeUrl, configName, logger };

if (!sku || !contentUrl) {
return errorResponse(400, 'Invalid path', logger);
Expand Down
6 changes: 6 additions & 0 deletions actions/pdp-renderer/ldJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ function getOffer(product, url) {
}

async function getVariants(baseProduct, url, axes, context) {
const { logger } = context;
const variantsData = await requestSaaS(VariantsQuery, 'VariantsQuery', { sku: baseProduct.sku }, context);
const variants = variantsData.data.variants.variants;

return variants.map(variant => {
if (!variant.product) {
logger.error(`Variant of product ${baseProduct?.sku} is null. Variant data is not correctly synchronized.`, variant);
throw new Error('Product variant is null');
}

const variantImage = getPrimaryImage(variant.product, null);
const variantUrl = new URL(url);
variantUrl.searchParams.append('optionsUIDs', variant.selections.sort().join(','));
Expand Down
48 changes: 47 additions & 1 deletion test/ldJson.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

const { graphql, HttpResponse } = require('msw');

const { generateLdJson } = require('../actions/pdp-renderer/ldJson');
const { useMockServer, handlers } = require('./mock-server.js');

describe('ldJson', () => {

const mockContext = { contentUrl: 'https://content.com', storeUrl: 'https://example.com', configName: 'config' };
const mockContext = { contentUrl: 'https://content.com', storeUrl: 'https://example.com', configName: 'config', logger: { error: jest.fn() } };
const server = useMockServer();

beforeEach(() => {
mockContext.logger.error.mockClear();
});

test('generate ldJson for simple product', async () => {
const product = {
__typename: 'SimpleProductView',
Expand Down Expand Up @@ -164,4 +170,44 @@ describe('ldJson', () => {
const context = {};
await expect(generateLdJson(product, context)).rejects.toThrow('Unsupported product type');
});

test('fail for null variant', async () => {
const product = {
__typename: 'ComplexProductView',
name: 'Complex Product',
sku: 'complex-sku',
urlKey: 'complex-product',
shortDescription: 'short description',
metaDescription: 'meta description',
description: 'full description',
options: [
{ id: 'color' },
{ id: 'size' }
],
priceRange: {},
inStock: true,
images: [
{ url: 'image1.jpg', roles: ['image'] }
]
};

server.use(graphql.query('VariantsQuery', () => HttpResponse.json({
data: {
variants: {
variants: [
{
selections: [
'color-green',
'size-l'
],
product: null
}
]
}
}
})));

await expect(generateLdJson(product, mockContext)).rejects.toThrow('Product variant is null');
expect(mockContext.logger.error).toHaveBeenCalled();
})
});
Loading