Skip to content

Commit

Permalink
Adding test case for federated modal (#2731)
Browse files Browse the repository at this point in the history
* Adding test case for federated modal

* Setting ogFetch on after each
  • Loading branch information
bandana147 authored Aug 13, 2024
1 parent d6b7e33 commit 302e9be
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions test/blocks/modals/mocks/body.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@
<a id="faas-modal-link" href="https://milo.adobe.com/test/blocks/modals/mocks/faas"
data-modal-path="/test/blocks/modals/mocks/faas"
data-modal-hash="#faas">FaaS</a>

<a id="geo-modal-link" href="https://milo.adobe.com/test/blocks/modals/mocks/federal/geo"
data-modal-path="/test/blocks/modals/mocks/federal/geo"
data-modal-hash="#geo">FaaS</a>
5 changes: 5 additions & 0 deletions test/blocks/modals/mocks/federal/geo.plain.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
<h1>Federated modal</h1>
<button id="milo-button-1">Geo Button 1</button>
<button id="milo-button-2">Geo Button 2</button>
</div>
15 changes: 15 additions & 0 deletions test/blocks/modals/modals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { readFile, sendKeys } from '@web/test-runner-commands';
import { expect } from '@esm-bundle/chai';
import sinon from 'sinon';
import { delay, waitForElement, waitForRemoval } from '../../helpers/waitfor.js';
import { mockFetch } from '../../helpers/generalHelpers.js';

document.body.innerHTML = await readFile({ path: './mocks/body.html' });

const {
default: init,
getModal,
Expand All @@ -14,6 +16,8 @@ const {
} = await import('../../../libs/blocks/modal/modal.js');
const satellite = { track: sinon.spy() };

const ogFetch = window.fetch;

describe('Modals', () => {
beforeEach(() => {
window._satellite = satellite;
Expand All @@ -22,6 +26,7 @@ describe('Modals', () => {

afterEach(() => {
sinon.restore();
window.fetch = ogFetch;
});

it('Doesnt load modals on page load with no hash', async () => {
Expand Down Expand Up @@ -265,4 +270,14 @@ describe('sendAnalytics', () => {
sendAnalytics({});
expect(window._satellite.track.called).to.be.true;
});

it('Loads a federated modal on load with hash and closes when removed from hash', async () => {
window.fetch = mockFetch({ payload: { data: '' } });
window.location.hash = '#geo';
await waitForElement('#geo');
expect(document.getElementById('geo')).to.exist;
window.location.hash = '';
await waitForRemoval('#geo');
expect(document.getElementById('geo')).to.be.null;
});
});

0 comments on commit 302e9be

Please sign in to comment.