UNPKG

3.98 kBPlain TextView Raw
1/*
2 * Copyright 2021 Inrupt Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal in
6 * the Software without restriction, including without limitation the rights to use,
7 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 * Software, and to permit persons to whom the Software is furnished to do so,
9 * subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22import { jest, it } from "@jest/globals";
23import {
24 fetch,
25 getDefaultSession,
26 handleIncomingRedirect,
27 login,
28 logout,
29 onLogin,
30 onSessionRestore,
31 onLogout,
32} from "../src/defaultSession";
33import type * as SessionModuleType from "../src/Session";
34
35jest.mock("../src/Session.ts");
36
37it("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
45it("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
61it("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 // eslint-disable-next-line @typescript-eslint/no-floating-promises
79 fetch("https://example.com");
80 expect(fetchSpy).toHaveBeenCalledTimes(1);
81
82 expect(loginSpy).not.toHaveBeenCalled();
83 // eslint-disable-next-line @typescript-eslint/no-floating-promises
84 login({});
85 expect(loginSpy).toHaveBeenCalledTimes(1);
86
87 expect(logoutSpy).not.toHaveBeenCalled();
88 // eslint-disable-next-line @typescript-eslint/no-floating-promises
89 logout();
90 expect(logoutSpy).toHaveBeenCalledTimes(1);
91
92 expect(handleIncomingRedirectSpy).not.toHaveBeenCalled();
93 // eslint-disable-next-line @typescript-eslint/no-floating-promises
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});