UNPKG

@brightlayer-ui/react-native-auth-workflow

Version:

Re-usable workflow components for Authentication and Registration within Eaton applications.

193 lines (192 loc) 12 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import React from 'react'; import { cleanup, fireEvent, render, screen, renderHook, act, waitFor, } from '@testing-library/react-native'; import { RegistrationWorkflow } from '../../../components/RegistrationWorkflow/RegistrationWorkflow'; import { RegistrationContextProvider, useRegistrationWorkflowContext } from '../../../contexts'; import { CreateAccountScreen } from '../../../screens'; import { registrationContextProviderProps } from '../../../testUtils'; import { Button, Text, Provider as PaperProvider, TextInput } from 'react-native-paper'; import { View } from 'react-native'; afterEach(cleanup); const defaultProps = { initialScreenIndex: 0, }; const renderer = (props = defaultProps) => render(React.createElement(RegistrationContextProvider, Object.assign({}, registrationContextProviderProps), React.createElement(RegistrationWorkflow, Object.assign({}, props), React.createElement(Text, null, "Screen 1"), React.createElement(Text, null, "Screen 2")))); describe('RegistrationWorkflow', () => { it('should render without crashing', () => { renderer(); expect(render); expect(screen.getByText('Screen 1')).toBeOnTheScreen(); }); it('should render multiple screens', () => { const nextScreen = jest.fn(); const { getByText } = render(React.createElement(RegistrationContextProvider, Object.assign({}, registrationContextProviderProps), React.createElement(RegistrationWorkflow, null, React.createElement(React.Fragment, null, React.createElement(Text, null, "Indexed Screen 1"), React.createElement(Button, { onPress: () => { nextScreen({ screenId: 'Eula', values: { accepted: true } }); } }, "Next")), React.createElement(Text, null, "Indexed Screen 2")))); fireEvent.press(getByText('Next')); expect(screen.getByText('Indexed Screen 2')).toBeOnTheScreen(); }); it('should render the correct screen, when initialScreenIndex prop is passed', () => { renderer({ initialScreenIndex: 1 }); expect(screen.getByText('Screen 2')).toBeOnTheScreen(); }); it('should call nextScreen function', () => { const nextScreen = jest.fn(); const { getByText } = render(React.createElement(RegistrationContextProvider, Object.assign({}, registrationContextProviderProps), React.createElement(RegistrationWorkflow, null, React.createElement(React.Fragment, null, React.createElement(Text, null, "Indexed Screen 1"), React.createElement(Button, { onPress: () => { nextScreen({ screenId: 'Eula', values: { accepted: true } }); } }, "Next")), React.createElement(Text, null, "Indexed Screen 2")))); fireEvent.press(getByText('Next')); expect(nextScreen).toHaveBeenCalledWith({ screenId: 'Eula', values: { accepted: true } }); }); it('should set screen data for default registration workflow in the context', () => { const wrapper = ({ children }) => (React.createElement(PaperProvider, null, React.createElement(RegistrationContextProvider, Object.assign({}, registrationContextProviderProps), React.createElement(RegistrationWorkflow, Object.assign({ isInviteRegistration: true, initialRegistrationParams: { code: '123', email: 'emailAddress@emailAddress.com' } }, defaultProps), children)))); const { result } = renderHook(() => useRegistrationWorkflowContext(), { wrapper }); void act(() => { void result.current.nextScreen({ screenId: 'Eula', values: { accepted: true } }); }); expect(result.current.screenData.Eula.accepted).toBeTruthy(); expect(result.current.screenData.CreateAccount.emailAddress).toBe('emailAddress@emailAddress.com'); expect(result.current.screenData.VerifyCode.code).toBe('123'); }); it('should set screen data for custom registration workflow in the context', () => { const wrapper = ({ children }) => (React.createElement(PaperProvider, null, React.createElement(RegistrationContextProvider, Object.assign({}, registrationContextProviderProps), React.createElement(RegistrationWorkflow, Object.assign({}, defaultProps), children)))); const { result } = renderHook(() => useRegistrationWorkflowContext(), { wrapper }); void act(() => { void result.current.nextScreen({ screenId: 'Screen1', values: { test: 'test' } }); }); void act(() => { void result.current.nextScreen({ screenId: 'Screen2', values: { test2: 'test2' }, }); }); // @ts-ignore expect(result.current.screenData.Other.Screen1.test).toBe('test'); // @ts-ignore void (() => expect(result.current.screenData.Other.Screen2.test2).toBe('test2')); }); it('should check for lower bound of initialScreenIndex props', () => { renderer({ initialScreenIndex: -1 }); expect(screen.getByText('Screen 1')).toBeOnTheScreen(); }); it('should check for upper bound of initialScreenIndex props', () => { renderer({ initialScreenIndex: 2 }); expect(screen.getByText('Screen 2')).toBeOnTheScreen(); }); it('should render custom success screen', () => { const props = defaultProps; defaultProps.successScreen = (React.createElement(View, null, React.createElement(Text, null, "Custom Success"))); const { getByTestId, getByText } = render(React.createElement(PaperProvider, null, React.createElement(RegistrationContextProvider, Object.assign({}, registrationContextProviderProps), React.createElement(RegistrationWorkflow, Object.assign({}, props), React.createElement(CreateAccountScreen, null))))); const verifyEmailInput = getByTestId('blui-create-account-email-text-input'); fireEvent.changeText(verifyEmailInput, 'test@test.net'); const nextButton = getByText('Next'); fireEvent.press(nextButton); void (() => expect(screen.getByText('Custom Success')).toBeInTheDocument()); }); it('should render existing account screen', () => { defaultProps.existingAccountSuccessScreen = (React.createElement(View, null, React.createElement(Text, null, "Account Exists!!!"))); const wrapper = ({ children }) => (React.createElement(PaperProvider, null, React.createElement(RegistrationContextProvider, Object.assign({}, registrationContextProviderProps), React.createElement(RegistrationWorkflow, Object.assign({}, defaultProps), children)))); const { result } = renderHook(() => useRegistrationWorkflowContext(), { wrapper }); expect(result.current.screenData.Eula.accepted).toBeFalsy(); expect(result.current.screenData.CreateAccount.emailAddress).toBe(''); void act(() => { void result.current.nextScreen({ screenId: 'Eula', values: { accepted: true } }); }); void act(() => { void result.current.nextScreen({ screenId: 'CreateAccount', values: { emailAddress: 'emailAddress@emailAddress.com' }, isAccountExist: true, }); }); expect(result.current.screenData.Eula.accepted).toBeTruthy(); expect(screen.getByText('Account Exists!!!')).toBeOnTheScreen(); }); it('should render existing account screen', () => { defaultProps.existingAccountSuccessScreen = (React.createElement(View, null, React.createElement(Text, null, "Account Exists!!!"))); const wrapper = ({ children }) => (React.createElement(PaperProvider, null, React.createElement(RegistrationContextProvider, Object.assign({}, registrationContextProviderProps), React.createElement(RegistrationWorkflow, Object.assign({}, defaultProps), children)))); const { result } = renderHook(() => useRegistrationWorkflowContext(), { wrapper }); expect(result.current.screenData.Eula.accepted).toBeFalsy(); expect(result.current.screenData.CreateAccount.emailAddress).toBe(''); void act(() => { void result.current.nextScreen({ screenId: 'Eula', values: { accepted: true } }); }); void act(() => { void result.current.nextScreen({ screenId: 'CreateAccount', values: { emailAddress: 'emailAddress@emailAddress.com' }, isAccountExist: true, }); }); expect(result.current.screenData.Eula.accepted).toBeTruthy(); void (() => expect(screen.getByText('Account Exists!!!')).toBeInTheDocument()); }); it('should display default registration workflow', () => __awaiter(void 0, void 0, void 0, function* () { render(React.createElement(PaperProvider, null, React.createElement(RegistrationContextProvider, Object.assign({}, registrationContextProviderProps), React.createElement(RegistrationWorkflow, null)))); yield waitFor(() => expect(screen.queryByText('Loading...')).toBeNull()); fireEvent.press(screen.getAllByText('Next')[0]); expect(screen.getByText('Create an Account')).toBeOnTheScreen(); expect(screen.getAllByText('Next').length).toBe(5); }), 100000); it('should skip create account screen in invite registration workflow', () => __awaiter(void 0, void 0, void 0, function* () { render(React.createElement(PaperProvider, null, React.createElement(RegistrationContextProvider, Object.assign({}, registrationContextProviderProps), React.createElement(RegistrationWorkflow, { isInviteRegistration: true, initialRegistrationParams: { code: '123', email: 'emailAddress@emailAddress.com' } })))); yield waitFor(() => expect(screen.queryByText('Loading...')).toBeNull()); fireEvent.press(screen.getAllByText('Next')[0]); expect(screen.queryByText('Create Account')).not.toBeOnTheScreen(); expect(screen.getByText('Create Password')).toBeOnTheScreen(); expect(screen.getAllByText('Next').length).toBe(3); })); it('should display custom screen', () => { render(React.createElement(PaperProvider, null, React.createElement(RegistrationContextProvider, Object.assign({}, registrationContextProviderProps), React.createElement(RegistrationWorkflow, null, React.createElement(TextInput, { testID: "blui-registration-workflow-text-input" }))))); expect(screen.getByTestId('blui-registration-workflow-text-input')).toBeOnTheScreen(); }); it('should display single screen', () => { render(React.createElement(PaperProvider, null, React.createElement(RegistrationContextProvider, Object.assign({}, registrationContextProviderProps), React.createElement(RegistrationWorkflow, null, React.createElement(CreateAccountScreen, null))))); expect(screen.getByTestId('blui-create-account-email-text-input')).toBeOnTheScreen(); }); });