-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ui): fix defaultAuthHubListener unit tests, remove asynchronous…
… declaration (#4777) * chore(ui): fix defaultAuthHubListener unit tests, remove asynchronous declaration * Create light-zoos-raise.md
- Loading branch information
1 parent
8a564b8
commit 4f643b0
Showing
8 changed files
with
90 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@aws-amplify/ui-react-core": patch | ||
"@aws-amplify/ui": patch | ||
"@aws-amplify/ui-vue": patch | ||
"@aws-amplify/ui-angular": patch | ||
--- | ||
|
||
chore(ui): fix defaultAuthHubListener unit tests, remove asynchronous declaration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 63 additions & 125 deletions
188
packages/ui/src/helpers/authenticator/__tests__/defaultAuthHubHandler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,155 +1,93 @@ | ||
import { defaultAuthHubHandler } from '../defaultAuthHubHandler'; | ||
import { Hub } from 'aws-amplify/utils'; | ||
import { | ||
defaultAuthHubHandler, | ||
listenToAuthHub, | ||
} from '../defaultAuthHubHandler'; | ||
import { AuthInterpreter } from '../types'; | ||
|
||
jest.mock('xstate/lib/waitFor', () => ({ | ||
waitFor: jest.fn(), | ||
})); | ||
|
||
jest.mock('../actor', () => ({ | ||
getActorState: () => ({ matches: () => true }), | ||
})); | ||
|
||
const authenticatedStateMachine = { | ||
getSnapshot: () => ({ | ||
// this is the state.matches function | ||
matches: (state: string) => state === 'authenticated.idle', | ||
}), | ||
send: jest.fn(), | ||
} as unknown as AuthInterpreter; | ||
|
||
const unauthenticatedStateMachine = { | ||
getSnapshot: () => ({ | ||
// this is the state.matches function | ||
matches: (state: string) => state === 'signIn', | ||
}), | ||
send: jest.fn(), | ||
} as unknown as AuthInterpreter; | ||
|
||
const authSendSpy = jest.spyOn(authenticatedStateMachine, 'send'); | ||
const unauthSendSpy = jest.spyOn(unauthenticatedStateMachine, 'send'); | ||
|
||
const onSignIn = jest.fn(); | ||
const onSignOut = jest.fn(); | ||
const service = { send: jest.fn() } as unknown as AuthInterpreter; | ||
|
||
describe('defaultAuthHubHandler', () => { | ||
beforeEach(() => { | ||
authSendSpy.mockClear(); | ||
unauthSendSpy.mockClear(); | ||
onSignIn.mockClear(); | ||
onSignOut.mockClear(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
// @todo-migration probably remove token refresh event handling | ||
it.skip('responds to token refresh event when state is authenticated', async () => { | ||
await defaultAuthHubHandler( | ||
{ channel: 'auth', payload: { event: 'tokenRefresh' } }, | ||
authenticatedStateMachine | ||
); | ||
expect(authSendSpy).toHaveBeenCalledWith('TOKEN_REFRESH'); | ||
}); | ||
it.each(['tokenRefresh_failure', 'signedOut'])( | ||
'handles a %s event as expected', | ||
(event) => { | ||
defaultAuthHubHandler({ channel: 'auth', payload: { event } }, service); | ||
expect(service.send).toHaveBeenCalledTimes(1); | ||
expect(service.send).toHaveBeenCalledWith('SIGN_OUT'); | ||
} | ||
); | ||
|
||
it('ignores token refresh event when state is unauthenticated', async () => { | ||
await defaultAuthHubHandler( | ||
{ channel: 'auth', payload: { event: 'tokenRefresh' } }, | ||
unauthenticatedStateMachine | ||
it('handles a signInWithRedirect event as expected', () => { | ||
defaultAuthHubHandler( | ||
{ channel: 'auth', payload: { event: 'signInWithRedirect' } }, | ||
service | ||
); | ||
expect(unauthSendSpy).not.toHaveBeenCalled(); | ||
expect(service.send).toHaveBeenCalledTimes(1); | ||
expect(service.send).toHaveBeenCalledWith('SIGN_IN_WITH_REDIRECT'); | ||
}); | ||
|
||
// @todo-migration | ||
// expect(jest.fn()).toHaveBeenCalledWith(...expected) | ||
// Expected: "SIGN_OUT" | ||
// Number of calls: 0 | ||
it.skip('responds to signOut event when state is authenticated', async () => { | ||
await defaultAuthHubHandler( | ||
{ channel: 'auth', payload: { event: 'signOut' } }, | ||
authenticatedStateMachine | ||
it('calls onSignOut callabck on signedOut event if provided', () => { | ||
defaultAuthHubHandler( | ||
{ channel: 'auth', payload: { event: 'signedOut' } }, | ||
service, | ||
{ onSignOut } | ||
); | ||
expect(authSendSpy).toHaveBeenCalledWith('SIGN_OUT'); | ||
expect(onSignOut).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('ignores signOut event when state is unauthenticated', async () => { | ||
await defaultAuthHubHandler( | ||
{ channel: 'auth', payload: { event: 'signOut' } }, | ||
unauthenticatedStateMachine | ||
it('does not call onSignOut callback on tokenRefreh_failure event if provided', () => { | ||
defaultAuthHubHandler( | ||
{ channel: 'auth', payload: { event: 'tokenRefreh_failure' } }, | ||
service, | ||
{ onSignOut } | ||
); | ||
expect(unauthSendSpy).not.toHaveBeenCalled(); | ||
expect(onSignOut).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('signs user out when token refresh failed in authenticated state', async () => { | ||
await defaultAuthHubHandler( | ||
{ channel: 'auth', payload: { event: 'tokenRefresh_failure' } }, | ||
authenticatedStateMachine | ||
it('calls onSignIn callabck on signedIn event if provided', () => { | ||
defaultAuthHubHandler( | ||
{ channel: 'auth', payload: { event: 'signedIn' } }, | ||
service, | ||
{ onSignIn } | ||
); | ||
expect(authSendSpy).toHaveBeenCalledWith('SIGN_OUT'); | ||
expect(onSignIn).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
// @todo-migration potentially remove | ||
it.skip('ignores token refresh failure event when state is unauthenticated', async () => { | ||
await defaultAuthHubHandler( | ||
{ channel: 'auth', payload: { event: 'tokenRefresh_failure' } }, | ||
unauthenticatedStateMachine | ||
); | ||
expect(unauthSendSpy).not.toHaveBeenCalled(); | ||
}); | ||
describe('listenToAuthHub', () => { | ||
it('creates a Hub listener', () => { | ||
// add empty mockImplementation to prevent logging "auth channel" warning output to the console | ||
jest.spyOn(console, 'warn').mockImplementation(); | ||
|
||
// @todo-migration | ||
// expect(jest.fn()).toHaveBeenCalledWith(...expected) | ||
// Expected: {"type": "AUTO_SIGN_IN"} | ||
// Number of calls: 0 | ||
it.skip('responds to autoSignIn event when state is unauthenticated', async () => { | ||
await defaultAuthHubHandler( | ||
{ channel: 'auth', payload: { event: 'autoSignIn' } }, | ||
unauthenticatedStateMachine | ||
); | ||
expect(unauthSendSpy).toHaveBeenCalledWith({ type: 'AUTO_SIGN_IN' }); | ||
}); | ||
const hubListenSpy = jest.spyOn(Hub, 'listen'); | ||
const handler = jest.fn(); | ||
|
||
it('ignores autoSignIn event when state is authenticated', async () => { | ||
await defaultAuthHubHandler( | ||
{ channel: 'auth', payload: { event: 'autoSignIn' } }, | ||
authenticatedStateMachine | ||
); | ||
expect(unauthSendSpy).not.toHaveBeenCalled(); | ||
}); | ||
listenToAuthHub(service, handler); | ||
|
||
// @todo-migration | ||
// expect(jest.fn()).toHaveBeenCalledWith(...expected) | ||
// Expected: {"type": "AUTO_SIGN_IN_FAILURE"} | ||
// Number of calls: 0 | ||
it.skip('responds to autoSignIn_failure event', async () => { | ||
await defaultAuthHubHandler( | ||
{ channel: 'auth', payload: { event: 'autoSignIn_failure' } }, | ||
unauthenticatedStateMachine | ||
expect(hubListenSpy).toHaveBeenCalledTimes(1); | ||
expect(hubListenSpy).toHaveBeenCalledWith( | ||
'auth', | ||
expect.any(Function), | ||
'authenticator-hub-handler' | ||
); | ||
expect(unauthSendSpy).toHaveBeenCalledWith({ | ||
type: 'AUTO_SIGN_IN_FAILURE', | ||
}); | ||
}); | ||
|
||
// @todo-migration | ||
// Expected number of calls: 1 | ||
// Received number of calls: 0 | ||
it.skip.each(['signIn', 'signOut'])( | ||
'calls the %s event handler as expected', | ||
async (event) => { | ||
const handler = event === 'signIn' ? onSignIn : onSignOut; | ||
const handlerKey = event === 'signIn' ? 'onSignIn' : 'onSignOut'; | ||
await defaultAuthHubHandler( | ||
{ channel: 'auth', payload: { event } }, | ||
authenticatedStateMachine, | ||
{ [handlerKey]: handler } | ||
); | ||
expect(handler).toHaveBeenCalledTimes(1); | ||
} | ||
); | ||
|
||
it("doesn't break when unsupported event is passed", async () => { | ||
const spyError = jest.spyOn(console, 'error'); | ||
await defaultAuthHubHandler( | ||
{ channel: 'auth', payload: { event: 'unsupported' } }, | ||
unauthenticatedStateMachine | ||
Hub.dispatch('auth', { event: 'signedIn' }, 'authenticator-hub-handler'); | ||
|
||
expect(handler).toHaveBeenCalledTimes(1); | ||
expect(handler).toHaveBeenCalledWith( | ||
{ | ||
channel: 'auth', | ||
patternInfo: [], | ||
payload: { event: 'signedIn' }, | ||
source: 'authenticator-hub-handler', | ||
}, | ||
{ send: expect.any(Function) } | ||
); | ||
expect(spyError).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters