From c6529ad453bf64ea9a69c1b0ff114208cc235edc Mon Sep 17 00:00:00 2001 From: alexjanousekGSA Date: Thu, 7 Aug 2025 16:49:14 -0400 Subject: [PATCH] More govuk cleanup --- .../javascripts/collapsibleCheckboxes.js | 4 +- app/assets/javascripts/enhancedTextbox.js | 5 +- app/assets/javascripts/errorTracking.js | 22 ++++--- app/assets/javascripts/liveSearch.js | 6 +- .../javascripts/collapsibleCheckboxes.test.js | 41 ++++++------ tests/javascripts/copyToClipboard.test.js | 20 +++--- tests/javascripts/enhancedTextbox.test.js | 36 +++++------ tests/javascripts/errorBanner.test.js | 4 +- tests/javascripts/errorTracking.test.js | 12 ++-- tests/javascripts/fileUpload.test.js | 6 +- tests/javascripts/fullscreenTable.test.js | 16 ++--- tests/javascripts/listEntry.test.js | 32 +++++----- tests/javascripts/liveSearch.test.js | 16 ++--- tests/javascripts/loginAlert.test.js | 2 +- tests/javascripts/radioSelect.test.js | 12 ++-- .../stick-to-window-when-scrolling.test.js | 64 +++++++++---------- tests/javascripts/support/jest.setup.js | 18 +++++- tests/javascripts/support/setup.js | 3 +- tests/javascripts/templateFolderForm.test.js | 22 +++---- tests/javascripts/timeoutPopup.test.js | 16 ++--- tests/javascripts/updateContent.test.js | 28 ++++---- tests/javascripts/updateStatus.test.js | 12 ++-- 22 files changed, 209 insertions(+), 188 deletions(-) diff --git a/app/assets/javascripts/collapsibleCheckboxes.js b/app/assets/javascripts/collapsibleCheckboxes.js index 4f4017818..ee5d4133f 100644 --- a/app/assets/javascripts/collapsibleCheckboxes.js +++ b/app/assets/javascripts/collapsibleCheckboxes.js @@ -220,6 +220,8 @@ this.summary.bindEvents(this); }; - NotifyModules['collapsible-checkboxes'] = CollapsibleCheckboxes; + NotifyModules['collapsible-checkboxes'] = function() { + return new CollapsibleCheckboxes(); + }; }(window)); diff --git a/app/assets/javascripts/enhancedTextbox.js b/app/assets/javascripts/enhancedTextbox.js index d583c7581..2f5f3a3c3 100644 --- a/app/assets/javascripts/enhancedTextbox.js +++ b/app/assets/javascripts/enhancedTextbox.js @@ -11,8 +11,9 @@ window.NotifyModules['enhanced-textbox'] = function() { - this.start = function(textarea) { + this.start = function(element) { + let textarea = $(element); let visibleTextbox; this.highlightPlaceholders = ( @@ -20,7 +21,7 @@ !!textarea.data('highlightPlaceholders') ); - this.$textbox = $(textarea) + this.$textbox = textarea .wrap(`
`) diff --git a/app/assets/javascripts/errorTracking.js b/app/assets/javascripts/errorTracking.js index 0e125c8e9..d69620f60 100644 --- a/app/assets/javascripts/errorTracking.js +++ b/app/assets/javascripts/errorTracking.js @@ -3,17 +3,19 @@ window.NotifyModules['track-error'] = function() { - this.start = function(component) { + this.start = function(element) { + var component = $(element); - // Analytics tracking removed - no longer using GOVUK analytics - // If analytics is needed, integrate with USWDS analytics or custom solution - console.log( - 'Error', - $(component).data('error-type'), - { - 'label': $(component).data('error-label') - } - ); + // Track error to analytics if available + if (window.NotifyModules && window.NotifyModules.analytics && window.NotifyModules.analytics.trackEvent) { + window.NotifyModules.analytics.trackEvent( + 'Error', + component.data('error-type'), + { + 'label': component.data('error-label') + } + ); + } }; diff --git a/app/assets/javascripts/liveSearch.js b/app/assets/javascripts/liveSearch.js index e4d0f9bd9..ea5da63d9 100644 --- a/app/assets/javascripts/liveSearch.js +++ b/app/assets/javascripts/liveSearch.js @@ -61,10 +61,8 @@ // make sticky JS recalculate its cache of the element's position // because live search can change the height document - if ('stickAtBottomWhenScrolling' in GOVUK) { - if (window.NotifyModules && window.NotifyModules.stickAtBottomWhenScrolling) { - window.NotifyModules.stickAtBottomWhenScrolling.recalculate(); - } + if (window.NotifyModules && 'stickAtBottomWhenScrolling' in window.NotifyModules) { + window.NotifyModules.stickAtBottomWhenScrolling.recalculate(); } }; diff --git a/tests/javascripts/collapsibleCheckboxes.test.js b/tests/javascripts/collapsibleCheckboxes.test.js index 3d6c6bc1b..0e4b0f2f6 100644 --- a/tests/javascripts/collapsibleCheckboxes.test.js +++ b/tests/javascripts/collapsibleCheckboxes.test.js @@ -75,7 +75,7 @@ describe('Collapsible fieldset', () => { beforeEach(() => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); }); @@ -172,7 +172,7 @@ describe('Collapsible fieldset', () => { test('has the right summary text when started with no checkboxes selected', () => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); const summaryText = document.querySelector('.selection-summary__text'); @@ -189,7 +189,7 @@ describe('Collapsible fieldset', () => { }); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); const summaryText = document.querySelector('.selection-summary__text'); @@ -203,7 +203,7 @@ describe('Collapsible fieldset', () => { checkboxes.forEach(el => el.setAttribute('checked', '')); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); const summaryText = document.querySelector('.selection-summary__text'); @@ -216,7 +216,7 @@ describe('Collapsible fieldset', () => { wrapper.dataset.fieldLabel = 'team member'; // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); const summaryText = document.querySelector('.selection-summary__text'); @@ -229,7 +229,7 @@ describe('Collapsible fieldset', () => { beforeEach(() => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); helpers.triggerEvent(formGroup.querySelector('.selection-footer__button'), 'click'); @@ -266,7 +266,7 @@ describe('Collapsible fieldset', () => { beforeEach(() => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); // show the checkboxes helpers.triggerEvent(formGroup.querySelector('.selection-footer__button'), 'click'); @@ -308,7 +308,7 @@ describe('Collapsible fieldset', () => { test("after the fieldset", () => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); // show the checkboxes helpers.triggerEvent(formGroup.querySelector('.selection-footer__button'), 'click'); @@ -326,7 +326,7 @@ describe('Collapsible fieldset', () => { checkboxesContainer.querySelector('.usa-checkbox').appendChild(nestedCheckboxes); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); // show the checkboxes helpers.triggerEvent(formGroup.querySelector('.selection-footer__button'), 'click'); @@ -342,10 +342,11 @@ describe('Collapsible fieldset', () => { beforeEach(() => { // track calls to sticky JS - window.GOVUK.stickAtBottomWhenScrolling.recalculate = jest.fn(() => {}); + window.NotifyModules.stickAtBottomWhenScrolling = window.NotifyModules.stickAtBottomWhenScrolling || {}; + window.NotifyModules.stickAtBottomWhenScrolling.recalculate = jest.fn(() => {}); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); // show the checkboxes helpers.triggerEvent(formGroup.querySelector('.selection-footer__button'), 'click'); @@ -374,7 +375,7 @@ describe('Collapsible fieldset', () => { describe("check/uncheck all functionality", () => { beforeEach(() => { - window.GOVUK.modules.start(); + window.NotifyModules.start(); helpers.triggerEvent(formGroup.querySelector('.selection-footer__button'), 'click'); }); @@ -434,7 +435,7 @@ describe('Collapsible fieldset', () => { describe("toggle button visibility on re-expansion", () => { test("shows toggle button again when fieldset is re-opened", () => { - window.GOVUK.modules.start(); + window.NotifyModules.start(); helpers.triggerEvent(formGroup.querySelector('.selection-footer__button'), 'click'); const toggleButton = document.querySelector('.usa-button--small'); @@ -487,7 +488,7 @@ describe('Collapsible fieldset', () => { checkFirstCheckbox(); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); showCheckboxes(); @@ -507,7 +508,7 @@ describe('Collapsible fieldset', () => { checkFirstCheckbox(); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); showCheckboxes(); @@ -527,7 +528,7 @@ describe('Collapsible fieldset', () => { checkFirstCheckbox(); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); showCheckboxes(); @@ -551,7 +552,7 @@ describe('Collapsible fieldset', () => { checkAllCheckboxes(); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); showCheckboxes(); @@ -571,7 +572,7 @@ describe('Collapsible fieldset', () => { checkAllCheckboxes(); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); showCheckboxes(); @@ -595,7 +596,7 @@ describe('Collapsible fieldset', () => { checkAllCheckboxesButTheLast(); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); showCheckboxes(); @@ -614,7 +615,7 @@ describe('Collapsible fieldset', () => { checkAllCheckboxesButTheLast(); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); showCheckboxes(); diff --git a/tests/javascripts/copyToClipboard.test.js b/tests/javascripts/copyToClipboard.test.js index b282faf91..a2ce7b850 100644 --- a/tests/javascripts/copyToClipboard.test.js +++ b/tests/javascripts/copyToClipboard.test.js @@ -45,7 +45,7 @@ describe('copy to clipboard', () => { document.execCommand = jest.fn(() => {}); // mock sticky JS - window.GOVUK.stickAtBottomWhenScrolling = { + window.NotifyModules.stickAtBottomWhenScrolling = { recalculate: jest.fn(() => {}) } @@ -63,7 +63,7 @@ describe('copy to clipboard', () => { component = document.querySelector('[data-module=copy-to-clipboard]'); // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); expect(component.querySelector('button')).toBeNull(); @@ -120,7 +120,7 @@ describe('copy to clipboard', () => { }); // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); }); @@ -139,7 +139,7 @@ describe('copy to clipboard', () => { test("It should tell any sticky JS present the page has changed", () => { // recalculate forces the sticky JS to recalculate any stored DOM position/dimensions - expect(window.GOVUK.stickAtBottomWhenScrolling.recalculate).toHaveBeenCalled(); + expect(window.NotifyModules.stickAtBottomWhenScrolling.recalculate).toHaveBeenCalled(); }); @@ -154,7 +154,7 @@ describe('copy to clipboard', () => { expect(component.querySelector('.copy-to-clipboard__value').textContent).toBe('00000000-0000-0000-0000-000000000000'); - }); + }); }); @@ -169,7 +169,7 @@ describe('copy to clipboard', () => { component = document.querySelector('[data-module=copy-to-clipboard]'); // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); }); @@ -205,7 +205,7 @@ describe('copy to clipboard', () => { component = document.querySelector('[data-module=copy-to-clipboard]'); // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); }); @@ -233,7 +233,7 @@ describe('copy to clipboard', () => { setUpDOM({ 'thing': 'Some Thing', 'name': 'Some Thing' }); // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); component = document.querySelector('[data-module=copy-to-clipboard]'); keyEl = component.querySelector('.copy-to-clipboard__value'); @@ -332,7 +332,7 @@ describe('copy to clipboard', () => { setUpDOM({ 'thing': 'ID', 'name': 'Default' }); // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); component = document.querySelector('[data-module=copy-to-clipboard]'); @@ -375,7 +375,7 @@ describe('copy to clipboard', () => { setUpDOM({ 'thing': 'Some Thing', 'name': 'Some Thing' }); // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); component = document.querySelector('[data-module=copy-to-clipboard]'); diff --git a/tests/javascripts/enhancedTextbox.test.js b/tests/javascripts/enhancedTextbox.test.js index 23392ae3f..30d0d7f8b 100644 --- a/tests/javascripts/enhancedTextbox.test.js +++ b/tests/javascripts/enhancedTextbox.test.js @@ -63,7 +63,7 @@ describe('Enhanced textbox', () => { beforeEach(() => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); }); @@ -98,7 +98,7 @@ describe('Enhanced textbox', () => { beforeEach(() => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); }); @@ -135,7 +135,7 @@ describe('Enhanced textbox', () => { setDisplayPropertyOfFormGroups('none'); - window.GOVUK.modules.start(); + window.NotifyModules.start(); setDisplayPropertyOfFormGroups('block'); @@ -163,7 +163,7 @@ describe('Enhanced textbox', () => { textarea.textContent = "Dear ((title)) ((name))"; // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); backgroundEl = textarea.nextElementSibling; @@ -180,7 +180,7 @@ describe('Enhanced textbox', () => { input.value = "Dear ((title)) ((name))"; // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); backgroundEl = input.nextElementSibling; @@ -198,7 +198,7 @@ describe('Enhanced textbox', () => { textarea.setAttribute('data-highlight-placeholders', 'false') // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); backgroundEl = textarea.nextElementSibling; @@ -217,7 +217,7 @@ describe('Enhanced textbox', () => { textarea.textContent = "When you arrive, please go to the ((weekday??main entrance))((weekend??side entrance))"; // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); backgroundEl = textarea.nextElementSibling; @@ -234,7 +234,7 @@ describe('Enhanced textbox', () => { input.value = "When you arrive, please go to the ((weekday??main entrance))((weekend??side entrance))"; // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); backgroundEl = input.nextElementSibling; @@ -270,7 +270,7 @@ describe('Enhanced textbox', () => { `; // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); backgroundEl = textarea.nextElementSibling; @@ -288,7 +288,7 @@ describe('Enhanced textbox', () => { test("If a resize changes the textarea's width, the width of the element below should still match", () => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); backgroundEl = textarea.nextElementSibling; @@ -310,7 +310,7 @@ describe('Enhanced textbox', () => { textarea.textContent = "Dear ((title)) ((name))"; // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); backgroundEl = textarea.nextElementSibling; @@ -330,7 +330,7 @@ describe('Enhanced textbox', () => { input.value = "Hospital appointment for ((name))"; // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); backgroundEl = input.nextElementSibling; @@ -355,7 +355,7 @@ describe('Enhanced textbox', () => { textarea.textContent = "Dear ((title)) ((name))"; // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); backgroundEl = textarea.nextElementSibling; @@ -378,7 +378,7 @@ describe('Enhanced textbox', () => { input.value = "Hospital appointment"; // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); backgroundEl = input.nextElementSibling; @@ -405,7 +405,7 @@ describe('Enhanced textbox', () => { Ref: ((reference))`; // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); backgroundEl = textarea.nextElementSibling; @@ -426,7 +426,7 @@ describe('Enhanced textbox', () => { input.value = "Hospital appointment for ((name)), ref: ((reference))"; // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); backgroundEl = input.nextElementSibling; @@ -453,7 +453,7 @@ describe('Enhanced textbox', () => { Your appointment will be on ((date)). When you arrive, please go to the ((weekday??main entrance))((weekend??side entrance)).`; // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); backgroundEl = textarea.nextElementSibling; @@ -482,7 +482,7 @@ describe('Enhanced textbox', () => { input.value = "Hospital appointment for ((name))((important?? - IMPORTANT))"; // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); backgroundEl = input.nextElementSibling; diff --git a/tests/javascripts/errorBanner.test.js b/tests/javascripts/errorBanner.test.js index 7acc7e822..8516afb84 100644 --- a/tests/javascripts/errorBanner.test.js +++ b/tests/javascripts/errorBanner.test.js @@ -16,7 +16,7 @@ describe("Error Banner", () => { document.body.innerHTML = ` `; - window.GOVUK.ErrorBanner.hideBanner(); + window.NotifyModules.ErrorBanner.hideBanner(); expect(document.querySelector('.banner-dangerous').classList).toContain('display-none') }); }); @@ -27,7 +27,7 @@ describe("Error Banner", () => { `; - window.GOVUK.ErrorBanner.showBanner('Some Err'); + window.NotifyModules.ErrorBanner.showBanner('Some Err'); }); test("Will show the element", () => { diff --git a/tests/javascripts/errorTracking.test.js b/tests/javascripts/errorTracking.test.js index c9db2aded..1a4278984 100644 --- a/tests/javascripts/errorTracking.test.js +++ b/tests/javascripts/errorTracking.test.js @@ -18,21 +18,21 @@ describe('Error tracking', () => { afterEach(() => { document.body.innerHTML = ''; - delete window.GOVUK.analytics; + delete window.NotifyModules.analytics; }); - test("If there is an analytics tracker set up, it should send details of the error to window.GOVUK.analytic", () => { + test("If there is an analytics tracker set up, it should send details of the error to window.NotifyModules.analytic", () => { - window.GOVUK.analytics = { + window.NotifyModules.analytics = { 'trackEvent': jest.fn() }; // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); - expect(window.GOVUK.analytics.trackEvent).toHaveBeenCalled(); - expect(window.GOVUK.analytics.trackEvent.mock.calls[0]).toEqual(['Error', 'validation', { + expect(window.NotifyModules.analytics.trackEvent).toHaveBeenCalled(); + expect(window.NotifyModules.analytics.trackEvent.mock.calls[0]).toEqual(['Error', 'validation', { 'label': 'missing field' }]); diff --git a/tests/javascripts/fileUpload.test.js b/tests/javascripts/fileUpload.test.js index a2184a68d..b88380b53 100644 --- a/tests/javascripts/fileUpload.test.js +++ b/tests/javascripts/fileUpload.test.js @@ -44,7 +44,7 @@ describe('File upload', () => { form.reset = jest.fn(() => {}); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); helpers.triggerEvent(window, 'pageshow'); @@ -59,7 +59,7 @@ describe('File upload', () => { form.submit = jest.fn(() => {}); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); helpers.triggerEvent(uploadControl, 'change', { eventInit: { bubbles: true } }); @@ -96,7 +96,7 @@ describe('File upload "upload-trigger" click handler', () => { form = document.querySelector('form'); // Register the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); }); afterEach(() => { diff --git a/tests/javascripts/fullscreenTable.test.js b/tests/javascripts/fullscreenTable.test.js index ef1013a1f..afa67ba79 100644 --- a/tests/javascripts/fullscreenTable.test.js +++ b/tests/javascripts/fullscreenTable.test.js @@ -121,7 +121,7 @@ describe('FullscreenTable', () => { test("it fixes the number column for each row without changing the semantics", () => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); tableFrame = document.querySelector('.fullscreen-scrollable-table'); numberColumnFrame = document.querySelector('.fullscreen-fixed-table'); @@ -134,10 +134,10 @@ describe('FullscreenTable', () => { test("it calls the sticky JS to update any cached dimensions", () => { - const stickyJSSpy = jest.spyOn(window.GOVUK.stickAtBottomWhenScrolling, 'recalculate'); + const stickyJSSpy = jest.spyOn(window.NotifyModules.stickAtBottomWhenScrolling, 'recalculate'); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); expect(stickyJSSpy.mock.calls.length).toBe(1); @@ -148,7 +148,7 @@ describe('FullscreenTable', () => { test("the scrolling section is focusable and has an accessible name matching the table caption", () => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); tableFrame = document.querySelector('.fullscreen-scrollable-table'); caption = tableFrame.querySelector('caption'); @@ -163,7 +163,7 @@ describe('FullscreenTable', () => { test("the section providing the fixed row headers is not focusable and is hidden from assistive tech'", () => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); fixedRowHeaders = document.querySelector('.fullscreen-fixed-table'); @@ -194,7 +194,7 @@ describe('FullscreenTable', () => { }); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); tableFrame = document.querySelector('.fullscreen-scrollable-table'); numberColumnFrame = document.querySelector('.fullscreen-fixed-table'); @@ -254,7 +254,7 @@ describe('FullscreenTable', () => { rowNumberColumnCell.setAttribute('style', 'width: 40px'); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); tableFrame = document.querySelector('.fullscreen-scrollable-table'); numberColumnFrame = document.querySelector('.fullscreen-fixed-table'); @@ -301,7 +301,7 @@ describe('FullscreenTable', () => { beforeEach(() => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); tableFrame = document.querySelector('.fullscreen-scrollable-table'); table = tableFrame.querySelector('table'); diff --git a/tests/javascripts/listEntry.test.js b/tests/javascripts/listEntry.test.js index 4bcc50e03..78e10783c 100644 --- a/tests/javascripts/listEntry.test.js +++ b/tests/javascripts/listEntry.test.js @@ -88,7 +88,7 @@ describe("List entry", () => { test("Should remove all the fields except the first 2 if no values are present", () => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); expect(inputList.querySelectorAll('.list-entry').length).toEqual(2); @@ -102,7 +102,7 @@ describe("List entry", () => { fields[0].setAttribute('value', domains[0]); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); // re-select fields, based on updated DOM fields = inputList.querySelectorAll('.list-entry input[type=text]'); @@ -120,7 +120,7 @@ describe("List entry", () => { fields[1].setAttribute('value', domains[1]); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); // re-select fields, based on updated DOM fields = inputList.querySelectorAll('.list-entry input[type=text]'); @@ -138,7 +138,7 @@ describe("List entry", () => { fourDomains.forEach((domain, idx) => { fields[idx].setAttribute('value', domain) }); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); // re-select fields, based on updated DOM fields = inputList.querySelectorAll('.list-entry input[type=text]'); @@ -154,7 +154,7 @@ describe("List entry", () => { test("Should add 'remove' buttons to all fields except the first", () => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); inputList.querySelectorAll('.list-entry').forEach((listEntry, idx) => { @@ -171,7 +171,7 @@ describe("List entry", () => { test("Should add an 'add feature' button to the bottom of the list", () => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); const listItems = inputList.children; @@ -192,7 +192,7 @@ describe("List entry", () => { }); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); // re-select fields, based on updated DOM fields = inputList.querySelectorAll('.list-entry input[type=text]').forEach((field, idx) => { @@ -214,7 +214,7 @@ describe("List entry", () => { }); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); // re-select fields, based on updated DOM fields = inputList.querySelectorAll('.list-entry input[type=text]'); @@ -230,7 +230,7 @@ describe("List entry", () => { setFieldValues(10); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); }); test("Should remove the associated field", () => { @@ -307,7 +307,7 @@ describe("List entry", () => { test("Should add a new field", () => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); triggerEvent(inputList.querySelector('.input-list__button--add'), 'click'); @@ -318,7 +318,7 @@ describe("List entry", () => { test("Should update the number of fields users are allowed to enter if one is removed", () => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); triggerEvent(inputList.querySelectorAll('.input-list__button--remove')[0], 'click'); @@ -329,7 +329,7 @@ describe("List entry", () => { test("Should update the number of fields users are allowed to enter if one is added", () => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); triggerEvent(inputList.querySelector('.input-list__button--add'), 'click'); @@ -342,7 +342,7 @@ describe("List entry", () => { setFieldValues(9); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); triggerEvent(inputList.querySelector('.input-list__button--add'), 'click'); @@ -364,7 +364,7 @@ describe("List entry", () => { inputList = document.querySelector('.input-list'); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); // Check that it handles missing classes gracefully const newInput = inputList.querySelector('.list-entry input'); @@ -381,7 +381,7 @@ describe("List entry", () => { // This should not throw an error expect(() => { - window.GOVUK.modules.start(); + window.NotifyModules.start(); }).not.toThrow(); }); }); @@ -402,7 +402,7 @@ describe("List entry", () => { inputList = document.querySelector('.input-list'); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); // After module starts, check that IDs are generated correctly // The module will have regenerated the DOM diff --git a/tests/javascripts/liveSearch.test.js b/tests/javascripts/liveSearch.test.js index b14a36996..97fd8492c 100644 --- a/tests/javascripts/liveSearch.test.js +++ b/tests/javascripts/liveSearch.test.js @@ -132,7 +132,7 @@ describe('Live search', () => { test("If there is no search term, the results should be unchanged", () => { // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); const listItems = list.querySelectorAll('.user-list-item'); const listItemsShowing = Array.from(listItems).filter(item => !item.classList.contains('js-hidden')); @@ -147,7 +147,7 @@ describe('Live search', () => { searchTextbox.value = 'admin'; // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); const listItems = list.querySelectorAll('.user-list-item'); const listItemsShowing = Array.from(listItems).filter(item => !item.classList.contains('js-hidden')); @@ -163,7 +163,7 @@ describe('Live search', () => { searchTextbox.value = 'Administrator (admin@nhs.uk)'; // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); const listItems = list.querySelectorAll('.user-list-item'); const listItemsShowing = Array.from(listItems).filter(item => !item.classList.contains('js-hidden')); @@ -179,7 +179,7 @@ describe('Live search', () => { searchTextbox.value = "Add and edit templates"; // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); const listItems = list.querySelectorAll('.user-list-item'); const listItemsShowing = Array.from(listItems).filter(item => !item.classList.contains('js-hidden')); @@ -200,7 +200,7 @@ describe('Live search', () => { searchTextbox.value = 'Admin'; // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); // simulate input of new search text searchTextbox.value = ''; @@ -218,7 +218,7 @@ describe('Live search', () => { searchTextbox.value = 'Admin'; // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); // simulate input of new search text searchTextbox.value = 'Administrator'; @@ -237,7 +237,7 @@ describe('Live search', () => { searchTextbox.value = 'Admin'; // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); // simulate input of new search text searchTextbox.value = 'Administrator (admin@nhs.uk)'; @@ -256,7 +256,7 @@ describe('Live search', () => { searchTextbox.value = "Admin"; // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); // simulate input of new search text searchTextbox.value = 'Add and edit templates'; diff --git a/tests/javascripts/loginAlert.test.js b/tests/javascripts/loginAlert.test.js index 085434b6a..6ca170437 100644 --- a/tests/javascripts/loginAlert.test.js +++ b/tests/javascripts/loginAlert.test.js @@ -13,7 +13,7 @@ beforeAll(() => { ` const sessionTimerModule = require('../../app/assets/javascripts/loginAlert.js'); - window.GOVUK.modules.start(); + window.NotifyModules.start(); }); jest.useFakeTimers(); diff --git a/tests/javascripts/radioSelect.test.js b/tests/javascripts/radioSelect.test.js index c0d97f703..652e54b9b 100644 --- a/tests/javascripts/radioSelect.test.js +++ b/tests/javascripts/radioSelect.test.js @@ -127,7 +127,7 @@ describe('RadioSelect', () => { // default is for it to be set to true // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); expect(document.querySelectorAll('.radio-select__column').length).toEqual(2); @@ -138,7 +138,7 @@ describe('RadioSelect', () => { document.querySelector('.radio-select').setAttribute('data-show-now-as-default', 'false'); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); expect(document.querySelectorAll('.radio-select__column').length).toEqual(1); @@ -153,7 +153,7 @@ describe('RadioSelect', () => { beforeEach(() => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); categoryButtons = document.querySelectorAll('.radio-select__column:nth-child(2) .radio-select__button--category'); @@ -201,7 +201,7 @@ describe('RadioSelect', () => { originalOptionsForCategory = originalOptionsForAllCategories.filter(option => categoryRegExp.test(option.label)); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); clickButtonForCategory(category); @@ -236,7 +236,7 @@ describe('RadioSelect', () => { test(`clicking the button for a category should add a 'Done' button below its options`, () => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); clickButtonForCategory(CATEGORIES[0]); @@ -255,7 +255,7 @@ describe('RadioSelect', () => { beforeEach(() => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); clickButtonForCategory(CATEGORIES[0]); diff --git a/tests/javascripts/stick-to-window-when-scrolling.test.js b/tests/javascripts/stick-to-window-when-scrolling.test.js index f75ee067f..a73a0c9eb 100644 --- a/tests/javascripts/stick-to-window-when-scrolling.test.js +++ b/tests/javascripts/stick-to-window-when-scrolling.test.js @@ -140,7 +140,7 @@ describe("Stick to top/bottom of window when scrolling", () => { document.body.innerHTML = ''; - window.GOVUK.stickAtTopWhenScrolling.clearEvents(); + window.NotifyModules.stickAtTopWhenScrolling.clearEvents(); screenMock.reset(); @@ -150,7 +150,7 @@ describe("Stick to top/bottom of window when scrolling", () => { // scroll position defaults to 0, element top defaults to 138px - window.GOVUK.stickAtTopWhenScrolling.init(); + window.NotifyModules.stickAtTopWhenScrolling.init(); expect(inputForm.classList.contains('content-fixed-onload')).toBe(false); expect(inputForm.classList.contains('content-fixed')).toBe(false); @@ -167,7 +167,7 @@ describe("Stick to top/bottom of window when scrolling", () => { // scroll past top of form screenMock.scrollTo(inputForm.offsetTop + 10); - window.GOVUK.stickAtTopWhenScrolling.init(); + window.NotifyModules.stickAtTopWhenScrolling.init(); expect(inputForm.classList.contains('content-fixed')).toBe(false); expect(inputForm.classList.contains('content-fixed-onload')).toBe(false); // check the class for onload isn't applied @@ -181,7 +181,7 @@ describe("Stick to top/bottom of window when scrolling", () => { // scroll past the top of the form screenMock.scrollTo(inputForm.offsetTop + 10); - window.GOVUK.stickAtTopWhenScrolling.init(); + window.NotifyModules.stickAtTopWhenScrolling.init(); }); @@ -215,7 +215,7 @@ describe("Stick to top/bottom of window when scrolling", () => { // scroll past the furthest point screenMock.scrollTo(furthestTopPoint + 10); - window.GOVUK.stickAtTopWhenScrolling.init(); + window.NotifyModules.stickAtTopWhenScrolling.init(); // `.content-fixed-onload` adds the drop-shadow without fading in to show it did not become sticky from user interaction expect(inputForm.classList.contains('content-fixed-onload')).toBe(true); @@ -232,7 +232,7 @@ describe("Stick to top/bottom of window when scrolling", () => { beforeEach(() => { // default scroll position is above top of form - window.GOVUK.stickAtTopWhenScrolling.init(); + window.NotifyModules.stickAtTopWhenScrolling.init(); }); @@ -275,7 +275,7 @@ describe("Stick to top/bottom of window when scrolling", () => { // scroll past top of form screenMock.scrollTo(inputForm.offsetTop + 10); - window.GOVUK.stickAtTopWhenScrolling.init(); + window.NotifyModules.stickAtTopWhenScrolling.init(); }); @@ -315,7 +315,7 @@ describe("Stick to top/bottom of window when scrolling", () => { // move the sticky over the link. It's 168px high so this position will cause it to overlap. screenMock.scrollTo(link.offsetTop - 140); - window.GOVUK.stickAtTopWhenScrolling.init(); + window.NotifyModules.stickAtTopWhenScrolling.init(); }); @@ -336,7 +336,7 @@ describe("Stick to top/bottom of window when scrolling", () => { expect(stickyPosition.top).toBeLessThanOrEqual(link.offsetTop); expect(stickyPosition.bottom).toBeGreaterThanOrEqual(linkBottom); - window.GOVUK.stickAtTopWhenScrolling.scrollToRevealElement(link); + window.NotifyModules.stickAtTopWhenScrolling.scrollToRevealElement(link); stickyPosition = getStickyGroupPosition(screenMock, { stickyEls: [inputForm], edge: 'top' }); @@ -370,7 +370,7 @@ describe("Stick to top/bottom of window when scrolling", () => { // move the sticky over the checkbox. It's 168px high so this position will cause it to overlap. screenMock.scrollTo(checkbox.offsetTop - 10); - window.GOVUK.stickAtTopWhenScrolling.init(); + window.NotifyModules.stickAtTopWhenScrolling.init(); }); @@ -439,7 +439,7 @@ describe("Stick to top/bottom of window when scrolling", () => { // update inputForm position as DOM normally would inputForm.offsetTop = screenMock.window.top; - window.GOVUK.stickAtTopWhenScrolling.init(); + window.NotifyModules.stickAtTopWhenScrolling.init(); }); @@ -522,7 +522,7 @@ describe("Stick to top/bottom of window when scrolling", () => { const inputFormBottom = getScreenItemBottomPosition(inputForm); // set mode to 'dialog' so sticky elements are treated as one item - window.GOVUK.stickAtTopWhenScrolling.setMode('dialog') + window.NotifyModules.stickAtTopWhenScrolling.setMode('dialog') // add another sticky element before the form footer radios = helpers.getRadioGroup({ @@ -567,7 +567,7 @@ describe("Stick to top/bottom of window when scrolling", () => { afterEach(() => { - window.GOVUK.stickAtTopWhenScrolling.setMode('default'); + window.NotifyModules.stickAtTopWhenScrolling.setMode('default'); }); @@ -578,7 +578,7 @@ describe("Stick to top/bottom of window when scrolling", () => { // scroll past top of first sticky element screenMock.scrollTo(inputForm.offsetTop + 10); - window.GOVUK.stickAtTopWhenScrolling.init(); + window.NotifyModules.stickAtTopWhenScrolling.init(); }); @@ -623,7 +623,7 @@ describe("Stick to top/bottom of window when scrolling", () => { // scroll past top of first sticky element screenMock.scrollTo(furthestTopPoint + 10); - window.GOVUK.stickAtTopWhenScrolling.init(); + window.NotifyModules.stickAtTopWhenScrolling.init(); }); @@ -684,7 +684,7 @@ describe("Stick to top/bottom of window when scrolling", () => { screenMock.scrollTo(inputForm.offsetTop + 10); - window.GOVUK.stickAtTopWhenScrolling.init() + window.NotifyModules.stickAtTopWhenScrolling.init() }); @@ -804,7 +804,7 @@ describe("Stick to top/bottom of window when scrolling", () => { afterEach(() => { - window.GOVUK.stickAtBottomWhenScrolling.clearEvents(); + window.NotifyModules.stickAtBottomWhenScrolling.clearEvents(); }); @@ -815,7 +815,7 @@ describe("Stick to top/bottom of window when scrolling", () => { // scroll so the bottom of the window goes past the bottom of the element screenMock.scrollTo((pageFooterBottom - windowHeight) + 10); - window.GOVUK.stickAtBottomWhenScrolling.init(); + window.NotifyModules.stickAtBottomWhenScrolling.init(); // `.content-fixed-onload` adds the drop-shadow without fading in to show it did not become sticky from user interaction expect(pageFooter.classList.contains('content-fixed-onload')).toBe(false); @@ -835,7 +835,7 @@ describe("Stick to top/bottom of window when scrolling", () => { // scroll past top of form screenMock.scrollTo(pageFooterBottom - 10); - window.GOVUK.stickAtTopWhenScrolling.init(); + window.NotifyModules.stickAtTopWhenScrolling.init(); expect(pageFooter.classList.contains('content-fixed')).toBe(false); expect(pageFooter.classList.contains('content-fixed-onload')).toBe(false); // check the class for onload isn't applied @@ -847,7 +847,7 @@ describe("Stick to top/bottom of window when scrolling", () => { beforeEach(() => { // scroll position defaults to 0 so bottom of window starts at 940px. Element bottom defaults to 1160px. - window.GOVUK.stickAtBottomWhenScrolling.init(); + window.NotifyModules.stickAtBottomWhenScrolling.init(); }); @@ -883,7 +883,7 @@ describe("Stick to top/bottom of window when scrolling", () => { // scroll the window bottom past the furthest point screenMock.scrollTo((furthestBottomPoint - windowHeight) - 10); - window.GOVUK.stickAtBottomWhenScrolling.init(); + window.NotifyModules.stickAtBottomWhenScrolling.init(); // `.content-fixed-onload` adds the drop-shadow without fading in to show it did not become sticky from user interaction expect(pageFooter.classList.contains('content-fixed-onload')).toBe(true); @@ -911,7 +911,7 @@ describe("Stick to top/bottom of window when scrolling", () => { // scroll to just below the element screenMock.scrollTo((pageFooterBottom - windowHeight) + 10); - window.GOVUK.stickAtBottomWhenScrolling.init(); + window.NotifyModules.stickAtBottomWhenScrolling.init(); }); @@ -982,7 +982,7 @@ describe("Stick to top/bottom of window when scrolling", () => { // move the sticky over the link. It's 50px high so this position will cause it to overlap. screenMock.scrollTo((linkBottom - windowHeight) + 5); - window.GOVUK.stickAtBottomWhenScrolling.init(); + window.NotifyModules.stickAtBottomWhenScrolling.init(); }); @@ -1003,7 +1003,7 @@ describe("Stick to top/bottom of window when scrolling", () => { expect(stickyPosition.top).toBeLessThanOrEqual(link.offsetTop); expect(stickyPosition.bottom).toBeGreaterThanOrEqual(linkBottom); - window.GOVUK.stickAtBottomWhenScrolling.scrollToRevealElement(link) + window.NotifyModules.stickAtBottomWhenScrolling.scrollToRevealElement(link) stickyPosition = getStickyGroupPosition(screenMock, { stickyEls: [pageFooter], edge: 'bottom' }); @@ -1031,7 +1031,7 @@ describe("Stick to top/bottom of window when scrolling", () => { offsetTop: pageFooter.offsetTop }; - window.GOVUK.stickAtBottomWhenScrolling.init(); + window.NotifyModules.stickAtBottomWhenScrolling.init(); // add mock for shim pageFooterShim = document.querySelector('.shim'); @@ -1103,7 +1103,7 @@ describe("Stick to top/bottom of window when scrolling", () => { // move the sticky over the checkbox. It's 50px high so this position will cause it to overlap. screenMock.scrollTo((checkboxBottom - windowHeight) + 5); - window.GOVUK.stickAtBottomWhenScrolling.init(); + window.NotifyModules.stickAtBottomWhenScrolling.init(); }); @@ -1171,7 +1171,7 @@ describe("Stick to top/bottom of window when scrolling", () => { // update content position as DOM normally would pageFooter.offsetTop = screenMock.window.bottom - pageFooter.offsetHeight; - window.GOVUK.stickAtBottomWhenScrolling.init(); + window.NotifyModules.stickAtBottomWhenScrolling.init(); }); @@ -1251,7 +1251,7 @@ describe("Stick to top/bottom of window when scrolling", () => { const contentBottom = getScreenItemBottomPosition(content); // set mode to 'dialog' so sticky elements are treated as one item - window.GOVUK.stickAtBottomWhenScrolling.setMode('dialog') + window.NotifyModules.stickAtBottomWhenScrolling.setMode('dialog') // add another sticky element before the form footer radios = helpers.getRadioGroup({ @@ -1294,7 +1294,7 @@ describe("Stick to top/bottom of window when scrolling", () => { afterEach(() => { - window.GOVUK.stickAtBottomWhenScrolling.setMode('default'); + window.NotifyModules.stickAtBottomWhenScrolling.setMode('default'); }); @@ -1309,7 +1309,7 @@ describe("Stick to top/bottom of window when scrolling", () => { // scroll to just above the element screenMock.scrollTo((pageFooterBottom - windowHeight) - 10); - window.GOVUK.stickAtBottomWhenScrolling.init(); + window.NotifyModules.stickAtBottomWhenScrolling.init(); }); @@ -1352,7 +1352,7 @@ describe("Stick to top/bottom of window when scrolling", () => { screenMock.scrollTo((furthestBottomPoint - windowHeight) - 10) - window.GOVUK.stickAtBottomWhenScrolling.init(); + window.NotifyModules.stickAtBottomWhenScrolling.init(); }); @@ -1414,7 +1414,7 @@ describe("Stick to top/bottom of window when scrolling", () => { screenMock.scrollTo((pageFooterBottom - windowHeight) - 10); - window.GOVUK.stickAtBottomWhenScrolling.init() + window.NotifyModules.stickAtBottomWhenScrolling.init() }); diff --git a/tests/javascripts/support/jest.setup.js b/tests/javascripts/support/jest.setup.js index 60c29d760..59a5dee29 100644 --- a/tests/javascripts/support/jest.setup.js +++ b/tests/javascripts/support/jest.setup.js @@ -11,4 +11,20 @@ global.io = jest.fn().mockReturnValue({ }); // Load module code -require('govuk_frontend_toolkit/javascripts/govuk/modules.js'); +global.window = global.window || {}; +global.window.NotifyModules = global.window.NotifyModules || {}; +global.window.NotifyModules.start = function() { + var modules = document.querySelectorAll('[data-module]'); + modules.forEach(function(element) { + var moduleName = element.getAttribute('data-module'); + var moduleStarted = element.getAttribute('data-module-started'); + + if (!moduleStarted && global.window.NotifyModules[moduleName]) { + var module = new global.window.NotifyModules[moduleName](); + if (module.start) { + module.start(element); + } + element.setAttribute('data-module-started', 'true'); + } + }); +}; diff --git a/tests/javascripts/support/setup.js b/tests/javascripts/support/setup.js index 8aca90791..a07575440 100644 --- a/tests/javascripts/support/setup.js +++ b/tests/javascripts/support/setup.js @@ -8,4 +8,5 @@ require('./polyfills.js'); global.$ = global.jQuery = require('jquery'); // Bring in the GOV.UK modules system -require('govuk_frontend_toolkit/javascripts/govuk/modules.js'); +// Removing - now using NotifyModules instead +// require('govuk_frontend_toolkit/javascripts/govuk/modules.js'); diff --git a/tests/javascripts/templateFolderForm.test.js b/tests/javascripts/templateFolderForm.test.js index d12a40748..69c237ce7 100644 --- a/tests/javascripts/templateFolderForm.test.js +++ b/tests/javascripts/templateFolderForm.test.js @@ -132,8 +132,8 @@ function setFixtures (hierarchy, newTemplateDataModules = "") { function resetStickyMocks () { - GOVUK.stickAtBottomWhenScrolling.recalculate.mockClear(); - GOVUK.stickAtBottomWhenScrolling.setMode.mockClear(); + window.NotifyModules.stickAtBottomWhenScrolling.recalculate.mockClear(); + window.NotifyModules.stickAtBottomWhenScrolling.setMode.mockClear(); }; @@ -192,7 +192,7 @@ describe('TemplateFolderForm', () => { beforeAll(() => { // stub out calls to sticky JS - GOVUK.stickAtBottomWhenScrolling = { + window.NotifyModules.stickAtBottomWhenScrolling = { setMode: jest.fn(), recalculate: jest.fn() }; @@ -201,7 +201,7 @@ describe('TemplateFolderForm', () => { afterAll(() => { - GOVUK.stickAtBottomWhenScrolling = undefined; + window.NotifyModules.stickAtBottomWhenScrolling = undefined; }); @@ -252,7 +252,7 @@ describe('TemplateFolderForm', () => { beforeEach(() => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); formControls = templateFolderForm.querySelector('#sticky_template_forms'); visibleCounter = getVisibleCounter(); @@ -307,7 +307,7 @@ describe('TemplateFolderForm', () => { templateFolderForm = document.querySelector('form[data-module=template-folder-form]'); // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); formControls = templateFolderForm.querySelector('#sticky_template_forms'); @@ -339,7 +339,7 @@ describe('TemplateFolderForm', () => { beforeEach(() => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); formControls = templateFolderForm.querySelector('#sticky_template_forms'); @@ -449,7 +449,7 @@ describe('TemplateFolderForm', () => { beforeEach(() => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); formControls = templateFolderForm.querySelector('#sticky_template_forms'); @@ -513,7 +513,7 @@ describe('TemplateFolderForm', () => { expect(formControls.querySelector('#add_new_folder_form .js-stick-at-bottom-when-scrolling')).not.toBeNull(); // .recalculate should have been called so the sticky JS picks up the controls - expect(GOVUK.stickAtBottomWhenScrolling.recalculate.mock.calls.length).toEqual(0); + expect(window.NotifyModules.stickAtBottomWhenScrolling.recalculate.mock.calls.length).toEqual(0); }); @@ -552,7 +552,7 @@ describe('TemplateFolderForm', () => { beforeEach(() => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); templateFolderCheckboxes = getTemplateFolderCheckboxes(); @@ -836,7 +836,7 @@ describe('TemplateFolderForm', () => { beforeEach(() => { // start module - window.GOVUK.modules.start(); + window.NotifyModules.start(); templateFolderCheckboxes = getTemplateFolderCheckboxes(); visibleCounterText = getVisibleCounter().textContent.trim(); diff --git a/tests/javascripts/timeoutPopup.test.js b/tests/javascripts/timeoutPopup.test.js index b1329c035..1afea4b86 100644 --- a/tests/javascripts/timeoutPopup.test.js +++ b/tests/javascripts/timeoutPopup.test.js @@ -35,7 +35,7 @@ beforeAll(() => { `; const sessionTimerModule = require('../../app/assets/javascripts/timeoutPopup.js'); - window.GOVUK.modules.start(); + window.NotifyModules.start(); }); afterAll(() => { @@ -73,7 +73,7 @@ describe('The session timer ', () => { const restore = mockWindowLocation(); // Test the actual function, not a mock - const signoutUserMethod = window.GOVUK.Modules.TimeoutPopup.signoutUser; + const signoutUserMethod = window.NotifyModules.TimeoutPopup.signoutUser; // This will try to set location.href but our mock will catch it expect(() => signoutUserMethod()).not.toThrow(); @@ -85,7 +85,7 @@ describe('The session timer ', () => { const restore = mockWindowLocation(); // Test the actual function, not a mock - const expireUserSessionMethod = window.GOVUK.Modules.TimeoutPopup.expireUserSession; + const expireUserSessionMethod = window.NotifyModules.TimeoutPopup.expireUserSession; // This will try to set location.href but our mock will catch it expect(() => expireUserSessionMethod()).not.toThrow(); @@ -97,7 +97,7 @@ describe('The session timer ', () => { const restore = mockWindowLocation(); // Test the actual function, not a mock - const extendSessionMethod = window.GOVUK.Modules.TimeoutPopup.extendSession; + const extendSessionMethod = window.NotifyModules.TimeoutPopup.extendSession; // This will try to call location.reload but our mock will catch it expect(() => extendSessionMethod()).not.toThrow(); @@ -111,7 +111,7 @@ describe('The session timer ', () => { const showTimerMock = jest.spyOn(sessionTimer, 'showModal'); - window.GOVUK.Modules.TimeoutPopup.showTimer(); + window.NotifyModules.TimeoutPopup.showTimer(); expect(showTimerMock).toHaveBeenCalled(); }); @@ -122,17 +122,17 @@ describe('The session timer ', () => { const closeTimerMock = jest.spyOn(sessionTimer, 'close'); - window.GOVUK.Modules.TimeoutPopup.closeTimer(); + window.NotifyModules.TimeoutPopup.closeTimer(); expect(closeTimerMock).toHaveBeenCalled(); }); test('checkTimer is called', () => { const checkTimerMock = jest.spyOn( - window.GOVUK.Modules.TimeoutPopup, + window.NotifyModules.TimeoutPopup, 'checkTimer' ); - window.GOVUK.Modules.TimeoutPopup.checkTimer(); + window.NotifyModules.TimeoutPopup.checkTimer(); expect(checkTimerMock).toHaveBeenCalled(); }); }); diff --git a/tests/javascripts/updateContent.test.js b/tests/javascripts/updateContent.test.js index f8229f63a..6100b1b0f 100644 --- a/tests/javascripts/updateContent.test.js +++ b/tests/javascripts/updateContent.test.js @@ -30,10 +30,10 @@ beforeAll(() => { $.ajax.mockImplementation(() => jqueryAJAXReturnObj); - // RollupJS assigns our bundled module code, including morphdom, to window.GOVUK. + // RollupJS assigns our bundled module code, including morphdom, to window.NotifyModules. // morphdom is assigned to its vendor property so we need to copy that here for the updateContent // code to pick it up. - window.GOVUK.vendor = { + window.NotifyModules.vendor = { morphdom: require('morphdom') }; require('../../app/assets/javascripts/updateContent.js'); @@ -68,7 +68,7 @@ describe('Update content', () => { beforeEach(() => { // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); }); @@ -115,7 +115,7 @@ describe('Update content', () => { [24000, 10000], ]).test('It calculates a delay of %dms if the API responds in %dms', (waitTime, responseTime) => { expect( - window.GOVUK.Modules.UpdateContent.calculateBackoff(responseTime) + window.NotifyModules['update-content'].calculateBackoff(responseTime) ).toBe( waitTime ); @@ -138,7 +138,7 @@ describe('Update content', () => { document.querySelector('[data-module=update-content]').setAttribute('data-form', 'service'); // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); }); @@ -225,7 +225,7 @@ describe('Update content', () => { responseObj[updateKey] = getPartial(partialData); // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); expect(document.querySelector('.ajax-block-container').parentNode.hasAttribute('data-resource')).toBe(false); @@ -237,7 +237,7 @@ describe('Update content', () => { responseObj[updateKey] = getPartial(partialData); // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); jest.advanceTimersByTime(2000); expect($.ajax.mock.calls[0][0]).toEqual(resourceURL); @@ -250,7 +250,7 @@ describe('Update content', () => { responseObj[updateKey] = getPartial(partialData); // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); jest.advanceTimersByTime(2000); // check a sample DOM node is unchanged @@ -266,7 +266,7 @@ describe('Update content', () => { responseObj[updateKey] = getPartial(partialData); // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); jest.advanceTimersByTime(2000); // check the right DOM node is updated @@ -349,7 +349,7 @@ describe('Update content', () => { responseObj[updateKey] = getPartial(partialData); // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); jest.advanceTimersByTime(2000); // check it has the same number of items @@ -376,7 +376,7 @@ describe('Update content', () => { responseObj[updateKey] = getPartial(partialData); // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); jest.advanceTimersByTime(2000); // check the node has been added @@ -408,7 +408,7 @@ describe('Update content', () => { responseObj[updateKey] = getPartial(partialData); // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); jest.advanceTimersByTime(2000); // check the node has been removed @@ -442,7 +442,7 @@ describe('Update content', () => { responseObj[updateKey] = getPartial(partialData); // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); jest.advanceTimersByTime(2000); // check the class is still there @@ -480,7 +480,7 @@ describe('Update content', () => { responseObj[updateKey] = getPartial(partialData); // start the module - window.GOVUK.modules.start(); + window.NotifyModules.start(); jest.advanceTimersByTime(2000); // re-select in case nodes in partialsInPage have changed diff --git a/tests/javascripts/updateStatus.test.js b/tests/javascripts/updateStatus.test.js index 7ebf0d8e3..ef01ca10e 100644 --- a/tests/javascripts/updateStatus.test.js +++ b/tests/javascripts/updateStatus.test.js @@ -68,7 +68,7 @@ describe('Update content', () => { test("It should add attributes to the elements", () => { - window.GOVUK.modules.start(); + window.NotifyModules.start(); expect( document.querySelectorAll('[data-module=update-status]')[0].id @@ -88,7 +88,7 @@ describe('Update content', () => { document.getElementById('template_content').removeAttribute('aria-describedby'); - window.GOVUK.modules.start(); + window.NotifyModules.start(); expect( document.getElementById('template_content').getAttribute('aria-describedby') @@ -100,7 +100,7 @@ describe('Update content', () => { test("It should make requests to the URL specified in the data-updates-url attribute", () => { - window.GOVUK.modules.start(); + window.NotifyModules.start(); expect($.ajax.mock.calls[0][0]).toEqual(updatesURL); expect($.ajax.mock.calls[0]).toEqual([ @@ -123,7 +123,7 @@ describe('Update content', () => { "Initial content" ); - window.GOVUK.modules.start(); + window.NotifyModules.start(); expect( document.querySelectorAll('[data-module=update-status]')[0].textContent.trim() @@ -138,7 +138,7 @@ describe('Update content', () => { let textarea = document.getElementById('template_content'); // Initial update triggered - window.GOVUK.modules.start(); + window.NotifyModules.start(); expect($.ajax.mock.calls.length).toEqual(1); // 150ms of inactivity @@ -154,7 +154,7 @@ describe('Update content', () => { let textarea = document.getElementById('template_content'); // Initial update triggered - window.GOVUK.modules.start(); + window.NotifyModules.start(); expect($.ajax.mock.calls.length).toEqual(1); helpers.triggerEvent(textarea, 'input');