diff --git a/spec/javascripts/modules/mobile-filters-modal.spec.js b/spec/javascripts/modules/mobile-filters-modal.spec.js index 5ef3788310..77cc06fff0 100644 --- a/spec/javascripts/modules/mobile-filters-modal.spec.js +++ b/spec/javascripts/modules/mobile-filters-modal.spec.js @@ -1,4 +1,4 @@ -describe('Mobile filters modal', function () { +describe('Mobile filters', function () { 'use strict' var container @@ -44,107 +44,127 @@ describe('Mobile filters modal', function () { container.addEventListener('submit', function (e) { e.preventDefault() }) - - var element = $('[data-module="mobile-filters-modal"]')[0] - new GOVUK.Modules.MobileFiltersModal(element).init() }) afterEach(function () { document.body.removeChild(container) }) - describe('open button', function () { + describe('Mobile filters modal', function () { beforeEach(function () { - document.querySelector('.js-toggle-mobile-filters').click() - }) - - afterEach(function () { - document.querySelector('.js-toggle-mobile-filters').click() - }) - - it('should show the modal', function () { - var modal = document.querySelector('.facets') - expect($(modal).hasClass('facets--visible')).toBe(true) + var element = $('[data-module="mobile-filters-modal"]')[0] + new GOVUK.Modules.MobileFiltersModal(element).init() }) - it('should hide the modal', function () { - var modal = document.querySelector('.facets') - document.querySelector('.js-toggle-mobile-filters').click() - expect($(modal).hasClass('facets--visible')).toBe(false) + describe('open button', function () { + beforeEach(function () { + document.querySelector('.js-toggle-mobile-filters').click() + }) + + afterEach(function () { + document.querySelector('.js-toggle-mobile-filters').click() + }) + + it('should show the modal', function () { + var modal = document.querySelector('.facets') + expect($(modal).hasClass('facets--visible')).toBe(true) + }) + + it('should hide the modal', function () { + var modal = document.querySelector('.facets') + document.querySelector('.js-toggle-mobile-filters').click() + expect($(modal).hasClass('facets--visible')).toBe(false) + }) }) - }) - describe('open', function () { - beforeEach(function () { - var modal = document.querySelector('.facets') - modal.open() + describe('open', function () { + beforeEach(function () { + var modal = document.querySelector('.facets') + modal.open() + }) + + afterEach(function () { + var modal = document.querySelector('.facets') + modal.close() + }) + + it('should show the modal', function () { + var modal = document.querySelector('.facets') + expect($(modal).hasClass('facets--visible')).toBe(true) + }) }) - afterEach(function () { - var modal = document.querySelector('.facets') - modal.close() + describe('close', function () { + it('should hide the modal', function () { + var modal = document.querySelector('.facets') + modal.open() + modal.close() + expect($(modal).hasClass('facets--visible')).toBe(false) + }) }) - it('should show the modal', function () { - var modal = document.querySelector('.facets') - expect($(modal).hasClass('facets--visible')).toBe(true) + describe('clear filters', function () { + it('should reset checkboxes, clear text input and values', function () { - var modal = document.querySelector('.facets') - modal.clearFilters() - expect($(modal).find('input:checked').length).toBe(0) - // number of text inputs with value should now be 0 - expect($(modal).find('input[type="text"]') - .filter(function () { return $(this).val() }).length).toBe(0) - expect($(modal).find('select').val()).toBe('') + describe('ga4 tracking', function () { + it('adds the ga4 event tracker to the button', function () { + var button = document.querySelector('.js-toggle-mobile-filters') + var expected = { + event_name: 'select_content', + type: 'finder', + text: 'Filter', + section: 'Filter', + index: { + index_section: 0, + index_section_count: 5 + } + } + expect(button.getAttribute('data-ga4-event')).toEqual(JSON.stringify(expected)) + }) }) }) - describe('accessibility', function () { - it('should add aria-expanded="false" on load to the Filter button', function () { - var button = document.querySelector('.js-toggle-mobile-filters') - expect(button.getAttribute('aria-expanded')).toEqual('false') - }) - - it('should set aria-expanded to true when clicking the Filter button', function () { - var button = document.querySelector('.js-toggle-mobile-filters') - button.click() - expect(button.getAttribute('aria-expanded')).toEqual('true') - }) + describe('Mobile filters modal (open on load)', function () { + beforeEach(function () { + container.querySelector('button').dataset.openOnLoad = true - it('should add aria-controls on load to the Filter button', function () { - var button = document.querySelector('.js-toggle-mobile-filters') - expect(button.getAttribute('aria-controls')).toEqual('facet-wrapper') - expect(document.querySelector('#facet-wrapper')).not.toEqual(null) + var element = $('[data-module="mobile-filters-modal"]')[0] + new GOVUK.Modules.MobileFiltersModal(element).init() }) - }) - describe('ga4 tracking', function () { - it('adds the ga4 event tracker to the button', function () { - var button = document.querySelector('.js-toggle-mobile-filters') - var expected = { - event_name: 'select_content', - type: 'finder', - text: 'Filter', - section: 'Filter', - index: { - index_section: 0, - index_section_count: 5 - } - } - expect(button.getAttribute('data-ga4-event')).toEqual(JSON.stringify(expected)) + describe('open on load', function () { + it('should show the modal', function () { + var modal = document.querySelector('.facets') + expect($(modal).hasClass('facets--visible')).toBe(true) + }) }) }) })