Skip to content

Commit

Permalink
MWPW-156357 Price-literals - fail gracefully (#2815)
Browse files Browse the repository at this point in the history
* MWPW-156357 Price-literals - fail gracefully

* MWPW-156357 Price-literals - fail gracefully

* Trigger Build

* MWPW-156357 Price-literals - fail gracefully

* MWPW-156357 Price-literals - fail gracefully

* MWPW-156357 Price-literals - fail gracefully

* MWPW-156357 Price-literals - fail gracefully

* MWPW-156357 Price-literals - fail gracefully

* MWPW-156357 Price-literals - fail gracefully

* MWPW-156357 Price-literals - fail gracefully

* MWPW-156357 Price-literals - fail gracefully

* MWPW-156357 Price-literals - fail gracefully

* Trigger Build

* MWPW-156357 Price-literals - fail gracefully

---------

Co-authored-by: Bozo Jovicic <bozo@hitthecode.com>
  • Loading branch information
bozojovicic and Bozo Jovicic authored Sep 10, 2024
1 parent 5ad94df commit 3fd877c
Show file tree
Hide file tree
Showing 29 changed files with 44 additions and 666 deletions.
10 changes: 0 additions & 10 deletions libs/blocks/merch/merch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
} from '../../utils/utils.js';
import { replaceKey } from '../../features/placeholders.js';

export const PRICE_LITERALS_URL = 'https://www.adobe.com/federal/commerce/price-literals.json';
export const CHECKOUT_LINK_CONFIG_PATH = '/commerce/checkout-link.json'; // relative to libs.

export const PRICE_TEMPLATE_DISCOUNT = 'discount';
Expand Down Expand Up @@ -179,14 +178,6 @@ export async function fetchEntitlements() {
return fetchEntitlements.promise;
}

export async function fetchLiterals(url) {
fetchLiterals.promise = fetchLiterals.promise ?? new Promise((resolve) => {
fetch(url)
.then((response) => response.json().then(({ data }) => resolve(data)));
});
return fetchLiterals.promise;
}

export async function fetchCheckoutLinkConfigs(base = '') {
fetchCheckoutLinkConfigs.promise = fetchCheckoutLinkConfigs.promise
?? fetch(`${base}${CHECKOUT_LINK_CONFIG_PATH}`).catch((e) => {
Expand Down Expand Up @@ -430,7 +421,6 @@ export async function initService(force = false) {
fetchCheckoutLinkConfigs.promise = undefined;
}
const { env, commerce = {}, locale } = getConfig();
commerce.priceLiteralsPromise = fetchLiterals(PRICE_LITERALS_URL);
initService.promise = initService.promise ?? polyfills().then(async () => {
const { hostname, searchParams } = new URL(window.location.href);
let commerceLibPath = '../../deps/mas/commerce.js';
Expand Down
4 changes: 2 additions & 2 deletions libs/deps/mas/commerce.js

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions libs/features/mas/commerce/libs/commerce.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ declare global {
Commerce.Checkout.Settings &
Commerce.Price.Settings &
Commerce.Wcs.Settings,
'locale' | 'priceLiteralsURL' | 'quantity'
'locale' | 'quantity'
> & {
quantity: number;
}
Expand Down Expand Up @@ -514,8 +514,6 @@ declare global {
displayRecurrence: boolean;
displayTax: boolean;
forceTaxExclusive: boolean;
priceLiteralsURL: string;
priceLiteralsPromise: Promise<Response>;
}
}

Expand Down
1 change: 1 addition & 0 deletions libs/features/mas/commerce/price-literals.json

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions libs/features/mas/commerce/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ declare global {
Commerce.Checkout.Settings &
Commerce.Price.Settings &
Commerce.Wcs.Settings,
'locale' | 'priceLiteralsURL' | 'quantity'
'locale' | 'quantity'
> & {
quantity: number;
}
Expand Down Expand Up @@ -514,8 +514,6 @@ declare global {
displayRecurrence: boolean;
displayTax: boolean;
forceTaxExclusive: boolean;
priceLiteralsURL: string;
priceLiteralsPromise: Promise<Response>;
}
}

Expand Down
24 changes: 6 additions & 18 deletions libs/features/mas/commerce/src/literals.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import { ERROR_MESSAGE_MISSING_LITERALS_URL } from './constants.js';
import { Defaults } from './defaults.js';
import { equalsCaseInsensitive } from './external.js';

function generateLiteralsPromise(settings) {
if (!settings.priceLiteralsURL) {
throw new Error(ERROR_MESSAGE_MISSING_LITERALS_URL);
}
return new Promise((resolve) => {
window.fetch(settings.priceLiteralsURL).then((response) => {
response.json().then(({ data }) => {
resolve(data);
});
});
});
}

/**
* Method resolves price literals for the given language from the group of price literals.
* That group is either imported from json file or it is received as a parameter (in case of unit tests).
*
* @param {Commerce.Price.Settings} settings
* @param priceLiterals
* @returns {Promise<Record<string, string>>}
*/
export async function fetchPriceLiterals(settings) {
const priceLiteralsPromise =
settings.priceLiteralsPromise || generateLiteralsPromise(settings);
export async function getPriceLiterals(settings, priceLiterals) {
//we are expecting an array of objects with lang and literals
const data = await priceLiteralsPromise;
const { data } = priceLiterals ? priceLiterals : await import('../price-literals.json');
if (Array.isArray(data)) {
const find = (language) =>
data.find((candidate) =>
Expand Down
4 changes: 2 additions & 2 deletions libs/features/mas/commerce/src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EVENT_TYPE_READY, TAG_NAME_SERVICE } from './constants.js';
import { Defaults } from './defaults.js';
import { isFunction } from './external.js';
import { Ims } from './ims.js';
import { fetchPriceLiterals } from './literals.js';
import { getPriceLiterals } from './literals.js';
import { Log } from './log.js';
import { Price } from './price.js';
import { getSettings } from './settings.js';
Expand Down Expand Up @@ -43,7 +43,7 @@ async function activateService(config, dataProviders) {
const settings = Object.freeze(getSettings(config));
// Fetch price literals
try {
literals.price = await fetchPriceLiterals(settings);
literals.price = await getPriceLiterals(settings, config.commerce.priceLiterals);
} catch (error) {
log.warn('Price literals were not fetched:', error);
}
Expand Down
2 changes: 0 additions & 2 deletions libs/features/mas/commerce/src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,6 @@ function getSettings(config = {}) {
modal,
env,
forceTaxExclusive,
priceLiteralsURL: commerce.priceLiteralsURL,
priceLiteralsPromise: commerce.priceLiteralsPromise,
promotionCode,
quantity,
wcsApiKey,
Expand Down
3 changes: 1 addition & 2 deletions libs/features/mas/commerce/test/checkout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { mockConfig } from './mocks/config.js';
import { mockFetch } from './mocks/fetch.js';
import { mockIms, unmockIms } from './mocks/ims.js';
import { mockLana, unmockLana } from './mocks/lana.js';
import { withLiterals } from './mocks/literals.js';
import { mockProviders } from './mocks/providers.js';
import { withWcs } from './mocks/wcs.js';
import { expect, sinon } from './utilities.js';
Expand Down Expand Up @@ -48,7 +47,7 @@ afterEach(() => {
});

beforeEach(async () => {
await mockFetch(withWcs, withLiterals);
await mockFetch(withWcs);
mockLana();
});

Expand Down
40 changes: 10 additions & 30 deletions libs/features/mas/commerce/test/literals.test.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,24 @@
import { Defaults } from '../src/index.js';
import { fetchPriceLiterals } from '../src/literals.js';

import { mockFetch } from './mocks/fetch.js';
import { priceLiteralsURL, withLiterals } from './mocks/literals.js';
import { getPriceLiterals } from '../src/literals.js';
import { expect } from './utilities.js';
import { readFile } from '@web/test-runner-commands';

describe('function "fetchPriceLiterals"', () => {
beforeEach(async () => {
await mockFetch(withLiterals);
});

it('throws if settings argument does not have `priceLiteralsURL` nor `priceLiteralsPromise` property', async () => {
// @ts-ignore
expect(fetchPriceLiterals({})).eventually.be.rejected;
});

it('returns literals from promise', async () => {
const priceLiteralsPromise = new Promise((resolve) =>
resolve([
{
lang: 'en',
recurrenceLabel:
'{recurrenceTerm, select, MONTH {/mo} YEAR {/yr} other {}}',
},
]),
);
describe('function "getPriceLiterals"', () => {
it('returns literals', async () => {
const priceLiterals = await (readFile({ path: '../price-literals.json' }));
// @ts-ignore
const literals = await fetchPriceLiterals({
const literals = await getPriceLiterals({
language: 'en',
priceLiteralsPromise,
});
}, JSON.parse(priceLiterals));
expect(literals.lang).to.equal(Defaults.language);
});

it('returns literals for default language if requested does not exist', async () => {
const priceLiterals = await (readFile({ path: '../price-literals.json' }));
// @ts-ignore
const literals = await fetchPriceLiterals({
const literals = await getPriceLiterals({
language: 'test',
priceLiteralsURL,
});
}, JSON.parse(priceLiterals));
expect(literals.lang).to.equal(Defaults.language);
});
});
4 changes: 1 addition & 3 deletions libs/features/mas/commerce/test/placeholder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { mockLana, unmockLana } from './mocks/lana.js';
import { expect } from './utilities.js';
import { initService } from '../src/service.js';
import { mockFetch } from './mocks/fetch.js';
import { withLiterals } from './mocks/literals.js';

let id = 1;
/**
Expand Down Expand Up @@ -62,8 +61,7 @@ describe('custom span-based placeholder', () => {
document.body.innerHTML = '';
});

before(async () => {
await mockFetch(withLiterals);
before( () => {
mockLana();
});

Expand Down
3 changes: 1 addition & 2 deletions libs/features/mas/commerce/test/price.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { initService, resetService } from '../src/service.js';
import { mockConfig } from './mocks/config.js';
import { mockFetch } from './mocks/fetch.js';
import { mockLana, unmockLana } from './mocks/lana.js';
import { withLiterals } from './mocks/literals.js';
import snapshots from './mocks/snapshots.js';
import { withWcs } from './mocks/wcs.js';
import { expect } from './utilities.js';
Expand All @@ -32,7 +31,7 @@ afterEach(() => {
});

beforeEach(async () => {
await mockFetch(withWcs, withLiterals);
await mockFetch(withWcs);
mockLana();
});

Expand Down
10 changes: 7 additions & 3 deletions libs/features/mas/commerce/test/service.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { readFile } from '@web/test-runner-commands';
import { delay } from '../src/external.js';
import { TAG_NAME_SERVICE, Defaults, init, reset } from '../src/index.js';
import { HTMLWcmsCommerceElement } from '../src//service.js';
Expand All @@ -8,11 +9,10 @@ import { mockIms, unmockIms } from './mocks/ims.js';
import { expect } from './utilities.js';
import { mockProviders } from './mocks/providers.js';
import { withWcs } from './mocks/wcs.js';
import { withLiterals } from './mocks/literals.js';

describe('commerce service', () => {
before(async () => {
await mockFetch(withWcs, withLiterals);
await mockFetch(withWcs);
});

afterEach(() => {
Expand Down Expand Up @@ -71,7 +71,11 @@ describe('commerce service', () => {

describe('property "literals"', () => {
it('returns "price literals" object', async () => {
const instance = await init(mockConfig());
const priceLiterals = await (readFile({ path: '../price-literals.json' }));
const commerce = {
priceLiterals: JSON.parse(priceLiterals),
};
const instance = await init(mockConfig(commerce));
[
'alternativePriceAriaLabel',
'freeAriaLabel',
Expand Down
6 changes: 0 additions & 6 deletions libs/features/mas/commerce/test/settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ describe('getSettings', () => {
expect(getSettings()).to.deep.equal({
...Defaults,
locale: `${Defaults.language}_${Defaults.country}`,
priceLiteralsURL: undefined,
priceLiteralsPromise: undefined,
quantity: [Defaults.quantity],
});
});
Expand Down Expand Up @@ -76,8 +74,6 @@ describe('getSettings', () => {
quantity: [2],
wcsApiKey: 'testapikey',
locale: "en_US",
priceLiteralsURL: undefined,
priceLiteralsPromise: undefined,
env: "STAGE",
wcsURL: WCS_STAGE_URL
});
Expand Down Expand Up @@ -111,8 +107,6 @@ describe('getSettings', () => {
env: Env.STAGE,
language: 'nb',
locale: 'nb_NO',
priceLiteralsURL: undefined,
priceLiteralsPromise: undefined,
quantity: [Defaults.quantity],
wcsApiKey,
wcsURL: WCS_STAGE_URL,
Expand Down
6 changes: 1 addition & 5 deletions libs/features/mas/mas/src/mas.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ const isStage = searchParams.get('env') === 'stage';
const envName = isStage ? 'stage' : 'prod';
const commerceEnv = isStage ? 'STAGE' : 'PROD';

const priceLiteralsPromise = fetch(
'https://www.adobe.com/federal/commerce/price-literals.json',
).then((response) => response.json().then(({ data }) => data));

const config = () => ({
env: { name: envName },
commerce: { 'commerce.env': commerceEnv, priceLiteralsPromise },
commerce: { 'commerce.env': commerceEnv },
locale: { prefix: locale },
});

Expand Down
3 changes: 0 additions & 3 deletions libs/features/mas/mocks/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { priceLiteralsURL } from './literals.js';

export const mockConfig =
(
commerce = {},
Expand All @@ -11,7 +9,6 @@ export const mockConfig =
) =>
() => ({
commerce: {
priceLiteralsURL,
...commerce,
},
env,
Expand Down
17 changes: 0 additions & 17 deletions libs/features/mas/mocks/literals.js

This file was deleted.

Loading

0 comments on commit 3fd877c

Please sign in to comment.