From 76d2680345251ec6d1545f157e25ffcc20a18c69 Mon Sep 17 00:00:00 2001 From: Andrew Hosgood Date: Wed, 6 Dec 2023 14:29:41 +0000 Subject: [PATCH] Change cookie class parameters (#60) --- CHANGELOG.md | 3 ++ .../cookie-banner/cookie-banner.mjs | 23 ++++++----- src/nationalarchives/lib/cookies.mjs | 5 ++- src/nationalarchives/tests/cookies.test.js | 39 +++++++++++++++++++ 4 files changed, 56 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 708cb640..fbfb334d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added ### Changed + +- Cookies class parameter `extraPolicies` moved to key inside the options object parameter + ### Deprecated ### Removed ### Fixed diff --git a/src/nationalarchives/components/cookie-banner/cookie-banner.mjs b/src/nationalarchives/components/cookie-banner/cookie-banner.mjs index ad25e561..22cbf634 100644 --- a/src/nationalarchives/components/cookie-banner/cookie-banner.mjs +++ b/src/nationalarchives/components/cookie-banner/cookie-banner.mjs @@ -28,24 +28,23 @@ export class CookieBanner { } const policies = this.$module.getAttribute("data-policies") || ""; + const extraPolicies = policies + .split(",") + .filter((x) => x) + .map((policy) => policy.trim()); const domain = this.$module.getAttribute("data-domain") || undefined; const path = this.$module.getAttribute("data-path") || undefined; const secure = this.$module.getAttribute("data-secure") || undefined; const policiesKey = this.$module.getAttribute("data-policies-key") || undefined; - this.cookies = new (window.TNAFrontend?.Cookies || Cookies)( - policies - .split(",") - .filter((x) => x) - .map((policy) => policy.trim()), - { - domain, - path, - secure, - policiesKey, - }, - ); + this.cookies = new (window.TNAFrontend?.Cookies || Cookies)({ + extraPolicies, + domain, + path, + secure, + policiesKey, + }); this.cookiePreferencesSet = this.$module.getAttribute("data-preferenceskey") || diff --git a/src/nationalarchives/lib/cookies.mjs b/src/nationalarchives/lib/cookies.mjs index c0803200..05757afc 100644 --- a/src/nationalarchives/lib/cookies.mjs +++ b/src/nationalarchives/lib/cookies.mjs @@ -50,14 +50,15 @@ export default class Cookies { /** * Create a cookie handler. - * @param {string[]} [extraPolicies=[]] - The extra cookie policies to manage in addition to essential, settings and usage. + * @param {string} [options.extraPolicies=[]] - The extra cookie policies to manage in addition to essential, settings and usage. * @param {string} [options.domain=""] - The domain to register the cookie with. * @param {string} [options.path=""] - The domain to register the cookie with. * @param {string} [options.secure=true] - Only set cookie in HTTPS environments. * @param {string} [options.policiesKey=cookies_policy] - The name of the cookie. */ - constructor(extraPolicies = [], options = {}) { + constructor(options = {}) { const { + extraPolicies = [], domain = "", path = "/", secure = true, diff --git a/src/nationalarchives/tests/cookies.test.js b/src/nationalarchives/tests/cookies.test.js index 361278d7..0c41cf07 100644 --- a/src/nationalarchives/tests/cookies.test.js +++ b/src/nationalarchives/tests/cookies.test.js @@ -356,6 +356,45 @@ describe("No existing cookies", () => { cookies2.set(testKey, testValue); expect(mockCallback.mock.calls).toHaveLength(3); }); + + test("Custom policies", async () => { + const cookies = new Cookies({ extraPolicies: ["custom"] }); + + expect(cookies.policies).toHaveProperty("essential"); + expect(cookies.policies).toHaveProperty("settings"); + expect(cookies.policies).toHaveProperty("usage"); + expect(cookies.policies).toHaveProperty("custom"); + expect(cookies.policies.custom).toEqual(false); + expect(cookies.isPolicyAccepted("custom")).toEqual(false); + + cookies.acceptPolicy("custom"); + expect(cookies.policies.custom).toEqual(true); + expect(cookies.isPolicyAccepted("custom")).toEqual(true); + + cookies.rejectPolicy("custom"); + expect(cookies.policies.custom).toEqual(false); + expect(cookies.isPolicyAccepted("custom")).toEqual(false); + + cookies.acceptAllPolicies(); + expect(cookies.policies.custom).toEqual(true); + expect(cookies.isPolicyAccepted("custom")).toEqual(true); + + cookies.rejectAllPolicies(); + expect(cookies.policies.custom).toEqual(false); + expect(cookies.isPolicyAccepted("custom")).toEqual(false); + }); + + test("Custom policy key", async () => { + const cookies = new Cookies({ policiesKey: ["custom"] }); + + expect(cookies.all).not.toHaveProperty("cookies_policy"); + expect(cookies.all).toHaveProperty("custom"); + + expect(cookies.policies.settings).toEqual(false); + + cookies.acceptAllPolicies(); + expect(cookies.policies.settings).toEqual(true); + }); }); describe("Existing cookies", () => {