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

[733] Storage function rework #268

Merged
merged 6 commits into from
Sep 1, 2022
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
215 changes: 215 additions & 0 deletions cypress/integration/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/* eslint-disable require-jsdoc */
var Countly = require("../../lib/countly");
var hp = require("../support/helper");

function initMain(val) {
Countly.init({
app_key: "YOUR_APP_KEY",
url: "https://try.count.ly",
max_events: -1,
test_mode: true,
debug: true,
storage: val
});
}

const valueToStore = "value";
const key = "key";
const testArray = ["default", "cookie", "none", "localstorage", "randomValue"];

for (let i = 0; i < 5; i++) {
const flag = testArray[i];
const isCookie = flag === "cookie";
const isLocal = flag === "localstorage";
const isNone = flag === "none";

describe("Storage tests, storage: " + flag, () => {
// for everything at default
describe("basic setting", () => {
it("Checks if setValueInStorage function stores a value correctly", () => {
hp.haltAndClearStorage(() => {
initMain(flag);
Countly._internals.setValueInStorage(key, valueToStore);
cy.getLocalStorage(`${hp.appKey}/${key}`).then((value) => {
if (isCookie) {
expect(value).to.equal(null);
expect(document.cookie).to.include(`${hp.appKey}/${key}=${valueToStore}`);
}
else if (isNone) {
expect(value).to.equal(null);
expect(document.cookie).to.equal("");
}
else {
expect(value).to.equal(valueToStore);
}
});
});
});
it("Checks if getValueFromStorage function gets a value correctly", () => {
hp.haltAndClearStorage(() => {
initMain(flag);
if (isNone) {
expect(document.cookie).to.equal("");
}
Countly._internals.setValueInStorage(key, valueToStore);
expect(isNone ? undefined : valueToStore).to.equal(Countly._internals.getValueFromStorage(key));
});
});
it("Checks if getValueFromStorage function can not get a value if it does not exist", () => {
hp.haltAndClearStorage(() => {
initMain(flag);
expect(isNone ? undefined : null).to.equal(Countly._internals.getValueFromStorage(key));
});
});
it("Checks if removeValueFromStorage function removes a value correctly", () => {
hp.haltAndClearStorage(() => {
initMain(flag);
if (isNone) {
expect(document.cookie).to.equal("");
}
Countly._internals.setValueInStorage(key, valueToStore);
expect(isNone ? undefined : valueToStore).to.equal(Countly._internals.getValueFromStorage(key));
Countly._internals.removeValueFromStorage(key);
expect(isNone ? undefined : null).to.equal(Countly._internals.getValueFromStorage(key));
});
});
});

// check basic functionality for cookies. No rawKey or useLocalstorage.
describe("uselocalstorage: false ", () => {
it("Checks if setValueInStorage function stores a cookie correctly", () => {
hp.haltAndClearStorage(() => {
initMain(flag);
Countly._internals.setValueInStorage(key, valueToStore, false);
cy.getLocalStorage(`${hp.appKey}/${key}`).then((value) => {
expect(value).to.equal(null);
});
if (isNone || isLocal) {
expect(document.cookie).to.equal("");
}
else {
expect(document.cookie).to.include(`${hp.appKey}/${key}=${valueToStore}`);
}
});
});
it("Checks if getValueFromStorage function gets a value correctly", () => {
hp.haltAndClearStorage(() => {
initMain(flag);
Countly._internals.setValueInStorage(key, valueToStore, false);
if (isCookie) {
expect(isNone || isLocal ? undefined : null).to.equal(Countly._internals.getValueFromStorage(key, !!isCookie));
}
expect(isNone || isLocal ? undefined : valueToStore).to.equal(Countly._internals.getValueFromStorage(key, false));
});
});
it("Checks if getValueFromStorage function can not get a value if it does not exist", () => {
hp.haltAndClearStorage(() => {
initMain(flag);
Countly._internals.setValueInStorage(key, valueToStore);
expect(isNone || isLocal ? undefined : null).to.equal(Countly._internals.getValueFromStorage(key, !!isCookie));
});
});
it("Checks if removeValueFromStorage function removes a value correctly", () => {
hp.haltAndClearStorage(() => {
initMain(flag);
Countly._internals.setValueInStorage(key, valueToStore, false);
expect(isNone || isLocal ? undefined : valueToStore).to.equal(Countly._internals.getValueFromStorage(key, false));
Countly._internals.removeValueFromStorage(key, false);
expect(isNone || isLocal ? undefined : null).to.equal(Countly._internals.getValueFromStorage(key, false));
});
});
});

// check for local storage functionality with rawKey but no cookies.
describe("useRawKey: true", () => {
it("Checks if setValueInStorage function stores a value correctly", () => {
hp.haltAndClearStorage(() => {
initMain(flag);
Countly._internals.setValueInStorage(key, valueToStore, undefined, true);
cy.getLocalStorage(`${key}`).then((value) => {
if (isCookie) {
expect(value).to.equal(null);
expect(document.cookie).to.contain(`${key}=${valueToStore}`);
}
else if (isNone) {
expect(value).to.equal(null);
}
else {
expect(value).to.equal(valueToStore);
}
});
cy.getLocalStorage(`${hp.appKey}/${key}`).then((value) => {
expect(value).to.equal(null);
});
});
});
it("Checks if getValueFromStorage function gets a value correctly", () => {
hp.haltAndClearStorage(() => {
initMain(flag);
Countly._internals.setValueInStorage(key, valueToStore, undefined, true);
expect(isNone ? undefined : valueToStore).to.equal(Countly._internals.getValueFromStorage(key, undefined, true));
});
});
it("Checks if getValueFromStorage function can not get a value if it does not exist", () => {
hp.haltAndClearStorage(() => {
initMain(flag);
expect(isNone ? undefined : null).to.equal(Countly._internals.getValueFromStorage(key, undefined, true));
});
});
it("Checks if removeValueFromStorage function removes a value correctly", () => {
hp.haltAndClearStorage(() => {
initMain(flag);
Countly._internals.setValueInStorage(key, valueToStore, undefined, true);
expect(isNone ? undefined : valueToStore).to.equal(Countly._internals.getValueFromStorage(key, undefined, true));
Countly._internals.removeValueFromStorage(key, undefined, true);
expect(isNone ? undefined : null).to.equal(Countly._internals.getValueFromStorage(key, undefined, true));
});
});
});

// check for cookies functionality with rawKey but no uselocalstorage.
describe("uselocalstorage: false, useRawKey: true", () => {
it("Checks if setValueInStorage function stores a value correctly", () => {
hp.haltAndClearStorage(() => {
initMain(flag);
Countly._internals.setValueInStorage(key, valueToStore, false, true);
cy.getLocalStorage(`${key}`).then((value) => {
expect(value).to.equal(null);
});
cy.getLocalStorage(`${hp.appKey}/${key}`).then((value) => {
expect(value).to.equal(null);
});
if (isNone || isLocal) {
expect(document.cookie).to.include("");
}
else {
expect(document.cookie).to.include(`${key}=${valueToStore}`);
}
});
});
it("Checks if getValueFromStorage function gets a value correctly", () => {
hp.haltAndClearStorage(() => {
initMain(flag);
Countly._internals.setValueInStorage(key, valueToStore, false, true);
expect(isNone || isLocal ? undefined : valueToStore).to.equal(Countly._internals.getValueFromStorage(key, false, true));
expect(isNone || isLocal ? undefined : null).to.equal(Countly._internals.getValueFromStorage(key, false));
});
});
it("Checks if getValueFromStorage function can not get a value if it does not exist", () => {
hp.haltAndClearStorage(() => {
initMain(flag);
expect(isNone || isLocal ? undefined : null).to.equal(Countly._internals.getValueFromStorage(key, false, true));
});
});
it("Checks if removeValueFromStorage function removes a value correctly", () => {
hp.haltAndClearStorage(() => {
initMain(flag);
Countly._internals.setValueInStorage(key, valueToStore, false, true);
expect(isNone || isLocal ? undefined : valueToStore).to.equal(Countly._internals.getValueFromStorage(key, false, true));
Countly._internals.removeValueFromStorage(key, false, true);
expect(isNone || isLocal ? undefined : null).to.equal(Countly._internals.getValueFromStorage(key, false, true));
});
});
});
});
}
3 changes: 2 additions & 1 deletion cypress/support/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ function haltAndClearStorage(callback) {
Countly.halt();
}
cy.wait(sWait).then(() => {
cy.clearLocalStorage().then(() => {
cy.clearLocalStorage();
cy.wait(sWait).then(() => {
callback();
});
});
Expand Down
Loading