1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | import { jest, it } from "@jest/globals";
|
23 | import {
|
24 | fetch,
|
25 | getDefaultSession,
|
26 | handleIncomingRedirect,
|
27 | login,
|
28 | logout,
|
29 | onLogin,
|
30 | onSessionRestore,
|
31 | onLogout,
|
32 | } from "../src/defaultSession";
|
33 | import type * as SessionModuleType from "../src/Session";
|
34 |
|
35 | jest.mock("../src/Session.ts");
|
36 |
|
37 | it("does not instantiate a Session without calling one of its methods", () => {
|
38 | const mockedSession = jest.requireMock(
|
39 | "../src/Session.ts"
|
40 | ) as typeof SessionModuleType;
|
41 |
|
42 | expect(mockedSession.Session).not.toHaveBeenCalled();
|
43 | });
|
44 |
|
45 | it("re-uses the same Session when calling multiple methods", () => {
|
46 | const mockedSession = jest.requireMock(
|
47 | "../src/Session.ts"
|
48 | ) as typeof SessionModuleType;
|
49 |
|
50 | expect(mockedSession.Session).not.toHaveBeenCalled();
|
51 |
|
52 | onLogin(jest.fn());
|
53 |
|
54 | expect(mockedSession.Session).toHaveBeenCalledTimes(1);
|
55 |
|
56 | onLogout(jest.fn());
|
57 |
|
58 | expect(mockedSession.Session).toHaveBeenCalledTimes(1);
|
59 | });
|
60 |
|
61 | it("all functions pass on their arguments to the default session", () => {
|
62 | const defaultSession = getDefaultSession();
|
63 |
|
64 | const fetchSpy = jest.fn();
|
65 | defaultSession.fetch = fetchSpy as typeof defaultSession.fetch;
|
66 | const loginSpy = jest.fn();
|
67 | defaultSession.login = loginSpy as typeof defaultSession.login;
|
68 | const logoutSpy = jest.fn();
|
69 | defaultSession.logout = logoutSpy as typeof defaultSession.logout;
|
70 | const handleIncomingRedirectSpy = jest.fn();
|
71 | defaultSession.handleIncomingRedirect =
|
72 | handleIncomingRedirectSpy as typeof defaultSession.handleIncomingRedirect;
|
73 | const onLoginSpy = jest.spyOn(defaultSession, "onLogin");
|
74 | const onLogoutSpy = jest.spyOn(defaultSession, "onLogout");
|
75 | const onSessionRestoreSpy = jest.spyOn(defaultSession, "onSessionRestore");
|
76 |
|
77 | expect(fetchSpy).not.toHaveBeenCalled();
|
78 |
|
79 | fetch("https://example.com");
|
80 | expect(fetchSpy).toHaveBeenCalledTimes(1);
|
81 |
|
82 | expect(loginSpy).not.toHaveBeenCalled();
|
83 |
|
84 | login({});
|
85 | expect(loginSpy).toHaveBeenCalledTimes(1);
|
86 |
|
87 | expect(logoutSpy).not.toHaveBeenCalled();
|
88 |
|
89 | logout();
|
90 | expect(logoutSpy).toHaveBeenCalledTimes(1);
|
91 |
|
92 | expect(handleIncomingRedirectSpy).not.toHaveBeenCalled();
|
93 |
|
94 | handleIncomingRedirect("https://example.com");
|
95 | expect(handleIncomingRedirectSpy).toHaveBeenCalledTimes(1);
|
96 |
|
97 | expect(onLoginSpy).not.toHaveBeenCalled();
|
98 | onLogin(jest.fn());
|
99 | expect(onLoginSpy).toHaveBeenCalledTimes(1);
|
100 |
|
101 | expect(onSessionRestoreSpy).not.toHaveBeenCalled();
|
102 | onSessionRestore(jest.fn());
|
103 | expect(onSessionRestoreSpy).toHaveBeenCalledTimes(1);
|
104 |
|
105 | expect(onLogoutSpy).not.toHaveBeenCalled();
|
106 | onLogout(jest.fn());
|
107 | expect(onLogoutSpy).toHaveBeenCalledTimes(1);
|
108 | });
|