Skip to content

Commit

Permalink
MWPW-131664: increase merch resilience to errors (adobecom#765)
Browse files Browse the repository at this point in the history
* MWPW-131664: icrease merch resilience to errors

* fix(adobecom#748): icrease merch resilience to errors

* fix test

---------

Co-authored-by: Chris Millar <cmillar@adobe.com>
Co-authored-by: Honwai Wong <honstar.wong@gmail.com>
  • Loading branch information
3 people authored and vgoodric committed Feb 1, 2024
1 parent 3842abd commit ea4f0dd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions libs/blocks/merch/merch.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ window.tacocat.loadPromise = new Promise((resolve) => {
.then(() => loadScript(scriptUrl))
.then(() => {
runTacocat(tacocatEnv, country, language);
resolve();
resolve(false);
})
.catch((error) => {
console.error('Failed to load tacocat', error);
resolve(true);
});
});

Expand Down Expand Up @@ -161,10 +165,8 @@ function getCheckoutContext(searchParams, config) {

export default async function init(el) {
if (!el?.classList?.contains('merch')) return undefined;
try {
await window.tacocat.loadPromise;
} catch (e) {
console.error('Tacocat not loaded', e);
const fail = await window.tacocat.loadPromise;
if (fail) {
return undefined;
}
const { searchParams } = new URL(el.href);
Expand Down
4 changes: 2 additions & 2 deletions test/blocks/merch/merch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ document.body.innerHTML = await readFile({ path: './mocks/body.html' });
describe('Merch Block', () => {
before(async () => {
Object.assign(window.tacocat, {
loadPromise: Promise.resolve(),
loadPromise: Promise.resolve(false),
price: { optionProviders: [] },
defaults: {
apiKey: 'wcms-commerce-ims-ro-user-milo',
Expand Down Expand Up @@ -346,7 +346,7 @@ describe('Merch Block', () => {
});

it('does not initialize the block when tacocat fails to load', async () => {
window.tacocat.loadPromise = Promise.reject(new Error('404'));
window.tacocat.loadPromise = Promise.resolve(true);
let el = document.querySelector('.merch.cta.notacocat');
el = await merch(el);
expect(el).to.be.undefined;
Expand Down

0 comments on commit ea4f0dd

Please sign in to comment.