Skip to content

Commit

Permalink
MWPW-141252: Commerce: handle language in WCS (adobecom#1747)
Browse files Browse the repository at this point in the history
* MWPW-141252: Commerce: handle language in WCS

for GB(UK) locale
  • Loading branch information
yesil authored Jan 17, 2024
1 parent 9db723a commit 94c3009
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 15 deletions.
6 changes: 3 additions & 3 deletions libs/deps/commerce.js

Large diffs are not rendered by default.

25 changes: 23 additions & 2 deletions test/blocks/merch/merch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const validatePriceSpan = async (selector, expectedAttributes) => {
const value = expectedAttributes[key];
expect(dataset[key], ` ${key} should equal ${value}`).to.equal(value);
});
return el;
};

describe('Merch Block', () => {
Expand All @@ -50,14 +51,13 @@ describe('Merch Block', () => {
window.lana = { log: () => { } };
document.head.innerHTML = await readFile({ path: './mocks/head.html' });
document.body.innerHTML = await readFile({ path: './mocks/body.html' });
await mockIms('CH');
await mockFetch();
setConfig(config);
});

beforeEach(async () => {
const { init, Log } = await import('../../../libs/deps/commerce.js');
await init(() => config);
await init(() => config, true);
Log.reset();
Log.use(Log.Plugins.quietFilter);
});
Expand Down Expand Up @@ -105,6 +105,11 @@ describe('Merch Block', () => {
it('renders merch link to tax exclusive price with tax exclusive attribute', async () => {
await validatePriceSpan('.merch.price.tax-exclusive', { forceTaxExclusive: 'true' });
});

it('renders merch link to GB price', async () => {
const el = await validatePriceSpan('.merch.price.gb', {});
expect(/£/.test(el.textContent)).to.be.true;
});
});

describe('promo prices', () => {
Expand Down Expand Up @@ -209,6 +214,19 @@ describe('Merch Block', () => {
await init(() => config, true);
});

it('renders merch link to cta for GB locale', async () => {
const { init } = await import('../../../libs/deps/commerce.js');
await mockIms();
await init(() => config, true);
const el = await merch(document.querySelector(
'.merch.cta.gb',
));
const { nodeName, href } = await el.onceSettled();
expect(nodeName).to.equal('A');
expect(el.getAttribute('is')).to.equal('checkout-link');
expect(/0ADF92A6C8514F2800BE9E87DB641D2A/.test(href)).to.be.true;
});

it('renders merch link to cta with empty promo', async () => {
const el = await merch(document.querySelector(
'.merch.cta.nopromo',
Expand Down Expand Up @@ -263,6 +281,9 @@ describe('Merch Block', () => {
});

it('adds ims country to checkout link', async () => {
const { init } = await import('../../../libs/deps/commerce.js');
await mockIms('CH');
await init(() => config, true);
const el = await merch(document.querySelector(
'.merch.cta.ims',
));
Expand Down
7 changes: 7 additions & 0 deletions test/blocks/merch/mocks/body.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ <h2>Regular prices</h2>
href="/tools/ost?osi=06&type=price&seat=true&tax=true">Price - 632B3ADD940A7FBB7864AA5AD19B8D28 - All Apps </a>
</p>

<p>Price for GB locale: <a class="merch price gb"
href="/tools/ost?osi=gb1&type=price">Price - 49133266E474B3E6EE5D1CB98B95B824 - Photoshop </a>
</p>

<h3>Strikethrough price</h3>
<p>Display term, seat and tax texts: <a class="merch price strikethrough"
href="/tools/ost?osi=07&type=strikethrough&seat=true&tax=true">Price - 632B3ADD940A7FBB7864AA5AD19B8D28 - All Apps </a>
Expand Down Expand Up @@ -81,6 +85,9 @@ <h2>CTAs</h2>
<p>CTA with blue background: <a class="merch cta strong"
href="/tools/ost?osi=20&type=checkoutUrl"><strong>CTA Buy Now</strong></a>
</p>
<p>CTA for GB locale: <a class="merch cta gb"
href="/tools/ost?osi=gb1&type=checkoutUrl">CTA Buy Now</a>
</p>

<div data-promotion-code="nicopromo" class="fragment">
<h2>Promo prices inside a fragment</h2>
Expand Down
24 changes: 14 additions & 10 deletions test/blocks/merch/mocks/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export async function mockFetch() {
// this path allows to import this mock from tests for other blocks (e.g. commerce)
const literals = JSON.parse(await readFile({ path: '../merch/mocks/literals.json' }));
const offers = JSON.parse(await readFile({ path: '../merch/mocks/offers.json' }));
const namedOffers = JSON.parse(await readFile({ path: '../merch/mocks/named-offers.json' }));

const { fetch } = window;
sinon.stub(window, 'fetch').callsFake((...args) => {
Expand All @@ -21,20 +22,23 @@ export async function mockFetch() {
// wcs mock
if (pathname.endsWith('/web_commerce_artifact')) {
const osis = searchParams.get('offer_selector_ids').split(',');
const firstOsi = osis[0];
return Promise.resolve({
status: 200,
statusText: '',
ok: true,
json: () => Promise.resolve({
resolvedOffers: osis.map((osi) => {
let index = Number.parseInt(osi, 10);
if (Number.isNaN(index) || !Number.isFinite(index) || index < 0) index = 0;
return {
...offers[index % offers.length],
offerSelectorIds: [osi],
};
}),
}),
json: () => Promise.resolve(
(/^[A-Za-z]/.test(firstOsi)) ? namedOffers[firstOsi] : {
resolvedOffers: osis.map((osi) => {
let index = Number.parseInt(osi, 10);
if (Number.isNaN(index) || !Number.isFinite(index) || index < 0) index = 0;
return {
...offers[index % offers.length],
offerSelectorIds: [osi],
};
}),
},
),
});
}
// fallback to original fetch, should not happen!
Expand Down
64 changes: 64 additions & 0 deletions test/blocks/merch/mocks/named-offers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"gb1": {
"resolvedOffers": [
{
"offerSelectorIds": [
"gb1"
],
"offerId": "0ADF92A6C8514F2800BE9E87DB641D2A",
"priceDetails": {
"price": 273.17,
"priceWithoutTax": 0.0,
"priceWithoutDiscountAndTax": 227.64,
"usePrecision": true,
"formatString": "'&pound;'#,##0.00",
"taxDisplay": "TAX_INCLUSIVE_DETAILS",
"taxTerm": "VAT"
},
"analytics": "{\"offerId\":\"0ADF92A6C8514F2800BE9E87DB641D2A\",\"label\":\"phsp_direct_individual\",\"price\":\"273.17\",\"amountWithoutTax\":\"0.0\",\"commitmentType\":\"YEAR\",\"billingFrequency\":\"ANNUAL\",\"currencyCode\":\"GBP\"}",
"productArrangementCode": "phsp_direct_individual",
"buyingProgram": "RETAIL",
"commitment": "YEAR",
"term": "ANNUAL",
"customerSegment": "INDIVIDUAL",
"marketSegments": [
"COM"
],
"salesChannel": "DIRECT",
"offerType": "TRIAL",
"pricePoint": "TRIAL_TWP3060_60_DAY_TRIAL_FOR_CCI_SINGLE_APPS_WINBACK",
"language": "MULT",
"merchant": "ADOBE"
},
{
"offerSelectorIds": [
"gb1"
],
"offerId": "49133266E474B3E6EE5D1CB98B95B824",
"priceDetails": {
"price": 262.51,
"priceWithoutTax": 0.0,
"priceWithoutDiscountAndTax": 218.76,
"usePrecision": true,
"formatString": "'&pound;'#,##0.00",
"taxDisplay": "TAX_INCLUSIVE_DETAILS",
"taxTerm": "VAT"
},
"analytics": "{\"offerId\":\"49133266E474B3E6EE5D1CB98B95B824\",\"label\":\"phsp_direct_individual\",\"price\":\"262.51\",\"amountWithoutTax\":\"0.0\",\"commitmentType\":\"YEAR\",\"billingFrequency\":\"ANNUAL\",\"currencyCode\":\"GBP\"}",
"productArrangementCode": "phsp_direct_individual",
"buyingProgram": "RETAIL",
"commitment": "YEAR",
"term": "ANNUAL",
"customerSegment": "INDIVIDUAL",
"marketSegments": [
"COM"
],
"salesChannel": "DIRECT",
"offerType": "TRIAL",
"pricePoint": "TRIAL_TWP3060_60_DAY_TRIAL_FOR_CCI_SINGLE_APPS_WINBACK",
"language": "EN",
"merchant": "ADOBE"
}
]
}
}

0 comments on commit 94c3009

Please sign in to comment.