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-147284: discard default params in merch link #2231

Merged
merged 6 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions libs/blocks/ost/ost.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ export const WCS_LANDSCAPE = 'PUBLISHED';
*/
const METADATA_MAPPINGS = { 'checkout-workflow': 'workflow' };

const priceDefaultOptions = {
term: true,
seat: true,
tax: false,
old: false,
exclusive: false,
};

const updateParams = (params, key, value) => {
if (value !== priceDefaultOptions[key]) {
params.set(key, value);
}
};

document.body.classList.add('tool', 'tool-ost');

/**
Expand Down Expand Up @@ -65,11 +79,11 @@ export const createLinkMarkup = (
displayOldPrice,
forceTaxExclusive,
} = options;
params.set('term', displayRecurrence);
params.set('seat', displayPerUnit);
params.set('tax', displayTax);
params.set('old', displayOldPrice);
params.set('exclusive', forceTaxExclusive);
updateParams(params, 'term', displayRecurrence);
updateParams(params, 'seat', displayPerUnit);
updateParams(params, 'tax', displayTax);
updateParams(params, 'old', displayOldPrice);
updateParams(params, 'exclusive', forceTaxExclusive);
}
return `https://milo.adobe.com/tools/ost?${params.toString()}`;
};
Expand Down
2 changes: 1 addition & 1 deletion test/blocks/ost/mocks/ost-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ function unmockOstDeps() {
}

export {
getConfig, getLocale, getMetadata, loadScript, loadStyle, mockOstDeps, mockRes, unmockOstDeps,
getConfig, getLocale, getMetadata, loadScript, loadStyle, mockOstDeps, unmockOstDeps, mockRes,
};
123 changes: 115 additions & 8 deletions test/blocks/ost/ost.test.html.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,59 @@
import { expect } from '@esm-bundle/chai';

import { mockOstDeps, unmockOstDeps } from './mocks/ost-utils.js';
import { CheckoutWorkflow, CheckoutWorkflowStep } from '../../../libs/deps/commerce.js';
import { DEFAULT_CTA_TEXT, createLinkMarkup } from '../../../libs/blocks/ost/ost.js';

const { perpM2M } = await fetch('./mocks/wcs-artifacts-mock.json').then((res) => res.json());
const defaults = {
checkoutWorkflow: 'UCv3',
checkoutWorkflowStep: 'email',
};
const osi = 'cea462e983f649bca2293325c9894bdd';
const promo = 'test-promo';
const texts = {
buy: DEFAULT_CTA_TEXT,
try: 'free-trial',
};
const types = {
checkoutUrl: 'checkoutUrl',
price: 'price',
opticalPrice: 'opticalPrice',
};

function assertLink(link, offer, params, text = texts.buy) {
const { searchParams } = new URL(link.href);
Object.entries(params).forEach(([key, value]) => {
expect(searchParams.get(key)).to.equal(String(value));
});
if (params.type === types.checkoutUrl) {
expect(searchParams.get('text')).to.equal(text);
expect(link.text).to.equal(`CTA {{${text}}}`);
} else {
expect(link.text).to.equal(`PRICE - ${offer.planType} - ${offer.name}`);
}
}

function createLink(params = {}) {
return createLinkMarkup(
defaults,
params.osi ?? osi,
params.type,
perpM2M,
params,
params.promo,
);
}

beforeEach(() => {
sessionStorage.clear();
});

afterEach(() => {
unmockOstDeps();
});

describe('loadOstEnv', async () => {
beforeEach(() => {
sessionStorage.clear();
});
describe('OST: loadOstEnv', async () => {
it('fetches and returns page status and metadata', async () => {
const {
options: { country, language, workflow },
Expand Down Expand Up @@ -81,10 +125,7 @@ describe('loadOstEnv', async () => {
});
});

describe('init', () => {
beforeEach(() => {
sessionStorage.clear();
});
describe('OST: init', () => {
it('opens OST without waiting for IMS if query string includes token', async () => {
const {
options: { country, language, workflow },
Expand Down Expand Up @@ -179,3 +220,69 @@ describe('init', () => {
});
});
});

describe('OST: merch link creation', () => {
describe('checkout-link', () => {
const type = types.checkoutUrl;

it('with default params', async () => {
const link = createLink({ type });
assertLink(link, perpM2M, { osi, type });
expect({ ...link.dataset }).to.eql({});
});

it('with promo and custom text', async () => {
const ctaText = texts.try;
const link = createLink({ ctaText, promo, type });
assertLink(link, perpM2M, { osi, promo, type }, ctaText);
});

it('to UCv2 workflow', async () => {
const workflow = CheckoutWorkflow.V2;
const workflowStep = CheckoutWorkflowStep.CHECKOUT_EMAIL;
const link = createLink({ type, workflow, workflowStep });
assertLink(link, perpM2M, { osi, type, workflow, workflowStep });
});
});

describe('inline-price', () => {
const type = types.price;

it('with default params', async () => {
const link = createLink({ type });
assertLink(link, perpM2M, { osi, type });
});

it('with default params from OST', async () => {
const link = createLink({
type,
displayRecurrence: true,
displayPerUnit: true,
displayTax: false,
displayOldPrice: false,
forceTaxExclusive: false,
});
expect(link.href).to.eql('https://milo.adobe.com/tools/ost?osi=cea462e983f649bca2293325c9894bdd&type=price&perp=true');
});

it('with custom options', async () => {
const displayRecurrence = true;
const displayPerUnit = true;
const displayTax = true;
const forceTaxExclusive = true;
const link = createLink({
displayRecurrence,
displayPerUnit,
displayTax,
forceTaxExclusive,
type,
});
assertLink(link, perpM2M, {
tax: displayTax,
exclusive: forceTaxExclusive,
osi,
type,
});
});
});
});
102 changes: 0 additions & 102 deletions test/blocks/ost/ost.test.js

This file was deleted.

Loading