Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MWPW-159374: Support MAS switch to Prosemirror RTE #3117

Merged
merged 10 commits into from
Nov 8, 2024
4 changes: 2 additions & 2 deletions libs/deps/mas/commerce.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [eslint] reported by reviewdog 🐶
File ignored because of a matching ignore pattern. Use "--no-ignore" to override.

Large diffs are not rendered by default.

162 changes: 87 additions & 75 deletions libs/deps/mas/mas.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [eslint] reported by reviewdog 🐶
File ignored because of a matching ignore pattern. Use "--no-ignore" to override.

Large diffs are not rendered by default.

206 changes: 105 additions & 101 deletions libs/deps/mas/merch-card.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [eslint] reported by reviewdog 🐶
File ignored because of a matching ignore pattern. Use "--no-ignore" to override.

Large diffs are not rendered by default.

35 changes: 27 additions & 8 deletions libs/features/mas/commerce/src/checkout-link.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [eslint] reported by reviewdog 🐶
File ignored because of a matching ignore pattern. Use "--no-ignore" to override.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class CheckoutLink extends HTMLAnchorElement {
static is = 'checkout-link';
static tag = 'a';

/* c8 ignore next 1 */
#checkoutActionHandler;

masElement = new MasElement(this);
Expand All @@ -23,10 +24,12 @@ export class CheckoutLink extends HTMLAnchorElement {

connectedCallback() {
this.masElement.connectedCallback();
this.addEventListener('click', this.handleClick);
}

disconnectedCallback() {
this.masElement.disconnectedCallback();
this.removeEventListener('click', this.handleClick);
}

onceSettled() {
Expand All @@ -37,13 +40,17 @@ export class CheckoutLink extends HTMLAnchorElement {
return this.masElement.value;
}

get options() {
return this.masElement.options;
}

requestUpdate(force = false) {
return this.masElement.requestUpdate(force);
}

constructor() {
super();
this.addEventListener('click', this.clickHandler);
this.handleClick = this.handleClick.bind(this);
}

static get observedAttributes() {
Expand Down Expand Up @@ -107,8 +114,20 @@ export class CheckoutLink extends HTMLAnchorElement {
* Triggers checkout action handler, if provided.
* @param {*} event
*/
clickHandler(event) {
this.#checkoutActionHandler?.(event);
handleClick(event) {
if (event.target !== this) {
event.preventDefault();
event.stopImmediatePropagation();
this.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window,
}),
);
return;
}
this.#checkoutActionHandler?.(event);
}

async render(overrides = {}) {
Expand All @@ -126,8 +145,8 @@ export class CheckoutLink extends HTMLAnchorElement {
let extraOptions;
try {
extraOptions = JSON.parse(options.extraOptions ?? '{}');
/* c8 ignore next 3 */
} catch (e) {
/* c8 ignore next 2 */
this.masElement.log?.error('cannot parse exta checkout options', e);
}
const version = this.masElement.togglePending(options);
Expand All @@ -139,7 +158,7 @@ export class CheckoutLink extends HTMLAnchorElement {
const checkoutAction = await service.buildCheckoutAction?.(
offers.flat(),
{ ...extraOptions, ...options },
this
this,
);
return this.renderOffers(
offers.flat(),
Expand Down Expand Up @@ -173,6 +192,7 @@ export class CheckoutLink extends HTMLAnchorElement {
options = { ...extraOptions, ...options, ...overrides };
version ??= this.masElement.togglePending(options);
if (this.#checkoutActionHandler) {
/* c8 ignore next 2 */
this.#checkoutActionHandler = undefined;
}
if (checkoutAction) {
Expand All @@ -183,8 +203,8 @@ export class CheckoutLink extends HTMLAnchorElement {
if (text) this.firstElementChild.innerHTML = text;
if (className) this.classList.add(...className.split(' '));
if (handler) {
this.setAttribute('href', '#');
this.#checkoutActionHandler = handler.bind(this);
this.setAttribute('href', '#');
this.#checkoutActionHandler = handler.bind(this);
}
return true;
} else if (offers.length) {
Expand All @@ -200,7 +220,6 @@ export class CheckoutLink extends HTMLAnchorElement {
return true;
}
}
return false;
}

updateOptions(options = {}) {
Expand Down
21 changes: 13 additions & 8 deletions libs/features/mas/commerce/src/inline-price.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [eslint] reported by reviewdog 🐶
File ignored because of a matching ignore pattern. Use "--no-ignore" to override.

Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,22 @@ export class InlinePrice extends HTMLSpanElement {

disconnectedCallback() {
this.masElement.disconnectedCallback();
this.removeEventListener('click', this.handleClick.bind(this));
this.removeEventListener('click', this.handleClick);
}


handleClick(event) {
/* c8 ignore next 4 */
if (event.target === this) return;
// re-dispatch click event from the price element
event.stopImmediatePropagation();
this.dispatchEvent(new CustomEvent('click', { bubbles: true }));
}
/* c8 ignore next 4 */
if (event.target === this) return;
// re-dispatch click event from the price element
event.stopImmediatePropagation();
this.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window,
}),
);
}

onceSettled() {
return this.masElement.onceSettled();
Expand Down
25 changes: 25 additions & 0 deletions libs/features/mas/commerce/test/checkout.test.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [eslint] reported by reviewdog 🐶
File ignored because of a matching ignore pattern. Use "--no-ignore" to override.

Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@ describe('class "CheckoutLink"', () => {
'https://commerce.adobe.com/store/email?items%5B0%5D%5Bid%5D=632B3ADD940A7FBB7864AA5AD19B8D28&cli=adobe_com&ctx=fp&co=US&lang=en',
);
expect(checkoutLink.value).to.be.not.empty;
expect(checkoutLink.options).to.be.not.empty;
});

it('re-dispatches click event', async () => {
await initMasCommerceService();
const checkoutLink = mockCheckoutLink('abm');
let targetIsCheckoutlink = false;
checkoutLink.addEventListener(
'click',
(event) => {
event.preventDefault();
event.stopImmediatePropagation();
targetIsCheckoutlink = event.target === checkoutLink;
},
{ once: true },
);
await checkoutLink.onceSettled();
checkoutLink.firstElementChild.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window,
}),
);
expect(targetIsCheckoutlink).to.be.true;
});

it('renders link with workflow step from settings', async () => {
Expand Down
95 changes: 73 additions & 22 deletions libs/features/mas/commerce/test/price.test.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [eslint] reported by reviewdog 🐶
File ignored because of a matching ignore pattern. Use "--no-ignore" to override.

Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import {
} from '../src/constants.js';
import { InlinePrice } from '../src/inline-price.js';
import { Price } from '../src/price.js';
import { getSettings } from '../src/settings.js';
import { getSettings } from '../src/settings.js';

import { mockFetch } from './mocks/fetch.js';
import { mockLana, unmockLana } from './mocks/lana.js';
import snapshots from './mocks/snapshots.js';
import { withWcs } from './mocks/wcs.js';
import { initMasCommerceService, expect, disableMasCommerceService } from './utilities.js';
import {
initMasCommerceService,
expect,
disableMasCommerceService,
} from './utilities.js';

/**
* @param {string} wcsOsi
Expand Down Expand Up @@ -44,6 +48,28 @@ describe('class "InlinePrice"', () => {
expect(inlinePrice.value).to.be.not.empty;
});

it('re-dispatches click event', async () => {
await initMasCommerceService();
const inlinePrice = mockInlinePrice('puf');
let targetIsInlinePrice = false;
inlinePrice.addEventListener(
'click',
(event) => {
targetIsInlinePrice = event.target === inlinePrice;
},
{ once: true },
);
await inlinePrice.onceSettled();
inlinePrice.firstElementChild.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window,
}),
);
expect(targetIsInlinePrice).to.be.true;
});

it('renders strikethrough price', async () => {
await initMasCommerceService();
const inlinePrice = mockInlinePrice('puf');
Expand Down Expand Up @@ -174,18 +200,22 @@ describe('class "InlinePrice"', () => {
it('fails placeholder if "orders" array is empty', async () => {
await initMasCommerceService();
const inlinePrice = mockInlinePrice('abm');
inlinePrice.renderOffers([], {}, inlinePrice.masElement.togglePending());
inlinePrice.renderOffers(
[],
{},
inlinePrice.masElement.togglePending(),
);
expect(inlinePrice.state).to.equal(InlinePrice.STATE_FAILED);
});
});

describe('method "requestUpdate"', () => {
it('has requestUpdate method', async () => {
await initMasCommerceService();
const inlinePrice = mockInlinePrice('abm');
inlinePrice.requestUpdate();
});
});
it('has requestUpdate method', async () => {
await initMasCommerceService();
const inlinePrice = mockInlinePrice('abm');
inlinePrice.requestUpdate();
});
});

describe('method "updateOptions"', () => {
it('updates element data attributes', async () => {
Expand Down Expand Up @@ -632,17 +662,38 @@ describe('commerce service', () => {
});
});
describe('function "direct price calls"', () => {
it('works as expected', async () => {
const service = await initMasCommerceService();
const { collectPriceOptions, buildPriceHTML } = new Price({
literals: { price: {} }, providers: { price: [(p,o) => {/*nop*/} ]}, settings: getSettings(service.config)});
const inlinePrice1 = mockInlinePrice('abm');
const options = collectPriceOptions({}, inlinePrice1);
expect(options).not.to.be.empty;
buildPriceHTML({ priceDetails:{} }, { template: 'discount', ...options } );
buildPriceHTML({ priceDetails:{} }, { template: 'strikethrough', ...options });
buildPriceHTML({ priceDetails:{} }, { template: 'optical', ...options });
buildPriceHTML({ priceDetails:{} }, { template: 'annual', ...options });
});
});
it('works as expected', async () => {
const service = await initMasCommerceService();
const { collectPriceOptions, buildPriceHTML } = new Price({
literals: { price: {} },
providers: {
price: [
(p, o) => {
/*nop*/
},
],
},
settings: getSettings(service.config),
});
const inlinePrice1 = mockInlinePrice('abm');
const options = collectPriceOptions({}, inlinePrice1);
expect(options).not.to.be.empty;
buildPriceHTML(
{ priceDetails: {} },
{ template: 'discount', ...options },
);
buildPriceHTML(
{ priceDetails: {} },
{ template: 'strikethrough', ...options },
);
buildPriceHTML(
{ priceDetails: {} },
{ template: 'optical', ...options },
);
buildPriceHTML(
{ priceDetails: {} },
{ template: 'annual', ...options },
);
});
});
});
162 changes: 87 additions & 75 deletions libs/features/mas/mas/dist/mas.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [eslint] reported by reviewdog 🐶
File ignored because of a matching ignore pattern. Use "--no-ignore" to override.

Large diffs are not rendered by default.

Loading
Loading