|
| 1 | +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import { AccessTokenEvents } from '../../src/AccessTokenEvents'; |
| 5 | +import { Timer } from '../../src/Timer'; |
| 6 | +import { User } from '../../src/User'; |
| 7 | + |
| 8 | +describe("AccessTokenEvents", () => { |
| 9 | + |
| 10 | + let subject: AccessTokenEvents; |
| 11 | + let accessTokenExpiringTimer: StubTimer; |
| 12 | + let accessTokenExpiredTimer: StubTimer; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + accessTokenExpiringTimer = new StubTimer("stub expiring timer"); |
| 16 | + accessTokenExpiredTimer = new StubTimer("stub expired timer"); |
| 17 | + subject = new AccessTokenEvents({ |
| 18 | + accessTokenExpiringTimer, accessTokenExpiredTimer |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + /* TODO: port-ts |
| 23 | + describe("constructor", () => { |
| 24 | +
|
| 25 | + it("should use default expiringNotificationTime", () => { |
| 26 | + subject._accessTokenExpiringNotificationTime.should.equal(60); |
| 27 | + }); |
| 28 | +
|
| 29 | + });*/ |
| 30 | + |
| 31 | + describe("load", () => { |
| 32 | + |
| 33 | + it("should cancel existing timers", () => { |
| 34 | + // act |
| 35 | + subject.load({} as User); |
| 36 | + |
| 37 | + // assert |
| 38 | + expect(accessTokenExpiringTimer.cancelWasCalled).toEqual(true); |
| 39 | + expect(accessTokenExpiredTimer.cancelWasCalled).toEqual(true); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should initialize timers", () => { |
| 43 | + // act |
| 44 | + subject.load({ |
| 45 | + access_token:"token", |
| 46 | + expires_in : 70 |
| 47 | + } as User); |
| 48 | + |
| 49 | + // assert |
| 50 | + expect(accessTokenExpiringTimer.duration).toEqual(10); |
| 51 | + expect(accessTokenExpiredTimer.duration).toEqual(71); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should immediately schedule expiring timer if expiration is soon", () => { |
| 55 | + // act |
| 56 | + subject.load({ |
| 57 | + access_token:"token", |
| 58 | + expires_in : 10 |
| 59 | + } as User); |
| 60 | + |
| 61 | + // assert |
| 62 | + expect(accessTokenExpiringTimer.duration).toEqual(1); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should not initialize expiring timer if already expired", () => { |
| 66 | + // act |
| 67 | + subject.load({ |
| 68 | + access_token:"token", |
| 69 | + expires_in : 0 |
| 70 | + } as User); |
| 71 | + |
| 72 | + // assert |
| 73 | + expect(accessTokenExpiringTimer.duration).toEqual(undefined); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should initialize expired timer if already expired", () => { |
| 77 | + // act |
| 78 | + subject.load({ |
| 79 | + access_token:"token", |
| 80 | + expires_in : 0 |
| 81 | + } as User); |
| 82 | + |
| 83 | + // assert |
| 84 | + expect(accessTokenExpiredTimer.duration).toEqual(1); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should not initialize timers if no access token", () => { |
| 88 | + // act |
| 89 | + subject.load({ |
| 90 | + expires_in : 70 |
| 91 | + } as User); |
| 92 | + |
| 93 | + // assert |
| 94 | + expect(accessTokenExpiringTimer.duration).toEqual(undefined); |
| 95 | + expect(accessTokenExpiredTimer.duration).toEqual(undefined); |
| 96 | + }); |
| 97 | + |
| 98 | + it("should not initialize timers if no expiration on access token", () => { |
| 99 | + // act |
| 100 | + subject.load({ |
| 101 | + access_token:"token" |
| 102 | + } as User); |
| 103 | + |
| 104 | + // assert |
| 105 | + expect(accessTokenExpiringTimer.duration).toEqual(undefined); |
| 106 | + expect(accessTokenExpiredTimer.duration).toEqual(undefined); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe("unload", () => { |
| 111 | + |
| 112 | + it("should cancel timers", () => { |
| 113 | + // act |
| 114 | + subject.unload(); |
| 115 | + |
| 116 | + // assert |
| 117 | + expect(accessTokenExpiringTimer.cancelWasCalled).toEqual(true); |
| 118 | + expect(accessTokenExpiredTimer.cancelWasCalled).toEqual(true); |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +class StubTimer extends Timer { |
| 124 | + cancelWasCalled: boolean; |
| 125 | + duration: any; |
| 126 | + |
| 127 | + constructor(name: string) { |
| 128 | + super(name) |
| 129 | + this.cancelWasCalled = false; |
| 130 | + } |
| 131 | + |
| 132 | + init(duration: number) { |
| 133 | + this.duration = duration; |
| 134 | + } |
| 135 | + |
| 136 | + cancel() { |
| 137 | + this.cancelWasCalled = true; |
| 138 | + } |
| 139 | + |
| 140 | + addHandler() {} |
| 141 | + removeHandler() {} |
| 142 | +} |
0 commit comments