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 14, 2021
1 parent 56d55f2 commit b27bafb
Show file tree
Hide file tree
Showing 51 changed files with 5,771 additions and 4,722 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).toEqual(true);
expect(accessTokenExpiredTimer.cancelWasCalled).toEqual(true);
});

it("should initialize timers", () => {
// act
subject.load({
access_token:"token",
expires_in : 70
} as User);

// assert
expect(accessTokenExpiringTimer.duration).toEqual(10);
expect(accessTokenExpiredTimer.duration).toEqual(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).toEqual(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).toEqual(undefined);
});

it("should initialize expired timer if already expired", () => {
// act
subject.load({
access_token:"token",
expires_in : 0
} as User);

// assert
expect(accessTokenExpiredTimer.duration).toEqual(1);
});

it("should not initialize timers if no access token", () => {
// act
subject.load({
expires_in : 70
} as User);

// assert
expect(accessTokenExpiringTimer.duration).toEqual(undefined);
expect(accessTokenExpiredTimer.duration).toEqual(undefined);
});

it("should not initialize timers if no expiration on access token", () => {
// act
subject.load({
access_token:"token"
} as User);

// assert
expect(accessTokenExpiringTimer.duration).toEqual(undefined);
expect(accessTokenExpiredTimer.duration).toEqual(undefined);
});
});

describe("unload", () => {

it("should cancel timers", () => {
// act
subject.unload();

// assert
expect(accessTokenExpiringTimer.cancelWasCalled).toEqual(true);
expect(accessTokenExpiredTimer.cancelWasCalled).toEqual(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() {}
}
77 changes: 0 additions & 77 deletions test/unit/ErrorResponse.spec.js

This file was deleted.

Loading

0 comments on commit b27bafb

Please sign in to comment.