From 4f129b84b4e676465ec516922070a8a49a99a166 Mon Sep 17 00:00:00 2001 From: Theodor Vararu Date: Thu, 11 Oct 2018 14:09:03 +0100 Subject: [PATCH] Initialise accordion correctly when all sections are expanded The button would still read 'Open all' --- .../__snapshots__/accordion.test.js.snap | 15 +++++++++++++++ src/Assets/Javascript/accordion.js | 1 + src/Assets/Javascript/accordion.test.js | 14 ++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/Assets/Javascript/__snapshots__/accordion.test.js.snap b/src/Assets/Javascript/__snapshots__/accordion.test.js.snap index 720732f4..26b01ad2 100644 --- a/src/Assets/Javascript/__snapshots__/accordion.test.js.snap +++ b/src/Assets/Javascript/__snapshots__/accordion.test.js.snap @@ -15,6 +15,21 @@ exports[`Accordion module init() when all sections are closed initialises accord " `; +exports[`Accordion module init() when all sections are open initialises accordion HTML correctly 1`] = ` +" +
+
+
Header 1
+
Body 1
+
+
+
Header 2
+
Body 2
+
+
+" +`; + exports[`Accordion module init() when in an incompatible environment leaves HTML untouched 1`] = ` "
diff --git a/src/Assets/Javascript/accordion.js b/src/Assets/Javascript/accordion.js index f057d098..ade0f030 100644 --- a/src/Assets/Javascript/accordion.js +++ b/src/Assets/Javascript/accordion.js @@ -23,6 +23,7 @@ Accordion.prototype.init = function () { accordion_controls.appendChild(open_or_close_all_button) this.$module.insertBefore(accordion_controls, this.$module.firstChild) this.$module.classList.add('with-js') + this.updateOpenAll() } } diff --git a/src/Assets/Javascript/accordion.test.js b/src/Assets/Javascript/accordion.test.js index 6c012a4b..9605c499 100644 --- a/src/Assets/Javascript/accordion.test.js +++ b/src/Assets/Javascript/accordion.test.js @@ -61,6 +61,20 @@ describe("Accordion module", () => { expect(document.body.innerHTML).toMatchSnapshot() }) }) + + describe("when all sections are open", () => { + beforeEach(() => { + document + .querySelectorAll(".accordion-section") + .forEach($section => $section.classList.add("accordion-section--expanded")) + accordion.init() + }) + + it("initialises accordion HTML correctly", () => { + expect(document.querySelector(".accordion-expand-all").innerHTML).toBe("Close all") + expect(document.body.innerHTML).toMatchSnapshot() + }) + }) }) describe("events", () => {