@inrupt/solid-client-authn-browser 1.13.4 → 1.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,6 +19,7 @@
19
19
  // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
20
  //
21
21
 
22
+ import { EVENTS } from "@inrupt/solid-client-authn-core";
22
23
  import { jest, it, expect } from "@jest/globals";
23
24
  import {
24
25
  fetch,
@@ -29,33 +30,34 @@ import {
29
30
  onLogin,
30
31
  onSessionRestore,
31
32
  onLogout,
33
+ events,
32
34
  } from "./defaultSession";
33
- import type * as SessionModuleType from "./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();
35
+ import * as SessionModule from "./Session";
36
+
37
+ it("instantiates the default session lazily", () => {
38
+ const singletonSession = new SessionModule.Session();
39
+ const spiedConstructor = jest
40
+ .spyOn(SessionModule, "Session")
41
+ .mockImplementation(() => singletonSession);
42
+ expect(spiedConstructor).not.toHaveBeenCalled();
43
+ events().on(EVENTS.LOGIN, jest.fn());
44
+ expect(spiedConstructor).toHaveBeenCalled();
43
45
  });
44
46
 
45
47
  it("re-uses the same Session when calling multiple methods", () => {
46
- const mockedSession = jest.requireMock(
47
- "../src/Session.ts"
48
- ) as typeof SessionModuleType;
48
+ const singletonSession = new SessionModule.Session();
49
+ const spiedConstructor = jest
50
+ .spyOn(SessionModule, "Session")
51
+ .mockImplementation(() => singletonSession);
49
52
 
50
- expect(mockedSession.Session).not.toHaveBeenCalled();
53
+ events().on(EVENTS.LOGIN, jest.fn());
51
54
 
52
- onLogin(jest.fn());
55
+ expect(spiedConstructor).toHaveBeenCalledTimes(1);
53
56
 
54
- expect(mockedSession.Session).toHaveBeenCalledTimes(1);
57
+ events().on(EVENTS.LOGOUT, jest.fn());
55
58
 
56
- onLogout(jest.fn());
57
-
58
- expect(mockedSession.Session).toHaveBeenCalledTimes(1);
59
+ // No new session has been instantiated.
60
+ expect(spiedConstructor).toHaveBeenCalledTimes(1);
59
61
  });
60
62
 
61
63
  it("all functions pass on their arguments to the default session", () => {
@@ -73,6 +75,7 @@ it("all functions pass on their arguments to the default session", () => {
73
75
  const onLoginSpy = jest.spyOn(defaultSession, "onLogin");
74
76
  const onLogoutSpy = jest.spyOn(defaultSession, "onLogout");
75
77
  const onSessionRestoreSpy = jest.spyOn(defaultSession, "onSessionRestore");
78
+ const eventsOnSpy = jest.spyOn(defaultSession.events, "on");
76
79
 
77
80
  expect(fetchSpy).not.toHaveBeenCalled();
78
81
  // eslint-disable-next-line @typescript-eslint/no-floating-promises
@@ -94,6 +97,12 @@ it("all functions pass on their arguments to the default session", () => {
94
97
  handleIncomingRedirect("https://example.com");
95
98
  expect(handleIncomingRedirectSpy).toHaveBeenCalledTimes(1);
96
99
 
100
+ // onLogin will internall call events.on(...), so the following test
101
+ // must come first.
102
+ expect(eventsOnSpy).not.toHaveBeenCalled();
103
+ events().on(EVENTS.LOGIN, jest.fn());
104
+ expect(eventsOnSpy).toHaveBeenCalledTimes(1);
105
+
97
106
  expect(onLoginSpy).not.toHaveBeenCalled();
98
107
  onLogin(jest.fn());
99
108
  expect(onLoginSpy).toHaveBeenCalledTimes(1);
@@ -21,7 +21,7 @@
21
21
 
22
22
  import { Session } from "./Session";
23
23
 
24
- let defaultSession: Session;
24
+ let defaultSession: Session | undefined;
25
25
 
26
26
  /**
27
27
  * Obtain the {@link Session} used when not explicitly instantiating one yourself.
@@ -97,6 +97,8 @@ export const handleIncomingRedirect: Session["handleIncomingRedirect"] = (
97
97
  * @since 1.3.0
98
98
  *
99
99
  * @param callback The function called when a user completes login.
100
+ * @deprecated Prefer events.on(EVENTS.LOGIN, callback)
101
+
100
102
  */
101
103
  export const onLogin: Session["onLogin"] = (...args) => {
102
104
  const session = getDefaultSession();
@@ -108,6 +110,8 @@ export const onLogin: Session["onLogin"] = (...args) => {
108
110
  *
109
111
  * @param callback The function called when a user completes logout.
110
112
  * @since 1.3.0
113
+ * @deprecated Prefer events.on(EVENTS.LOGOUT, callback)
114
+
111
115
  */
112
116
  export const onLogout: Session["onLogout"] = (...args) => {
113
117
  const session = getDefaultSession();
@@ -119,8 +123,18 @@ export const onLogout: Session["onLogout"] = (...args) => {
119
123
  *
120
124
  * @param callback The function called when a session is restored.
121
125
  * @since 1.3.0
126
+ * @deprecated Prefer events.on(EVENTS.SESSION_RESTORED, callback)
122
127
  */
123
128
  export const onSessionRestore: Session["onSessionRestore"] = (...args) => {
124
129
  const session = getDefaultSession();
125
130
  return session.onSessionRestore(...args);
126
131
  };
132
+
133
+ /**
134
+ * {@link SessionEventEmitter} instance to subscribe to events by the default session.
135
+ *
136
+ * @since Unreleased
137
+ */
138
+ export const events = (): Session["events"] => {
139
+ return getDefaultSession().events;
140
+ };
package/src/index.ts CHANGED
@@ -34,4 +34,5 @@ export {
34
34
  NotImplementedError,
35
35
  ConfigurationError,
36
36
  InMemoryStorage,
37
+ EVENTS,
37
38
  } from "@inrupt/solid-client-authn-core";