From 454b778582ed36079654296b9be4a35743866c80 Mon Sep 17 00:00:00 2001 From: Steven Reilly Date: Wed, 3 May 2023 15:33:27 -0400 Subject: [PATCH] Cookie streamlining (#493) * remove cookie banner * remove form from cookies.html * move cookie content to privacy page --- app/assets/javascripts/cookieSettings.js | 84 ------ .../components/cookie-message.scss | 122 -------- app/assets/stylesheets/main.scss | 2 - app/assets/stylesheets/views/cookies.scss | 8 - app/main/views/index.py | 5 - app/templates/admin_template.html | 5 - app/templates/components/cookie-banner.html | 25 -- app/templates/views/cookies.html | 151 ---------- app/templates/views/privacy.html | 97 ++++++- gulpfile.js | 1 - tests/app/main/test_errorhandlers.py | 6 +- .../service_settings/test_service_settings.py | 2 +- tests/app/main/views/test_index.py | 2 +- tests/app/test_navigation.py | 1 - tests/javascripts/cookieMessage.test.js | 263 ------------------ tests/javascripts/cookieSettings.test.js | 255 ----------------- 16 files changed, 101 insertions(+), 928 deletions(-) delete mode 100644 app/assets/javascripts/cookieSettings.js delete mode 100644 app/assets/stylesheets/components/cookie-message.scss delete mode 100644 app/templates/components/cookie-banner.html delete mode 100644 app/templates/views/cookies.html delete mode 100644 tests/javascripts/cookieMessage.test.js delete mode 100644 tests/javascripts/cookieSettings.test.js diff --git a/app/assets/javascripts/cookieSettings.js b/app/assets/javascripts/cookieSettings.js deleted file mode 100644 index 684b70a7e..000000000 --- a/app/assets/javascripts/cookieSettings.js +++ /dev/null @@ -1,84 +0,0 @@ -window.GOVUK = window.GOVUK || {}; -window.GOVUK.Modules = window.GOVUK.Modules || {}; - -(function (Modules) { - function CookieSettings () {} - - CookieSettings.prototype.start = function ($module) { - this.$module = $module[0]; - - this.$module.submitSettingsForm = this.submitSettingsForm.bind(this); - - document.querySelector('form[data-module=cookie-settings]') - .addEventListener('submit', this.$module.submitSettingsForm); - - this.setInitialFormValues(); - }; - - CookieSettings.prototype.setInitialFormValues = function () { - var currentConsentCookie = window.GOVUK.getConsentCookie('consent'); - - if (!currentConsentCookie) { return; } - - var radioButton; - - if (currentConsentCookie.analytics) { - radioButton = document.querySelector('input[name=cookies-analytics][value=on]'); - } else { - radioButton = document.querySelector('input[name=cookies-analytics][value=off]'); - } - - radioButton.checked = true; - }; - - CookieSettings.prototype.submitSettingsForm = function (event) { - event.preventDefault(); - - var formInputs = event.target.querySelectorAll("input[name=cookies-analytics]"); - var options = {}; - - for ( var i = 0; i < formInputs.length; i++ ) { - var input = formInputs[i]; - if (input.checked) { - var value = input.value === "on" ? true : false; - - options.analytics = value; - break; - } - } - - window.GOVUK.setConsentCookie(options); - - this.showConfirmationMessage(); - - if(window.GOVUK.hasConsentFor('analytics')) { - window.GOVUK.initAnalytics(); - } - - return false; - }; - - CookieSettings.prototype.showConfirmationMessage = function () { - var confirmationMessage = document.querySelector('div[data-cookie-confirmation]'); - var previousPageLink = document.querySelector('.cookie-settings__prev-page'); - var referrer = CookieSettings.prototype.getReferrerLink(); - - document.body.scrollTop = document.documentElement.scrollTop = 0; - - if (referrer && referrer !== document.location.pathname) { - previousPageLink.href = referrer; - previousPageLink.style.display = "block"; - } else { - previousPageLink.style.display = "none"; - } - - confirmationMessage.style.display = "block"; - }; - - CookieSettings.prototype.getReferrerLink = function () { - return document.referrer ? new URL(document.referrer).pathname : false; - }; - - Modules.CookieSettings = CookieSettings; -})(window.GOVUK.Modules); - diff --git a/app/assets/stylesheets/components/cookie-message.scss b/app/assets/stylesheets/components/cookie-message.scss deleted file mode 100644 index 28684d611..000000000 --- a/app/assets/stylesheets/components/cookie-message.scss +++ /dev/null @@ -1,122 +0,0 @@ -// GOV.UK Publishing components cookie banner styles -// https://github.com/alphagov/govuk_publishing_components/blob/master/app/assets/stylesheets/govuk_publishing_components/components/_cookie-banner.scss - -// component uses .govuk-body and .govuk-button classes from govuk-frontend -@import 'core/typography'; -@import 'components/button/_button'; - -.notify-cookie-banner__wrapper { - @include govuk-responsive-padding(4, "top"); - @include govuk-responsive-padding(5, "bottom"); -} - -// component should only be shown if JS is available, by the cookieMessage JS, so hide by default -.notify-cookie-banner { - display: none; -} - -.notify-cookie-banner__buttons { - display: flex; - flex-wrap: wrap; - - @include govuk-media-query($from: tablet) { - flex-wrap: nowrap; - } -} - -.notify-cookie-banner__button, -.notify-cookie-banner__link { - vertical-align: baseline; -} - -.notify-cookie-banner__button { - display: inline-block; - flex: 1 0; - padding-left: govuk-spacing(9); - padding-right: govuk-spacing(9); - margin-bottom: govuk-spacing(2); - - @include govuk-media-query($from: tablet) { - flex: 0 0 150px; - padding-left: govuk-spacing(2); - padding-right: govuk-spacing(2); - margin-bottom: govuk-spacing(1); - } -} - -.notify-cookie-banner__button-accept { - margin-right: govuk-spacing(4); -} - -.notify-cookie-banner__link { - @include govuk-font(19); - line-height: 1; - display: block; - width: 100%; - padding: 9px 0px 6px; - - @include govuk-media-query($from: tablet) { - display: inline; - width: auto; - margin-left: govuk-spacing(6); - } -} - -.notify-cookie-banner__confirmation { - display: none; - position: relative; - padding: govuk-spacing(4) 0; - - @include govuk-media-query($from: desktop) { - padding: govuk-spacing(4); - } - - // This element is focused using JavaScript so that it's being read out by screen readers - // for this reason we don't want to show the default outline or emphasise it visually using `govuk-focused-text` - &:focus { - outline: none; - } -} - -.notify-cookie-banner__confirmation-message, -.notify-cookie-banner__hide-button { - display: block; - - @include govuk-media-query($from: desktop) { - display: inline-block; - } -} - -.notify-cookie-banner__confirmation-message { - margin-right: govuk-spacing(4); - - @include govuk-media-query($from: desktop) { - max-width: 90%; - } -} - -.notify-cookie-banner__hide-button { - @include govuk-font($size: 19); - color: $govuk-link-colour; - outline: 0; - border: 0; - background: none; - text-decoration: underline; - padding: govuk-spacing(0); - margin-top: govuk-spacing(2); - right: govuk-spacing(3); - cursor: pointer; - - @include govuk-media-query($from: desktop) { - margin-top: govuk-spacing(0); - position: absolute; - right: govuk-spacing(4); - } -} - -// Additions - -// Override margin-bottom, inherited from using .govuk-body class -.notify-cookie-banner__confirmation-message { - margin-bottom: 0; -} diff --git a/app/assets/stylesheets/main.scss b/app/assets/stylesheets/main.scss index 6b09332c5..1d7c104db 100644 --- a/app/assets/stylesheets/main.scss +++ b/app/assets/stylesheets/main.scss @@ -37,7 +37,6 @@ $path: '/static/images/'; // Specific to this application @import 'local/typography'; @import 'grids'; -@import 'components/cookie-message'; @import 'components/site-footer'; @import 'components/placeholder'; @import 'components/sms-message'; @@ -81,7 +80,6 @@ $path: '/static/images/'; @import 'views/send'; @import 'views/get_started'; @import 'views/history'; -@import 'views/cookies'; @import 'views/webauthn'; // TODO: break this up diff --git a/app/assets/stylesheets/views/cookies.scss b/app/assets/stylesheets/views/cookies.scss index 05974bb08..930540966 100644 --- a/app/assets/stylesheets/views/cookies.scss +++ b/app/assets/stylesheets/views/cookies.scss @@ -1,11 +1,3 @@ -.cookie-settings__form-wrapper { - display: none; - - .js-enabled & { - display: block; - } -} - .cookie-settings__no-js { .js-enabled & { display: none; diff --git a/app/main/views/index.py b/app/main/views/index.py index 92e44cea5..7c418d179 100644 --- a/app/main/views/index.py +++ b/app/main/views/index.py @@ -41,11 +41,6 @@ def error(status_code): abort(status_code) -@main.route('/cookies') -def cookies(): - return render_template('views/cookies.html') - - @main.route('/privacy') def privacy(): return render_template('views/privacy.html') diff --git a/app/templates/admin_template.html b/app/templates/admin_template.html index 70b3f0d21..fc74687e6 100644 --- a/app/templates/admin_template.html +++ b/app/templates/admin_template.html @@ -1,6 +1,5 @@ {% extends "main_template.html" %} {% from "components/banner.html" import banner %} -{% from "components/cookie-banner.html" import cookie_banner %} {% block headIcons %} @@ -41,10 +40,6 @@ {% block bodyStart %} {% block extra_javascripts_before_body %} {% endblock %} - - {% block cookie_message %} - {{ cookie_banner() }} - {% endblock %} {% endblock %} {% block header %} diff --git a/app/templates/components/cookie-banner.html b/app/templates/components/cookie-banner.html deleted file mode 100644 index 53803dd89..000000000 --- a/app/templates/components/cookie-banner.html +++ /dev/null @@ -1,25 +0,0 @@ -{% macro cookie_banner(id='global-cookie-message') %} - - -{% endmacro %} diff --git a/app/templates/views/cookies.html b/app/templates/views/cookies.html deleted file mode 100644 index 0efd5630e..000000000 --- a/app/templates/views/cookies.html +++ /dev/null @@ -1,151 +0,0 @@ -{% extends "withoutnav_template.html" %} -{% from "components/banner.html" import banner %} - -{% block per_page_title %} - Cookies -{% endblock %} - -{% block cookie_message %}{% endblock %} - -{% block maincolumn_content %} - -
-
- -

Cookies

-

- Cookies are small files saved on your phone, tablet or computer when you visit a website. -

-

We use cookies to make U.S. Notify work and collect information about how you use our service.

- -

Essential cookies

-

- Essential cookies keep your information secure while you use Notify. We do not need to ask permission to use them. -

- - - - - - - - - - - - - - - - - - - - - -
Essential cookies
NamePurposeExpires
- notify_admin_session - - Used to keep you signed in - - 20 hours -
- cookie_policy - - Saves your cookie consent settings - - 1 year -
- -

Analytics cookies (optional)

-

- With your permission, we use Google Analytics to collect data about how you use Notify. This information helps us to improve our service. -

-

- Google is not allowed to use or share our analytics data with anyone. -

-

- Google Analytics stores anonymised information about: -

-
    -
  • how you got to U.S. Notify
  • -
  • the pages you visit on Notify and how long you spend on them
  • -
  • any errors you see while using Notify
  • -
- - - - - - - - - - - - - - - - - - - - - -
Google Analytics cookies
NamePurposeExpires
- _ga - - Checks if you’ve visited Notify before. This helps us count how many people visit our site. - - 2 years -
- _gid - - Checks if you’ve visited Notify before. This helps us count how many people visit our site. - - 24 hours -
- - -
-
- -{% endblock %} diff --git a/app/templates/views/privacy.html b/app/templates/views/privacy.html index 14b736d0b..fe8ff6e33 100644 --- a/app/templates/views/privacy.html +++ b/app/templates/views/privacy.html @@ -13,7 +13,7 @@ {{ content_metadata( data={ - "Last updated": "21 November 2022" + "Last updated": "May 3, 2023" } ) }} @@ -92,6 +92,101 @@

If you have any of these requests, get in contact with our Data Protection Officer - you can find their contact details below.

--> +

Cookies

+

+ Cookies are small files saved on your phone, tablet or computer when you visit a website. +

+

We use cookies to make U.S. Notify work and collect information about how you use our service.

+ +

Essential cookies

+

+ Essential cookies keep your information secure while you use Notify. We do not need to ask permission to use them. +

+ + + + + + + + + + + + + + + + + + + + + +
Essential cookies
NamePurposeExpires
+ notify_admin_session + + Used to keep you signed in + + 20 hours +
+ cookie_policy + + Saves your cookie consent settings + + 1 year +
+ +

Analytics cookies

+

+ We also use Google Analytics to collect data about how you use Notify. This information helps us to improve our service. +

+

+ Google is not allowed to use or share our analytics data with anyone. +

+

+ Google Analytics stores anonymised information about: +

+ + + + + + + + + + + + + + + + + + + + + + +
Google Analytics cookies
NamePurposeExpires
+ _ga + + Checks if you’ve visited Notify before. This helps us count how many people visit our site. + + 2 years +
+ _gid + + Checks if you’ve visited Notify before. This helps us count how many people visit our site. + + 24 hours +
+

Changes to this notice

We may modify or amend this privacy notice at our discretion at any time. When we make changes to this notice, we will amend the last modified date at the top of this page. Any modification or amendment to this privacy notice will be applied diff --git a/gulpfile.js b/gulpfile.js index 44814a76d..e12d62ee2 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -106,7 +106,6 @@ const javascripts = () => { paths.src + 'javascripts/analytics/analytics.js', paths.src + 'javascripts/analytics/init.js', paths.src + 'javascripts/cookieMessage.js', - paths.src + 'javascripts/cookieSettings.js', paths.src + 'javascripts/stick-to-window-when-scrolling.js', paths.src + 'javascripts/copyToClipboard.js', paths.src + 'javascripts/autofocus.js', diff --git a/tests/app/main/test_errorhandlers.py b/tests/app/main/test_errorhandlers.py index 38ee216d2..8e8be0808 100644 --- a/tests/app/main/test_errorhandlers.py +++ b/tests/app/main/test_errorhandlers.py @@ -46,7 +46,7 @@ def test_csrf_returns_400(client_request, mocker): mocker.patch('app.main.views.index.render_template', side_effect=csrf_err) page = client_request.get_url( - '/cookies', + '/privacy', _expected_status=400, _test_page_title=False, ) @@ -61,8 +61,8 @@ def test_csrf_redirects_to_sign_in_page_if_not_signed_in(client_request, mocker) client_request.logout() client_request.get_url( - '/cookies', - _expected_redirect=url_for('main.sign_in', next='/cookies'), + '/privacy', + _expected_redirect=url_for('main.sign_in', next='/privacy'), ) diff --git a/tests/app/main/views/service_settings/test_service_settings.py b/tests/app/main/views/service_settings/test_service_settings.py index 4a7d0b736..5ef3264f1 100644 --- a/tests/app/main/views/service_settings/test_service_settings.py +++ b/tests/app/main/views/service_settings/test_service_settings.py @@ -3483,7 +3483,7 @@ def test_send_files_by_email_contact_details_page( page = client_request.get( 'main.send_files_by_email_contact_details', service_id=SERVICE_ONE_ID ) - assert normalize_spaces(page.find_all('h2')[1].text) == subheader + assert normalize_spaces(page.find_all('h2')[0].text) == subheader if button_selected: assert 'checked' in page.find('input', {'name': 'contact_details_type', 'value': 'email_address'}).attrs else: diff --git a/tests/app/main/views/test_index.py b/tests/app/main/views/test_index.py index 621542261..9a224fe83 100644 --- a/tests/app/main/views/test_index.py +++ b/tests/app/main/views/test_index.py @@ -91,7 +91,7 @@ def test_hiding_pages_from_search_engines( @pytest.mark.parametrize('view', [ - 'cookies', 'privacy', 'pricing', 'terms', 'roadmap', + 'privacy', 'pricing', 'terms', 'roadmap', 'features', 'documentation', 'security', 'message_status', 'features_email', 'features_sms', 'how_to_pay', 'get_started', diff --git a/tests/app/test_navigation.py b/tests/app/test_navigation.py index 86347d6d4..94dfe9d92 100644 --- a/tests/app/test_navigation.py +++ b/tests/app/test_navigation.py @@ -52,7 +52,6 @@ EXCLUDED_ENDPOINTS = tuple(map(Navigation.get_endpoint_with_blueprint, { 'conversation_reply', 'conversation_reply_with_template', 'conversation_updates', - 'cookies', 'copy_template', 'count_content_length', 'create_and_send_messages', diff --git a/tests/javascripts/cookieMessage.test.js b/tests/javascripts/cookieMessage.test.js deleted file mode 100644 index 71b9cab6f..000000000 --- a/tests/javascripts/cookieMessage.test.js +++ /dev/null @@ -1,263 +0,0 @@ -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(() => { - - const cookieMessageStyles = document.createElement('style'); - - // add the CSS that hides the cookie message by default - cookieMessageStyles.textContent = '.notify-cookie-banner { display: none; }'; - document.getElementsByTagName('head')[0].appendChild(cookieMessageStyles); - - // protect against any previous tests setting a cookies-policy cookie - 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 = ` -

`; - - 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 is hidden. - - This works through CSS, based on the presence of the `js-enabled` class on the so is not tested here. - */ - - describe("The `clearOldCookies` method", () => { - - test("Will clear the seen_cookie_message cookie if it still exists", () => { - - // seen_cookie_message was set on the www domain, which setCookie defaults to - helpers.setCookie('seen_cookie_message', 'true', { 'days': 365 }); - - window.GOVUK.Modules.CookieBanner.clearOldCookies({ "analytics": false }); - - expect(window.GOVUK.cookie('seen_cookie_message')).toBeNull(); - - }); - - test("Will clear any existing Google Analytics cookies if consent is not set", () => { - - // GA cookies are set on the root domain - helpers.setCookie('_ga', 'GA1.1.123.123', { 'days': 365, 'domain': '.notifications.service.gov.uk' }); - helpers.setCookie('_gid', 'GA1.1.456.456', { 'days': 1, 'domain': '.notifications.service.gov.uk' }); - - window.GOVUK.Modules.CookieBanner.clearOldCookies(null); - - expect(window.GOVUK.cookie('_ga')).toBeNull(); - expect(window.GOVUK.cookie('_gid')).toBeNull(); - - }); - - test("Will leave any existing Google Analytics cookies if consent is set", () => { - - helpers.setCookie('_ga', 'GA1.1.123.123', { 'days': 365 }); - helpers.setCookie('_gid', 'GA1.1.456.456', { 'days': 1 }); - - window.GOVUK.Modules.CookieBanner.clearOldCookies({ "analytics": true }); - - expect(window.GOVUK.cookie('_ga')).not.toBeNull(); - expect(window.GOVUK.cookie('_gid')).not.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 'false'", () => { - - expect(window.GOVUK.getConsentCookie()).toEqual({ 'analytics': false }); - - }); - - test("The analytics should not be set up", () => { - - expect(window.GOVUK.analytics).not.toBeDefined(); - - }); - - }); - - }); - -}); diff --git a/tests/javascripts/cookieSettings.test.js b/tests/javascripts/cookieSettings.test.js deleted file mode 100644 index d4416ab73..000000000 --- a/tests/javascripts/cookieSettings.test.js +++ /dev/null @@ -1,255 +0,0 @@ -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 = ` - -

Cookies

-

- Cookies are small files saved on your phone, tablet or computer when you visit a website. -

-

We use cookies to make GOV.UK Notify work and collect information about how you use our service.

- -

Analytics cookies (optional)

- `; - - 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 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 5 calls are configuration - expect(window.ga.mock.calls[5]).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(); - - }); - - }); - - }); - -});