From 825c03bcadd79522c87a6e908a563e3402a52f4c Mon Sep 17 00:00:00 2001 From: fkakatie Date: Thu, 14 Dec 2023 11:08:54 -0700 Subject: [PATCH] refactor: tests should remain in aem.js --- test/blocks/footer/footer.plain.html | 3 - test/blocks/footer/footer.test.js | 31 ------ test/blocks/fragment/block.html | 25 ----- test/blocks/fragment/fragment.test.js | 27 ------ test/blocks/fragment/test.plain.html | 14 --- test/blocks/header/header.test.js | 45 --------- test/blocks/header/nav.plain.html | 46 --------- test/blocks/hero/hero.test.js | 14 --- test/scripts/block-utils.test.js | 130 -------------------------- test/scripts/body.html | 59 ------------ test/scripts/config.html | 25 ----- test/scripts/dummy.html | 4 - test/scripts/head.html | 12 --- test/scripts/media_mock.png | Bin 1346 -> 0 bytes test/scripts/scripts.test.js | 43 --------- test/scripts/test.css | 3 - 16 files changed, 481 deletions(-) delete mode 100644 test/blocks/footer/footer.plain.html delete mode 100644 test/blocks/footer/footer.test.js delete mode 100644 test/blocks/fragment/block.html delete mode 100644 test/blocks/fragment/fragment.test.js delete mode 100644 test/blocks/fragment/test.plain.html delete mode 100644 test/blocks/header/header.test.js delete mode 100644 test/blocks/header/nav.plain.html delete mode 100644 test/blocks/hero/hero.test.js delete mode 100644 test/scripts/block-utils.test.js delete mode 100644 test/scripts/body.html delete mode 100644 test/scripts/config.html delete mode 100644 test/scripts/dummy.html delete mode 100644 test/scripts/head.html delete mode 100644 test/scripts/media_mock.png delete mode 100644 test/scripts/scripts.test.js delete mode 100644 test/scripts/test.css diff --git a/test/blocks/footer/footer.plain.html b/test/blocks/footer/footer.plain.html deleted file mode 100644 index 5e3637b9..00000000 --- a/test/blocks/footer/footer.plain.html +++ /dev/null @@ -1,3 +0,0 @@ -

Copyright © 2022 Adobe. All rights reserved.

-

Privacy / Terms of Use / Cookie preferences / Do not sell my personal information / AdChoices

-
diff --git a/test/blocks/footer/footer.test.js b/test/blocks/footer/footer.test.js deleted file mode 100644 index 074c6f4e..00000000 --- a/test/blocks/footer/footer.test.js +++ /dev/null @@ -1,31 +0,0 @@ -/* eslint-disable no-unused-expressions */ -/* global describe it */ - -import { readFile } from '@web/test-runner-commands'; -import { expect } from '@esm-bundle/chai'; - -document.body.innerHTML = await readFile({ path: '../../scripts/dummy.html' }); - -const { buildBlock, decorateBlock, loadBlock } = await import('../../../scripts/aem.js'); - -document.body.innerHTML = await readFile({ path: '../../scripts/body.html' }); - -const sleep = async (time = 1000) => new Promise((resolve) => { - setTimeout(() => { - resolve(true); - }, time); -}); - -const footerBlock = buildBlock('footer', [['Footer', '/test/blocks/footer/footer']]); -document.querySelector('footer').append(footerBlock); -decorateBlock(footerBlock); -await loadBlock(footerBlock); -await sleep(); - -describe('Footer block', () => { - it('Displays footer content', async () => { - const a = document.querySelector('footer a'); - expect(a).to.exist; - expect(a.href).to.equal('https://www.adobe.com/privacy.html'); - }); -}); diff --git a/test/blocks/fragment/block.html b/test/blocks/fragment/block.html deleted file mode 100644 index 83e41b82..00000000 --- a/test/blocks/fragment/block.html +++ /dev/null @@ -1,25 +0,0 @@ -
-
-
- -
-
-
-
-
/test/blocks/fragment/test
-
-
-
-
-
-
-
/does/not/exist
-
-
-
-
- \ No newline at end of file diff --git a/test/blocks/fragment/fragment.test.js b/test/blocks/fragment/fragment.test.js deleted file mode 100644 index afdb28d3..00000000 --- a/test/blocks/fragment/fragment.test.js +++ /dev/null @@ -1,27 +0,0 @@ -/* eslint-disable no-unused-expressions */ -/* global describe it */ - -import { readFile } from '@web/test-runner-commands'; -import { expect } from '@esm-bundle/chai'; - -const sectionLoaded = async (section) => new Promise((resolve) => { - // wait for section to finish loading - const check = setInterval(() => { - if (section.dataset.sectionStatus === 'loaded') { - clearInterval(check); - resolve(); - } - }, 100); -}); - -describe('Fragment block', () => { - it('Replaces fragment block with fragment content', async () => { - document.body.innerHTML = await readFile({ path: './block.html' }); - await import('../../../scripts/scripts.js'); - const section = document.querySelector('.section'); - await sectionLoaded(section); - expect(section.textContent.trim()).to.equal('Hello world!'); - expect(section.classList.contains('example-container')).to.be.true; - expect(document.querySelectorAll('.fragment').length).to.equal(1); - }); -}); diff --git a/test/blocks/fragment/test.plain.html b/test/blocks/fragment/test.plain.html deleted file mode 100644 index 383b2781..00000000 --- a/test/blocks/fragment/test.plain.html +++ /dev/null @@ -1,14 +0,0 @@ -
-
-
-
-
-
-
-
-
-

Hello world!

-
-
-
-
diff --git a/test/blocks/header/header.test.js b/test/blocks/header/header.test.js deleted file mode 100644 index 63080236..00000000 --- a/test/blocks/header/header.test.js +++ /dev/null @@ -1,45 +0,0 @@ -/* eslint-disable no-unused-expressions */ -/* global describe it */ - -import { readFile } from '@web/test-runner-commands'; -import { expect } from '@esm-bundle/chai'; - -document.body.innerHTML = await readFile({ path: '../../scripts/dummy.html' }); - -const { buildBlock, decorateBlock, loadBlock } = await import('../../../scripts/aem.js'); - -document.body.innerHTML = await readFile({ path: '../../scripts/body.html' }); - -const sleep = async (time = 1000) => new Promise((resolve) => { - setTimeout(() => { - resolve(true); - }, time); -}); - -const headerBlock = buildBlock('header', [['Nav', '/test/blocks/header/nav']]); -document.querySelector('header').append(headerBlock); -decorateBlock(headerBlock); -await loadBlock(headerBlock); -await sleep(); - -describe('Header block', () => { - it('Hamburger shows and hides nav', async () => { - const hamburger = document.querySelector('.header .nav-hamburger'); - const nav = document.querySelector('.header nav'); - expect(hamburger).to.exist; - expect(nav).to.exist; - hamburger.click(); - expect(nav.getAttribute('aria-expanded')).to.equal('true'); - hamburger.click(); - expect(nav.getAttribute('aria-expanded')).to.equal('false'); - }); - - it('Section title shows and hides section', async () => { - const sections = document.querySelector('.header .nav-sections'); - const title = sections.querySelector(':scope li'); - title.click(); - expect(title.getAttribute('aria-expanded')).to.equal('true'); - title.click(); - expect(title.getAttribute('aria-expanded')).to.equal('false'); - }); -}); diff --git a/test/blocks/header/nav.plain.html b/test/blocks/header/nav.plain.html deleted file mode 100644 index 340ef061..00000000 --- a/test/blocks/header/nav.plain.html +++ /dev/null @@ -1,46 +0,0 @@ -
-

- Boilerplate -

-
-
- -
-
-

- - search icon - -

-
\ No newline at end of file diff --git a/test/blocks/hero/hero.test.js b/test/blocks/hero/hero.test.js deleted file mode 100644 index 7e59287c..00000000 --- a/test/blocks/hero/hero.test.js +++ /dev/null @@ -1,14 +0,0 @@ -/* eslint-disable no-unused-expressions */ -/* global describe it */ - -import { readFile } from '@web/test-runner-commands'; -import { expect } from '@esm-bundle/chai'; - -document.body.innerHTML = await readFile({ path: '../../scripts/body.html' }); - -describe('Hero block', () => { - it('Builds hero block from picture and h1', async () => { - await import('../../../scripts/scripts.js'); - expect(document.querySelector('.hero')).to.exist; - }); -}); diff --git a/test/scripts/block-utils.test.js b/test/scripts/block-utils.test.js deleted file mode 100644 index cb56e0ef..00000000 --- a/test/scripts/block-utils.test.js +++ /dev/null @@ -1,130 +0,0 @@ -/* eslint-disable no-unused-expressions */ -/* global describe before it */ - -import { readFile } from '@web/test-runner-commands'; -import { expect } from '@esm-bundle/chai'; -import sinon from 'sinon'; - -let blockUtils; - -document.body.innerHTML = await readFile({ path: './dummy.html' }); -document.head.innerHTML = await readFile({ path: './head.html' }); - -describe('Utils methods', () => { - before(async () => { - blockUtils = await import('../../scripts/aem.js'); - document.body.innerHTML = await readFile({ path: './body.html' }); - }); - - it('Sanitizes class name', async () => { - expect(blockUtils.toClassName('Hello world')).to.equal('hello-world'); - expect(blockUtils.toClassName(null)).to.equal(''); - }); - - it('Extracts metadata', async () => { - expect(blockUtils.getMetadata('description')).to.equal('Lorem ipsum dolor sit amet.'); - expect(blockUtils.getMetadata('og:title')).to.equal('Foo'); - }); - - it('Loads CSS', async () => { - // loads a css file and calls callback - const load = await new Promise((resolve) => { - blockUtils.loadCSS('/test/scripts/test.css', (e) => resolve(e)); - }); - expect(load).to.equal('load'); - expect(getComputedStyle(document.body).color).to.equal('rgb(255, 0, 0)'); - - // does nothing if css already loaded - const noop = await new Promise((resolve) => { - blockUtils.loadCSS('/test/scripts/test.css', (e) => resolve(e)); - }); - expect(noop).to.equal('noop'); - - // calls callback in case of error - const error = await new Promise((resolve) => { - blockUtils.loadCSS('/test/scripts/nope.css', (e) => resolve(e)); - }); - expect(error).to.equal('error'); - }); - - it('Collects RUM data', async () => { - const sendBeacon = sinon.stub(navigator, 'sendBeacon'); - // turn on RUM - window.history.pushState({}, '', `${window.location.href}&rum=on`); - delete window.hlx; - - // sends checkpoint beacon - await blockUtils.sampleRUM('test', { foo: 'bar' }); - expect(sendBeacon.called).to.be.true; - sendBeacon.resetHistory(); - - // sends cwv beacon - await blockUtils.sampleRUM('cwv', { foo: 'bar' }); - expect(sendBeacon.called).to.be.true; - - // test error handling - sendBeacon.throws(); - await blockUtils.sampleRUM('error', { foo: 'bar' }); - - sendBeacon.restore(); - }); - - it('Creates optimized picture', async () => { - const $picture = blockUtils.createOptimizedPicture('/test/scripts/mock.png'); - expect($picture.querySelector(':scope source[type="image/webp"]')).to.exist; // webp - expect($picture.querySelector(':scope source:not([type="image/webp"])')).to.exist; // fallback - expect($picture.querySelector(':scope img').src).to.include('format=png&optimize=medium'); // default - }); - - it('Normalizes headings', async () => { - const numHeadings = document.querySelectorAll('h1, h2, h3, h4, h5, h6').length; - blockUtils.normalizeHeadings(document.querySelector('main'), ['h1', 'h2', 'h3']); - expect(document.querySelectorAll('h1, h2, h3, h4, h5, h6').length).to.equal(numHeadings); - expect(document.querySelectorAll('h4, h5, h6').length).to.equal(0); - }); -}); - -describe('Sections and blocks', () => { - it('Decorates sections', async () => { - blockUtils.decorateSections(document.querySelector('main')); - expect(document.querySelectorAll('main .section').length).to.equal(2); - }); - - it('Decorates blocks', async () => { - blockUtils.decorateBlocks(document.querySelector('main')); - expect(document.querySelectorAll('main .block').length).to.equal(1); - }); - - it('Loads blocks', async () => { - await blockUtils.loadBlocks(document.querySelector('main')); - document.querySelectorAll('main .block').forEach(($block) => { - expect($block.dataset.blockStatus).to.equal('loaded'); - }); - }); - - it('Updates section status', async () => { - blockUtils.updateSectionsStatus(document.querySelector('main')); - document.querySelectorAll('main .section').forEach(($section) => { - expect($section.dataset.sectionStatus).to.equal('loaded'); - }); - - // test section with block still loading - const $section = document.querySelector('main .section'); - delete $section.dataset.sectionStatus; - $section.querySelector(':scope .block').dataset.blockStatus = 'loading'; - blockUtils.updateSectionsStatus(document.querySelector('main')); - expect($section.dataset.sectionStatus).to.equal('loading'); - }); - - it('Reads block config', async () => { - document.querySelector('main .section > div').innerHTML += await readFile({ path: './config.html' }); - const cfg = blockUtils.readBlockConfig(document.querySelector('main .config')); - expect(cfg).to.deep.include({ - 'prop-0': 'Plain text', - 'prop-1': 'Paragraph', - 'prop-2': ['First paragraph', 'Second paragraph'], - 'prop-3': 'https://www.adobe.com/', - 'prop-4': ['https://www.adobe.com/', 'https://www.hlx.live/'], - }); - }); -}); diff --git a/test/scripts/body.html b/test/scripts/body.html deleted file mode 100644 index 8146b31f..00000000 --- a/test/scripts/body.html +++ /dev/null @@ -1,59 +0,0 @@ -
-
-
-

- - - - -

-

This is a Heading 1

-

This is a Heading 2

-

This is a Heading 3

-

This is a Heading 4

-
This is a Heading 5
-
This is a Heading 6
-
    -
  • Red
  • -
  • Green
  • -
  • Blue
  • -
-
    -
  1. First
  2. -
  3. Second
  4. -
  5. Third
  6. -
-

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut - labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut - aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse - cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in - culpa qui officia deserunt mollit anim id est laborum.

-

- - - - -

-

Button

-

Button Bold

-

Button Italic

-

Button Italic Bold

-

Testing a code block

-
<meta name="viewport" content="width=device-width, initial-scale=1"/>
-<script src="/scripts.js" type="module"></script>
-<link rel="stylesheet" href="/styles.css"/>
-        
-
-

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut - labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut - aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse - cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in - culpa qui officia deserunt mollit anim id est laborum.

-
-
-
-
-
- diff --git a/test/scripts/config.html b/test/scripts/config.html deleted file mode 100644 index 8a3d5ae6..00000000 --- a/test/scripts/config.html +++ /dev/null @@ -1,25 +0,0 @@ -
-
-
Prop 0
-
Plain text
-
-
-
Prop 1
-

Paragraph

-
-
-
Prop 2
-

First paragraph

Second paragraph

-
-
-
Prop 3
-
Link
-
-
-
Prop 4
-
- First link - Second link -
-
-
diff --git a/test/scripts/dummy.html b/test/scripts/dummy.html deleted file mode 100644 index fbe89155..00000000 --- a/test/scripts/dummy.html +++ /dev/null @@ -1,4 +0,0 @@ - -
-
- diff --git a/test/scripts/head.html b/test/scripts/head.html deleted file mode 100644 index 7068482f..00000000 --- a/test/scripts/head.html +++ /dev/null @@ -1,12 +0,0 @@ -Foo - - - - - - - - - - - diff --git a/test/scripts/media_mock.png b/test/scripts/media_mock.png deleted file mode 100644 index 4faad891dd0ec0155689ff31761997b5a2b94abc..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1346 zcmV-I1-<%-P)ZMEF8w2uHrD^A&3O8Uer!fh}!z)2zBzwXh%F5<13# z(alu1OUmn!%z$~JraJu&0C@^~h`J~IAhdUu-836!26r;m?K9Oi9U?m~#Lic==7k!y z&6Dh=+4qn$xQ)Lh!B_ws{ZCXKIwDWBq@4vveo3QA3ELP$F$3xp9XY`xqiq*l^P>pe z#?P+XK9-_YvgZ3Z*I)4;#v#m19u{9;JY#NoQsaH4W z?TO$DVCa00humPsxk)QAY#J4{Li{%9&p`xlU(gG|HuD||dDR>((_#c~wW)|r4{ZkMcy)zJ@nU+W>?EN(~5aB~O z`tTTfB!Z43muM_ixd7PvtB?NDd5Rn!J0Bz^X%1;hG6+e^hbKx>UJwL95ClOG1VIo4 zK@bE>uzs-V(wG-&D*S$rJK+2~2AAFkrpl`cS@KB)*gIag(#5R|Dz7Gq_;&O~gI{_a zY3`9#za3#)BT_vB=7n01;QX6@c{Pcgq-PgkH_dt}yL-$QM3b31I+aZp)a=#Utscx;MIdRP&5u3e>a!+8p2kfTV9)3IYTtS2D44f&p zp!&q}F_CPt2(X)G>zZq8$*}4$H2A2=aqv(PZEHa;0FLA+qv3Wp&2k(F=jT32wmD`% z-Jp(&{opp{5-M%m90GhZXltXsjPu+xZQC3IT>80UM&Q|+p-KIlFg19jOK3O*xaJZi zaySI|YtYul7I)5b&$MlG2rx9LAbBJ1OAwr)kNDq&F{BVBI5o~xw>IhwI7QaB%@zy7 zP(;`lE4OOIRy=54sQqJj995_OebsHm_7s$#r20h+9cNP8+%a)Jq#F>;A0gkY~Ed>Dn1BoiN7#^%+1poj507*qoM6N<$ Eg2UEYg#Z8m diff --git a/test/scripts/scripts.test.js b/test/scripts/scripts.test.js deleted file mode 100644 index 7988036b..00000000 --- a/test/scripts/scripts.test.js +++ /dev/null @@ -1,43 +0,0 @@ -/* eslint-disable no-unused-expressions */ -/* global describe before it */ - -import { readFile } from '@web/test-runner-commands'; -import { expect } from '@esm-bundle/chai'; - -let scripts; - -document.body.innerHTML = await readFile({ path: './dummy.html' }); -document.head.innerHTML = await readFile({ path: './head.html' }); - -describe('Core Helix features', () => { - before(async () => { - scripts = await import('../../scripts/scripts.js'); - document.body.innerHTML = await readFile({ path: './body.html' }); - }); - - it('Initializes window.hlx', async () => { - // simulate code base path and turn on lighthouse - /* needs fixing - document.head.appendChild(document.createElement('script')).src = '/foo/scripts/scripts.js'; - window.history.pushState({}, '', `${window.location.href}&lighthouse=on`); - - expect(window.hlx.codeBasePath).to.equal('/foo'); - expect(window.hlx.lighthouse).to.equal(true); - - // test error handling - const url = sinon.stub(window, 'URL'); - - // cleanup - url.restore(); - window.hlx.codeBasePath = ''; - window.hlx.lighthouse = false; - Array.from(document.querySelectorAll('script')).pop().remove(); - */ - }); - - it('Adds favicon', async () => { - scripts.addFavIcon('/foo.svg'); - const $favIcon = document.querySelector('link[rel="icon"]'); - expect($favIcon.getAttribute('href')).to.equal('/foo.svg'); - }); -}); diff --git a/test/scripts/test.css b/test/scripts/test.css deleted file mode 100644 index b05faf8e..00000000 --- a/test/scripts/test.css +++ /dev/null @@ -1,3 +0,0 @@ -body { - color: red; -}