diff --git a/CHANGELOG.md b/CHANGELOG.md index c30cb3ef6b..fb75d4442a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ useful summary for people upgrading their application, not a replication of the commit log. +## Unreleased + +* Add js tests for accordion component ([PR #1977](https://github.com/alphagov/govuk_publishing_components/pull/1977)) + ## 24.6.0 * Remove title attribute from summary list ([PR #1973](https://github.com/alphagov/govuk_publishing_components/pull/1973)) diff --git a/spec/javascripts/components/accordion-spec.js b/spec/javascripts/components/accordion-spec.js new file mode 100644 index 0000000000..187d2d9222 --- /dev/null +++ b/spec/javascripts/components/accordion-spec.js @@ -0,0 +1,108 @@ +/* eslint-env jasmine, jquery */ +/* global GOVUK */ + +describe('An accordion component', function () { + 'use strict' + + var element + + beforeEach(function () { + var container = document.createElement('div') + container.innerHTML = + '
' + + '
' + + '
' + + '

' + + 'Accordion 1' + + '

' + + '
' + + '
' + + '

This is content for accordion 1 of 2

' + + '

This content contains a link

' + + '
' + + '
' + + '
' + + '
' + + '

' + + 'Accordion 2' + + '

' + + '
' + + '
' + + '

This is content for accordion 2 of 2

' + + '
' + + '
' + + '
' + + document.body.appendChild(container) + element = document.getElementById('default-id') + new GOVUK.Modules.GemAccordion().start($(element)) + }) + + describe('on page load', function () { + it('marks the accordion as active', function () { + expect(element).toHaveClass('gem-c-accordion--active') + }) + + it('loads the accordion show/all/hide all controls', function () { + var controls = element.querySelector('.gem-c-accordion__controls') + + expect(controls).toBeTruthy() + expect(controls.innerText).toEqual('Show all sections') + }) + + it('marks up the accordion titles as buttons with show/hide controls', function () { + var header = element.querySelector('.gem-c-accordion__section-header') + var headerControl = header.querySelector('.gem-c-accordion__toggle-text') + + expect(headerControl).toBeTruthy() + expect(headerControl.innerText).toEqual('Show') + }) + }) + + describe('when opening and closing accordions', function () { + it('opens and closes an accordion', function () { + var section = element.querySelector('.gem-c-accordion__section') + var header = section.querySelector('.gem-c-accordion__section-header') + var headerControl = header.querySelector('.gem-c-accordion__toggle-text') + + header.click() + expect(section).toHaveClass('gem-c-accordion__section--expanded') + expect(headerControl.innerText).toEqual('Hide') + + header.click() + expect(section).not.toHaveClass('gem-c-accordion__section--expanded') + expect(headerControl.innerText).toEqual('Show') + }) + + it('opens and closes all accordions when the show/hide all controls are used', function () { + var topLevelControl = element.querySelector('.gem-c-accordion__open-all') + + topLevelControl.click() + expect(topLevelControl.innerText).toEqual('Hide all sections') + + element.querySelectorAll('.gem-c-accordion__section').forEach(function (section, i) { + expect(section).toHaveClass('gem-c-accordion__section--expanded') + expect(section.querySelector('.gem-c-accordion__toggle-text').innerText).toEqual('Hide') + }) + + topLevelControl.click() + expect(topLevelControl.innerText).toEqual('Show all sections') + + element.querySelectorAll('.gem-c-accordion__section').forEach(function (section, i) { + expect(section).not.toHaveClass('gem-c-accordion__section--expanded') + expect(section.querySelector('.gem-c-accordion__toggle-text').innerText).toEqual('Show') + }) + }) + }) + + describe('when using the anchor navigation feature flag', function () { + // For this test, we base the function on the markup snapshot above. + // The anchor link is in the first accordion whilst the anchor is in the second accordion + it('opens an accordion based on anchors within that accordion when the associated anchor link is clicked', function () { + var link = element.querySelector('.gem-c-accordion__section').querySelector('.govuk-link') + + link.click() + expect(element.querySelectorAll('.gem-c-accordion__section')[1]).toHaveClass('gem-c-accordion__section--expanded') + }) + }) +})