Skip to content

Commit

Permalink
Change cookie class parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood committed Dec 6, 2023
1 parent 4ca8b2a commit 8515134
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 11 additions & 12 deletions src/nationalarchives/components/cookie-banner/cookie-banner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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") ||
Expand Down
5 changes: 3 additions & 2 deletions src/nationalarchives/lib/cookies.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
39 changes: 39 additions & 0 deletions src/nationalarchives/tests/cookies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit 8515134

Please sign in to comment.