Browse Source

Updated the tests

main
Cédric Belin 1 month ago
parent
commit
4fcbdb3415
2 changed files with 63 additions and 63 deletions
  1. +18
    -18
      test/cookie_options_test.ts
  2. +45
    -45
      test/cookies_test.ts

+ 18
- 18
test/cookie_options_test.ts View File

@@ -1,7 +1,7 @@
import {CookieOptions, JsonObject} from "../src/index";

/** Tests the features of the `CookieOptions` class. */
describe("CookieOptions", () => {
describe("CookieOptions", function() {
const {expect} = chai;
const options = new CookieOptions({
domain: "domain.com",
@@ -10,21 +10,21 @@ describe("CookieOptions", () => {
secure: true
});

describe(".maxAge", () => {
it("should return `-1` if the expiration time is not set", () => {
describe(".maxAge", function() {
it("should return `-1` if the expiration time is not set", function() {
expect(new CookieOptions().maxAge).to.equal(-1);
});

it("should return zero if the cookie has expired", () => {
it("should return zero if the cookie has expired", function() {
expect(new CookieOptions({expires: new Date(2000, 0)}).maxAge).to.equal(0);
});

it("should return the difference with now if the cookie has not expired", () => {
it("should return the difference with now if the cookie has not expired", function() {
const duration = 30 * 1000;
expect(new CookieOptions({expires: new Date(Date.now() + duration)}).maxAge).to.equal(30);
});

it("should set the expiration date accordingly", () => {
it("should set the expiration date accordingly", function() {
const cookieOptions = new CookieOptions;

cookieOptions.maxAge = 0;
@@ -40,8 +40,8 @@ describe("CookieOptions", () => {
});
});

describe(".fromJson()", () => {
it("should return an instance with default values for an empty map", () => {
describe(".fromJson()", function() {
it("should return an instance with default values for an empty map", function() {
const cookieOptions = CookieOptions.fromJson({});
expect(cookieOptions.domain).to.be.empty;
expect(cookieOptions.expires).to.be.undefined;
@@ -50,7 +50,7 @@ describe("CookieOptions", () => {
expect(cookieOptions.secure).to.be.false;
});

it("should return an initialized instance for a non-empty map", () => {
it("should return an initialized instance for a non-empty map", function() {
const cookieOptions = CookieOptions.fromJson(options.toJSON());
expect(cookieOptions.domain).to.equal("domain.com");
expect(cookieOptions.expires!.toISOString()).to.equal("1970-01-01T00:00:00.000Z");
@@ -60,8 +60,8 @@ describe("CookieOptions", () => {
});
});

describe(".fromString()", () => {
it("should return an instance with default values for an empty string", () => {
describe(".fromString()", function() {
it("should return an instance with default values for an empty string", function() {
const cookieOptions = CookieOptions.fromString("");
expect(cookieOptions.domain).to.be.empty;
expect(cookieOptions.expires).to.be.undefined;
@@ -70,7 +70,7 @@ describe("CookieOptions", () => {
expect(cookieOptions.secure).to.be.false;
});

it("should return an initialized instance for a cookie string", () => {
it("should return an initialized instance for a cookie string", function() {
const cookieOptions = CookieOptions.fromString(`foo=bar; ${options}`);
expect(cookieOptions.domain).to.equal("domain.com");
expect(cookieOptions.expires!.toISOString()).to.equal("1970-01-01T00:00:00.000Z");
@@ -80,8 +80,8 @@ describe("CookieOptions", () => {
});
});

describe(".toJSON()", () => {
it("should return a map with default values for a newly created instance", () => {
describe(".toJSON()", function() {
it("should return a map with default values for a newly created instance", function() {
expect(new CookieOptions().toJSON()).to.deep.equal({
domain: "",
expires: null,
@@ -90,7 +90,7 @@ describe("CookieOptions", () => {
} as JsonObject);
});

it("should return a non-empty map for an initialized instance", () => {
it("should return a non-empty map for an initialized instance", function() {
expect(options.toJSON()).to.deep.equal({
domain: "domain.com",
expires: "1970-01-01T00:00:00.000Z",
@@ -100,12 +100,12 @@ describe("CookieOptions", () => {
});
});

describe(".toString()", () => {
it("should return an empty string for a newly created instance", () => {
describe(".toString()", function() {
it("should return an empty string for a newly created instance", function() {
expect(String(new CookieOptions)).to.be.empty;
});

it("should return a format like 'expires=<expires>; domain=<domain>; path=<path>; secure' for an initialized instance", () => {
it("should return a format like 'expires=<expires>; domain=<domain>; path=<path>; secure' for an initialized instance", function() {
expect(String(options)).to.equal("expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=domain.com; path=/path; secure");
});
});


+ 45
- 45
test/cookies_test.ts View File

@@ -1,7 +1,7 @@
import {Cookies} from "../src/index";

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

// Returns a map of the native cookies.
@@ -15,24 +15,24 @@ describe("Cookies", () => {
return nativeCookies;
}

describe(".keys", () => {
it("should return an empty array if the current document has no associated cookie", () => {
describe(".keys", function() {
it("should return an empty array if the current document has no associated cookie", function() {
expect(new Cookies().keys).to.be.an("array").and.have.lengthOf(getNativeCookies().size);
});

it("should return the keys of the cookies associated with the current document", () => {
it("should return the keys of the cookies associated with the current document", function() {
document.cookie = "key1=foo";
document.cookie = "key2=bar";
expect(new Cookies().keys).to.be.an("array").and.include.members(["key1", "key2"]);
});
});

describe(".length", () => {
it("should return zero if the current document has no associated cookie", () => {
describe(".length", function() {
it("should return zero if the current document has no associated cookie", function() {
expect(new Cookies).to.have.lengthOf(getNativeCookies().size);
});

it("should return the number of cookies associated with the current document", () => {
it("should return the number of cookies associated with the current document", function() {
const count = getNativeCookies().size;
document.cookie = "length1=foo";
document.cookie = "length2=bar";
@@ -40,8 +40,8 @@ describe("Cookies", () => {
});
});

describe(".onChanges", () => {
it("should trigger an event when a cookie is added", done => {
describe(".onChanges", function() {
it("should trigger an event when a cookie is added", function(done) {
document.cookie = "onChanges=; expires=Thu, 01 Jan 1970 00:00:00 GMT";

const cookies = new Cookies;
@@ -61,7 +61,7 @@ describe("Cookies", () => {
subscription.unsubscribe();
});

it("should trigger an event when a cookie is updated", done => {
it("should trigger an event when a cookie is updated", function(done) {
document.cookie = "onChanges=foo";

const cookies = new Cookies;
@@ -81,7 +81,7 @@ describe("Cookies", () => {
subscription.unsubscribe();
});

it("should trigger an event when a cookie is removed", done => {
it("should trigger an event when a cookie is removed", function(done) {
document.cookie = "onChanges=bar";

const cookies = new Cookies;
@@ -101,7 +101,7 @@ describe("Cookies", () => {
subscription.unsubscribe();
});

it("should trigger an event when all the cookies are removed", done => {
it("should trigger an event when all the cookies are removed", function(done) {
document.cookie = "onChanges1=foo";
document.cookie = "onChanges2=bar";

@@ -128,15 +128,15 @@ describe("Cookies", () => {
});
});

describe(".[Symbol.iterator]()", () => {
it("should return a done iterator if the current document has no associated cookie", () => {
describe(".[Symbol.iterator]()", function() {
it("should return a done iterator if the current document has no associated cookie", function() {
const cookies = new Cookies;
cookies.clear();
const iterator = cookies[Symbol.iterator]();
expect(iterator.next().done).to.be.true;
});

it("should return a value iterator if the current document has associated cookies", () => {
it("should return a value iterator if the current document has associated cookies", function() {
const cookies = new Cookies;
cookies.clear();

@@ -157,7 +157,7 @@ describe("Cookies", () => {
expect(iterator.next().done).to.be.true;
});

it("should allow the 'iterable' protocol", () => {
it("should allow the 'iterable' protocol", function() {
const cookies = new Cookies;
cookies.clear();
document.cookie = "iterator1=foo";
@@ -179,8 +179,8 @@ describe("Cookies", () => {
});
});

describe(".clear()", () => {
it("should remove all the cookies associated with the current document", () => {
describe(".clear()", function() {
it("should remove all the cookies associated with the current document", function() {
document.cookie = "clear1=foo";
document.cookie = "clear2=bar";

@@ -190,8 +190,8 @@ describe("Cookies", () => {
});
});

describe(".get()", () => {
it("should properly get the cookies associated with the current document", () => {
describe(".get()", function() {
it("should properly get the cookies associated with the current document", function() {
const cookies = new Cookies;
expect(cookies.get("foo")).to.be.undefined;
expect(cookies.get("foo", "123")).to.equal("123");
@@ -204,8 +204,8 @@ describe("Cookies", () => {
});
});

describe(".getObject()", () => {
it("should properly get the deserialized cookies associated with the current document", () => {
describe(".getObject()", function() {
it("should properly get the deserialized cookies associated with the current document", function() {
const cookies = new Cookies;
expect(cookies.getObject("foo")).to.be.undefined;
expect(cookies.getObject("foo", {key: "value"})).to.deep.equal({key: "value"});
@@ -220,18 +220,18 @@ describe("Cookies", () => {
expect(cookies.getObject("getObject3")).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() {
document.cookie = "getObject4=bar";
expect(new Cookies().getObject("getObject4", "defaultValue")).to.equal("defaultValue");
});
});

describe(".has()", () => {
it("should return `false` if the current document has an associated cookie with the specified key", () => {
describe(".has()", function() {
it("should return `false` if the current document has an associated cookie with the specified key", function() {
expect(new Cookies().has("foo")).to.be.false;
});

it("should return `true` if the current document does not have an associated cookie with the specified key", () => {
it("should return `true` if the current document does not have an associated cookie with the specified key", function() {
document.cookie = "has1=foo";
document.cookie = "has2=bar";

@@ -243,15 +243,15 @@ describe("Cookies", () => {
});
});

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 cookies = new Cookies;
expect(document.cookie).not.to.contain("putIfAbsent1");
expect(cookies.putIfAbsent("putIfAbsent1", () => "foo")).to.equal("foo");
expect(document.cookie).to.contain("putIfAbsent1=foo");
});

it("should not add a new entry if it already exists", () => {
it("should not add a new entry if it already exists", function() {
const cookies = new Cookies;
document.cookie = "putIfAbsent2=foo";
expect(cookies.putIfAbsent("putIfAbsent2", () => "bar")).to.equal("foo");
@@ -259,15 +259,15 @@ describe("Cookies", () => {
});
});

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 cookies = new Cookies;
expect(document.cookie).not.to.contain("putObjectIfAbsent1");
expect(cookies.putObjectIfAbsent("putObjectIfAbsent1", () => 123)).to.equal(123);
expect(document.cookie).to.contain("putObjectIfAbsent1=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 cookies = new Cookies;
document.cookie = "putObjectIfAbsent2=123";
expect(cookies.putObjectIfAbsent("putObjectIfAbsent2", () => 456)).to.equal(123);
@@ -275,8 +275,8 @@ describe("Cookies", () => {
});
});

describe(".remove()", () => {
it("should properly remove the cookies associated with the current document", () => {
describe(".remove()", function() {
it("should properly remove the cookies associated with the current document", function() {
const cookies = new Cookies;
document.cookie = "remove1=foo";
document.cookie = "remove2=bar";
@@ -290,8 +290,8 @@ describe("Cookies", () => {
});
});

describe(".set()", () => {
it("should properly set the cookies associated with the current document", () => {
describe(".set()", function() {
it("should properly set the cookies associated with the current document", function() {
const cookies = new Cookies;
expect(document.cookie).to.not.contain("set1");
expect(document.cookie).to.not.contain("set2");
@@ -309,13 +309,13 @@ describe("Cookies", () => {
expect(document.cookie).to.contain("set2=bar");
});

it("should throw an error if the specified key is empty", () => {
it("should throw an error if the specified key is empty", function() {
expect(() => new Cookies().set("", "foo")).to.throw(TypeError);
});
});

describe(".setObject()", () => {
it("should properly serialize and set the cookies associated with the current document", () => {
describe(".setObject()", function() {
it("should properly serialize and set the cookies associated with the current document", function() {
const cookies = new Cookies;
expect(document.cookie).to.not.contain("setObject1");
expect(document.cookie).to.not.contain("setObject2");
@@ -333,19 +333,19 @@ describe("Cookies", () => {
expect(document.cookie).to.contain("setObject2=%22foo%22");
});

it("should throw an error if the specified key is empty", () => {
it("should throw an error if the specified key is empty", function() {
expect(() => new Cookies().setObject("", "foo")).to.throw(TypeError);
});
});

describe(".toJSON()", () => {
it("should return an empty map if the current document has no associated cookie", () => {
describe(".toJSON()", function() {
it("should return an empty map if the current document has no associated cookie", function() {
const cookies = new Cookies;
cookies.clear();
expect(cookies.toJSON()).to.be.an("object").that.is.empty;
});

it("should return a non-empty map if the current document has associated cookies", () => {
it("should return a non-empty map if the current document has associated cookies", function() {
const cookies = new Cookies;
cookies.clear();
cookies.set("toJSON1", "foo").set("toJSON2", "bar");
@@ -353,8 +353,8 @@ describe("Cookies", () => {
});
});

describe(".toString()", () => {
it("should be the same value as `document.cookie` global property", () => {
describe(".toString()", function() {
it("should be the same value as `document.cookie` global property", function() {
expect(String(new Cookies)).to.equal(document.cookie);
});
});


Loading…
Cancel
Save