Skip to content

Commit

Permalink
Add tests for override properties
Browse files Browse the repository at this point in the history
  • Loading branch information
slimkrazy committed Jan 13, 2025
1 parent e7b71af commit 92fcb64
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 38 deletions.
6 changes: 4 additions & 2 deletions modules/nodalsAiRtdProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ class NodalsAiRtdProvider {

// Private methods
#setOverrides(config) {
this.#overrides.storageTTL = config?.storage?.ttl;
if (config?.storage?.ttl && typeof config.storage.ttl === "number") {
this.#overrides.storageTTL = config.storage.ttl * 1000;
}
this.#overrides.storageKey = config?.storage?.key;
this.#overrides.endpointOrigin = config?.endpoint?.origin;
}
Expand Down Expand Up @@ -197,7 +199,7 @@ class NodalsAiRtdProvider {
#dataIsStale(storedData) {
const currentTime = Date.now();
const dataTime = storedData.createdAt || 0;
const staleThreshold = STORAGE_TTL;
const staleThreshold = this.#overrides?.storageTTL ?? STORAGE_TTL;
return currentTime - dataTime >= staleThreshold;
}

Expand Down
142 changes: 106 additions & 36 deletions test/spec/modules/nodalsAiRtdProvider_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,6 @@ import { server } from "test/mocks/xhr.js";

import { nodalsAiRtdSubmodule } from "modules/nodalsAiRtdProvider.js";

const generateGdprConsent = (consent = {}) => {
const defaults = {
gdprApplies: true,
purpose1Consent: true,
nodalsConsent: true,
};
const mergedConsent = Object.assign({}, defaults, consent);
return {
gdpr: {
gdprApplies: mergedConsent.gdprApplies,
consentString: mergedConsent.consentString,
vendorData: {
purpose: {
consents: {
1: mergedConsent.purpose1Consent,
3: true,
4: true,
5: true,
6: true,
9: true,
},
},
specialFeatureOptins: {
1: true,
},
vendor: {
consents: {
1360: mergedConsent.nodalsConsent,
},
},
},
},
};
};

const jsonResponseHeaders = {
"Content-Type": "application/json",
};
Expand All @@ -59,7 +24,7 @@ const successPubEndpointResponse = {
weighting: 1,
kvs: [
{
k: "nodalsSky12",
k: "nodals",
v: "1",
},
],
Expand Down Expand Up @@ -90,6 +55,41 @@ const engineGetTargetingDataReturnValue = {
},
};

const generateGdprConsent = (consent = {}) => {
const defaults = {
gdprApplies: true,
purpose1Consent: true,
nodalsConsent: true,
};
const mergedConsent = Object.assign({}, defaults, consent);
return {
gdpr: {
gdprApplies: mergedConsent.gdprApplies,
consentString: mergedConsent.consentString,
vendorData: {
purpose: {
consents: {
1: mergedConsent.purpose1Consent,
3: true,
4: true,
5: true,
6: true,
9: true,
},
},
specialFeatureOptins: {
1: true,
},
vendor: {
consents: {
1360: mergedConsent.nodalsConsent,
},
},
},
},
};
};

const setDataInLocalStorage = (data) => {
const storageData = { ...data };
nodalsAiRtdSubmodule.storage.setDataInLocalStorage(
Expand Down Expand Up @@ -188,6 +188,41 @@ describe("NodalsAI RTD Provider", () => {
expect(result).to.be.true;
expect(server.requests.length).to.equal(1);
});

it("should detect stale data if override TTL is exceeded", function () {
const fiveMinutesAgoMs = Date.now() - 5 * 60 * 1000;
setDataInLocalStorage({
data: { foo: "bar" },
createdAt: fiveMinutesAgoMs,
});
const config = Object.assign({}, validConfig);
config.storage = { ttl: 4 * 60 };
const result = nodalsAiRtdSubmodule.init(config, {});
expect(result).to.be.true;
expect(server.requests.length).to.equal(1);
});

it("should NOT detect stale data if override TTL is not exceeded", function () {
const fiveMinutesAgoMs = Date.now() - 5 * 60 * 1000;
setDataInLocalStorage({
data: { foo: "bar" },
createdAt: fiveMinutesAgoMs,
});
const config = Object.assign({}, validConfig);
config.storage = { ttl: 6 * 60 };
const result = nodalsAiRtdSubmodule.init(config, {});
expect(result).to.be.true;
expect(server.requests.length).to.equal(0);
});

it("should return true and make a remote request when data stored under default key, but override key specified", () => {
setDataInLocalStorage({ data: { foo: "bar" }, createdAt: Date.now() });
const config = Object.assign({}, validConfig);
config.storage = { key: "_foobarbaz_" };
const result = nodalsAiRtdSubmodule.init(config, {});
expect(result).to.be.true;
expect(server.requests.length).to.equal(1);
});
});

describe("when performing requests to the publisher endpoint", () => {
Expand All @@ -203,6 +238,20 @@ describe("NodalsAI RTD Provider", () => {
expect(requestUrl.origin).to.equal("https://nodals.io");
});

it("should construct the URL to the overridden origin when specified in the config", () => {
const config = Object.assign({}, validConfig);
config.endpoint = { origin: "http://localhost:8000" };
const userConsent = generateGdprConsent();
nodalsAiRtdSubmodule.init(config, userConsent);

let request = server.requests[0];

expect(request.method).to.equal("GET");
expect(request.withCredentials).to.be.false;
const requestUrl = new URL(request.url);
expect(requestUrl.origin).to.equal("http://localhost:8000");
});

it("should construct the correct URL with the correct path", () => {
const userConsent = generateGdprConsent();
nodalsAiRtdSubmodule.init(validConfig, userConsent);
Expand Down Expand Up @@ -253,6 +302,27 @@ describe("NodalsAI RTD Provider", () => {
expect(storedData.data).to.deep.equal(successPubEndpointResponse);
});

it("should store successful response data in local storage under the override key", () => {
const userConsent = generateGdprConsent();
const config = Object.assign({}, validConfig);
config.storage = { key: "_foobarbaz_" };
nodalsAiRtdSubmodule.init(config, userConsent);

let request = server.requests[0];
request.respond(
200,
jsonResponseHeaders,
JSON.stringify(successPubEndpointResponse)
);

const storedData = JSON.parse(
nodalsAiRtdSubmodule.storage.getDataFromLocalStorage("_foobarbaz_")
);
expect(request.method).to.equal("GET");
expect(storedData).to.have.property("createdAt");
expect(storedData.data).to.deep.equal(successPubEndpointResponse);
});

it("should attempt to load the referenced script libraries contained in the response payload", () => {
const userConsent = generateGdprConsent();
nodalsAiRtdSubmodule.init(validConfig, userConsent);
Expand Down

0 comments on commit 92fcb64

Please sign in to comment.