Skip to content

Commit 49d2551

Browse files
committed
test: add more tests
1 parent a30314d commit 49d2551

2 files changed

Lines changed: 237 additions & 5 deletions

File tree

src/UserManager.test.ts

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from "./navigators";
1212
import type { SigninResponse } from "./SigninResponse";
1313
import type { SignoutResponse } from "./SignoutResponse";
14-
import { UserManager, type SigninPopupArgs, type SigninRedirectArgs, type SigninSilentArgs, type SignoutSilentArgs } from "./UserManager";
14+
import { UserManager, type SigninPopupArgs, type SigninRedirectArgs, type SigninSilentArgs, type SignoutSilentArgs, type SignoutPopupArgs } from "./UserManager";
1515
import { UserManagerSettingsStore } from "./UserManagerSettings";
1616
import { User } from "./User";
1717
import type { UserProfile } from "./User";
@@ -689,7 +689,7 @@ describe("UserManager", () => {
689689
const url = "http://app/cb?state=test&code=code";
690690

691691
// act
692-
await subject.signoutCallback(url, true);
692+
await subject.signoutCallback(url);
693693

694694
// assert
695695
expect(signoutRedirectCallbackMock).toHaveBeenCalledWith(url);
@@ -715,6 +715,38 @@ describe("UserManager", () => {
715715
expect(signoutPopupCallbackMock).toHaveBeenCalledWith(url, keepOpen);
716716
});
717717

718+
it("should signout silent callback for request type so:s", async () => {
719+
// arrange
720+
const responseState = {
721+
state: { request_type: "so:s" } as State,
722+
response: { } as SignoutResponse,
723+
};
724+
jest.spyOn(subject["_client"], "readSignoutResponseState")
725+
.mockImplementation(() => Promise.resolve(responseState));
726+
const signoutSilentCallbackMock = jest.spyOn(subject, "signoutSilentCallback")
727+
.mockImplementation();
728+
const url = "http://app/cb?state=test&code=code";
729+
730+
// act
731+
await subject.signoutCallback(url);
732+
733+
// assert
734+
expect(signoutSilentCallbackMock).toHaveBeenCalledWith(url);
735+
});
736+
737+
it("should do nothing without state", async () => {
738+
// arrange
739+
const responseState = {
740+
state: undefined,
741+
response: { } as SignoutResponse,
742+
};
743+
jest.spyOn(subject["_client"], "readSignoutResponseState")
744+
.mockImplementation(() => Promise.resolve(responseState));
745+
746+
// act & assert (no throw)
747+
await subject.signoutCallback();
748+
});
749+
718750
it("should have valid request type", async () => {
719751
// arrange
720752
const responseState = {
@@ -884,6 +916,55 @@ describe("UserManager", () => {
884916
});
885917
});
886918

919+
describe("signoutPopup", () => {
920+
it("should pass navigator params to navigator", async () => {
921+
// arrange
922+
const handle = { } as PopupWindow;
923+
const prepareMock = jest.spyOn(subject["_popupNavigator"], "prepare")
924+
.mockImplementation(() => Promise.resolve(handle));
925+
subject["_signout"] = jest.fn();
926+
const navParams: SignoutPopupArgs = {
927+
popupWindowFeatures: {
928+
location: false,
929+
toolbar: false,
930+
height: 100,
931+
},
932+
popupWindowTarget: "popupWindowTarget",
933+
};
934+
935+
// act
936+
await subject.signoutPopup(navParams);
937+
938+
// assert
939+
expect(prepareMock).toHaveBeenCalledWith(navParams);
940+
});
941+
942+
it("should pass extra args to _signoutStart", async () => {
943+
// arrange
944+
const handle = { } as PopupWindow;
945+
jest.spyOn(subject["_popupNavigator"], "prepare")
946+
.mockImplementation(() => Promise.resolve(handle));
947+
subject["_signout"] = jest.fn().mockResolvedValue({} as SignoutResponse);
948+
const extraArgs: SignoutPopupArgs = {
949+
extraQueryParams: { q : "q" },
950+
state: "state",
951+
post_logout_redirect_uri: "http://app/extra_callback",
952+
};
953+
954+
// act
955+
await subject.signoutPopup(extraArgs);
956+
957+
// assert
958+
expect(subject["_signout"]).toHaveBeenCalledWith(
959+
{
960+
request_type: "so:p",
961+
...extraArgs,
962+
},
963+
handle,
964+
);
965+
});
966+
});
967+
887968
describe("storeUser", () => {
888969
it("should add user to store", async () => {
889970
// arrange

src/UserManagerEvents.test.ts

Lines changed: 154 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

4+
import type { User } from "./User";
45
import { UserManagerEvents } from "./UserManagerEvents";
56
import { UserManagerSettingsStore } from "./UserManagerSettings";
67

@@ -17,9 +18,78 @@ describe("UserManagerEvents", () => {
1718
subject = new UserManagerEvents(settings);
1819
});
1920

20-
describe("silent renew error", () => {
21+
describe("user loaded", () => {
22+
it("should allow add callback", async () => {
23+
// arrange
24+
const cb = jest.fn();
25+
26+
// act
27+
subject.addUserLoaded(cb);
28+
await subject.load({} as User);
29+
30+
// assert
31+
expect(cb).toHaveBeenCalled();
32+
});
33+
34+
it("should allow remove callback", async () => {
35+
// arrange
36+
const cb = jest.fn();
37+
38+
// act
39+
subject.addUserLoaded(cb);
40+
subject.removeUserLoaded(cb);
41+
await subject.load({} as User);
42+
43+
// assert
44+
expect(cb).toHaveBeenCalledTimes(0);
45+
});
46+
47+
it("should pass user to callback", async () => {
48+
// arrange
49+
let u: User | null = null;
50+
const cb = function (arg_user: User) {
51+
u = arg_user;
52+
};
53+
const expected = { access_token: "token" } as User;
54+
55+
// act
56+
subject.addUserLoaded(cb);
57+
await subject.load(expected);
58+
59+
// assert
60+
expect(u).toEqual(expected);
61+
});
62+
});
63+
64+
describe("user unloaded", () => {
65+
it("should allow add callback", async () => {
66+
// arrange
67+
const cb = jest.fn();
68+
69+
// act
70+
subject.addUserUnloaded(cb);
71+
await subject.unload();
72+
73+
// assert
74+
expect(cb).toHaveBeenCalled();
75+
});
76+
77+
it("should allow remove callback", async () => {
78+
// arrange
79+
const cb = jest.fn();
2180

22-
it("should allow callback", async () => {
81+
// act
82+
subject.addUserUnloaded(cb);
83+
subject.removeUserUnloaded(cb);
84+
await subject.unload();
85+
86+
// assert
87+
expect(cb).toHaveBeenCalledTimes(0);
88+
});
89+
});
90+
91+
describe("silent renew error", () => {
92+
it("should allow add callback", async () => {
2393
// arrange
2494
const cb = jest.fn();
2595

@@ -31,7 +101,7 @@ describe("UserManagerEvents", () => {
31101
expect(cb).toHaveBeenCalled();
32102
});
33103

34-
it("should allow unregistering callback", async () => {
104+
it("should allow remove callback", async () => {
35105
// arrange
36106
const cb = jest.fn();
37107

@@ -60,4 +130,85 @@ describe("UserManagerEvents", () => {
60130
expect(e).toEqual(expected);
61131
});
62132
});
133+
134+
describe("user signed in", () => {
135+
it("should allow add callback", async () => {
136+
// arrange
137+
const cb = jest.fn();
138+
139+
// act
140+
subject.addUserSignedIn(cb);
141+
await subject._raiseUserSignedIn();
142+
143+
// assert
144+
expect(cb).toHaveBeenCalled();
145+
});
146+
147+
it("should allow remove callback", async () => {
148+
// arrange
149+
const cb = jest.fn();
150+
151+
// act
152+
subject.addUserSignedIn(cb);
153+
subject.removeUserSignedIn(cb);
154+
await subject._raiseUserSignedIn();
155+
156+
// assert
157+
expect(cb).toHaveBeenCalledTimes(0);
158+
});
159+
});
160+
161+
describe("user signed out", () => {
162+
it("should allow add callback", async () => {
163+
// arrange
164+
const cb = jest.fn();
165+
166+
// act
167+
subject.addUserSignedOut(cb);
168+
await subject._raiseUserSignedOut();
169+
170+
// assert
171+
expect(cb).toHaveBeenCalled();
172+
});
173+
174+
it("should allow remove callback", async () => {
175+
// arrange
176+
const cb = jest.fn();
177+
178+
// act
179+
subject.addUserSignedOut(cb);
180+
subject.removeUserSignedOut(cb);
181+
await subject._raiseUserSignedOut();
182+
183+
// assert
184+
expect(cb).toHaveBeenCalledTimes(0);
185+
});
186+
});
187+
188+
describe("user session changed", () => {
189+
it("should allow add callback", async () => {
190+
// arrange
191+
const cb = jest.fn();
192+
193+
// act
194+
subject.addUserSessionChanged(cb);
195+
await subject._raiseUserSessionChanged();
196+
197+
// assert
198+
expect(cb).toHaveBeenCalled();
199+
});
200+
201+
it("should allow remove callback", async () => {
202+
// arrange
203+
const cb = jest.fn();
204+
205+
// act
206+
subject.addUserSessionChanged(cb);
207+
subject.removeUserSessionChanged(cb);
208+
await subject._raiseUserSessionChanged();
209+
210+
// assert
211+
expect(cb).toHaveBeenCalledTimes(0);
212+
});
213+
});
63214
});

0 commit comments

Comments
 (0)