Skip to content

Commit

Permalink
feat: #1 adapt unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Ammann committed Jul 13, 2021
1 parent 2022544 commit 3283a4c
Show file tree
Hide file tree
Showing 15 changed files with 971 additions and 849 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
},
"jest": {
"clearMocks": true,
"testURL": "https://www.example.com/"
"testMatch": ["**/test/unit/*.test.ts"]
},
"engines": {
"node": ">=10"
Expand Down
127 changes: 0 additions & 127 deletions test/unit/AccessTokenEvents.spec.js

This file was deleted.

142 changes: 142 additions & 0 deletions test/unit/AccessTokenEvents.test.ts
Original file line number Diff line number Diff line change
@@ -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() {}
}
113 changes: 0 additions & 113 deletions test/unit/Event.spec.js

This file was deleted.

Loading

0 comments on commit 3283a4c

Please sign in to comment.