Skip to content
This repository has been archived by the owner on Mar 16, 2020. It is now read-only.

Commit

Permalink
Initialise accordion correctly when all sections are expanded
Browse files Browse the repository at this point in the history
The button would still read 'Open all'
  • Loading branch information
tvararu committed Oct 11, 2018
1 parent df4f5d5 commit 4f129b8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Assets/Javascript/__snapshots__/accordion.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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`] = `
"
<div data-module=\\"accordion\\" class=\\"with-js\\"><div class=\\"accordion-controls\\"><button class=\\"accordion-expand-all\\" aria-expanded=\\"true\\" type=\\"button\\">Close all</button></div>
<div class=\\"accordion-section\\" aria-expanded=\\"true\\">
<div class=\\"accordion-section-header\\" tabindex=\\"0\\" role=\\"button\\">Header 1<span class=\\"icon\\"></span></div>
<div class=\\"accordion-section-body\\">Body 1</div>
</div>
<div class=\\"accordion-section\\" aria-expanded=\\"true\\">
<div class=\\"accordion-section-header\\" tabindex=\\"0\\" role=\\"button\\">Header 2<span class=\\"icon\\"></span></div>
<div class=\\"accordion-section-body\\">Body 2</div>
</div>
</div>
"
`;
exports[`Accordion module init() when in an incompatible environment leaves HTML untouched 1`] = `
"
<div data-module=\\"accordion\\">
Expand Down
1 change: 1 addition & 0 deletions src/Assets/Javascript/accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/Assets/Javascript/accordion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit 4f129b8

Please sign in to comment.