Skip to content

Commit

Permalink
fix: rename offerPattern to offerVariantURLTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
dylandepass committed Nov 1, 2024
1 parent fcca717 commit e280e44
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ declare global {
params: Record<string, string>;
headers: Record<string, string>;
host: string;
offerPattern: string;
offerVariantURLTemplate: string;
matchedPath: string;
matchedPathConfig: Config;
attributeOverrides: AttributeOverrides;
Expand Down
6 changes: 3 additions & 3 deletions src/utils/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ export function constructProductUrl(config, product, variant) {
return `${productUrl}?pid=${variant.externalId}&o=${btoa(options)}`;
}

const offerPattern = config.matchedPathConfig?.offerPattern;
if (!offerPattern) {
const offerVariantURLTemplate = config.matchedPathConfig?.offerVariantURLTemplate;
if (!offerVariantURLTemplate) {
return `${productUrl}/?optionsUIDs=${encodeURIComponent(variant.selections.join(','))}`;
}

const variantPath = offerPattern
const variantPath = offerVariantURLTemplate
.replace('{{urlkey}}', product.urlKey)
.replace('{{sku}}', encodeURIComponent(variant.sku));
return `${config.host}${variantPath}`;
Expand Down
2 changes: 1 addition & 1 deletion test/template/html.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('Render Product HTML', () => {

it('should have the correct JSON-LD schema with custom offer pattern', () => {
config.matchedPathConfig = {
offerPattern: '/us/p/{{urlkey}}?selected_product={{sku}}',
offerVariantURLTemplate: '/us/p/{{urlkey}}?selected_product={{sku}}',
};
const html = htmlTemplate(config, product, variations);
dom = new JSDOM(html);
Expand Down
44 changes: 22 additions & 22 deletions test/utils/product.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ import assert from 'node:assert';
import { constructProductUrl } from '../../src/utils/product.js';

describe('constructProductUrl', () => {
const configWithOfferPattern = {
const configWithOfferVariantURLTemplate = {
host: 'https://www.example.com',
matchedPath: '/products/{{urlkey}}/{{sku}}',
matchedPathConfig: {
offerPattern: '/products/{{urlkey}}?selected_product={{sku}}',
offerVariantURLTemplate: '/products/{{urlkey}}?selected_product={{sku}}',
},
};

const configWithoutOfferPattern = {
const configWithoutOfferVariantURLTemplate = {
host: 'https://www.example.com',
matchedPath: '/products/{{urlkey}}/{{sku}}',
};
Expand All @@ -51,33 +51,33 @@ describe('constructProductUrl', () => {
};

it('should construct the correct product URL without variant', () => {
const url = constructProductUrl(configWithoutOfferPattern, product1);
const url = constructProductUrl(configWithoutOfferVariantURLTemplate, product1);
const expectedUrl = 'https://www.example.com/products/utopia-small-pendant/KW5531';
assert.strictEqual(url, expectedUrl, 'Product URL without variant does not match expected URL');
});

// Test Case 2: Construct URL with variant and offerPattern
it('should construct the correct variant URL with offerPattern', () => {
const url = constructProductUrl(configWithOfferPattern, product1, variant1);
// Test Case 2: Construct URL with variant and offerVariantURLTemplate
it('should construct the correct variant URL with offerVariantURLTemplate', () => {
const url = constructProductUrl(configWithOfferVariantURLTemplate, product1, variant1);
const expectedUrl = 'https://www.example.com/products/utopia-small-pendant?selected_product=VAR%20001';
assert.strictEqual(url, expectedUrl, 'Variant URL with offerPattern does not match expected URL');
assert.strictEqual(url, expectedUrl, 'Variant URL with offerVariantURLTemplate does not match expected URL');
});

it('should construct the correct variant URL without offerPattern', () => {
const url = constructProductUrl(configWithoutOfferPattern, product1, variant1);
it('should construct the correct variant URL without offerVariantURLTemplate', () => {
const url = constructProductUrl(configWithoutOfferVariantURLTemplate, product1, variant1);
const expectedUrl = 'https://www.example.com/products/utopia-small-pendant/KW5531/?optionsUIDs=Y29uZmlndXJhYmxlLzE2NTEvODI3MQ==';
assert.strictEqual(url, expectedUrl, 'Variant URL without offerPattern does not match expected URL');
assert.strictEqual(url, expectedUrl, 'Variant URL without offerVariantURLTemplate does not match expected URL');
});

// Test Case 4: Encode special characters in sku and urlKey
it('should correctly encode special characters in sku and urlKey', () => {
const url = constructProductUrl(configWithoutOfferPattern, productWithSpecialCharacters);
const url = constructProductUrl(configWithoutOfferVariantURLTemplate, productWithSpecialCharacters);
const expectedUrl = 'https://www.example.com/products/summer-sun/KW%2055%2F31';
assert.strictEqual(url, expectedUrl, 'URL with special characters does not match expected URL');
});

it('should correctly encode special characters in variant sku and selections', () => {
const url = constructProductUrl(configWithoutOfferPattern, productWithSpecialCharacters, variantWithSpecialSelections);
const url = constructProductUrl(configWithoutOfferVariantURLTemplate, productWithSpecialCharacters, variantWithSpecialSelections);
const expectedUrl = 'https://www.example.com/products/summer-sun/KW%2055%2F31/?optionsUIDs=Y29uZmlndXJhYmxlLzE2NTEvODI3MQ==%3D%2CY29uZmlndXJhYmxlLzI0NjEvMzYzNDE=';
assert.strictEqual(url, expectedUrl, 'Variant URL with special characters does not match expected URL');
});
Expand All @@ -87,19 +87,19 @@ describe('constructProductUrl', () => {
sku: 'VAR-EMPTY',
selections: [],
};
const url = constructProductUrl(configWithoutOfferPattern, product1, variantEmptySelections);
const url = constructProductUrl(configWithoutOfferVariantURLTemplate, product1, variantEmptySelections);
const expectedUrl = 'https://www.example.com/products/utopia-small-pendant/KW5531/?optionsUIDs=';
assert.strictEqual(url, expectedUrl, 'URL with empty variant selections does not match expected URL');
});

it('should handle missing matchedPathConfig when variant is present', () => {
const url = constructProductUrl(configWithoutOfferPattern, product1, variant1);
const url = constructProductUrl(configWithoutOfferVariantURLTemplate, product1, variant1);
const expectedUrl = 'https://www.example.com/products/utopia-small-pendant/KW5531/?optionsUIDs=Y29uZmlndXJhYmxlLzE2NTEvODI3MQ==';
assert.strictEqual(url, expectedUrl, 'URL without offerPattern but with variant does not match expected URL');
assert.strictEqual(url, expectedUrl, 'URL without offerVariantURLTemplate but with variant does not match expected URL');
});

it('should construct the correct URL when variant is undefined', () => {
const url = constructProductUrl(configWithOfferPattern, product1);
const url = constructProductUrl(configWithOfferVariantURLTemplate, product1);
const expectedUrl = 'https://www.example.com/products/utopia-small-pendant/KW5531';
assert.strictEqual(url, expectedUrl, 'Product URL with undefined variant does not match expected URL');
});
Expand All @@ -118,16 +118,16 @@ describe('constructProductUrl', () => {
assert.strictEqual(url, expectedUrl, 'URL with multiple placeholders does not match expected URL');
});

it('should handle empty offerPattern by falling back to optionsUIDs', () => {
const configEmptyOfferPattern = {
it('should handle empty offerVariantURLTemplate by falling back to optionsUIDs', () => {
const configEmptyOfferVariantURLTemplate = {
host: 'https://www.example.com',
matchedPath: '/products/{{urlkey}}-{{sku}}',
matchedPathConfig: {
offerPattern: '',
offerVariantURLTemplate: '',
},
};
const url = constructProductUrl(configEmptyOfferPattern, product1, variant1);
const url = constructProductUrl(configEmptyOfferVariantURLTemplate, product1, variant1);
const expectedUrl = 'https://www.example.com/products/utopia-small-pendant-KW5531/?optionsUIDs=Y29uZmlndXJhYmxlLzE2NTEvODI3MQ==';
assert.strictEqual(url, expectedUrl, 'URL with empty offerPattern does not match expected URL');
assert.strictEqual(url, expectedUrl, 'URL with empty offerVariantURLTemplate does not match expected URL');
});
});

0 comments on commit e280e44

Please sign in to comment.