From e280e445e652acdcb7c10e86ccefe18570a62da0 Mon Sep 17 00:00:00 2001 From: Dylan Depass Date: Fri, 1 Nov 2024 16:51:11 -0400 Subject: [PATCH] fix: rename offerPattern to offerVariantURLTemplate --- src/types.d.ts | 2 +- src/utils/product.js | 6 +++--- test/template/html.test.js | 2 +- test/utils/product.test.js | 44 +++++++++++++++++++------------------- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/types.d.ts b/src/types.d.ts index ec2d53b..d473b31 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -34,7 +34,7 @@ declare global { params: Record; headers: Record; host: string; - offerPattern: string; + offerVariantURLTemplate: string; matchedPath: string; matchedPathConfig: Config; attributeOverrides: AttributeOverrides; diff --git a/src/utils/product.js b/src/utils/product.js index ac6420d..b95d1f1 100644 --- a/src/utils/product.js +++ b/src/utils/product.js @@ -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}`; diff --git a/test/template/html.test.js b/test/template/html.test.js index cabe87e..2f74f5c 100644 --- a/test/template/html.test.js +++ b/test/template/html.test.js @@ -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); diff --git a/test/utils/product.test.js b/test/utils/product.test.js index 76f684e..465bb8f 100644 --- a/test/utils/product.test.js +++ b/test/utils/product.test.js @@ -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}}', }; @@ -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'); }); @@ -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'); }); @@ -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'); }); });