Skip to content

Commit

Permalink
cr - variables and remove try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
meganthecoder committed Oct 22, 2024
1 parent 22fde56 commit 0234a96
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 41 deletions.
51 changes: 27 additions & 24 deletions libs/blocks/marketo/marketo.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,32 +93,35 @@ export const setPreferences = (formData) => {
};

const showSuccessSection = (formData, scroll = true) => {
try {
const show = (el) => {
el.classList.remove('hide-block');
if (scroll) el.scrollIntoView({ behavior: 'smooth' });
};
const section = formData[SUCCESS_SECTION].toLowerCase().replaceAll(' ', '-');
const success = document.querySelector(`.section.${section}`);
if (success) {
show(success);
return;
}
// For Marquee use case
let count = 0;
const interval = setInterval(() => {
const el = document.querySelector(`.section.${section}`);
if (el) {
clearInterval(interval);
show(el);
}
count += 1;
if (count > 6) clearInterval(interval);
}, 500);
} catch (e) {
/* c8 ignore next 2 */
const show = (el) => {
el.classList.remove('hide-block');
if (scroll) el.scrollIntoView({ behavior: 'smooth' });
};
const successClass = formData[SUCCESS_SECTION]?.toLowerCase().replaceAll(' ', '-');
if (!successClass) {
window.lana?.log('Error showing Marketo success section', { tags: 'warn,marketo' });
return;
}
const section = document.querySelector(`.section.${successClass}`);
if (section) {
show(section);
return;
}
// For Marquee use case
const maxIntervals = 6;
let count = 0;
const interval = setInterval(() => {
const el = document.querySelector(`.section.${successClass}`);
if (el) {
clearInterval(interval);
show(el);
}
count += 1;
if (count > maxIntervals) {
clearInterval(interval);
window.lana?.log('Error showing Marketo success section', { tags: 'warn,marketo' });
}
}, 500);
};

export const formSuccess = (formEl, formData) => {
Expand Down
48 changes: 31 additions & 17 deletions test/blocks/marketo/marketo.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { readFile } from '@web/test-runner-commands';
import { expect } from '@esm-bundle/chai';
import sinon from 'sinon';
import sinon, { stub } from 'sinon';
import { delay } from '../../helpers/waitfor.js';
import { setConfig } from '../../../libs/utils/utils.js';
import init, { setPreferences, decorateURL, FORM_PARAM } from '../../../libs/blocks/marketo/marketo.js';

const innerHTML = await readFile({ path: './mocks/body.html' });
window.lana = { log: stub() };

describe('marketo', () => {
beforeEach(() => {
Expand Down Expand Up @@ -108,38 +109,51 @@ describe('marketo decorateURL', () => {

const onePage = await readFile({ path: './mocks/one-page-experience.html' });

describe('Marketo one page experience', () => {
describe('Marketo ungated one page experience', () => {
let url;
let clock;

beforeEach(() => {
url = new URL(window.location);
url.searchParams.set(FORM_PARAM, 'off');
window.history.pushState({}, '', url);
document.body.innerHTML = onePage;
clock = sinon.useFakeTimers();
});

it('shows success section if ungated', () => {
const url = new URL(window.location);
url.searchParams.set(FORM_PARAM, 'off');
afterEach(() => {
url.searchParams.delete(FORM_PARAM);
window.history.pushState({}, '', url);
clock.restore();
});

it('shows success section', () => {
init(document.querySelector('.marketo'));
expect(document.querySelector('.section.form-success').classList.contains('hide-block')).to.be.false;

url.searchParams.delete(FORM_PARAM);
window.history.pushState({}, '', url);
});

it('shows success section that appears after marketo if ungated', () => {
const url = new URL(window.location);
url.searchParams.set(FORM_PARAM, 'off');
window.history.pushState({}, '', url);
it('shows success section that appears after marketo', () => {
document.querySelector('#success-section').classList.remove('form-success');
const clock = sinon.useFakeTimers();

init(document.querySelector('.marketo'));
expect(document.querySelector('#success-section').classList.contains('hide-block')).to.be.true;
document.querySelector('#success-section').classList.add('form-success');
clock.tick(500);
expect(document.querySelector('#success-section').classList.contains('hide-block')).to.be.false;
});

url.searchParams.delete(FORM_PARAM);
window.history.pushState({}, '', url);
clock.restore();
it('logs error if success section is not provided', async () => {
document.body.innerHTML = await readFile({ path: './mocks/one-page-error.html' });

init(document.querySelector('.marketo'));
expect(window.lana.log.args[0][0]).to.equal('Error showing Marketo success section');
});

it('logs error if success section does not appear after maximum intervals', async () => {
document.querySelector('#success-section').classList.remove('form-success');

init(document.querySelector('.marketo'));
expect(document.querySelector('#success-section').classList.contains('hide-block')).to.be.true;
clock.runAll();
expect(window.lana.log.args[0][0]).to.equal('Error showing Marketo success section');
});
});
51 changes: 51 additions & 0 deletions test/blocks/marketo/mocks/one-page-error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<div id="success-section" class="section hide-block form-success">
<div class="content" daa-lh="b1|content"><p>Form Success</p></div>
<div class="section-metadata">
<div>
<div>style</div>
<div>hide block, form success</div>
</div>
</div>
</div>
<div class="section container xxl spacing two-up resource-form">
<div class="columns contained">
<div class="row">
<div class="col">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec cursus mi id tincidunt pretium. Praesent a porta ex.
Etiam eu metus urna. Etiam vulputate nibh nisi, sed gravida diam dictum id. Cras et justo metus. Morbi consectetur
diam eu mi ultricies, molestie efficitur quam posuere. Integer iaculis euismod pulvinar.</p>
</div>
</div>
</div>
<div class="marketo">
<div>
<div>
<a
href="https://milo.adobe.com/tools/marketo#eyJmaWVsZCB2aXNpYmlsaXR5LnBob25lIjoiaGlkZGVuIiwiZmllbGQgZmlsdGVycy5qb2Jfcm9sZSI6ImFsbCIsImZpZWxkIGZpbHRlcnMuZnVuY3Rpb25hbF9hcmVhIjoiYWxsIiwiZmllbGQgZmlsdGVycy5pbmR1c3RyeSI6ImFsbCIsImZpZWxkIGZpbHRlcnMucHJvZHVjdHMiOiJhbGwiLCJmaWVsZCB2aXNpYmlsaXR5LmRlbW8iOiJ2aXNpYmxlIiwicHJvZ3JhbS5jb3BhcnRuZXJuYW1lcyI6IiIsInByb2dyYW0uY2FtcGFpZ25pZHMuZXh0ZXJuYWwiOiIiLCJwcm9ncmFtLmNhbXBhaWduaWRzLnJldG91Y2giOiIiLCJwcm9ncmFtLmNhbXBhaWduaWRzLm9uc2l0ZSI6IiIsInByb2dyYW0uYWRkaXRpb25hbCBmb3JtX2lkIjoiIiwiZm9ybSBpZCI6IjE3MjMiLCJtYXJrZXRvIG11bmNraW4iOiIzNjAtS0NJLTgwNCIsIm1hcmtldG8gaG9zdCI6ImVuZ2FnZS5hZG9iZS5jb20iLCJmb3JtIHR5cGUiOiJtYXJrZXRvX2Zvcm0iLCJmb3JtLnN1YnR5cGUiOiJ3aGl0ZXBhcGVyX2Zvcm0iLCJwcm9ncmFtLmNhbXBhaWduaWRzLnNmZGMiOiIiLCJwcm9ncmFtLnBvaSI6IiJ9">Marketo
Configurator</a>
</div>
</div>
<div>
<div>Title</div>
<div>New Title</div>
</div>
<div>
<div>Description</div>
<div>New Description</div>
</div>
<div>
<div>Destination Type</div>
<div>section</div>
</div>
<div>
<div>Success Content</div>
<div>Thank you</div>
</div>
</div>
<div class="section-metadata">
<div>
<div>style</div>
<div>container, xxl spacing, two-up, resource-form</div>
</div>
</div>
</div>

0 comments on commit 0234a96

Please sign in to comment.