Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change cookie class parameters #60

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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