Skip to content

Commit 5adbcce

Browse files
committed
update tests
1 parent c7cf5fb commit 5adbcce

File tree

2 files changed

+24
-39
lines changed

2 files changed

+24
-39
lines changed

frontend/src/index.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,13 @@ const mockAppSettings: AppSettings = {
5151
describe("browsertrix-app", () => {
5252
beforeEach(() => {
5353
AppStateService.resetAll();
54-
AuthService.broadcastChannel = new BroadcastChannel(AuthService.storageKey);
5554
window.sessionStorage.clear();
5655
window.localStorage.clear();
5756
stub(window.history, "pushState");
5857
stub(NotifyController.prototype, "toast");
5958
});
6059

6160
afterEach(() => {
62-
AuthService.broadcastChannel.close();
6361
restore();
6462
});
6563

@@ -69,7 +67,7 @@ describe("browsertrix-app", () => {
6967
});
7068

7169
it("don't block render if settings aren't defined", async () => {
72-
stub(AuthService, "initSessionStorage").returns(
70+
stub(AuthService.prototype, "initSessionStorage").returns(
7371
Promise.resolve({
7472
headers: { Authorization: "_fake_headers_" },
7573
tokenExpiresAt: 0,
@@ -85,7 +83,7 @@ describe("browsertrix-app", () => {
8583
});
8684

8785
it("renders 404 when not in org", async () => {
88-
stub(AuthService, "initSessionStorage").returns(
86+
stub(AuthService.prototype, "initSessionStorage").returns(
8987
Promise.resolve({
9088
headers: { Authorization: "_fake_headers_" },
9189
tokenExpiresAt: 0,
@@ -117,7 +115,7 @@ describe("browsertrix-app", () => {
117115
role: 10,
118116
};
119117

120-
stub(AuthService, "initSessionStorage").returns(
118+
stub(AuthService.prototype, "initSessionStorage").returns(
121119
Promise.resolve({
122120
headers: { Authorization: "_fake_headers_" },
123121
tokenExpiresAt: 0,
@@ -142,7 +140,9 @@ describe("browsertrix-app", () => {
142140
});
143141

144142
it("renders log in when not authenticated", async () => {
145-
stub(AuthService, "initSessionStorage").returns(Promise.resolve(null));
143+
stub(AuthService.prototype, "initSessionStorage").returns(
144+
Promise.resolve(null),
145+
);
146146
// @ts-expect-error checkFreshness is private
147147
stub(AuthService.prototype, "checkFreshness");
148148
stub(NavigateController, "createNavigateEvent").callsFake(
@@ -184,7 +184,7 @@ describe("browsertrix-app", () => {
184184
Promise.resolve(mockAPIUser),
185185
);
186186
stub(AuthService.prototype, "startFreshnessCheck").callsFake(() => {});
187-
stub(AuthService, "initSessionStorage").callsFake(async () =>
187+
stub(AuthService.prototype, "initSessionStorage").callsFake(async () =>
188188
Promise.resolve({
189189
headers: { Authorization: "_fake_headers_" },
190190
tokenExpiresAt: 0,
@@ -203,7 +203,7 @@ describe("browsertrix-app", () => {
203203
Promise.resolve(mockAPIUser),
204204
);
205205
stub(AuthService.prototype, "startFreshnessCheck").callsFake(() => {});
206-
stub(AuthService, "initSessionStorage").callsFake(async () =>
206+
stub(AuthService.prototype, "initSessionStorage").callsFake(async () =>
207207
Promise.resolve({
208208
headers: { Authorization: "_fake_headers_" },
209209
tokenExpiresAt: 0,
@@ -235,7 +235,7 @@ describe("browsertrix-app", () => {
235235
);
236236
stub(App.prototype, "getLocationPathname").callsFake(() => `/orgs/${id}`);
237237
stub(AuthService.prototype, "startFreshnessCheck").callsFake(() => {});
238-
stub(AuthService, "initSessionStorage").callsFake(async () =>
238+
stub(AuthService.prototype, "initSessionStorage").callsFake(async () =>
239239
Promise.resolve({
240240
headers: { Authorization: "_fake_headers_" },
241241
tokenExpiresAt: 0,

frontend/src/utils/AuthService.test.ts

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,34 @@ import { AppStateService } from "./state";
77
describe("AuthService", () => {
88
beforeEach(() => {
99
AppStateService.resetAll();
10-
AuthService.broadcastChannel = new BroadcastChannel(AuthService.storageKey);
10+
window.sessionStorage.clear();
1111
window.sessionStorage.clear();
1212
stub(window.history, "pushState");
1313
});
1414

1515
afterEach(() => {
16-
AuthService.broadcastChannel.close();
1716
restore();
1817
});
1918

20-
describe("AuthService.initSessionStorage()", () => {
19+
describe("initSessionStorage()", () => {
20+
let authService = new AuthService();
21+
22+
beforeEach(() => {
23+
authService = new AuthService();
24+
});
25+
2126
it("returns auth in session storage", async () => {
2227
stub(window.sessionStorage, "getItem").returns(
2328
JSON.stringify({
2429
headers: { Authorization: "_fake_headers_" },
25-
tokenExpiresAt: "_fake_tokenExpiresAt_",
30+
tokenExpiresAt: 1111,
2631
username: "test-auth@example.com",
2732
}),
2833
);
29-
const result = await AuthService.initSessionStorage();
34+
const result = await authService.initSessionStorage();
3035
expect(result).to.deep.equal({
3136
headers: { Authorization: "_fake_headers_" },
32-
tokenExpiresAt: "_fake_tokenExpiresAt_",
37+
tokenExpiresAt: 1111,
3338
username: "test-auth@example.com",
3439
});
3540
});
@@ -41,22 +46,22 @@ describe("AuthService", () => {
4146
name: "responding_auth",
4247
auth: {
4348
headers: { Authorization: "_fake_headers_from_tab_" },
44-
tokenExpiresAt: "_fake_tokenExpiresAt_from_tab_",
49+
tokenExpiresAt: 9999,
4550
username: "test-auth@example.com_from_tab_",
4651
},
4752
});
4853
});
49-
const result = await AuthService.initSessionStorage();
54+
const result = await authService.initSessionStorage();
5055
expect(result).to.deep.equal({
5156
headers: { Authorization: "_fake_headers_from_tab_" },
52-
tokenExpiresAt: "_fake_tokenExpiresAt_from_tab_",
57+
tokenExpiresAt: 9999,
5358
username: "test-auth@example.com_from_tab_",
5459
});
5560
otherTabChannel.close();
5661
});
5762
it("resolves without stored auth or another tab", async () => {
5863
stub(window.sessionStorage, "getItem");
59-
const result = await AuthService.initSessionStorage();
64+
const result = await authService.initSessionStorage();
6065
expect(result).to.equal(null);
6166
});
6267
});
@@ -84,26 +89,6 @@ describe("AuthService", () => {
8489
expect(window.sessionStorage.setItem).to.have.been.called;
8590
});
8691

87-
it("posts to the broadcast channel", () => {
88-
stub(AuthService.storage, "getItem").returns("");
89-
stub(AuthService.broadcastChannel, "postMessage");
90-
stub(window.sessionStorage, "setItem");
91-
92-
const authValue = JSON.stringify({
93-
headers: { Authorization: self.crypto.randomUUID() },
94-
tokenExpiresAt: Date.now(),
95-
username: "test-auth@example.com",
96-
});
97-
AuthService.storage.setItem(authValue);
98-
99-
expect(
100-
AuthService.broadcastChannel.postMessage,
101-
).to.have.been.calledWith({
102-
name: "auth_storage",
103-
value: authValue,
104-
});
105-
});
106-
10792
it("does not store the same value", () => {
10893
stub(AuthService.storage, "getItem").returns(JSON.stringify(mockAuth));
10994
stub(window.sessionStorage, "setItem");

0 commit comments

Comments
 (0)