|
1 |
| -import { defaultAuthHubHandler } from '../defaultAuthHubHandler'; |
| 1 | +import { Hub } from 'aws-amplify/utils'; |
| 2 | +import { |
| 3 | + defaultAuthHubHandler, |
| 4 | + listenToAuthHub, |
| 5 | +} from '../defaultAuthHubHandler'; |
2 | 6 | import { AuthInterpreter } from '../types';
|
3 | 7 |
|
4 |
| -jest.mock('xstate/lib/waitFor', () => ({ |
5 |
| - waitFor: jest.fn(), |
6 |
| -})); |
7 |
| - |
8 |
| -jest.mock('../actor', () => ({ |
9 |
| - getActorState: () => ({ matches: () => true }), |
10 |
| -})); |
11 |
| - |
12 |
| -const authenticatedStateMachine = { |
13 |
| - getSnapshot: () => ({ |
14 |
| - // this is the state.matches function |
15 |
| - matches: (state: string) => state === 'authenticated.idle', |
16 |
| - }), |
17 |
| - send: jest.fn(), |
18 |
| -} as unknown as AuthInterpreter; |
19 |
| - |
20 |
| -const unauthenticatedStateMachine = { |
21 |
| - getSnapshot: () => ({ |
22 |
| - // this is the state.matches function |
23 |
| - matches: (state: string) => state === 'signIn', |
24 |
| - }), |
25 |
| - send: jest.fn(), |
26 |
| -} as unknown as AuthInterpreter; |
27 |
| - |
28 |
| -const authSendSpy = jest.spyOn(authenticatedStateMachine, 'send'); |
29 |
| -const unauthSendSpy = jest.spyOn(unauthenticatedStateMachine, 'send'); |
30 |
| - |
31 | 8 | const onSignIn = jest.fn();
|
32 | 9 | const onSignOut = jest.fn();
|
| 10 | +const service = { send: jest.fn() } as unknown as AuthInterpreter; |
33 | 11 |
|
34 | 12 | describe('defaultAuthHubHandler', () => {
|
35 | 13 | beforeEach(() => {
|
36 |
| - authSendSpy.mockClear(); |
37 |
| - unauthSendSpy.mockClear(); |
38 |
| - onSignIn.mockClear(); |
39 |
| - onSignOut.mockClear(); |
| 14 | + jest.clearAllMocks(); |
40 | 15 | });
|
41 | 16 |
|
42 |
| - // @todo-migration probably remove token refresh event handling |
43 |
| - it.skip('responds to token refresh event when state is authenticated', async () => { |
44 |
| - await defaultAuthHubHandler( |
45 |
| - { channel: 'auth', payload: { event: 'tokenRefresh' } }, |
46 |
| - authenticatedStateMachine |
47 |
| - ); |
48 |
| - expect(authSendSpy).toHaveBeenCalledWith('TOKEN_REFRESH'); |
49 |
| - }); |
| 17 | + it.each(['tokenRefresh_failure', 'signedOut'])( |
| 18 | + 'handles a %s event as expected', |
| 19 | + (event) => { |
| 20 | + defaultAuthHubHandler({ channel: 'auth', payload: { event } }, service); |
| 21 | + expect(service.send).toHaveBeenCalledTimes(1); |
| 22 | + expect(service.send).toHaveBeenCalledWith('SIGN_OUT'); |
| 23 | + } |
| 24 | + ); |
50 | 25 |
|
51 |
| - it('ignores token refresh event when state is unauthenticated', async () => { |
52 |
| - await defaultAuthHubHandler( |
53 |
| - { channel: 'auth', payload: { event: 'tokenRefresh' } }, |
54 |
| - unauthenticatedStateMachine |
| 26 | + it('handles a signInWithRedirect event as expected', () => { |
| 27 | + defaultAuthHubHandler( |
| 28 | + { channel: 'auth', payload: { event: 'signInWithRedirect' } }, |
| 29 | + service |
55 | 30 | );
|
56 |
| - expect(unauthSendSpy).not.toHaveBeenCalled(); |
| 31 | + expect(service.send).toHaveBeenCalledTimes(1); |
| 32 | + expect(service.send).toHaveBeenCalledWith('SIGN_IN_WITH_REDIRECT'); |
57 | 33 | });
|
58 | 34 |
|
59 |
| - // @todo-migration |
60 |
| - // expect(jest.fn()).toHaveBeenCalledWith(...expected) |
61 |
| - // Expected: "SIGN_OUT" |
62 |
| - // Number of calls: 0 |
63 |
| - it.skip('responds to signOut event when state is authenticated', async () => { |
64 |
| - await defaultAuthHubHandler( |
65 |
| - { channel: 'auth', payload: { event: 'signOut' } }, |
66 |
| - authenticatedStateMachine |
| 35 | + it('calls onSignOut callabck on signedOut event if provided', () => { |
| 36 | + defaultAuthHubHandler( |
| 37 | + { channel: 'auth', payload: { event: 'signedOut' } }, |
| 38 | + service, |
| 39 | + { onSignOut } |
67 | 40 | );
|
68 |
| - expect(authSendSpy).toHaveBeenCalledWith('SIGN_OUT'); |
| 41 | + expect(onSignOut).toHaveBeenCalledTimes(1); |
69 | 42 | });
|
70 | 43 |
|
71 |
| - it('ignores signOut event when state is unauthenticated', async () => { |
72 |
| - await defaultAuthHubHandler( |
73 |
| - { channel: 'auth', payload: { event: 'signOut' } }, |
74 |
| - unauthenticatedStateMachine |
| 44 | + it('does not call onSignOut callback on tokenRefreh_failure event if provided', () => { |
| 45 | + defaultAuthHubHandler( |
| 46 | + { channel: 'auth', payload: { event: 'tokenRefreh_failure' } }, |
| 47 | + service, |
| 48 | + { onSignOut } |
75 | 49 | );
|
76 |
| - expect(unauthSendSpy).not.toHaveBeenCalled(); |
| 50 | + expect(onSignOut).not.toHaveBeenCalled(); |
77 | 51 | });
|
78 | 52 |
|
79 |
| - it('signs user out when token refresh failed in authenticated state', async () => { |
80 |
| - await defaultAuthHubHandler( |
81 |
| - { channel: 'auth', payload: { event: 'tokenRefresh_failure' } }, |
82 |
| - authenticatedStateMachine |
| 53 | + it('calls onSignIn callabck on signedIn event if provided', () => { |
| 54 | + defaultAuthHubHandler( |
| 55 | + { channel: 'auth', payload: { event: 'signedIn' } }, |
| 56 | + service, |
| 57 | + { onSignIn } |
83 | 58 | );
|
84 |
| - expect(authSendSpy).toHaveBeenCalledWith('SIGN_OUT'); |
| 59 | + expect(onSignIn).toHaveBeenCalledTimes(1); |
85 | 60 | });
|
| 61 | +}); |
86 | 62 |
|
87 |
| - // @todo-migration potentially remove |
88 |
| - it.skip('ignores token refresh failure event when state is unauthenticated', async () => { |
89 |
| - await defaultAuthHubHandler( |
90 |
| - { channel: 'auth', payload: { event: 'tokenRefresh_failure' } }, |
91 |
| - unauthenticatedStateMachine |
92 |
| - ); |
93 |
| - expect(unauthSendSpy).not.toHaveBeenCalled(); |
94 |
| - }); |
| 63 | +describe('listenToAuthHub', () => { |
| 64 | + it('creates a Hub listener', () => { |
| 65 | + // add empty mockImplementation to prevent logging "auth channel" warning output to the console |
| 66 | + jest.spyOn(console, 'warn').mockImplementation(); |
95 | 67 |
|
96 |
| - // @todo-migration |
97 |
| - // expect(jest.fn()).toHaveBeenCalledWith(...expected) |
98 |
| - // Expected: {"type": "AUTO_SIGN_IN"} |
99 |
| - // Number of calls: 0 |
100 |
| - it.skip('responds to autoSignIn event when state is unauthenticated', async () => { |
101 |
| - await defaultAuthHubHandler( |
102 |
| - { channel: 'auth', payload: { event: 'autoSignIn' } }, |
103 |
| - unauthenticatedStateMachine |
104 |
| - ); |
105 |
| - expect(unauthSendSpy).toHaveBeenCalledWith({ type: 'AUTO_SIGN_IN' }); |
106 |
| - }); |
| 68 | + const hubListenSpy = jest.spyOn(Hub, 'listen'); |
| 69 | + const handler = jest.fn(); |
107 | 70 |
|
108 |
| - it('ignores autoSignIn event when state is authenticated', async () => { |
109 |
| - await defaultAuthHubHandler( |
110 |
| - { channel: 'auth', payload: { event: 'autoSignIn' } }, |
111 |
| - authenticatedStateMachine |
112 |
| - ); |
113 |
| - expect(unauthSendSpy).not.toHaveBeenCalled(); |
114 |
| - }); |
| 71 | + listenToAuthHub(service, handler); |
115 | 72 |
|
116 |
| - // @todo-migration |
117 |
| - // expect(jest.fn()).toHaveBeenCalledWith(...expected) |
118 |
| - // Expected: {"type": "AUTO_SIGN_IN_FAILURE"} |
119 |
| - // Number of calls: 0 |
120 |
| - it.skip('responds to autoSignIn_failure event', async () => { |
121 |
| - await defaultAuthHubHandler( |
122 |
| - { channel: 'auth', payload: { event: 'autoSignIn_failure' } }, |
123 |
| - unauthenticatedStateMachine |
| 73 | + expect(hubListenSpy).toHaveBeenCalledTimes(1); |
| 74 | + expect(hubListenSpy).toHaveBeenCalledWith( |
| 75 | + 'auth', |
| 76 | + expect.any(Function), |
| 77 | + 'authenticator-hub-handler' |
124 | 78 | );
|
125 |
| - expect(unauthSendSpy).toHaveBeenCalledWith({ |
126 |
| - type: 'AUTO_SIGN_IN_FAILURE', |
127 |
| - }); |
128 |
| - }); |
129 |
| - |
130 |
| - // @todo-migration |
131 |
| - // Expected number of calls: 1 |
132 |
| - // Received number of calls: 0 |
133 |
| - it.skip.each(['signIn', 'signOut'])( |
134 |
| - 'calls the %s event handler as expected', |
135 |
| - async (event) => { |
136 |
| - const handler = event === 'signIn' ? onSignIn : onSignOut; |
137 |
| - const handlerKey = event === 'signIn' ? 'onSignIn' : 'onSignOut'; |
138 |
| - await defaultAuthHubHandler( |
139 |
| - { channel: 'auth', payload: { event } }, |
140 |
| - authenticatedStateMachine, |
141 |
| - { [handlerKey]: handler } |
142 |
| - ); |
143 |
| - expect(handler).toHaveBeenCalledTimes(1); |
144 |
| - } |
145 |
| - ); |
146 | 79 |
|
147 |
| - it("doesn't break when unsupported event is passed", async () => { |
148 |
| - const spyError = jest.spyOn(console, 'error'); |
149 |
| - await defaultAuthHubHandler( |
150 |
| - { channel: 'auth', payload: { event: 'unsupported' } }, |
151 |
| - unauthenticatedStateMachine |
| 80 | + Hub.dispatch('auth', { event: 'signedIn' }, 'authenticator-hub-handler'); |
| 81 | + |
| 82 | + expect(handler).toHaveBeenCalledTimes(1); |
| 83 | + expect(handler).toHaveBeenCalledWith( |
| 84 | + { |
| 85 | + channel: 'auth', |
| 86 | + patternInfo: [], |
| 87 | + payload: { event: 'signedIn' }, |
| 88 | + source: 'authenticator-hub-handler', |
| 89 | + }, |
| 90 | + { send: expect.any(Function) } |
152 | 91 | );
|
153 |
| - expect(spyError).not.toHaveBeenCalled(); |
154 | 92 | });
|
155 | 93 | });
|
0 commit comments