From 3283a4c8f59b668f61db133d5cd18e4beaaba663 Mon Sep 17 00:00:00 2001 From: Patrick Ammann Date: Tue, 13 Jul 2021 09:25:20 +0200 Subject: [PATCH] feat: #1 adapt unit-tests --- package.json | 2 +- test/unit/AccessTokenEvents.spec.js | 127 ---------------- test/unit/AccessTokenEvents.test.ts | 142 ++++++++++++++++++ test/unit/Event.spec.js | 113 -------------- test/unit/Event.test.ts | 122 ++++++++++++++++ test/unit/Log.spec.js | 140 ------------------ test/unit/Log.test.ts | 157 ++++++++++++++++++++ test/unit/UrlUtility.spec.js | 79 ---------- test/unit/UrlUtility.test.ts | 118 +++++++++++++++ test/unit/UserManager.spec.js | 158 -------------------- test/unit/UserManager.test.ts | 177 ++++++++++++++++++++++ test/unit/UserManagerEvents.spec.js | 56 ------- test/unit/UserManagerEvents.test.ts | 59 ++++++++ test/unit/WebStorageStateStore.spec.js | 175 ---------------------- test/unit/WebStorageStateStore.test.ts | 195 +++++++++++++++++++++++++ 15 files changed, 971 insertions(+), 849 deletions(-) delete mode 100644 test/unit/AccessTokenEvents.spec.js create mode 100644 test/unit/AccessTokenEvents.test.ts delete mode 100644 test/unit/Event.spec.js create mode 100644 test/unit/Event.test.ts delete mode 100644 test/unit/Log.spec.js create mode 100644 test/unit/Log.test.ts delete mode 100644 test/unit/UrlUtility.spec.js create mode 100644 test/unit/UrlUtility.test.ts delete mode 100644 test/unit/UserManager.spec.js create mode 100644 test/unit/UserManager.test.ts delete mode 100644 test/unit/UserManagerEvents.spec.js create mode 100644 test/unit/UserManagerEvents.test.ts delete mode 100644 test/unit/WebStorageStateStore.spec.js create mode 100644 test/unit/WebStorageStateStore.test.ts diff --git a/package.json b/package.json index 804d6425f..61dc020b1 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ }, "jest": { "clearMocks": true, - "testURL": "https://www.example.com/" + "testMatch": ["**/test/unit/*.test.ts"] }, "engines": { "node": ">=10" diff --git a/test/unit/AccessTokenEvents.spec.js b/test/unit/AccessTokenEvents.spec.js deleted file mode 100644 index 43005a720..000000000 --- a/test/unit/AccessTokenEvents.spec.js +++ /dev/null @@ -1,127 +0,0 @@ -// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. -// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. - -import { AccessTokenEvents } from '../../src/AccessTokenEvents'; - -import chai from 'chai'; -chai.should(); -let assert = chai.assert; - -class StubTimer { - - constructor() { - this.cancelWasCalled = false; - } - - init(duration) { - this.duration = duration; - } - - cancel() { - this.cancelWasCalled = true; - } - - addHandler(){} - removeHandler(){} -} - -describe("AccessTokenEvents", function () { - - let subject; - let accessTokenExpiringTimer; - let accessTokenExpiredTimer; - - beforeEach(function () { - accessTokenExpiringTimer = new StubTimer(); - accessTokenExpiredTimer = new StubTimer(); - subject = new AccessTokenEvents({ - accessTokenExpiringTimer, accessTokenExpiredTimer - }); - }); - - describe("constructor", function () { - - it("should use default expiringNotificationTime", function () { - subject._accessTokenExpiringNotificationTime.should.equal(60); - }); - - }); - - describe("load", function () { - - it("should cancel existing timers", function () { - subject.load({}); - - accessTokenExpiringTimer.cancelWasCalled.should.be.true; - accessTokenExpiredTimer.cancelWasCalled.should.be.true; - }); - - it("should initialize timers", function () { - subject.load({ - access_token:"token", - expires_in : 70 - }); - - accessTokenExpiringTimer.duration.should.equal(10); - accessTokenExpiredTimer.duration.should.equal(71); - }); - - it("should immediately schedule expiring timer if expiration is soon", function () { - subject.load({ - access_token:"token", - expires_in : 10 - }); - - accessTokenExpiringTimer.duration.should.equal(1); - }); - - it("should not initialize expiring timer if already expired", function () { - subject.load({ - access_token:"token", - expires_in : 0 - }); - - assert.isUndefined(accessTokenExpiringTimer.duration); - }); - - it("should initialize expired timer if already expired", function () { - subject.load({ - access_token:"token", - expires_in : 0 - }); - - accessTokenExpiredTimer.duration.should.equal(1); - }); - - it("should not initialize timers if no access token", function () { - subject.load({ - expires_in : 70 - }); - - assert.isUndefined(accessTokenExpiringTimer.duration); - assert.isUndefined(accessTokenExpiredTimer.duration); - }); - - it("should not initialize timers if no expiration on access token", function () { - subject.load({ - access_token:"token" - }); - - assert.isUndefined(accessTokenExpiringTimer.duration); - assert.isUndefined(accessTokenExpiredTimer.duration); - }); - }); - - describe("unload", function () { - - it("should cancel timers", function () { - - subject.unload(); - - accessTokenExpiringTimer.cancelWasCalled.should.be.true; - accessTokenExpiredTimer.cancelWasCalled.should.be.true; - }); - - }); - -}); diff --git a/test/unit/AccessTokenEvents.test.ts b/test/unit/AccessTokenEvents.test.ts new file mode 100644 index 000000000..f539990f0 --- /dev/null +++ b/test/unit/AccessTokenEvents.test.ts @@ -0,0 +1,142 @@ +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. + +import { AccessTokenEvents } from '../../src/AccessTokenEvents'; +import { Timer } from '../../src/Timer'; +import { User } from '../../src/User'; + +describe("AccessTokenEvents", () => { + + let subject: AccessTokenEvents; + let accessTokenExpiringTimer: StubTimer; + let accessTokenExpiredTimer: StubTimer; + + beforeEach(() => { + accessTokenExpiringTimer = new StubTimer("stub expiring timer"); + accessTokenExpiredTimer = new StubTimer("stub expired timer"); + subject = new AccessTokenEvents({ + accessTokenExpiringTimer, accessTokenExpiredTimer + }); + }); + + /* TODO: port-ts + describe("constructor", () => { + + it("should use default expiringNotificationTime", () => { + subject._accessTokenExpiringNotificationTime.should.equal(60); + }); + + });*/ + + describe("load", () => { + + it("should cancel existing timers", () => { + // act + subject.load({} as User); + + // assert + expect(accessTokenExpiringTimer.cancelWasCalled).toBe(true); + expect(accessTokenExpiredTimer.cancelWasCalled).toBe(true); + }); + + it("should initialize timers", () => { + // act + subject.load({ + access_token:"token", + expires_in : 70 + } as User); + + // assert + expect(accessTokenExpiringTimer.duration).toBe(10); + expect(accessTokenExpiredTimer.duration).toBe(71); + }); + + it("should immediately schedule expiring timer if expiration is soon", () => { + // act + subject.load({ + access_token:"token", + expires_in : 10 + } as User); + + // assert + expect(accessTokenExpiringTimer.duration).toBe(1); + }); + + it("should not initialize expiring timer if already expired", () => { + // act + subject.load({ + access_token:"token", + expires_in : 0 + } as User); + + // assert + expect(accessTokenExpiringTimer.duration).toBe(undefined); + }); + + it("should initialize expired timer if already expired", () => { + // act + subject.load({ + access_token:"token", + expires_in : 0 + } as User); + + // assert + expect(accessTokenExpiredTimer.duration).toBe(1); + }); + + it("should not initialize timers if no access token", () => { + // act + subject.load({ + expires_in : 70 + } as User); + + // assert + expect(accessTokenExpiringTimer.duration).toBe(undefined); + expect(accessTokenExpiredTimer.duration).toBe(undefined); + }); + + it("should not initialize timers if no expiration on access token", () => { + // act + subject.load({ + access_token:"token" + } as User); + + // assert + expect(accessTokenExpiringTimer.duration).toBe(undefined); + expect(accessTokenExpiredTimer.duration).toBe(undefined); + }); + }); + + describe("unload", () => { + + it("should cancel timers", () => { + // act + subject.unload(); + + // assert + expect(accessTokenExpiringTimer.cancelWasCalled).toBe(true); + expect(accessTokenExpiredTimer.cancelWasCalled).toBe(true); + }); + }); +}); + +class StubTimer extends Timer { + cancelWasCalled: boolean; + duration: any; + + constructor(name: string) { + super(name) + this.cancelWasCalled = false; + } + + init(duration: number) { + this.duration = duration; + } + + cancel() { + this.cancelWasCalled = true; + } + + addHandler() {} + removeHandler() {} +} diff --git a/test/unit/Event.spec.js b/test/unit/Event.spec.js deleted file mode 100644 index 20bfa19a0..000000000 --- a/test/unit/Event.spec.js +++ /dev/null @@ -1,113 +0,0 @@ - -// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. -// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. - -import { Event } from '../../src/Event'; - -import chai from 'chai'; -chai.should(); -let assert = chai.assert; - -describe("Event", function () { - - let subject; - - beforeEach(function () { - subject = new Event("test name"); - }); - - describe("addHandler", function () { - - it("should allow callback to be invoked", function () { - var cb = function () { - cb.wasCalled = true; - }; - subject.addHandler(cb); - - subject.raise(); - - cb.wasCalled.should.be.true; - }); - - it("should allow multiple callbacks", function () { - var count = 0; - var cb = function () { - count++; - }; - subject.addHandler(cb); - subject.addHandler(cb); - subject.addHandler(cb); - subject.addHandler(cb); - - subject.raise(); - - count.should.equal(4); - }); - - }); - - describe("removeHandler", function () { - - it("should remove callback from being invoked", function () { - var cb = function () { - cb.wasCalled = true; - }; - cb.wasCalled = false; - - subject.addHandler(cb); - subject.removeHandler(cb); - subject.raise(); - - cb.wasCalled.should.be.false; - }); - - it("should remove individual callback", function () { - var count = 0; - var cb1 = function () { - count++; - }; - var cb2 = function () { - cb2.wasCalled = true; - }; - - subject.addHandler(cb1); - subject.addHandler(cb2); - subject.addHandler(cb1); - subject.removeHandler(cb1); - subject.removeHandler(cb1); - - subject.raise(); - - count.should.equal(0); - cb2.wasCalled.should.be.true; - }); - - }); - - describe("raise", function () { - - it("should pass params", function () { - var cb = function (a,b,c) { - a.should.equal(1); - b.should.equal(2); - c.should.equal(3); - }; - subject.addHandler(cb); - - subject.raise(1,2,3); - }); - - it("should allow passing no params", function () { - var cb = function (a,b,c) { - assert.isUndefined(a); - assert.isUndefined(b); - assert.isUndefined(c); - }; - subject.addHandler(cb); - - subject.raise(); - }); - - }); - -}); diff --git a/test/unit/Event.test.ts b/test/unit/Event.test.ts new file mode 100644 index 000000000..c1cf20be5 --- /dev/null +++ b/test/unit/Event.test.ts @@ -0,0 +1,122 @@ +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. + +import { Event } from '../../src/Event'; + +describe("Event", () => { + + let subject: Event; + + beforeEach(() => { + subject = new Event("test name"); + }); + + describe("addHandler", () => { + + it("should allow callback to be invoked", () => { + // arrange + var cb = jest.fn(); + + // act + subject.addHandler(cb); + subject.raise(); + + // assert + expect(cb).toBeCalled(); + }); + + it("should allow multiple callbacks", () => { + // arrange + var cb = jest.fn(); + + // act + subject.addHandler(cb); + subject.addHandler(cb); + subject.addHandler(cb); + subject.addHandler(cb); + subject.raise(); + + // assert + expect(cb).toBeCalledTimes(4); + }); + }); + + describe("removeHandler", () => { + + it("should remove callback from being invoked", () => { + // arrange + var cb = jest.fn(); + + // act + subject.addHandler(cb); + subject.removeHandler(cb); + subject.raise(); + + // assert + expect(cb).toBeCalledTimes(0); + }); + + it("should remove individual callback", () => { + // arrange + var cb1 = jest.fn(); + var cb2 = jest.fn(); + + // act + subject.addHandler(cb1); + subject.addHandler(cb2); + subject.addHandler(cb1); + subject.removeHandler(cb1); + subject.removeHandler(cb1); + + subject.raise(); + + // assert + expect(cb1).toBeCalledTimes(0); + expect(cb2).toBeCalledTimes(1); + }); + }); + + describe("raise", () => { + + it("should pass params", () => { + // arrange + let a: any = 10; + let b: any = 11; + let c: any = 12; + var cb = function (arg_a: any, arg_b: any, arg_c: any) { + a = arg_a; + b = arg_b; + c = arg_c; + }; + subject.addHandler(cb); + + // act + subject.raise(1, 2, 3); + + // assert + expect(a).toBe(1); + expect(b).toBe(2); + expect(c).toBe(3); + }); + + it("should allow passing no params", () => { + // arrange + let a: any = 10; + let b: any = 11; + let c: any = 12; + var cb = function (arg_a: any, arg_b: any, arg_c: any) { + a = arg_a; + b = arg_b; + c = arg_c; + }; + subject.addHandler(cb); + + subject.raise(); + + // assert + expect(a).toBe(undefined); + expect(b).toBe(undefined); + expect(c).toBe(undefined); + }); + }); +}); diff --git a/test/unit/Log.spec.js b/test/unit/Log.spec.js deleted file mode 100644 index d6c560a7f..000000000 --- a/test/unit/Log.spec.js +++ /dev/null @@ -1,140 +0,0 @@ -// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. -// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. - -import { Log } from '../../src/Log'; - -import chai from 'chai'; -chai.should(); -let assert = chai.assert; - -describe("Log", function() { - - beforeEach(function() { - Log.reset(); - Log.level = Log.INFO; - }); - - describe("level", function() { - - it("should not log when set to NONE", function() { - let stub = new StubLog(); - Log.logger = stub; - Log.level = Log.NONE; - - Log.info("test info"); - Log.warn("test warn"); - Log.error("test error"); - - stub.infoWasCalled.should.be.false; - stub.warnWasCalled.should.be.false; - stub.errorWasCalled.should.be.false; - }); - - it("should not log info or warn for ERROR level", function() { - let stub = new StubLog(); - - Log.logger = stub; - Log.level = Log.ERROR; - - Log.info("test info"); - Log.warn("test warn"); - Log.error("test error"); - - stub.infoWasCalled.should.be.false; - stub.warnWasCalled.should.be.false; - stub.errorWasCalled.should.be.true; - }); - - it("should not log info for WARN level", function() { - let stub = new StubLog(); - - Log.logger =stub; - Log.level = Log.WARN; - - Log.info("test info"); - Log.warn("test warn"); - Log.error("test error"); - - stub.infoWasCalled.should.be.false; - stub.warnWasCalled.should.be.true; - stub.errorWasCalled.should.be.true; - }); - - it("should log to all for INFO level", function() { - let stub = new StubLog(); - - Log.logger = stub; - Log.level = Log.INFO; - - Log.info("test info"); - Log.warn("test warn"); - Log.error("test error"); - - stub.infoWasCalled.should.be.true; - stub.warnWasCalled.should.be.true; - stub.errorWasCalled.should.be.true; - }); - - }); - - describe("logger", function() { - - it("should use the logger specified", function() { - let stub = new StubLog(); - Log.logger = stub; - - Log.info("test info"); - Log.warn("test warn"); - Log.error("test error"); - - stub.infoParam.should.equal("test info"); - stub.warnParam.should.equal("test warn"); - stub.errorParam.should.equal("test error"); - }); - - }); - - describe("info", function() { - - it("should work with no config", function() { - Log.info("test"); - }); - - }); - - describe("warn", function() { - - it("should work with no config", function() { - Log.warn("test"); - }); - - }); - - describe("error", function() { - - it("should work with no config", function() { - Log.error("test"); - }); - - }); -}); - -class StubLog { - constructor(){ - this.infoWasCalled = false; - this.warnWasCalled = false; - this.errorWasCalled = false; - } - info(arg) { - this.infoParam = arg; - this.infoWasCalled = true; - } - warn(arg) { - this.warnParam = arg; - this.warnWasCalled = true; - } - error(arg) { - this.errorParam = arg; - this.errorWasCalled = true; - } -} diff --git a/test/unit/Log.test.ts b/test/unit/Log.test.ts new file mode 100644 index 000000000..b17f4eba4 --- /dev/null +++ b/test/unit/Log.test.ts @@ -0,0 +1,157 @@ +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. + +import { Log, Logger } from '../../src/Log'; + +describe("Log", () => { + beforeEach(() => { + Log.reset(); + Log.level = Log.INFO; + }); + + describe("level", () => { + + it("should not log when set to NONE", () => { + // arrange + let stub = new StubLog(); + Log.logger = stub; + Log.level = Log.NONE; + + // act + Log.info("test info"); + Log.warn("test warn"); + Log.error("test error"); + + // assert + expect(stub.infoWasCalled).toBe(false); + expect(stub.warnWasCalled).toBe(false); + expect(stub.errorWasCalled).toBe(false); + }); + + it("should not log info or warn for ERROR level", () => { + // arrange + let stub = new StubLog(); + Log.logger = stub; + Log.level = Log.ERROR; + + // act + Log.info("test info"); + Log.warn("test warn"); + Log.error("test error"); + + // assert + expect(stub.infoWasCalled).toBe(false); + expect(stub.warnWasCalled).toBe(false); + expect(stub.errorWasCalled).toBe(true); + }); + + it("should not log info for WARN level", () => { + // arrange + let stub = new StubLog(); + Log.logger = stub; + Log.level = Log.WARN; + + // act + Log.info("test info"); + Log.warn("test warn"); + Log.error("test error"); + + // assert + expect(stub.infoWasCalled).toBe(false); + expect(stub.warnWasCalled).toBe(true); + expect(stub.errorWasCalled).toBe(true); + }); + + it("should log to all for INFO level", () => { + // arrange + let stub = new StubLog(); + Log.logger = stub; + Log.level = Log.INFO; + + // act + Log.info("test info"); + Log.warn("test warn"); + Log.error("test error"); + + // assert + expect(stub.infoWasCalled).toBe(true); + expect(stub.warnWasCalled).toBe(true); + expect(stub.errorWasCalled).toBe(true); + }); + }); + + describe("logger", () => { + + it("should use the logger specified", () => { + // arrange + let stub = new StubLog(); + Log.logger = stub; + + // act + Log.info("test info"); + Log.warn("test warn"); + Log.error("test error"); + + // assert + expect(stub.infoParam).toBe("test info"); + expect(stub.warnParam).toBe("test warn"); + expect(stub.errorParam).toBe("test error"); + }); + }); + + describe("info", () => { + + it("should work with no config", () => { + Log.info("test"); + }); + }); + + describe("warn", () => { + + it("should work with no config", () => { + Log.warn("test"); + }); + + }); + + describe("error", () => { + + it("should work with no config", () => { + Log.error("test"); + }); + }); +}); + +class StubLog implements Logger { + debugWasCalled: boolean; + infoWasCalled: boolean; + warnWasCalled: boolean; + errorWasCalled: boolean; + debugParam: any; + infoParam: any; + warnParam: any; + errorParam: any; + + constructor() { + this.debugWasCalled = false; + this.infoWasCalled = false; + this.warnWasCalled = false; + this.errorWasCalled = false; + } + debug(arg: any) { + this.debugParam = arg; + this.debugWasCalled = true; + } + info(arg: any) { + this.infoParam = arg; + this.infoWasCalled = true; + } + warn(arg: any) { + this.warnParam = arg; + this.warnWasCalled = true; + } + error(arg: any) { + this.errorParam = arg; + this.errorWasCalled = true; + } +} diff --git a/test/unit/UrlUtility.spec.js b/test/unit/UrlUtility.spec.js deleted file mode 100644 index bc9437672..000000000 --- a/test/unit/UrlUtility.spec.js +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. -// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. - -import { UrlUtility } from '../../src/UrlUtility'; - -import chai from 'chai'; -chai.should(); -let assert = chai.assert; - -describe("UrlUtility", function() { - - describe("addQueryParam", function() { - - it("should add ? if not present", function() { - UrlUtility.addQueryParam("url", "foo", "test").should.equal("url?foo=test"); - }); - - it("should not add ? if already present", function() { - UrlUtility.addQueryParam("url?", "foo", "test").should.equal("url?foo=test"); - }); - - it("should add & if needed", function() { - UrlUtility.addQueryParam("url?x=1", "foo", "test").should.equal("url?x=1&foo=test"); - }); - - it("should urlencode key and value", function() { - UrlUtility.addQueryParam("url", "#", "#").should.equal("url?%23=%23"); - }); - }); - - describe("parseUrlFragment", function() { - - it("should parse key/value pairs", function() { - let result = UrlUtility.parseUrlFragment("a=apple&b=banana&c=carrot"); - result.should.deep.equal({ a: "apple", b: "banana", c: "carrot" }); - }); - - it("should parse any order", function() { - let result = UrlUtility.parseUrlFragment("b=banana&c=carrot&a=apple"); - result.should.deep.equal({ a: "apple", b: "banana", c: "carrot" }); - }); - - it("should parse past host name and hash fragment", function() { - let result = UrlUtility.parseUrlFragment("http://server?test1=xoxo&test2=xoxo/#a=apple&b=banana&c=carrot"); - result.should.deep.equal({ a: "apple", b: "banana", c: "carrot" }); - }); - - it("should parse query string", function() { - let result = UrlUtility.parseUrlFragment("http://server?test1=xoxo&test2=yoyo", "?"); - result.should.deep.equal({ test1: "xoxo", test2: "yoyo" }); - }); - - it("should parse query string up to hash", function() { - let result = UrlUtility.parseUrlFragment("http://server?test1=xoxo&test2=yoyo#a=apple&b=banana&c=carrot", "?"); - result.should.deep.equal({ test1: "xoxo", test2: "yoyo" }); - }); - - it("should return error for long values", function() { - let result = UrlUtility.parseUrlFragment("a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple"); - result.should.have.property('error'); - }); - - it("should use Global.location when no value is passed", function() { - let w = { - location: { - href : "a=apple&b=banana&c=carrot" - } - }; - let result = UrlUtility.parseUrlFragment(null, "#", w); - result.should.deep.equal({ a: "apple", b: "banana", c: "carrot" }); - }); - - it("should return empty object for empty string", function() { - let result = UrlUtility.parseUrlFragment(""); - result.should.deep.equal({}); - }); - }); - -}); diff --git a/test/unit/UrlUtility.test.ts b/test/unit/UrlUtility.test.ts new file mode 100644 index 000000000..d61d3ff39 --- /dev/null +++ b/test/unit/UrlUtility.test.ts @@ -0,0 +1,118 @@ +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. + +import { UrlUtility } from '../../src/UrlUtility'; + +describe("UrlUtility", () => { + + describe("addQueryParam", () => { + + it("should add ? if not present", () => { + // act + let result = UrlUtility.addQueryParam("url", "foo", "test"); + + // assert + expect(result).toEqual("url?foo=test"); + }); + + it("should not add ? if already present", () => { + // act + let result = UrlUtility.addQueryParam("url?", "foo", "test"); + + // assert + expect(result).toEqual("url?foo=test"); + }); + + it("should add & if needed", () => { + // act + let result = UrlUtility.addQueryParam("url?x=1", "foo", "test"); + + // assert + expect(result).toEqual("url?x=1&foo=test"); + }); + + it("should urlencode key and value", () => { + // act + let result = UrlUtility.addQueryParam("url", "#", "#"); + + // assert + expect(result).toEqual("url?%23=%23"); + }); + }); + + describe("parseUrlFragment", () => { + + it("should parse key/value pairs", () => { + // act + let result = UrlUtility.parseUrlFragment("a=apple&b=banana&c=carrot"); + + // assert + expect(result).toEqual({ a: "apple", b: "banana", c: "carrot" }); + }); + + it("should parse any order", () => { + // act + let result = UrlUtility.parseUrlFragment("b=banana&c=carrot&a=apple"); + + // assert + expect(result).toEqual({ a: "apple", b: "banana", c: "carrot" }); + }); + + it("should parse past host name and hash fragment", () => { + // act + let result = UrlUtility.parseUrlFragment("http://server?test1=xoxo&test2=xoxo/#a=apple&b=banana&c=carrot"); + + // assert + expect(result).toEqual({ a: "apple", b: "banana", c: "carrot" }); + }); + + it("should parse query string", () => { + // act + let result = UrlUtility.parseUrlFragment("http://server?test1=xoxo&test2=yoyo", "?"); + + // assert + expect(result).toEqual({ test1: "xoxo", test2: "yoyo" }); + }); + + it("should parse query string up to hash", () => { + // act + let result = UrlUtility.parseUrlFragment("http://server?test1=xoxo&test2=yoyo#a=apple&b=banana&c=carrot", "?"); + + // assert + expect(result).toEqual({ test1: "xoxo", test2: "yoyo" }); + }); + + it("should return error for long values", () => { + // act + let result = UrlUtility.parseUrlFragment("a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple"); + + // assert + expect(result).toHaveProperty("error"); + }); + + /* TODO: port-ts + it("should use Global.location when no value is passed", () => { + // arrange + let w = { + location: { + href : "a=apple&b=banana&c=carrot" + } + }; + + // act + let result = UrlUtility.parseUrlFragment(null, "#", w); + + // assert + expect(result).toEqual({ a: "apple", b: "banana", c: "carrot" }); + }); + */ + + it("should return empty object for empty string", () => { + // act + let result = UrlUtility.parseUrlFragment(""); + + // assert + expect(result).toEqual({}); + }); + }); +}); diff --git a/test/unit/UserManager.spec.js b/test/unit/UserManager.spec.js deleted file mode 100644 index dba949f21..000000000 --- a/test/unit/UserManager.spec.js +++ /dev/null @@ -1,158 +0,0 @@ -// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. -// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. - -import { UserManager } from '../../src/UserManager'; -import { Log } from '../../src/Log'; -import { Global } from '../../src/Global'; -import { UserManagerSettings } from '../../src/UserManagerSettings'; -import { User } from '../../src/User'; - -import { StubMetadataService } from './StubMetadataService'; -import { StubSilentRenewService } from './StubSilentRenewService'; -import { StubStateStore } from './StubStateStore'; -import { StubResponseValidator } from './StubResponseValidator'; -import { StubTokenRevocationClient } from './StubTokenRevocationClient'; - -import chai from 'chai'; -chai.should(); -let assert = chai.assert; - -describe("UserManager", function () { - let settings; - let subject; - let stubMetadataService; - let stubStateStore; - let stubValidator; - let stubSilentRenewService; - let stubNavigator; - let stubUserStore; - let stubTokenRevocationClient; - - beforeEach(function () { - - Global._testing(); - - Log.logger = console; - Log.level = Log.NONE; - - stubNavigator = {}; - stubUserStore = new StubStateStore(); - stubStateStore = new StubStateStore(); - stubValidator = new StubResponseValidator(); - stubSilentRenewService = new StubSilentRenewService(); - stubMetadataService = new StubMetadataService(); - stubTokenRevocationClient = new StubTokenRevocationClient(); - - settings = { - authority: 'http://sts/oidc', - client_id: 'client', - monitorSession : false, - navigator: stubNavigator, - userStore: stubUserStore, - stateStore: stubStateStore, - ResponseValidatorCtor: () => stubValidator, - MetadataServiceCtor: () => stubMetadataService - }; - - subject = new UserManager(settings, - () => stubSilentRenewService, - null, - () => stubTokenRevocationClient); - }); - - describe("constructor", function () { - - it("should accept settings", function () { - subject.settings.client_id.should.equal('client'); - }); - - }); - - describe("settings", function () { - - it("should be UserManagerSettings", function () { - subject.settings.should.be.instanceof(UserManagerSettings); - }); - - }); - - describe("userLoaded", function () { - - it("should be able to call getUser without recursion", function (done) { - - stubUserStore.item = new User({id_token:"id_token"}).toStorageString(); - - subject.events.addUserLoaded(user => { - subject.getUser().then(user => { - done(); - }); - }); - - subject.events.load({}); - }); - - }); - - describe("signinSilent", function(){ - - it("should pass silentRequestTimeout from settings", function(done) { - - stubUserStore.item = new User({id_token:"id_token"}).toStorageString(); - - settings.silentRequestTimeout = 123; - settings.silent_redirect_uri = "http://client/silent_callback"; - subject = new UserManager(settings); - - subject._signin = function(args, nav, navArgs){ - Log.debug("_signin", args, nav, navArgs); - - navArgs.silentRequestTimeout.should.equal(123); - done(); - return Promise.resolve() - } - subject.signinSilent(); - }); - - it("should pass silentRequestTimeout from params", function(done){ - - stubUserStore.item = new User({id_token:"id_token"}).toStorageString(); - - settings.silent_redirect_uri = "http://client/silent_callback"; - subject = new UserManager(settings); - - subject._signin = function(args, nav, navArgs){ - navArgs.silentRequestTimeout.should.equal(234); - done(); - return Promise.resolve() - } - subject.signinSilent({silentRequestTimeout:234}); - }); - - it("should pass prompt from params", function(done){ - - stubUserStore.item = new User({id_token:"id_token"}).toStorageString(); - - settings.silent_redirect_uri = "http://client/silent_callback"; - subject = new UserManager(settings); - - subject._signin = function(args, nav, navArgs){ - args.prompt.should.equal("foo"); - done(); - return Promise.resolve() - } - subject.signinSilent({prompt:"foo"}); - }); - - it("should work when having no User present", function(done) { - settings.silent_redirect_uri = "http://client/silent_callback"; - subject = new UserManager(settings); - - subject._signin = function(){ - done(); - return Promise.resolve() - } - subject.signinSilent({prompt:"foo"}); - }) - }); - -}); diff --git a/test/unit/UserManager.test.ts b/test/unit/UserManager.test.ts new file mode 100644 index 000000000..f1db58be4 --- /dev/null +++ b/test/unit/UserManager.test.ts @@ -0,0 +1,177 @@ +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. + +import { mocked } from 'ts-jest/utils'; +import { Log } from '../../src/Log'; +import { UserManager } from '../../src/UserManager'; +import { UserManagerSettings } from '../../src/UserManagerSettings'; +import { User } from '../../src/User'; +import { WebStorageStateStore } from '../../src/WebStorageStateStore'; +//import { RedirectNavigator } from '../../src/RedirectNavigator'; + +// workaround jest parse error +jest.mock('../../jsrsasign/dist/jsrsasign.js', () => { + return { + jws: jest.fn(), + KEYUTIL: jest.fn(), + X509: jest.fn(), + crypto: jest.fn(), + hextob64u: jest.fn(), + b64tohex: jest.fn() + }; +}); + +describe("UserManager", () => { + let settings: UserManagerSettings; + let userStoreMock: any; + let subject: UserManager; + + beforeEach(() => { + Log.logger = console; + Log.level = Log.NONE; + + userStoreMock = mocked(new WebStorageStateStore()) + + settings = { + authority: 'http://sts/oidc', + client_id: 'client', + monitorSession : false, + userStore: userStoreMock, + } as unknown as UserManagerSettings; + + subject = new UserManager(settings); + }); + + describe("constructor", () => { + + it("should accept settings", () => { + // act + expect(subject.settings.client_id).toBe('client'); + }); + }); + + describe("settings", () => { + + it("should be UserManagerSettings", () => { + // act + expect(subject.settings).toBeInstanceOf(UserManagerSettings); + }); + }); + + describe("userLoaded", () => { + + it("should be able to call getUser without recursion", async () => { + // arrange + const user = new User({ id_token: "id_token" }); + userStoreMock.item = user.toStorageString(); + + subject.events.addUserLoaded(async (_user) => { + await subject.getUser(); + }); + + // act + subject.events.load({} as User); + }); + }); + + describe("signinSilent", function(){ + + it("should pass silentRequestTimeout from settings", async () => { + // arrange + const user = new User({id_token:"id_token"}); + userStoreMock.item = user.toStorageString(); + + settings = { + ...settings, + silentRequestTimeout: 123, + silent_redirect_uri: "http://client/silent_callback" + } as unknown as UserManagerSettings; + subject = new UserManager(settings); + + let navArgs: any = null; + + // @ts-ignore + subject._signin = function(args: any, nav: any, arg_navArgs: any) { + Log.debug("_signin", args, nav, navArgs); + + navArgs = arg_navArgs; + return Promise.resolve() + } + + // act + await subject.signinSilent(); + + // assert + expect(navArgs.silentRequestTimeout).toBe(123); + }); + + it("should pass silentRequestTimeout from params", async () =>{ + // arrange + const user = new User({id_token:"id_token"}); + userStoreMock.item = user.toStorageString(); + + settings = { + ...settings, + silent_redirect_uri: "http://client/silent_callback" + } as unknown as UserManagerSettings; + subject = new UserManager(settings); + + let navArgs: any = null; + + // @ts-ignore + subject._signin = function(args: any, nav: any, arg_navArgs: any) { + navArgs = arg_navArgs; + return Promise.resolve() + } + + // act + await subject.signinSilent({ silentRequestTimeout: 234 }); + + // assert + expect(navArgs.silentRequestTimeout).toBe(234); + }); + + it("should pass prompt from params", async () =>{ + // arrange + const user = new User({id_token:"id_token"}) + userStoreMock.item = user.toStorageString(); + + settings = { + ...settings, + silent_redirect_uri: "http://client/silent_callback" + } as unknown as UserManagerSettings; + subject = new UserManager(settings); + + let args: any = null; + + // @ts-ignore + subject._signin = function(arg_args: any, nav: any, navArgs: any) { + args = arg_args; + return Promise.resolve() + } + + // act + await subject.signinSilent({ prompt:"foo" }); + + // assert + expect(args.prompt).toBe("foo"); + }); + + it("should work when having no User present", async () => { + // arrange + settings = { + ...settings, + silent_redirect_uri: "http://client/silent_callback" + } as unknown as UserManagerSettings; + subject = new UserManager(settings); + + // @ts-ignore + subject._signin = function(args: any, nav: any, arg_navArgs: any) { + return Promise.resolve() + } + + // act + await subject.signinSilent({ prompt:"foo" }); + }); + }); +}); diff --git a/test/unit/UserManagerEvents.spec.js b/test/unit/UserManagerEvents.spec.js deleted file mode 100644 index 72f0e2e0a..000000000 --- a/test/unit/UserManagerEvents.spec.js +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. -// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. - -import { UserManagerEvents } from '../../src/UserManagerEvents'; - -import chai from 'chai'; -chai.should(); -let assert = chai.assert; - -describe("UserManagerEvents", function () { - - let subject; - - beforeEach(function () { - subject = new UserManagerEvents(); - }); - - describe("silent renew error", function () { - - it("should allow callback", function () { - var cb = function () { - cb.wasCalled = true; - }; - subject.addSilentRenewError(cb); - - subject._raiseSilentRenewError(new Error("boom")); - - cb.wasCalled.should.be.true; - }); - - it("should allow unregistering callback", function () { - var cb = function () { - cb.wasCalled = true; - }; - cb.wasCalled = false; - - subject.addSilentRenewError(cb); - subject.removeSilentRenewError(cb); - - subject._raiseSilentRenewError(new Error("boom")); - - cb.wasCalled.should.be.false; - }); - - it("should pass error to callback", function () { - var cb = function (e) { - e.message.should.equal("boom"); - }; - subject.addSilentRenewError(cb); - - subject._raiseSilentRenewError(new Error("boom")); - }); - - }); - -}); diff --git a/test/unit/UserManagerEvents.test.ts b/test/unit/UserManagerEvents.test.ts new file mode 100644 index 000000000..3254b8a5e --- /dev/null +++ b/test/unit/UserManagerEvents.test.ts @@ -0,0 +1,59 @@ +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. + +import { UserManagerEvents } from '../../src/UserManagerEvents'; +import { UserManagerSettings } from '../../src/UserManagerSettings'; + +describe("UserManagerEvents", () => { + + let subject: UserManagerEvents; + + beforeEach(() => { + subject = new UserManagerEvents({} as UserManagerSettings); + }); + + describe("silent renew error", () => { + + it("should allow callback", () => { + // arrange + var cb = jest.fn(); + + // act + subject.addSilentRenewError(cb); + subject._raiseSilentRenewError(new Error("boom")); + + // assert + expect(cb).toBeCalled(); + }); + + it("should allow unregistering callback", () => { + // arrange + var cb = jest.fn(); + + // act + subject.addSilentRenewError(cb); + subject.removeSilentRenewError(cb); + subject._raiseSilentRenewError(new Error("boom")); + + // assert + expect(cb).toBeCalledTimes(0); + }); + + it("should pass error to callback", () => { + // arrange + let e: Error | null = null; + var cb = function (arg_e: Error) { + console.log(arg_e); + e = arg_e; + }; + const expected = new Error("boom"); + + // act + subject.addSilentRenewError(cb); + subject._raiseSilentRenewError(expected); + + // assert + expect(e).toBe(expected); + }); + }); +}); diff --git a/test/unit/WebStorageStateStore.spec.js b/test/unit/WebStorageStateStore.spec.js deleted file mode 100644 index b5b06bd4d..000000000 --- a/test/unit/WebStorageStateStore.spec.js +++ /dev/null @@ -1,175 +0,0 @@ -// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. -// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. - -import { Log } from '../../src/Log'; -import { WebStorageStateStore } from '../../src/WebStorageStateStore'; -import { InMemoryWebStorage } from '../../src/InMemoryWebStorage'; - -import chai from 'chai'; -chai.should(); -let assert = chai.assert; -let expect = chai.expect; - -describe("WebStorageStateStore", function() { - let prefix; - let subject; - let store; - - beforeEach(function() { - prefix = ""; - store = new InMemoryWebStorage(); - subject = new WebStorageStateStore({ prefix: prefix, store: store }); - }); - - describe("set", function() { - - it("should return a promise", function() { - var p = subject.set("key", "value"); - p.should.be.instanceof(Promise); - p.catch(e=>{}); - }); - - it("should store item", function(done) { - subject.set("key", "value").then(() => { - store.getItem("key").should.equal("value"); - done(); - }) - }); - - it("should use prefix if specified", function(done) { - prefix = "foo."; - subject = new WebStorageStateStore({ prefix: prefix, store: store }); - - subject.set("key", "value").then(() => { - store.getItem(prefix + "key").should.equal("value"); - done(); - }) - }); - - }); - - describe("remove", function() { - - it("should return a promise", function() { - var p = subject.remove("key"); - p.should.be.instanceof(Promise); - p.catch(e=>{}); - }); - - it("should remove item", function(done) { - store.setItem("key", "value"); - - subject.remove("key").then(item => { - expect(store.getItem("key")).to.be.undefined; - done(); - }); - }); - - it("should return value if exists", function(done) { - store.setItem("key", "test"); - - subject.remove("key").then(value => { - value.should.equal('test'); - done(); - }); - }); - - it("should return undefined if doesn't exist", function(done) { - subject.remove("key").then(value => { - expect(value).to.be.undefined; - done(); - }); - }); - - it("should use prefix if specified", function(done) { - prefix = "foo."; - subject = new WebStorageStateStore({ prefix: prefix, store: store }); - - store.setItem("foo.key", "value"); - - subject.remove("key").then(item => { - expect(store.getItem("foo.key")).to.be.undefined; - done(); - }); - }); - - }); - - describe("get", function() { - - it("should return a promise", function() { - var p = subject.get("key"); - p.should.be.instanceof(Promise); - }); - - it("should return value if exists", function(done) { - store.setItem("key", "test"); - - subject.get("key").then(value => { - value.should.equal('test'); - done(); - }); - }); - - it("should return undefined if doesn't exist", function(done) { - subject.get("key").then(value => { - expect(value).to.be.undefined; - done(); - }); - }); - - it("should use prefix if specified", function(done) { - prefix = "foo."; - subject = new WebStorageStateStore({ prefix: prefix, store: store }); - - store.setItem("foo.key", "value"); - - subject.get("key").then(item => { - item.should.equal("value"); - done(); - }); - }); - - }); - - describe("getAllKeys", function() { - - it("should return a promise", function() { - var p = subject.getAllKeys(); - p.should.be.instanceof(Promise); - p.catch(e=>{}); - }); - - it("should return keys", function(done) { - store.setItem("key1", "test"); - store.setItem("key2", "test"); - - subject.getAllKeys().then(keys => { - keys.should.deep.equal(["key1", "key2"]); - done(); - }); - }); - - it("should return keys without prefix", function(done) { - prefix = "foo."; - subject = new WebStorageStateStore({ prefix: prefix, store: store }); - - store.setItem("foo.key1", "test"); - store.setItem("foo.key2", "test"); - - subject.getAllKeys().then(keys => { - keys.should.deep.equal(["key1", "key2"]); - done(); - }); - }); - - it("should return empty keys when empty", function(done) { - subject.getAllKeys().then(keys => { - keys.should.deep.equal([]); - done(); - }); - }); - - }); - -}); diff --git a/test/unit/WebStorageStateStore.test.ts b/test/unit/WebStorageStateStore.test.ts new file mode 100644 index 000000000..cfd2d6a3a --- /dev/null +++ b/test/unit/WebStorageStateStore.test.ts @@ -0,0 +1,195 @@ +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. + +import { WebStorageStateStore } from '../../src/WebStorageStateStore'; +import { InMemoryWebStorage } from '../../src/InMemoryWebStorage'; + +describe("WebStorageStateStore", () => { + let prefix: string; + let store: Storage; + let subject: WebStorageStateStore; + + beforeEach(() => { + prefix = ""; + store = new InMemoryWebStorage(); + subject = new WebStorageStateStore({ prefix: prefix, store: store }); + }); + + describe("set", () => { + + it("should return a promise", () => { + // act + let p = subject.set("key", "value"); + + // assert + expect(p).toBeInstanceOf(Promise); + }); + + it("should store item", async () => { + // act + await subject.set("key", "value"); + let result = await store.getItem("key"); + + // assert + expect(result).toBe("value"); + }); + + it("should use prefix if specified", async () => { + // arrange + prefix = "foo."; + subject = new WebStorageStateStore({ prefix: prefix, store: store }); + + // act + await subject.set("key", "value"); + + // assert + let result = await store.getItem(prefix + "key"); + expect(result).toBe("value"); + }); + }); + + describe("remove", () => { + + it("should return a promise", () => { + // act + let p = subject.remove("key"); + + // assert + expect(p).toBeInstanceOf(Promise); + }); + + it("should remove item", async () => { + // arrange + await store.setItem("key", "value"); + + // act + await subject.remove("key"); + let result = await store.getItem("key"); + + // assert + expect(result).toBeUndefined(); + }); + + it("should return value if exists", async () => { + // arrange + await store.setItem("key", "test"); + + // act + let result = await subject.remove("key"); + + // assert + expect(result).toBe("test"); + }); + + it("should return undefined if doesn't exist", async () => { + // act + let result = await subject.remove("key"); + + // assert + expect(result).toBeUndefined(); + }); + + it("should use prefix if specified", async () => { + // arrange + prefix = "foo."; + subject = new WebStorageStateStore({ prefix: prefix, store: store }); + await subject.set("key", "value"); + + // act + let result = await subject.remove("key"); + + // assert + expect(result).toBe("value"); + }); + + }); + + describe("get", () => { + + it("should return a promise", () => { + // act + var p = subject.get("key"); + + // assert + expect(p).toBeInstanceOf(Promise); + }); + + it("should return value if exists", async () => { + // arrange + await store.setItem("key", "test"); + + // act + let result = await subject.get("key"); + + // assert + expect(result).toBe("test"); + }); + + it("should return undefined if doesn't exist", async () => { + // act + let result = await subject.get("key"); + + // assert + expect(result).toBeUndefined(); + }); + + it("should use prefix if specified", async () => { + // arrange + prefix = "foo."; + subject = new WebStorageStateStore({ prefix: prefix, store: store }); + store.setItem("foo.key", "value"); + + // act + let result = await subject.get("key"); + + // assert + expect(result).toBe("value"); + }); + + }); + + describe("getAllKeys", () => { + + it("should return a promise", () => { + // act + var p = subject.getAllKeys(); + + // assert + expect(p).toBeInstanceOf(Promise); + }); + + it("should return keys", async () => { + // arrange + await store.setItem("key1", "test"); + await store.setItem("key2", "test"); + + // act + let result = await subject.getAllKeys(); + + // assert + expect(result).toStrictEqual(["key1", "key2"]); + }); + + it("should return keys without prefix", async () => { + // arrange + prefix = "foo."; + subject = new WebStorageStateStore({ prefix: prefix, store: store }); + await store.setItem("foo.key1", "test"); + await store.setItem("foo.key2", "test"); + + // act + let result = await subject.getAllKeys(); + + // assert + expect(result).toStrictEqual(["key1", "key2"]); + }); + + it("should return empty keys when empty", async () => { + // act + let result = await subject.getAllKeys(); + + // assert + expect(result).toStrictEqual([]); + }); + }); +});