Skip to content

Commit

Permalink
chore: Adds Test Cases for Auth onFail flow
Browse files Browse the repository at this point in the history
  • Loading branch information
sternetj committed Oct 6, 2023
1 parent 781fa1f commit dc03d24
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion src/screens/LoginScreen.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import React from 'react';
import { Text } from 'react-native';
import { render } from '@testing-library/react-native';
import { render, act } from '@testing-library/react-native';
import { Provider as PaperProvider } from 'react-native-paper';
import { useStyles, useDeveloperConfig, usePendingInvite } from '../hooks';
import { LoginScreen } from './LoginScreen';
import * as OAuthLoginButtonModule from '../components/OAuthLoginButton';

jest.mock('../hooks', () => ({
useStyles: jest.fn(),
useDeveloperConfig: jest.fn(),
usePendingInvite: jest.fn(),
}));
jest.mock('react-native-paper', () => {
const Dialog = ({ visible, children }: any) => visible && children;
Dialog.Icon = jest.fn();
Dialog.Title = Text;
Dialog.Content = ({ children }: any) => children;

return {
...jest.requireActual('react-native-paper'),
Dialog,
};
});

const useDeveloperConfigMock = useDeveloperConfig as jest.Mock;
const useStylesMock = useStyles as jest.Mock;
Expand Down Expand Up @@ -66,4 +78,53 @@ describe('LoginScreen', () => {

expect(getByText('Custom Login Screen')).toBeDefined();
});

it('renders an error when onFail is called', () => {
const oAuthLoginButtonSpy = jest.spyOn(
OAuthLoginButtonModule,
'OAuthLoginButton',
);

const { findByText } = render(loginScreenInContext);

expect(oAuthLoginButtonSpy).toHaveBeenCalledTimes(1);

const onFail = oAuthLoginButtonSpy.mock.calls[0][0].onFail!;

act(() => {
onFail('Fake error');
});

expect(findByText('Authentication Error')).toBeDefined();
expect(
findByText('We encountered an error trying to log you in.'),
).toBeDefined();
});

const NON_ERRORING_FAILURE_MESSAGES = [
'User cancelled flow',
"The operation couldn't be completed. (org.openid.appauth.general error -3.)",
];

test.each(NON_ERRORING_FAILURE_MESSAGES)(
'does not render an error when onFail is called with a %p event',
(errorMessage) => {
const oAuthLoginButtonSpy = jest.spyOn(
OAuthLoginButtonModule,
'OAuthLoginButton',
);

const { queryByText } = render(loginScreenInContext);

expect(oAuthLoginButtonSpy).toHaveBeenCalledTimes(1);

const onFail = oAuthLoginButtonSpy.mock.calls[0][0].onFail!;

act(() => {
onFail(errorMessage);
});

expect(queryByText('Authentication Error')).toBeNull();
},
);
});

0 comments on commit dc03d24

Please sign in to comment.