Skip to content

Commit

Permalink
Merge pull request #314 from TsayAdobe/MWPW-134816
Browse files Browse the repository at this point in the history
MWPW-134816 Improve Unit Test Coverage
  • Loading branch information
TsayAdobe authored Aug 10, 2023
2 parents 51628cc + 59af34c commit 8e4bc6b
Show file tree
Hide file tree
Showing 9 changed files with 376 additions and 4 deletions.
135 changes: 135 additions & 0 deletions test/blocks/dc-converter-widget-ootb/dc-converter-widget-ootb.jest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/**
* @jest-environment jsdom
*/
import path from 'path';
import fs from 'fs';
import init from '../../../acrobat/blocks/dc-converter-widget-ootb/dc-converter-widget-ootb';

describe('dc-converter-widget', () => {
beforeEach(async () => {
document.body.innerHTML = fs.readFileSync(
path.resolve(__dirname, './mocks/body.html'),
'utf8'
);
window.performance.mark = jest.fn();
});

afterEach(() => {
jest.clearAllMocks();
});

it('loads widget from prod env', async () => {
let fetchUrl = '';
window.fetch = jest.fn((url) => {
fetchUrl = url;
return Promise.resolve({
status: 200,
text: () =>
Promise.resolve(
fs.readFileSync(path.resolve(__dirname, './mocks/widget.html'))
),
});
});
delete window.location;
window.location = new URL('https://www.adobe.com');
const block = document.querySelector('.dc-converter-widget-ootb');
const widget = await init(block);
document.dispatchEvent(new Event('milo:deferred'));
const launcher = document.querySelector('#adobe_dc_sdk_launcher');
expect(launcher.getAttribute('src')).toMatch(/^https:\/\/acrobat.adobe.com\//);
});

it.each`
hostname
${'stage--dc--adobecom.hlx.page'}
${'main--dc--adobecom.hlx.page'}
${'stage--dc--adobecom.hlx.live'}
${'main--dc--adobecom.hlx.live'}
${'www.stage.adobe.com'}
`('loads widget from stage env', async ({ hostname }) => {
let fetchUrl = '';
window.fetch = jest.fn((url) => {
fetchUrl = url;
return Promise.resolve({
status: 200,
text: () =>
Promise.resolve(
fs.readFileSync(path.resolve(__dirname, './mocks/widget.html'))
),
});
});
delete window.location;
window.location = new URL(`https://${hostname}`);
const block = document.querySelector('.dc-converter-widget-ootb');
const widget = await init(block);
document.dispatchEvent(new Event('milo:deferred'));
const launcher = document.querySelector('#adobe_dc_sdk_launcher');
expect(launcher.getAttribute('src')).toMatch(/^https:\/\/stage.acrobat.adobe.com\//);
});

it('loads widget failed from prod env', async () => {
window.fetch = jest.fn(() =>
Promise.resolve({
status: 404,
})
);
delete window.location;
window.location = new URL('https://www.adobe.com');
const block = document.querySelector('.dc-converter-widget-ootb');
const widget = await init(block);
expect(document.querySelector('#CID').children).toHaveLength(3);
});

it.each`
hostname
${'stage--dc--adobecom.hlx.page'}
${'main--dc--adobecom.hlx.page'}
${'stage--dc--adobecom.hlx.live'}
${'www.stage.adobe.com'}
`('redirects when signed in on stage', async ({ hostname }) => {
window.fetch = jest.fn((url) =>
Promise.resolve({
status: 200,
text: () =>
Promise.resolve(
fs.readFileSync(path.resolve(__dirname, './mocks/widget.html'))
),
})
);
window.adobeIMS = {
isSignedInUser: jest.fn(() => true)
};
delete window.location;
window.location = new URL(`https://${hostname}`);
const block = document.querySelector('.dc-converter-widget-ootb');
const widget = await init(block);
window.dispatchEvent(new CustomEvent('IMS:Ready'));
expect(window.location).toMatch(/^https:\/\/www.adobe.com\/go\/acrobat-/);
});

it.each`
hostname
${'main--dc--adobecom.hlx.live'}
${'www.adobe.com'}
`('redirects when signed in', async ({ hostname }) => {
window.fetch = jest.fn((url) =>
Promise.resolve({
status: 200,
text: () =>
Promise.resolve(
fs.readFileSync(path.resolve(__dirname, './mocks/widget.html'))
),
})
);
window.adobeIMS = {
isSignedInUser: jest.fn(() => true)
};
delete window.location;
window.location = new URL(`https://${hostname}`);
const block = document.querySelector('.dc-converter-widget-ootb');
const widget = await init(block);
window.dispatchEvent(new CustomEvent('IMS:Ready'));
// Issue with jest and window.location
//expect(window.location).toMatch(/https:\/\/www.adobe.com\/go\/testredirect/);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { readFile } from '@web/test-runner-commands';
import { expect } from '@esm-bundle/chai';
import sinon from 'sinon';
import { waitForElement, delay } from '../../helpers/waitfor.js';

document.head.innerHTML = await readFile({ path: './mocks/head.html' });
document.body.innerHTML = await readFile({ path: './mocks/body_cache.html' });
const { default: init } = await import(
'../../../acrobat/blocks/dc-converter-widget-ootb/dc-converter-widget-ootb'
);

describe('dc-converter-widget block', () => {
before(() => {
const block = document.body.querySelector('.dc-converter-widget-ootb');
init(block);
});

afterEach(() => {
sinon.restore();
});

it('handles DC_Hosted:Ready event', async() => {
window.dc_hosted = {
getUserLimits: async () => ({
upload: {
can_upload: true
}
}),
};
window.dispatchEvent(new CustomEvent('DC_Hosted:Ready'));
await delay(100);
expect(window.doccloudPersonalization).to.be.exist;
});

it('handles IMS:Ready event', async () => {
window.adobeIMS = {
isSignedInUser: () => false,
};
window._satellite = {
track: sinon.stub(),
};
window.bowser = {
getParser: () => ({
getBrowserName: () => 'Chrome',
getBrowserVersion: () => '110',
}),
};
const widget = await readFile({ path: './mocks/widget.html' });
sinon.stub(window, 'fetch');
var res = new window.Response(widget, {
status: 200
});
window.fetch.returns(Promise.resolve(res));
window.dispatchEvent(new CustomEvent('IMS:Ready'));
await delay(1000);
expect(document.querySelector('#CID')).to.be.exist;
});

it('handles Bowser:Ready event', () => {
window.dispatchEvent(new CustomEvent('Bowser:Ready'));
});
});
10 changes: 10 additions & 0 deletions test/blocks/dc-converter-widget-ootb/mocks/body.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<main>
<div class="dc-converter-widget-ootb">
<div>
<div>pdf-to-ppt</div>
</div>
<div>
<div>https://www.adobe.com/go/testredirect</div>
</div>
</div>
</main>
16 changes: 16 additions & 0 deletions test/blocks/dc-converter-widget-ootb/mocks/body_cache.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<main>
<div class="dc-converter-widget-ootb">
<div>
<div>pdf-to-ppt</div>
</div>
<div>
<div>https://www.adobe.com/go/testredirect</div>
</div>
<div>
<div></div>
</div>
<div>
<div>http://localhost:2000/test/blocks/dc-converter-widget-ootb/mocks/widget.html</div>
</div>
</div>
</main>
3 changes: 3 additions & 0 deletions test/blocks/dc-converter-widget-ootb/mocks/head.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<link rel="icon" href="data:,">
<link rel="stylesheet" href="/acrobat/styles/styles.css" />
<link rel="stylesheet" href="/acrobat/blocks/dc-converter-widget-ootb/dc-converter-widget-ootb.css" />
16 changes: 16 additions & 0 deletions test/blocks/dc-converter-widget-ootb/mocks/widget.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>

<head>
<style>
p {
color: red;
}
</style>
</head>

<body>
<div class="mockWidget"></div>
</body>

</html>
36 changes: 35 additions & 1 deletion test/blocks/eventwrapper/eventwrapper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { default: init } = await import(

describe('eventwrapper block', () => {
let browserName = 'Chrome';
let chromeRuntimeSendMessage = false;

before(() => {
window.bowser = {
Expand Down Expand Up @@ -50,7 +51,7 @@ describe('eventwrapper block', () => {
window.chrome = {
runtime: {
sendMessage: function (message, version, callback) {
callback(false);
callback((() => chromeRuntimeSendMessage)());
},
},
};
Expand Down Expand Up @@ -90,6 +91,39 @@ describe('eventwrapper block', () => {
expect(window.modalDisplayed).to.be.true;
});

it('handles modalExist', async function () {
document.head.innerHTML = head;
document.body.innerHTML = body;
window.dc_hosted.listeners = [];
localStorage.removeItem('fricBrowExt');
window.modalDisplayed = false;
chromeRuntimeSendMessage = true;
const blocks = document.body.querySelectorAll('.eventwrapper');
blocks.forEach((x) => init(x));
window.dispatchEvent(new CustomEvent('DC_Hosted:Ready'));
window.dc_hosted.dispatchEvent('conversion-complete', {});

window.modalDisplayed = false;
window.dc_hosted.dispatchEvent('preview-displayed', {});

// modalExist
const event = window._satellite.track.args[1][1].data._adobe_corpnew.digitalData.primaryEvent.eventInfo.eventName;
expect(event).to.eql('Get the extension-1|viewer-extension-exists|Chrome-extension');
});

it('handles modalAlready', async function () {
document.head.innerHTML = head;
document.body.innerHTML = body;
localStorage.fricBrowExt = 'true';
const blocks = document.body.querySelectorAll('.eventwrapper');
blocks.forEach((x) => init(x));
window.dispatchEvent(new CustomEvent('DC_Hosted:Ready'));

// modalAlready
const event = window._satellite.track.args[1][1].data._adobe_corpnew.digitalData.primaryEvent.eventInfo.eventName;
expect(event).to.eql('Get the extension-1|already-closed-viewer-extension|Chrome-extension');
});

it('shows the ext modal on MS Edge when conversion complete and preview displayed', async function () {
browserName = 'Microsoft Edge';
document.head.innerHTML = head;
Expand Down
77 changes: 77 additions & 0 deletions test/blocks/pricing-card/mocks/body_noinitialoption.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<main>
<div>
<div class="pricing-card">
<div>
<div>title</div>
<div>
<h1 id="acrobat-pro2-2">Acrobat Pro2</h1>
<p>For students &#x26; teachers</p>
</div>
</div>
<div>
<div>option1</div>
<div>Annual, paid monthly2</div>
</div>
<div>
<div>option2</div>
<div>Annual, prepaid2</div>
</div>
<div>
<div>option3</div>
<div>Monthly2</div>
</div>
<div>
<div>promotionText</div>
<div>Best value2</div>
</div>
<div>
<div>price1</div>
<div>US$3339.99/m</div>
</div>
<div>
<div>price2</div>
<div>US$439.99/m</div>
</div>
<div>
<div>price3</div>
<div>US$539.99/m</div>
</div>
<div>
<div>cta1</div>
<div>link133</div>
</div>
<div>
<div>cta2</div>
<div>link233</div>
</div>
<div>
<div>cta3</div>
<div>link333</div>
</div>
<div>
<div>disclaimer1</div>
<div>Equal to US$5339.99/mo. Cancel within 14 days for a <a
href="https://helpx.adobe.com/manage-account/using/cancel-subscription.html">full refund</a>.
Windows and Mac.</div>
</div>
<div>
<div>disclaimer2</div>
<div>Equal to US$6339.99/mo. Cancel within 14 days for a <a
href="https://helpx.adobe.com/manage-account/using/cancel-subscription.html">full refund</a>.
Windows and Mac.</div>
</div>
<div>
<div>disclaimer3</div>
<div>Equal to US$7339.99/mo. Cancel within 14 days for a <a
href="https://helpx.adobe.com/manage-account/using/cancel-subscription.html">full refund</a>.
Windows and Mac.</div>
</div>
</div>
<div class="section-metadata">
<div>
<div data-valign="middle">style</div>
<div data-valign="middle">pricing-card-columns, w-400</div>
</div>
</div>
</div>
</main>
Loading

0 comments on commit 8e4bc6b

Please sign in to comment.