Skip to content

Commit

Permalink
Update tests to use new initilization method
Browse files Browse the repository at this point in the history
The modules in govuk_publishing_components have been updated to use the
new method of initilization. To get tests to work correctly, we also
need to update how the modules are initialized in the tests themselves
as they are individually initialized instead of using `initAll()`
  • Loading branch information
patrickpatrickpatrick committed Nov 7, 2023
1 parent 00fc4fd commit 40de532
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 57 deletions.
3 changes: 2 additions & 1 deletion spec/javascripts/components/accordion-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ describe('Accordion component', function () {
'</div>'

function startAccordion () {
new GOVUK.Modules.GemAccordion(accordion).init()
/* eslint-disable no-new */
new GOVUK.Modules.GemAccordion(accordion)
}

beforeEach(function () {
Expand Down
3 changes: 2 additions & 1 deletion spec/javascripts/components/checkboxes-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

describe('Checkboxes component', function () {
function loadCheckboxesComponent () {
new GOVUK.Modules.GemCheckboxes($('.gem-c-checkboxes')[0]).init()
/* eslint-disable no-new */
new GOVUK.Modules.GemCheckboxes($('.gem-c-checkboxes')[0])
}

var FIXTURE =
Expand Down
6 changes: 4 additions & 2 deletions spec/javascripts/components/contextual-guidance-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ describe('Contextual guidance component', function () {
document.body.appendChild(container)
var titleContextualGuidance = document.getElementById('document-title-guidance')
var summaryContextualGuidance = document.getElementById('document-summary-guidance')
new GOVUK.Modules.ContextualGuidance(titleContextualGuidance).init()
new GOVUK.Modules.ContextualGuidance(summaryContextualGuidance).init()
/* eslint-disable no-new */
new GOVUK.Modules.ContextualGuidance(titleContextualGuidance)
/* eslint-disable no-new */
new GOVUK.Modules.ContextualGuidance(summaryContextualGuidance)
})

afterEach(function () {
Expand Down
39 changes: 26 additions & 13 deletions spec/javascripts/components/cookie-banner-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ describe('Cookie banner', function () {

it('should show the cookie banner', function () {
var element = document.querySelector('[data-module="cookie-banner"]')
new GOVUK.Modules.CookieBanner(element).init()
/* eslint-disable no-new */
new GOVUK.Modules.CookieBanner(element)

var cookieBannerMain = document.querySelector('.js-banner-wrapper')
var cookieBannerConfirmationAccept = document.querySelector('.gem-c-cookie-banner__confirmation-message--accepted')
Expand All @@ -73,7 +74,8 @@ describe('Cookie banner', function () {
GOVUK.setDefaultConsentCookie() // Set default cookies, which are set whether there is any interaction or not.

var element = document.querySelector('[data-module="cookie-banner"]')
new GOVUK.Modules.CookieBanner(element).init()
/* eslint-disable no-new */
new GOVUK.Modules.CookieBanner(element)

var cookieBannerMain = document.querySelector('.js-banner-wrapper')
var cookieBannerConfirmationAccept = document.querySelector('.gem-c-cookie-banner__confirmation-message--accepted')
Expand All @@ -89,14 +91,16 @@ describe('Cookie banner', function () {
GOVUK.cookie('cookies_preferences_set', 'true', { days: 365 })

var element = document.querySelector('[data-module="cookie-banner"]')
new GOVUK.Modules.CookieBanner(element).init()
/* eslint-disable no-new */
new GOVUK.Modules.CookieBanner(element)

expect(element).toBeHidden()
})

it('sets a default consent cookie', function () {
var element = document.querySelector('[data-module="cookie-banner"]')
new GOVUK.Modules.CookieBanner(element).init()
/* eslint-disable no-new */
new GOVUK.Modules.CookieBanner(element)

expect(GOVUK.getCookie('cookies_preferences_set')).toEqual(null)
expect(GOVUK.getCookie('cookies_policy')).toEqual(DEFAULT_COOKIE_CONSENT)
Expand All @@ -107,7 +111,8 @@ describe('Cookie banner', function () {
spyOn(GOVUK, 'deleteUnconsentedCookies').and.callThrough()

var element = document.querySelector('[data-module="cookie-banner"]')
new GOVUK.Modules.CookieBanner(element).init()
/* eslint-disable no-new */
new GOVUK.Modules.CookieBanner(element)

expect(GOVUK.getCookie('cookies_policy')).toEqual(DEFAULT_COOKIE_CONSENT)
expect(GOVUK.deleteUnconsentedCookies).toHaveBeenCalled()
Expand All @@ -119,7 +124,8 @@ describe('Cookie banner', function () {
spyOn(GOVUK, 'setCookie').and.callThrough()

var element = document.querySelector('[data-module="cookie-banner"]')
new GOVUK.Modules.CookieBanner(element).init()
/* eslint-disable no-new */
new GOVUK.Modules.CookieBanner(element)

// Manually reset the consent cookie so we can check the accept button works as intended
expect(GOVUK.getCookie('cookies_policy')).toEqual(DEFAULT_COOKIE_CONSENT)
Expand All @@ -144,7 +150,8 @@ describe('Cookie banner', function () {
spyOn(GOVUK, 'setCookie').and.callThrough()

var element = document.querySelector('[data-module="cookie-banner"]')
new GOVUK.Modules.CookieBanner(element).init()
/* eslint-disable no-new */
new GOVUK.Modules.CookieBanner(element)

// Manually reset the consent cookie so we can check the accept button works as intended
expect(GOVUK.getCookie('cookies_policy')).toEqual(DEFAULT_COOKIE_CONSENT)
Expand All @@ -161,7 +168,8 @@ describe('Cookie banner', function () {

it('shows a confirmation message when cookies have been accepted', function () {
var element = document.querySelector('[data-module="cookie-banner"]')
new GOVUK.Modules.CookieBanner(element).init()
/* eslint-disable no-new */
new GOVUK.Modules.CookieBanner(element)

var acceptCookiesButton = document.querySelector('[data-accept-cookies]')
var confirmationMessageAccepted = document.querySelector('.gem-c-cookie-banner__confirmation-message--accepted')
Expand All @@ -176,7 +184,8 @@ describe('Cookie banner', function () {

it('shows a confirmation message when cookies have been rejected', function () {
var element = document.querySelector('[data-module="cookie-banner"]')
new GOVUK.Modules.CookieBanner(element).init()
/* eslint-disable no-new */
new GOVUK.Modules.CookieBanner(element)

var rejectCookiesButton = document.querySelector('[data-reject-cookies]')
var confirmationMessageRejected = document.querySelector('.gem-c-cookie-banner__confirmation-message--rejected')
Expand All @@ -191,7 +200,8 @@ describe('Cookie banner', function () {

it('set focus to the confirmation message after clicking button', function () {
var element = document.querySelector('[data-module="cookie-banner"]')
new GOVUK.Modules.CookieBanner(element).init()
/* eslint-disable no-new */
new GOVUK.Modules.CookieBanner(element)

var rejectCookiesButton = document.querySelector('[data-reject-cookies]')
var confirmationMessage = document.querySelector('.gem-c-cookie-banner__confirmation')
Expand All @@ -206,7 +216,8 @@ describe('Cookie banner', function () {
it('set cookies_preferences_set cookie, and re-set cookies_policy expiration date when rejecting cookies', function () {
spyOn(GOVUK, 'setCookie').and.callThrough()
var element = document.querySelector('[data-module="cookie-banner"]')
new GOVUK.Modules.CookieBanner(element).init()
/* eslint-disable no-new */
new GOVUK.Modules.CookieBanner(element)

var rejectCookiesButton = document.querySelector('[data-reject-cookies]')

Expand All @@ -220,7 +231,8 @@ describe('Cookie banner', function () {
spyOn(GOVUK, 'setCookie').and.callThrough()

var element = document.querySelector('[data-module="cookie-banner"]')
new GOVUK.Modules.CookieBanner(element).init()
/* eslint-disable no-new */
new GOVUK.Modules.CookieBanner(element)

var link = document.querySelector('button[data-hide-cookie-banner="true"]')
link.dispatchEvent(new window.Event('click'))
Expand All @@ -243,7 +255,8 @@ describe('Cookie banner', function () {

it('should hide the cookie banner', function () {
var element = document.querySelector('[data-module="cookie-banner"]')
new GOVUK.Modules.CookieBanner(element).init()
/* eslint-disable no-new */
new GOVUK.Modules.CookieBanner(element)
expect(element).toBeHidden()
})
})
Expand Down
3 changes: 2 additions & 1 deletion spec/javascripts/components/feedback-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,8 @@ describe('Feedback component', function () {
})

function loadFeedbackComponent () {
new GOVUK.Modules.Feedback($('.gem-c-feedback')[0]).init()
/* eslint-disable no-new */
new GOVUK.Modules.Feedback($('.gem-c-feedback')[0])
}

function fillAndSubmitSomethingIsWrongForm () {
Expand Down
3 changes: 3 additions & 0 deletions spec/javascripts/components/govspeak-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('Govspeak', function () {
document.body.appendChild(container)

var element = document.querySelector('[data-module="govspeak"]')
/* eslint-disable no-new */
new GOVUK.Modules.Govspeak(element).init()

expect(document.querySelectorAll('.gem-c-govspeak__youtube-video').length).toBe(1)
Expand All @@ -32,6 +33,7 @@ describe('Govspeak', function () {
document.body.appendChild(container)

var element = document.querySelector('[data-module="govspeak"]')
/* eslint-disable no-new */
new GOVUK.Modules.Govspeak(element).init()

expect(document.querySelectorAll('.gem-c-govspeak__youtube-video').length).toBe(0)
Expand All @@ -54,6 +56,7 @@ describe('Govspeak', function () {
document.body.appendChild(container)

var element = document.querySelector('[data-module="govspeak"]')
/* eslint-disable no-new */
new GOVUK.Modules.Govspeak(element).init()

expect(document.querySelectorAll('.mc-chart').length).toBe(1)
Expand Down
1 change: 1 addition & 0 deletions spec/javascripts/components/intervention-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe('Intervention banner component', function () {
it('does not display the banner', function () {
GOVUK.setCookie('intervention_campaign', 'test-intervention-name', { days: 1 })
var element = document.querySelector('[data-module="intervention"]')
/* eslint-disable no-new */
new GOVUK.Modules.Intervention(element).init()

var banner = document.querySelector('.gem-c-intervention')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ describe('The super header navigation', function () {
'use strict'

var container
var thisModule

beforeEach(function () {
container = document.createElement('div')
Expand Down Expand Up @@ -198,10 +197,6 @@ describe('The super header navigation', function () {
'</nav>'

document.body.appendChild(container)

var $element = document.querySelector('[data-module="super-navigation-mega-menu"]')
thisModule = new GOVUK.Modules.SuperNavigationMegaMenu($element)

spyOn(GOVUK.analytics, 'trackEvent')
})

Expand All @@ -215,7 +210,9 @@ describe('The super header navigation', function () {

describe('on both small and large screens', function () {
beforeEach(function () {
thisModule.init()
var $element = document.querySelector('[data-module="super-navigation-mega-menu"]')
/* eslint-disable no-new */
new GOVUK.Modules.SuperNavigationMegaMenu($element)
})

it('has the initialised class once the JavaScript has run', function () {
Expand All @@ -229,7 +226,9 @@ describe('The super header navigation', function () {
var $button

beforeEach(function () {
thisModule.init()
var $element = document.querySelector('[data-module="super-navigation-mega-menu"]')
/* eslint-disable no-new */
new GOVUK.Modules.SuperNavigationMegaMenu($element)
$button = document.querySelector('#super-navigation-menu-toggle')
})

Expand Down Expand Up @@ -347,7 +346,9 @@ describe('The super header navigation', function () {
var $button

beforeEach(function () {
thisModule.init()
var $element = document.querySelector('[data-module="super-navigation-mega-menu"]')
/* eslint-disable no-new */
new GOVUK.Modules.SuperNavigationMegaMenu($element)
$button = document.querySelector('#super-search-menu-toggle')
})

Expand Down
31 changes: 17 additions & 14 deletions spec/javascripts/components/option-select-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ describe('An option select component', function () {
var $closedOnLoadFixture = $(optionSelectWithAttrs('data-closed-on-load=true'))
$('body').append($closedOnLoadFixture)

new GOVUK.Modules.OptionSelect($closedOnLoadFixture[0]).init()
console.log($closedOnLoadFixture[0].outerHTML)

console.log(new GOVUK.Modules.OptionSelect($closedOnLoadFixture[0]))

console.log($closedOnLoadFixture[0].outerHTML)

expect($closedOnLoadFixture.find('button').attr('aria-expanded')).toBe('false')
})
Expand All @@ -113,7 +117,8 @@ describe('An option select component', function () {
var $openOnLoadFixture = $(optionSelectWithAttrs('data-closed-on-load=false'))
$('body').append($openOnLoadFixture)

new GOVUK.Modules.OptionSelect($openOnLoadFixture[0]).init()
/* eslint-disable no-new */
new GOVUK.Modules.OptionSelect($openOnLoadFixture[0])

expect($openOnLoadFixture.find('button').attr('aria-expanded')).toBe('true')
expect($('body').find('.js-options-container').is(':visible')).toBe(true)
Expand All @@ -123,7 +128,8 @@ describe('An option select component', function () {
var $openOnLoadFixture = $(optionSelectWithAttrs(''))
$('body').append($openOnLoadFixture)

new GOVUK.Modules.OptionSelect($openOnLoadFixture[0]).init()
/* eslint-disable no-new */
new GOVUK.Modules.OptionSelect($openOnLoadFixture[0])

expect($openOnLoadFixture.find('button').attr('aria-expanded')).toBe('true')
expect($('body').find('.js-options-container').is(':visible')).toBe(true)
Expand All @@ -132,23 +138,26 @@ describe('An option select component', function () {
it('sets the height of the options container as part of initialisation', function () {
$element = document.createElement('div')
$element.innerHTML = html
new GOVUK.Modules.OptionSelect($element.querySelector('.gem-c-option-select')).init()
/* eslint-disable no-new */
new GOVUK.Modules.OptionSelect($element.querySelector('.gem-c-option-select'))

expect($($element).find('.js-options-container').attr('style')).toContain('height')
})

it('doesn\'t set the height of the options container as part of initialisation if closed-on-load is true', function () {
var $closedOnLoadFixture = $(optionSelectWithAttrs('data-closed-on-load=true'))

new GOVUK.Modules.OptionSelect($closedOnLoadFixture[0]).init()
/* eslint-disable no-new */
new GOVUK.Modules.OptionSelect($closedOnLoadFixture[0])

expect($closedOnLoadFixture.find('.js-options-container').attr('style')).not.toContain('height')
})

it('replaces the `span.gem-c-option-select__title` with a button', function () {
$element = document.createElement('div')
$element.innerHTML = html
new GOVUK.Modules.OptionSelect($element.querySelector('.gem-c-option-select')).init()
/* eslint-disable no-new */
new GOVUK.Modules.OptionSelect($element.querySelector('.gem-c-option-select'))

expect($($element).find('button')).toBeDefined()
})
Expand All @@ -161,7 +170,6 @@ describe('An option select component', function () {
$('body').append($element)

optionSelect = new GOVUK.Modules.OptionSelect($element.querySelector('.gem-c-option-select'))
optionSelect.init()
})

it('calls optionSelect.close() if the optionSelect is currently open', function () {
Expand All @@ -185,7 +193,8 @@ describe('An option select component', function () {
$element.innerHTML = html
$('body').append($element)

new GOVUK.Modules.OptionSelect($element.querySelector('.gem-c-option-select')).init()
/* eslint-disable no-new */
new GOVUK.Modules.OptionSelect($element.querySelector('.gem-c-option-select'))
})

it('closes and opens the option select', function () {
Expand Down Expand Up @@ -215,7 +224,6 @@ describe('An option select component', function () {
$('body').append($element)

optionSelect = new GOVUK.Modules.OptionSelect($element.find('.gem-c-option-select')[0])
optionSelect.init()
optionSelect.setContainerHeight(100)
firstCheckbox = optionSelect.$allCheckboxes[0]
lastCheckbox = optionSelect.$allCheckboxes[optionSelect.$allCheckboxes.length - 1]
Expand All @@ -237,7 +245,6 @@ describe('An option select component', function () {
$element = $(html)
$('body').append($element)
optionSelect = new GOVUK.Modules.OptionSelect($element.find('.gem-c-option-select')[0])
optionSelect.init()
})

it('returns all the checkboxes if the container doesn\'t overflow', function () {
Expand Down Expand Up @@ -277,7 +284,6 @@ describe('An option select component', function () {
})

$checkboxListInner = $checkboxList.find(' > .js-auto-height-inner')
optionSelect.init()
})

it('expands the checkbox-container to fit checkbox list if the list is < 50px larger than the container', function () {
Expand Down Expand Up @@ -308,7 +314,6 @@ describe('An option select component', function () {
$wrapper.hide()

optionSelect = new GOVUK.Modules.OptionSelect($wrapper.find('.gem-c-option-select')[0])
optionSelect.init()
})

afterEach(function () {
Expand All @@ -328,7 +333,6 @@ describe('An option select component', function () {
var $wrapper = $('<div/>').addClass('wrapper').hide().html($element)
$('body').append($wrapper)
optionSelect = new GOVUK.Modules.OptionSelect($element.find('.gem-c-option-select')[0])
optionSelect.init()
})

afterEach(function () {
Expand Down Expand Up @@ -362,7 +366,6 @@ describe('An option select component', function () {
$element.find('.gem-c-checkboxes').prepend($(filterSpan))
$('body').append($element)
optionSelect = new GOVUK.Modules.OptionSelect($element.find('.gem-c-option-select')[0])
optionSelect.init()

jasmine.clock().install()
$filterInput = document.querySelector('[name="option-select-filter"]')
Expand Down
Loading

0 comments on commit 40de532

Please sign in to comment.