Browse Source

Updated the tests

tags/v6.0.0
Cédric Belin 5 months ago
parent
commit
8abc8ec2e6
1 changed files with 41 additions and 41 deletions
  1. +41
    -41
      test/web_storage_test.ts

+ 41
- 41
test/web_storage_test.ts View File

@@ -1,36 +1,36 @@
import {SessionStorage} from "../src/index";

/** Tests the features of the `WebStorage` class. */
describe("WebStorage", () => {
describe("WebStorage", function() {
const {expect} = chai;
beforeEach(() => sessionStorage.clear());

describe(".keys", () => {
it("should return an empty array for an empty storage", () => {
describe(".keys", function() {
it("should return an empty array for an empty storage", function() {
expect(new SessionStorage().keys).to.be.empty;
});

it("should return the list of keys for a non-empty storage", () => {
it("should return the list of keys for a non-empty storage", function() {
sessionStorage.setItem("foo", "bar");
sessionStorage.setItem("bar", "baz");
expect(new SessionStorage().keys).to.have.ordered.members(["foo", "bar"]);
});
});

describe(".length", () => {
it("should return zero for an empty storage", () => {
describe(".length", function() {
it("should return zero for an empty storage", function() {
expect(new SessionStorage()).to.have.lengthOf(0);
});

it("should return the number of entries for a non-empty storage", () => {
it("should return the number of entries for a non-empty storage", function() {
sessionStorage.setItem("foo", "bar");
sessionStorage.setItem("bar", "baz");
expect(new SessionStorage()).to.have.lengthOf(2);
});
});

describe(".onChange", () => {
it("should trigger an event when a value is added", done => {
describe(".onChange", function() {
it("should trigger an event when a value is added", function(done) {
const storage = new SessionStorage;
const subscription = storage.onChange.subscribe(event => {
expect(event.key).to.equal("foo");
@@ -43,7 +43,7 @@ describe("WebStorage", () => {
subscription.unsubscribe();
});

it("should trigger an event when a value is updated", done => {
it("should trigger an event when a value is updated", function(done) {
const storage = new SessionStorage;
sessionStorage.setItem("foo", "bar");

@@ -58,7 +58,7 @@ describe("WebStorage", () => {
subscription.unsubscribe();
});

it("should trigger an event when a value is removed", done => {
it("should trigger an event when a value is removed", function(done) {
const storage = new SessionStorage;
sessionStorage.setItem("foo", "bar");

@@ -73,7 +73,7 @@ describe("WebStorage", () => {
subscription.unsubscribe();
});

it("should trigger an event when the storage is cleared", done => {
it("should trigger an event when the storage is cleared", function(done) {
const storage = new SessionStorage;
sessionStorage.setItem("foo", "bar");
sessionStorage.setItem("bar", "baz");
@@ -90,14 +90,14 @@ describe("WebStorage", () => {
});
});

describe(".[Symbol.iterator]()", () => {
it("should return a done iterator if storage is empty", () => {
describe(".[Symbol.iterator]()", function() {
it("should return a done iterator if storage is empty", function() {
const storage = new SessionStorage;
const iterator = storage[Symbol.iterator]();
expect(iterator.next().done).to.be.true;
});

it("should return a value iterator if storage is not empty", () => {
it("should return a value iterator if storage is not empty", function() {
const storage = new SessionStorage;
sessionStorage.setItem("foo", "bar");
sessionStorage.setItem("bar", "baz");
@@ -119,8 +119,8 @@ describe("WebStorage", () => {
});
});

describe(".clear()", () => {
it("should remove all storage entries", () => {
describe(".clear()", function() {
it("should remove all storage entries", function() {
sessionStorage.setItem("foo", "bar");
sessionStorage.setItem("bar", "baz");

@@ -131,8 +131,8 @@ describe("WebStorage", () => {
});
});

describe(".get()", () => {
it("should properly get the storage entries", () => {
describe(".get()", function() {
it("should properly get the storage entries", function() {
const storage = new SessionStorage;
expect(storage.get("foo")).to.be.undefined;
expect(storage.get("foo", "123")).to.equal("123");
@@ -144,13 +144,13 @@ describe("WebStorage", () => {
expect(storage.get("foo")).to.equal("123");
});

it("should return the given default value if the key is not found", () => {
it("should return the given default value if the key is not found", function() {
expect(new SessionStorage().get("bar", "123")).to.equal("123");
});
});

describe(".getObject()", () => {
it("should properly get the deserialized storage entries", () => {
describe(".getObject()", function() {
it("should properly get the deserialized storage entries", function() {
const storage = new SessionStorage;
expect(storage.getObject("foo")).to.be.undefined;
expect(storage.getObject("foo", {key: "value"})).to.be.an("object").that.deep.equal({key: "value"});
@@ -165,18 +165,18 @@ describe("WebStorage", () => {
expect(storage.getObject("foo")).to.be.an("object").that.deep.equal({key: "value"});
});

it("should return the default value if the value can\"t be deserialized", () => {
it("should return the default value if the value can\"t be deserialized", function() {
sessionStorage.setItem("foo", "bar");
expect(new SessionStorage().getObject("foo", "defaultValue")).to.equal("defaultValue");
});
});

describe(".has()", () => {
it("should return `false` if the specified key is not contained", () => {
describe(".has()", function() {
it("should return `false` if the specified key is not contained", function() {
expect(new SessionStorage().has("foo")).to.be.false;
});

it("should return `true` if the specified key is contained", () => {
it("should return `true` if the specified key is contained", function() {
const storage = new SessionStorage;
sessionStorage.setItem("foo", "bar");
expect(storage.has("foo")).to.be.true;
@@ -184,15 +184,15 @@ describe("WebStorage", () => {
});
});

describe(".putIfAbsent()", () => {
it("should add a new entry if it does not exist", () => {
describe(".putIfAbsent()", function() {
it("should add a new entry if it does not exist", function() {
const storage = new SessionStorage;
expect(sessionStorage.getItem("foo")).to.be.null;
expect(storage.putIfAbsent("foo", () => "bar")).to.equal("bar");
expect(sessionStorage.getItem("foo")).to.equal("bar");
});

it("should not add a new entry if it already exists", () => {
it("should not add a new entry if it already exists", function() {
const storage = new SessionStorage;
sessionStorage.setItem("foo", "bar");
expect(storage.putIfAbsent("foo", () => "qux")).to.equal("bar");
@@ -200,15 +200,15 @@ describe("WebStorage", () => {
});
});

describe(".putObjectIfAbsent()", () => {
it("should add a new entry if it does not exist", () => {
describe(".putObjectIfAbsent()", function() {
it("should add a new entry if it does not exist", function() {
const storage = new SessionStorage;
expect(sessionStorage.getItem("foo")).to.be.null;
expect(storage.putObjectIfAbsent("foo", () => 123)).to.equal(123);
expect(sessionStorage.getItem("foo")).to.equal("123");
});

it("should not add a new entry if it already exists", () => {
it("should not add a new entry if it already exists", function() {
const storage = new SessionStorage;
sessionStorage.setItem("foo", "123");
expect(storage.putObjectIfAbsent("foo", () => 456)).to.equal(123);
@@ -216,8 +216,8 @@ describe("WebStorage", () => {
});
});

describe(".remove()", () => {
it("should properly remove the storage entries", () => {
describe(".remove()", function() {
it("should properly remove the storage entries", function() {
const storage = new SessionStorage;
sessionStorage.setItem("foo", "bar");
sessionStorage.setItem("bar", "baz");
@@ -232,8 +232,8 @@ describe("WebStorage", () => {
});
});

describe(".set()", () => {
it("should properly set the storage entries", () => {
describe(".set()", function() {
it("should properly set the storage entries", function() {
const storage = new SessionStorage;
expect(sessionStorage.getItem("foo")).to.be.null;
storage.set("foo", "bar");
@@ -243,8 +243,8 @@ describe("WebStorage", () => {
});
});

describe(".setObject()", () => {
it("should properly serialize and set the storage entries", () => {
describe(".setObject()", function() {
it("should properly serialize and set the storage entries", function() {
const storage = new SessionStorage;
expect(sessionStorage.getItem("foo")).to.be.null;
storage.setObject("foo", 123);
@@ -256,13 +256,13 @@ describe("WebStorage", () => {
});
});

describe(".toJSON()", () => {
it("should return an empty map for an empty storage", () => {
describe(".toJSON()", function() {
it("should return an empty map for an empty storage", function() {
const storage = new SessionStorage;
expect(storage.toJSON()).to.be.an("object").that.is.empty;
});

it("should return a non-empty map for a non-empty storage", () => {
it("should return a non-empty map for a non-empty storage", function() {
const storage = new SessionStorage;
storage.set("foo", "bar").set("baz", "qux");
expect(storage.toJSON()).to.deep.equal({baz: "qux", foo: "bar"});


Loading…
Cancel
Save