mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-04-29 05:30:48 -04:00
Add JS tests for analytics & cookies JS
Includes: - tests for the analytics interface ported from GOVUK Frontend Toolkit - tests for the cookie banner that appears on all pages except the cookies page - tests for the cookies page JS - tests for the hasConsentFor function - adding a deleteCookie helper to remove cookies during tests - polyfill for insertAdjacentText The last one is because JSDOM doesn't support insertAdjacentText but our target browsers do. This polyfill also includes one for insertAdjacentHTML.
This commit is contained in:
97
tests/javascripts/analytics/analytics.test.js
Normal file
97
tests/javascripts/analytics/analytics.test.js
Normal file
@@ -0,0 +1,97 @@
|
||||
const helpers = require('../support/helpers');
|
||||
|
||||
beforeAll(() => {
|
||||
|
||||
// add the script GA looks for in the document
|
||||
document.body.appendChild(document.createElement('script'));
|
||||
|
||||
require('../../../app/assets/javascripts/govuk/cookie-functions.js');
|
||||
require('../../../app/assets/javascripts/analytics/analytics.js');
|
||||
require('../../../app/assets/javascripts/analytics/init.js');
|
||||
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
|
||||
require('../support/teardown.js');
|
||||
|
||||
});
|
||||
|
||||
describe("Analytics", () => {
|
||||
|
||||
let analytics;
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
window.ga = jest.fn();
|
||||
|
||||
analytics = new GOVUK.Analytics({
|
||||
trackingId: 'UA-75215134-1',
|
||||
cookieDomain: 'auto',
|
||||
anonymizeIp: true,
|
||||
displayFeaturesTask: null,
|
||||
transport: 'beacon'
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
|
||||
window.ga.mockReset();
|
||||
|
||||
});
|
||||
|
||||
describe("When created", () => {
|
||||
|
||||
test("It configures a tracker", () => {
|
||||
|
||||
setUpArguments = window.ga.mock.calls;
|
||||
|
||||
expect(setUpArguments[0]).toEqual(['create', 'UA-75215134-1', 'auto']);
|
||||
expect(setUpArguments[1]).toEqual(['set', 'anonymizeIp', true]);
|
||||
expect(setUpArguments[2]).toEqual(['set', 'displayFeaturesTask', null]);
|
||||
expect(setUpArguments[3]).toEqual(['set', 'transport', 'beacon']);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe("When tracking pageviews", () => {
|
||||
|
||||
test("It sends the right URL for the page if no arguments", () => {
|
||||
|
||||
window.ga.mockClear();
|
||||
|
||||
jest.spyOn(window, 'location', 'get').mockImplementation(() => {
|
||||
return {
|
||||
'pathname': '/privacy',
|
||||
'search': ''
|
||||
};
|
||||
});
|
||||
|
||||
analytics.trackPageview();
|
||||
|
||||
expect(window.ga.mock.calls[0]).toEqual(['send', 'pageview', '/privacy']);
|
||||
|
||||
});
|
||||
|
||||
test("It strips the UUIDs from URLs", () => {
|
||||
|
||||
window.ga.mockClear();
|
||||
|
||||
jest.spyOn(window, 'location', 'get').mockImplementation(() => {
|
||||
return {
|
||||
'pathname': '/services/6658542f-0cad-491f-bec8-ab8457700ead',
|
||||
'search': ''
|
||||
};
|
||||
});
|
||||
|
||||
analytics.trackPageview();
|
||||
|
||||
expect(window.ga.mock.calls[0]).toEqual(['send', 'pageview', '/services/…']);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
123
tests/javascripts/analytics/init.test.js
Normal file
123
tests/javascripts/analytics/init.test.js
Normal file
@@ -0,0 +1,123 @@
|
||||
const helpers = require('../support/helpers');
|
||||
|
||||
beforeAll(() => {
|
||||
|
||||
// add the script GA looks for in the document
|
||||
document.body.appendChild(document.createElement('script'));
|
||||
|
||||
require('../../../app/assets/javascripts/govuk/cookie-functions.js');
|
||||
require('../../../app/assets/javascripts/analytics/analytics.js');
|
||||
require('../../../app/assets/javascripts/analytics/init.js');
|
||||
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
|
||||
require('../support/teardown.js');
|
||||
|
||||
});
|
||||
|
||||
describe("Analytics init", () => {
|
||||
|
||||
beforeAll(() => {
|
||||
|
||||
window.ga = jest.fn();
|
||||
jest.spyOn(window.GOVUK.Analytics, 'load');
|
||||
|
||||
// pretend we're on the /privacy page
|
||||
jest.spyOn(window, 'location', 'get').mockImplementation(() => {
|
||||
return {
|
||||
'pathname': '/privacy',
|
||||
'search': ''
|
||||
};
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
|
||||
window.GOVUK.Analytics.load.mockClear();
|
||||
window.ga.mockClear();
|
||||
|
||||
});
|
||||
|
||||
test("After the init.js script has been loaded, Google Analytics will be disabled", () => {
|
||||
|
||||
expect(window['ga-disable-UA-26179049-1']).toBe(true);
|
||||
|
||||
});
|
||||
|
||||
describe("If initAnalytics has already been called", () => {
|
||||
|
||||
beforeAll(() => {
|
||||
|
||||
// Fake a tracker instance
|
||||
window.GOVUK.analytics = {};
|
||||
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
window.GOVUK.initAnalytics();
|
||||
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
|
||||
delete window.GOVUK.analytics;
|
||||
|
||||
});
|
||||
|
||||
test("The Google Analytics libraries will not be loaded", () => {
|
||||
|
||||
expect(window.GOVUK.Analytics.load).not.toHaveBeenCalled();
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe("If initAnalytics has not been called", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
window.GOVUK.initAnalytics();
|
||||
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
|
||||
// window.GOVUK.initAnalytics sets up a new window.GOVUK.analytics which needs clearing
|
||||
delete window.GOVUK.analytics;
|
||||
|
||||
});
|
||||
|
||||
test("Google Analytics will not be disabled", () => {
|
||||
|
||||
expect(window['ga-disable-UA-26179049-1']).toBe(false);
|
||||
|
||||
});
|
||||
|
||||
test("The Google Analytics libraries will have been loaded", () => {
|
||||
|
||||
expect(window.GOVUK.Analytics.load).toHaveBeenCalled();
|
||||
|
||||
});
|
||||
|
||||
test("There will be an interface with the Google Analytics API", () => {
|
||||
|
||||
expect(window.GOVUK.analytics).toBeDefined();
|
||||
|
||||
});
|
||||
|
||||
test("A pageview will be registered", () => {
|
||||
|
||||
expect(window.ga.mock.calls.length).toEqual(5);
|
||||
|
||||
// The first 4 calls configure the analytics tracker. All subsequent calls send data
|
||||
expect(window.ga.mock.calls[4]).toEqual(['send', 'pageview', '/privacy']);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
55
tests/javascripts/consent.test.js
Normal file
55
tests/javascripts/consent.test.js
Normal file
@@ -0,0 +1,55 @@
|
||||
const helpers = require('./support/helpers');
|
||||
|
||||
beforeAll(() => {
|
||||
|
||||
require('../../app/assets/javascripts/govuk/cookie-functions.js');
|
||||
require('../../app/assets/javascripts/consent.js');
|
||||
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
|
||||
require('./support/teardown.js');
|
||||
|
||||
});
|
||||
|
||||
describe("Cookie consent", () => {
|
||||
|
||||
describe("hasConsentFor", () => {
|
||||
|
||||
afterEach(() => {
|
||||
|
||||
// remove cookie set by tests
|
||||
helpers.deleteCookie('cookies_policy');
|
||||
|
||||
});
|
||||
|
||||
test("If there is no consent cookie, return false", () => {
|
||||
|
||||
expect(window.GOVUK.hasConsentFor('analytics')).toBe(false);
|
||||
|
||||
});
|
||||
|
||||
describe("If a consent cookie is set", () => {
|
||||
|
||||
test("If the category is not saved in the cookie, return false", () => {
|
||||
|
||||
window.GOVUK.setConsentCookie({ 'usage': true });
|
||||
|
||||
expect(window.GOVUK.hasConsentFor('analytics')).toBe(false);
|
||||
|
||||
});
|
||||
|
||||
test("If the category is saved in the cookie, return its value", () => {
|
||||
|
||||
window.GOVUK.setConsentCookie({ 'analytics': true });
|
||||
|
||||
expect(window.GOVUK.hasConsentFor('analytics')).toBe(true);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
230
tests/javascripts/cookieMessage.test.js
Normal file
230
tests/javascripts/cookieMessage.test.js
Normal file
@@ -0,0 +1,230 @@
|
||||
const helpers = require('./support/helpers');
|
||||
|
||||
beforeAll(() => {
|
||||
|
||||
require('../../app/assets/javascripts/govuk/cookie-functions.js');
|
||||
require('../../app/assets/javascripts/analytics/analytics.js');
|
||||
require('../../app/assets/javascripts/analytics/init.js');
|
||||
require('../../app/assets/javascripts/cookieMessage.js');
|
||||
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
|
||||
require('./support/teardown.js');
|
||||
|
||||
});
|
||||
|
||||
describe("Cookie message", () => {
|
||||
|
||||
let cookieMessage;
|
||||
|
||||
beforeAll(() => {
|
||||
|
||||
helpers.deleteCookie('cookies-policy');
|
||||
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
// add the script GA looks for in the document
|
||||
document.body.appendChild(document.createElement('script'));
|
||||
|
||||
jest.spyOn(window.GOVUK, 'initAnalytics');
|
||||
|
||||
cookieMessage = `
|
||||
<div id="global-cookie-message" class="notify-cookie-banner" data-module="cookie-banner" role="region" aria-label="cookie banner" data-nosnippet="">
|
||||
<div class="notify-cookie-banner__wrapper govuk-width-container govuk-!-padding-4">
|
||||
<h2 class="notify-cookie-banner__heading govuk-heading-m">Cookies on GOV.UK Notify</h2>
|
||||
<p class="notify-cookie-banner__message govuk-body">We use <a class="govuk-link" href="/cookies">small files called cookies</a> to make GOV.UK Notify work.</p>
|
||||
<div class="notify-cookie-banner__buttons notify-cookie-banner__no-js">
|
||||
<div class="notify-cookie-banner__button">
|
||||
<a href="/cookies" class="govuk-button notify-cookie-banner-button--inline" role="button" data-accept-cookies="true">Set cookie preferences</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="notify-cookie-banner__with-js">
|
||||
<p class="notify-cookie-banner__message govuk-body">We'd also like to use analytics cookies to help us improve our service.</p>
|
||||
<p class="notify-cookie-banner__message govuk-body">Please let us know if this is OK.</p>
|
||||
<div class="notify-cookie-banner__buttons">
|
||||
<div class="notify-cookie-banner__button notify-cookie-banner__button-accept">
|
||||
<button class="govuk-button notify-cookie-banner-button--secondary notify-cookie-banner-button--inline" type="submit" data-accept-cookies="true">Yes, I accept analytics cookies</button>
|
||||
</div>
|
||||
<div class="notify-cookie-banner__button notify-cookie-banner__button-reject">
|
||||
<button class="govuk-button notify-cookie-banner-button--secondary notify-cookie-banner-button--inline" type="submit" data-accept-cookies="false">No, do not use analytics cookies</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="notify-cookie-banner__confirmation govuk-width-container" tabindex="-1">
|
||||
<p class="notify-cookie-banner__confirmation-message govuk-body">
|
||||
You can <a class="govuk-link" href="/cookies">change your cookie settings</a> at any time.
|
||||
</p>
|
||||
<button class="notify-cookie-banner__hide-button" data-hide-cookie-banner="true">Hide</button>
|
||||
</div>
|
||||
</div>`;
|
||||
|
||||
document.body.innerHTML += cookieMessage;
|
||||
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
|
||||
document.body.innerHTML = '';
|
||||
|
||||
// remove cookie set by tests
|
||||
helpers.deleteCookie('cookies_policy');
|
||||
|
||||
// reset spies
|
||||
window.GOVUK.initAnalytics.mockClear();
|
||||
|
||||
// remove analytics tracker
|
||||
delete window.GOVUK.analytics;
|
||||
|
||||
// reset global variable to state when init.js loaded
|
||||
window['ga-disable-UA-26179049-1'] = true;
|
||||
|
||||
});
|
||||
|
||||
/*
|
||||
Note: If no JS, the cookie banner shows a button to take you to the cookies page for more information.
|
||||
|
||||
This works through CSS, based on the presence of the `js-enabled` class on the <body> so is not tested here.
|
||||
*/
|
||||
|
||||
test("If the cookies set by the old banner still exist, they can be cleared with the `clearOldCookies` method", () => {
|
||||
|
||||
helpers.setCookie('seen_cookie_message', 'true', { 'days': 365 });
|
||||
helpers.setCookie('_ga', 'GA1.1.123.123', { 'days': 365 });
|
||||
helpers.setCookie('_gid', 'GA1.1.456.456', { 'days': 1 });
|
||||
|
||||
window.GOVUK.Modules.CookieBanner.clearOldCookies();
|
||||
|
||||
expect(window.GOVUK.cookie('seen_cookie_message')).toBeNull();
|
||||
expect(window.GOVUK.cookie('_ga')).toBeNull();
|
||||
expect(window.GOVUK.cookie('_gid')).toBeNull();
|
||||
|
||||
});
|
||||
|
||||
test("If user has made a choice to give their consent or not, the cookie banner should be hidden", () => {
|
||||
|
||||
window.GOVUK.setConsentCookie({ 'analytics': false });
|
||||
|
||||
window.GOVUK.modules.start()
|
||||
|
||||
expect(helpers.element(document.querySelector('.notify-cookie-banner')).is('hidden')).toBe(true);
|
||||
|
||||
});
|
||||
|
||||
describe("If user hasn't made a choice to give their consent or not", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
window.GOVUK.modules.start();
|
||||
|
||||
});
|
||||
|
||||
test("The cookie banner should show", () => {
|
||||
|
||||
const banner = helpers.element(document.querySelector('.notify-cookie-banner'));
|
||||
|
||||
expect(banner.is('hidden')).toBe(false);
|
||||
|
||||
});
|
||||
|
||||
test("No analytics should run", () => {
|
||||
|
||||
expect(window.GOVUK.initAnalytics).not.toHaveBeenCalled();
|
||||
|
||||
});
|
||||
|
||||
describe("If the user clicks the button to accept analytics", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
const acceptButton = document.querySelector('.notify-cookie-banner__button-accept button');
|
||||
|
||||
helpers.triggerEvent(acceptButton, 'click');
|
||||
|
||||
});
|
||||
|
||||
test("the banner should confirm your choice and link to the cookies page as a way to change your mind", () => {
|
||||
|
||||
confirmation = helpers.element(document.querySelector('.notify-cookie-banner__confirmation'));
|
||||
|
||||
expect(confirmation.is('hidden')).toBe(false);
|
||||
expect(confirmation.el.textContent.trim()).toEqual(expect.stringMatching(/^You’ve accepted analytics cookies/));
|
||||
|
||||
});
|
||||
|
||||
test("If the user clicks the 'hide' button, the banner should be hidden", () => {
|
||||
|
||||
const hideButton = document.querySelector('.notify-cookie-banner__hide-button');
|
||||
const banner = helpers.element(document.querySelector('.notify-cookie-banner'));
|
||||
|
||||
helpers.triggerEvent(hideButton, 'click');
|
||||
|
||||
expect(banner.is('hidden')).toBe(true);
|
||||
|
||||
});
|
||||
|
||||
test("The consent cookie should be set, with analytics set to 'true'", () => {
|
||||
|
||||
expect(window.GOVUK.getConsentCookie()).toEqual({ 'analytics': true });
|
||||
|
||||
});
|
||||
|
||||
test("The analytics should be set up", () => {
|
||||
|
||||
expect(window.GOVUK.analytics).toBeDefined();
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe("If the user clicks the button to reject analytics", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
const rejectButton = document.querySelector('.notify-cookie-banner__button-reject button');
|
||||
|
||||
helpers.triggerEvent(rejectButton, 'click');
|
||||
|
||||
});
|
||||
|
||||
test("the banner should confirm your choice and link to the cookies page as a way to change your mind", () => {
|
||||
|
||||
confirmation = helpers.element(document.querySelector('.notify-cookie-banner__confirmation'));
|
||||
|
||||
expect(confirmation.is('hidden')).toBe(false);
|
||||
expect(confirmation.el.textContent.trim()).toEqual(expect.stringMatching(/^You told us not to use analytics cookies/));
|
||||
|
||||
});
|
||||
|
||||
test("If the user clicks the 'hide' button, the banner should be hidden", () => {
|
||||
|
||||
const hideButton = document.querySelector('.notify-cookie-banner__hide-button');
|
||||
const banner = helpers.element(document.querySelector('.notify-cookie-banner'));
|
||||
|
||||
helpers.triggerEvent(hideButton, 'click');
|
||||
|
||||
expect(banner.is('hidden')).toBe(true);
|
||||
|
||||
});
|
||||
|
||||
test("The consent cookie should be set, with analytics set to 'true'", () => {
|
||||
|
||||
expect(window.GOVUK.getConsentCookie()).toEqual({ 'analytics': false });
|
||||
|
||||
});
|
||||
|
||||
test("The analytics should not be set up", () => {
|
||||
|
||||
expect(window.GOVUK.analytics).not.toBeDefined();
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
255
tests/javascripts/cookieSettings.test.js
Normal file
255
tests/javascripts/cookieSettings.test.js
Normal file
@@ -0,0 +1,255 @@
|
||||
const helpers = require('./support/helpers');
|
||||
|
||||
beforeAll(() => {
|
||||
|
||||
require('../../app/assets/javascripts/govuk/cookie-functions.js');
|
||||
require('../../app/assets/javascripts/consent.js');
|
||||
require('../../app/assets/javascripts/analytics/analytics.js');
|
||||
require('../../app/assets/javascripts/analytics/init.js');
|
||||
require('../../app/assets/javascripts/cookieSettings.js');
|
||||
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
|
||||
require('./support/teardown.js');
|
||||
|
||||
});
|
||||
|
||||
describe("Cookie settings", () => {
|
||||
|
||||
let cookiesPageContent;
|
||||
let yesRadio;
|
||||
let noRadio;
|
||||
let saveButton;
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
// add the script GA looks for in the document
|
||||
document.body.appendChild(document.createElement('script'));
|
||||
|
||||
window.ga = jest.fn();
|
||||
jest.spyOn(window.GOVUK, 'initAnalytics');
|
||||
|
||||
cookiesPageContent = `
|
||||
<div class="cookie-settings__confirmation banner banner-with-tick" data-cookie-confirmation="true" role="group" tabindex="-1">
|
||||
<h2 class="banner-title">Your cookie settings were saved</h2>
|
||||
<a class="govuk_link cookie-settings__prev-page" href="#" data-module="track-click" data-track-category="cookieSettings" data-track-action="Back to previous page">
|
||||
Go back to the page you were looking at
|
||||
</a>
|
||||
</div>
|
||||
<h1 class="heading-large">Cookies</h1>
|
||||
<p class="summary">
|
||||
Cookies are small files saved on your phone, tablet or computer when you visit a website.
|
||||
</p>
|
||||
<p>We use cookies to make GOV.UK Notify work and collect information about how you use our service.</p>
|
||||
<div class="cookie-settings__no-js">
|
||||
<h2 class="govuk-heading-s govuk-!-margin-top-6">Do you want to accept analytics cookies?</h2>
|
||||
<p>We use Javascript to set most of our cookies. Unfortunately Javascript is not running on your browser, so you cannot change your settings. You can try:</p>
|
||||
<ul class="govuk-list govuk-list--bullet">
|
||||
<li>reloading the page</li>
|
||||
<li>turning on Javascript in your browser</li>
|
||||
</ul>
|
||||
</div>
|
||||
<h2 class="heading-medium">Analytics cookies (optional)</h2>
|
||||
<div class="cookie-settings__form-wrapper">
|
||||
<form data-module="cookie-settings">
|
||||
<div class="govuk-form-group govuk-!-margin-top-6">
|
||||
<fieldset class="govuk-fieldset" aria-describedby="changed-name-hint">
|
||||
<legend class="govuk-fieldset__legend govuk-fieldset__legend--s">
|
||||
Do you want to accept analytics cookies?
|
||||
</legend>
|
||||
<div class="govuk-radios govuk-radios--inline">
|
||||
<div class="govuk-radios__item">
|
||||
<input class="govuk-radios__input" id="cookies-analytics-yes" name="cookies-analytics" type="radio" value="on">
|
||||
<label class="govuk-label govuk-radios__label" for="cookies-analytics-yes">
|
||||
Yes
|
||||
</label>
|
||||
</div>
|
||||
<div class="govuk-radios__item">
|
||||
<input class="govuk-radios__input" id="cookies-analytics-no" name="cookies-analytics" type="radio" value="off">
|
||||
<label class="govuk-label govuk-radios__label" for="cookies-analytics-no">
|
||||
No
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
</div>
|
||||
<button class="govuk-button" type="submit">Save cookie settings</button>
|
||||
</form>
|
||||
</div>`;
|
||||
|
||||
document.body.innerHTML += cookiesPageContent;
|
||||
|
||||
yesRadio = document.querySelector('#cookies-analytics-yes');
|
||||
noRadio = document.querySelector('#cookies-analytics-no');
|
||||
saveButton = document.querySelector('.govuk-button');
|
||||
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
|
||||
document.body.innerHTML = '';
|
||||
|
||||
// remove cookie set by tests
|
||||
helpers.deleteCookie('cookies_policy');
|
||||
|
||||
// reset spies
|
||||
window.ga.mockClear();
|
||||
window.GOVUK.initAnalytics.mockClear();
|
||||
|
||||
// remove analytics tracker
|
||||
delete window.GOVUK.analytics;
|
||||
|
||||
// reset global variable to state when init.js loaded
|
||||
window['ga-disable-UA-26179049-1'] = true;
|
||||
|
||||
});
|
||||
|
||||
/*
|
||||
Note: If no JS, the cookies page contains content to explain why JS is required to set analytics cookies.
|
||||
This is hidden if JS is available when the page loads.
|
||||
|
||||
The message displayed to confirm any selection made is also in the page but hidden on load.
|
||||
|
||||
Both of these work through CSS, based on the presence of the `js-enabled` class on the <body> so are not tested here.
|
||||
*/
|
||||
|
||||
describe("When the page loads", () => {
|
||||
|
||||
test("If user has not chosen to accept or reject analytics, the radios for making that choice should be set to unchecked", () => {
|
||||
|
||||
window.GOVUK.modules.start();
|
||||
|
||||
expect(yesRadio.checked).toBe(false);
|
||||
expect(noRadio.checked).toBe(false);
|
||||
|
||||
});
|
||||
|
||||
test("If analytics are accepted, the radio for 'accept analytics' should be set to checked", () => {
|
||||
|
||||
window.GOVUK.setConsentCookie({ 'analytics': true });
|
||||
|
||||
window.GOVUK.modules.start();
|
||||
|
||||
expect(yesRadio.checked).toBe(true);
|
||||
expect(noRadio.checked).toBe(false);
|
||||
|
||||
});
|
||||
|
||||
test("If analytics are rejected, the radio for 'reject analytics' should be set to checked", () => {
|
||||
|
||||
window.GOVUK.setConsentCookie({ 'analytics': false });
|
||||
|
||||
window.GOVUK.modules.start();
|
||||
|
||||
expect(yesRadio.checked).toBe(false);
|
||||
expect(noRadio.checked).toBe(true);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe("When the 'Save cookie settings' button is clicked", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
window.GOVUK.modules.start();
|
||||
|
||||
});
|
||||
|
||||
test("If no selection is made, set consent to reject analytics", () => {
|
||||
|
||||
helpers.triggerEvent(saveButton, 'click');
|
||||
|
||||
expect(window.GOVUK.getConsentCookie()).toEqual({ 'analytics': false });
|
||||
|
||||
});
|
||||
|
||||
test("If a selection is made, save this as consent", () => {
|
||||
|
||||
yesRadio.checked = true;
|
||||
|
||||
helpers.triggerEvent(saveButton, 'click');
|
||||
|
||||
expect(window.GOVUK.getConsentCookie()).toEqual({ 'analytics': true });
|
||||
|
||||
});
|
||||
|
||||
describe("The message confirming your choice", () => {
|
||||
|
||||
let confirmationMessage;
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
confirmationMessage = document.querySelector('.cookie-settings__confirmation');
|
||||
helpers.triggerEvent(saveButton, 'click');
|
||||
|
||||
});
|
||||
|
||||
test("Should be shown when the 'Save cookie settings' button is clicked", () => {
|
||||
|
||||
expect(helpers.element(confirmationMessage).is('hidden')).toBe(false);
|
||||
|
||||
});
|
||||
|
||||
test("Should include a link to the last page visited, if information on the referrer is available", () => {
|
||||
|
||||
jest.spyOn(document, 'referrer', 'get').mockReturnValue('https://notifications.service.gov.uk/privacy');
|
||||
|
||||
helpers.triggerEvent(saveButton, 'click');
|
||||
|
||||
expect(confirmationMessage.querySelector('.cookie-settings__prev-page').getAttribute('href')).toEqual('/privacy');
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe("Analytics code", () => {
|
||||
|
||||
beforeAll(() => {
|
||||
|
||||
jest.spyOn(window, 'location', 'get').mockImplementation(() => {
|
||||
|
||||
return {
|
||||
'pathname': '/privacy',
|
||||
'search': ''
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
test("if user accepted analytics, the analytics code should initialise and register a pageview", () => {
|
||||
|
||||
window.GOVUK.modules.start();
|
||||
|
||||
yesRadio.checked = true;
|
||||
|
||||
helpers.triggerEvent(saveButton, 'click');
|
||||
|
||||
expect(window.GOVUK.initAnalytics).toHaveBeenCalled();
|
||||
|
||||
expect(window.ga).toHaveBeenCalled();
|
||||
// the first 4 calls are configuration
|
||||
expect(window.ga.mock.calls[4]).toEqual(['send', 'pageview', '/privacy']);
|
||||
|
||||
});
|
||||
|
||||
test("if user rejected analytics, the analytics code should not run", () => {
|
||||
|
||||
window.GOVUK.modules.start();
|
||||
|
||||
noRadio.checked = true;
|
||||
|
||||
helpers.triggerEvent(saveButton, 'click');
|
||||
|
||||
expect(window.GOVUK.initAnalytics).not.toHaveBeenCalled();
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
@@ -1,6 +1,7 @@
|
||||
const globals = require('./helpers/globals.js');
|
||||
const events = require('./helpers/events.js');
|
||||
const domInterfaces = require('./helpers/dom_interfaces.js');
|
||||
const cookies = require('./helpers/cookies.js');
|
||||
const html = require('./helpers/html.js');
|
||||
const elements = require('./helpers/elements.js');
|
||||
const rendering = require('./helpers/rendering.js');
|
||||
@@ -14,6 +15,8 @@ exports.moveSelectionToRadio = events.moveSelectionToRadio;
|
||||
exports.activateRadioWithSpace = events.activateRadioWithSpace;
|
||||
exports.RangeMock = domInterfaces.RangeMock;
|
||||
exports.SelectionMock = domInterfaces.SelectionMock;
|
||||
exports.deleteCookie = cookies.deleteCookie;
|
||||
exports.setCookie = cookies.setCookie;
|
||||
exports.getRadioGroup = html.getRadioGroup;
|
||||
exports.getRadios = html.getRadios;
|
||||
exports.templatesAndFoldersCheckboxes = html.templatesAndFoldersCheckboxes;
|
||||
|
||||
22
tests/javascripts/support/helpers/cookies.js
Normal file
22
tests/javascripts/support/helpers/cookies.js
Normal file
@@ -0,0 +1,22 @@
|
||||
// Helper for deleting a cookie
|
||||
function deleteCookie (cookieName) {
|
||||
|
||||
document.cookie = cookieName + '=; path=/; expires=' + (new Date());
|
||||
|
||||
};
|
||||
|
||||
function setCookie (name, value, options) {
|
||||
if (typeof options === 'undefined') {
|
||||
options = {};
|
||||
}
|
||||
var cookieString = name + '=' + value + '; path=/;domain=' + window.location.hostname;
|
||||
if (options.days) {
|
||||
var date = new Date();
|
||||
date.setTime(date.getTime() + (options.days * 24 * 60 * 60 * 1000));
|
||||
cookieString = cookieString + '; expires=' + date.toGMTString();
|
||||
}
|
||||
document.cookie = cookieString;
|
||||
};
|
||||
|
||||
exports.deleteCookie = deleteCookie;
|
||||
exports.setCookie = setCookie;
|
||||
53
tests/javascripts/support/polyfills.js
Normal file
53
tests/javascripts/support/polyfills.js
Normal file
@@ -0,0 +1,53 @@
|
||||
// Polyfills for any parts of the DOM API available in browsers but not JSDOM
|
||||
|
||||
// From: https://gist.github.com/eligrey/1276030
|
||||
HTMLElement.prototype.insertAdjacentHTML = function(position, html) {
|
||||
"use strict";
|
||||
|
||||
var
|
||||
ref = this
|
||||
, container = ref.ownerDocument.createElementNS("http://www.w3.org/1999/xhtml", "_")
|
||||
, ref_parent = ref.parentNode
|
||||
, node, first_child, next_sibling
|
||||
;
|
||||
|
||||
container.innerHTML = html;
|
||||
|
||||
switch (position.toLowerCase()) {
|
||||
case "beforebegin":
|
||||
while ((node = container.firstChild)) {
|
||||
ref_parent.insertBefore(node, ref);
|
||||
}
|
||||
break;
|
||||
case "afterbegin":
|
||||
first_child = ref.firstChild;
|
||||
while ((node = container.lastChild)) {
|
||||
first_child = ref.insertBefore(node, first_child);
|
||||
}
|
||||
break;
|
||||
case "beforeend":
|
||||
while ((node = container.firstChild)) {
|
||||
ref.appendChild(node);
|
||||
}
|
||||
break;
|
||||
case "afterend":
|
||||
next_sibling = ref.nextSibling;
|
||||
while ((node = container.lastChild)) {
|
||||
next_sibling = ref_parent.insertBefore(node, next_sibling);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// from: https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentText#Polyfill
|
||||
if (!Element.prototype.insertAdjacentText) {
|
||||
Element.prototype.insertAdjacentText = function(type, txt){
|
||||
this.insertAdjacentHTML(
|
||||
type,
|
||||
(txt+'') // convert to string
|
||||
.replace(/&/g, '&') // embed ampersand symbols
|
||||
.replace(/</g, '<') // embed less-than symbols
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,6 @@
|
||||
// Polyfill holes in JSDOM
|
||||
require('./polyfills.js');
|
||||
|
||||
// set up jQuery
|
||||
window.jQuery = require('jquery');
|
||||
$ = window.jQuery;
|
||||
|
||||
Reference in New Issue
Block a user