From 86941a20126e05a0b22c14cc909fde6ed3447cd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corentin=20S=C3=A9chet?= Date: Sat, 4 Mar 2023 18:00:31 +0100 Subject: [PATCH] js: integrate jest and jsdom (#75131) --- tests/js/foldable-sections.test.js | 19 ++++++++++++++++++ tests/js/test-utils.js | 31 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 tests/js/foldable-sections.test.js create mode 100644 tests/js/test-utils.js diff --git a/tests/js/foldable-sections.test.js b/tests/js/foldable-sections.test.js new file mode 100644 index 0000000..b325735 --- /dev/null +++ b/tests/js/foldable-sections.test.js @@ -0,0 +1,19 @@ +const assert = require('assert') +const { loadDom } = require('./test-utils') + +test('test foldable sections', async () => { + const { window } = await loadDom(` +
+

Section Title

+
+ `) + + const foldableSection = window.document.querySelector('.section.foldable') + const title = window.document.querySelector('.section.foldable > h2') + + title.click() + assert(foldableSection.classList.contains('folded')) + + title.click() + assert(!foldableSection.classList.contains('folded')) +}) diff --git a/tests/js/test-utils.js b/tests/js/test-utils.js new file mode 100644 index 0000000..93c8678 --- /dev/null +++ b/tests/js/test-utils.js @@ -0,0 +1,31 @@ +const path = require('path') +const { JSDOM } = require('jsdom') + +async function loadDom (content) { + const dom = new JSDOM(` + + + + ${content} + + + + + `, { + runScripts: 'dangerously', + resources: 'usable' + }) + + await new Promise((resolve, reject) => { + dom.window.document.addEventListener('test:ready', () => { + resolve() + }) + }) + + return dom +}; + +module.exports = { loadDom } +