Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

poc: run e2e tests against #6081 #6367

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { AmplifyErrorCode } from '@aws-amplify/core/internals/utils';
import { Hub } from 'aws-amplify/utils';
import {
defaultAuthHubHandler,
listenToAuthHub,
} from '../defaultAuthHubHandler';
import { AuthInterpreter } from '../types';

const MockNetworkError = { name: AmplifyErrorCode.NetworkError };
const onSignIn = jest.fn();
const onSignOut = jest.fn();
const service = { send: jest.fn() } as unknown as AuthInterpreter;
Expand Down Expand Up @@ -41,16 +43,30 @@ describe('defaultAuthHubHandler', () => {
expect(onSignOut).toHaveBeenCalledTimes(1);
});

it('does not call onSignOut callback on tokenRefreh_failure event if provided', () => {
it('does not call onSignOut callback on tokenRefresh_failure event if provided', () => {
defaultAuthHubHandler(
{ channel: 'auth', payload: { event: 'tokenRefreh_failure' } },
{ channel: 'auth', payload: { event: 'tokenRefresh_failure' } },
service,
{ onSignOut }
);
expect(onSignOut).not.toHaveBeenCalled();
});

it('calls onSignIn callabck on signedIn event if provided', () => {
it('does not call service on tokenRefresh_failure event if NetworkError', () => {
defaultAuthHubHandler(
{
channel: 'auth',
payload: {
event: 'tokenRefresh_failure',
data: { error: MockNetworkError },
},
},
service
);
expect(service.send).not.toHaveBeenCalled();
});

it('calls onSignIn callback on signedIn event if provided', () => {
defaultAuthHubHandler(
{ channel: 'auth', payload: { event: 'signedIn' } },
service,
Expand Down
15 changes: 11 additions & 4 deletions packages/ui/src/helpers/authenticator/defaultAuthHubHandler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AmplifyErrorCode } from '@aws-amplify/core/internals/utils';
import { Hub } from 'aws-amplify/utils';

import { isFunction } from '../../utils';
Expand All @@ -13,7 +14,7 @@ export const defaultAuthHubHandler: AuthMachineHubHandler = (
service,
options
) => {
const { event } = payload;
const { data, event } = payload;
const { send } = service;
const { onSignIn, onSignOut } = options ?? {};

Expand All @@ -28,14 +29,20 @@ export const defaultAuthHubHandler: AuthMachineHubHandler = (
send('SIGN_IN_WITH_REDIRECT');
break;
}
case 'signedOut':
case 'tokenRefresh_failure': {
if (event === 'signedOut' && isFunction(onSignOut)) {
case 'signedOut': {
if (isFunction(onSignOut)) {
onSignOut();
}
send('SIGN_OUT');
break;
}
case 'tokenRefresh_failure': {
if (data?.error?.name === AmplifyErrorCode.NetworkError) {
return;
}
send('SIGN_OUT');
break;
}
default: {
break;
}
Expand Down
Loading