@brightlayer-ui/react-native-auth-workflow
Version:
Re-usable workflow components for Authentication and Registration within Eaton applications.
166 lines (165 loc) • 10.1 kB
JavaScript
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 '@testing-library/jest-dom';
import '@testing-library/jest-native/extend-expect';
import { cleanup, render, fireEvent, screen, waitFor } from '@testing-library/react-native';
import renderer from 'react-test-renderer';
import { EulaScreen } from '../../screens/EulaScreen';
import { RegistrationWorkflow } from '../../components';
import { RegistrationContextProvider, i18nRegistrationInstance } from '../../contexts';
import { registrationContextProviderProps } from '../../testUtils';
import { Provider as PaperProvider } from 'react-native-paper';
afterEach(cleanup);
describe('Eula Screen', () => {
it('Eula Screen renders correctly', () => {
const rendered = renderer
.create(React.createElement(PaperProvider, null,
React.createElement(RegistrationContextProvider, Object.assign({}, registrationContextProviderProps),
React.createElement(RegistrationWorkflow, { initialScreenIndex: 0 },
React.createElement(EulaScreen, null)))))
.toJSON();
expect(rendered).toBeTruthy();
});
it('check if next button is calling onNext function prop', () => {
const nextfn = jest.fn();
render(React.createElement(PaperProvider, null,
React.createElement(RegistrationContextProvider, Object.assign({}, registrationContextProviderProps),
React.createElement(RegistrationWorkflow, { initialScreenIndex: 0 },
React.createElement(EulaScreen, { eulaContent: 'Hello', checkboxLabel: 'check', checkboxProps: { disabled: false }, WorkflowCardActionsProps: {
onNext: () => nextfn(),
showNext: true,
nextLabel: 'NextButton',
} })))));
const checkbox = screen.getByTestId('blui-eula-checkbox');
fireEvent.press(checkbox);
const nextButton = screen.getByText('NextButton');
expect(nextButton).toBeEnabled();
fireEvent.press(nextButton);
expect(nextfn).toHaveBeenCalled();
});
it('check if previos button is calling onPrevious function prop', () => {
const prevFn = jest.fn();
render(React.createElement(PaperProvider, null,
React.createElement(RegistrationContextProvider, Object.assign({}, registrationContextProviderProps),
React.createElement(RegistrationWorkflow, { initialScreenIndex: 0 },
React.createElement(EulaScreen, { eulaContent: 'Hello', checkboxLabel: 'I have read and agree to the Terms & Conditions', initialCheckboxValue: true, WorkflowCardActionsProps: {
onPrevious: prevFn(),
showPrevious: true,
previousLabel: 'Back',
} })))));
fireEvent.press(screen.getByText('Back'));
expect(prevFn).toHaveBeenCalled();
});
it('should throw error in eula and clicking refresh button should call loadEula', () => __awaiter(void 0, void 0, void 0, function* () {
const loadFn = jest.fn().mockRejectedValue(new Error('qwertyuiop'));
const { findByText } = render(React.createElement(PaperProvider, null,
React.createElement(RegistrationContextProvider, Object.assign({}, {
language: 'en',
i18n: i18nRegistrationInstance,
navigate: () => { },
routeConfig: {},
actions: {
loadEula: loadFn,
acceptEula: jest.fn(),
requestRegistrationCode: jest.fn(),
validateUserRegistrationRequest: jest.fn(),
createPassword: jest.fn(),
setAccountDetails: jest.fn(),
completeRegistration: jest.fn().mockImplementation(() => Promise.resolve()),
},
}),
React.createElement(RegistrationWorkflow, null,
React.createElement(EulaScreen, null)))));
yield waitFor(() => expect(screen.queryByText('Loading...')).toBeNull());
fireEvent.press(yield findByText('Retry'));
expect(loadFn).toHaveBeenCalledTimes(2);
}), 10000);
it('should throw error in eula and clicking refresh button should call loadEula', () => __awaiter(void 0, void 0, void 0, function* () {
const loadFn = jest.fn().mockRejectedValue(new Error('qwertyuiop'));
const { findByText } = render(React.createElement(PaperProvider, null,
React.createElement(RegistrationContextProvider, Object.assign({}, {
language: 'en',
i18n: i18nRegistrationInstance,
navigate: () => { },
routeConfig: {},
actions: {
loadEula: loadFn,
acceptEula: jest.fn(),
requestRegistrationCode: jest.fn(),
validateUserRegistrationRequest: jest.fn(),
createPassword: jest.fn(),
setAccountDetails: jest.fn(),
completeRegistration: jest.fn().mockImplementation(() => Promise.resolve()),
},
}),
React.createElement(RegistrationWorkflow, null,
React.createElement(EulaScreen, null)))));
yield waitFor(() => expect(screen.queryByText('Loading...')).toBeNull());
fireEvent.press(yield findByText('Retry'));
expect(loadFn).toHaveBeenCalledTimes(2);
}), 10000);
it('should be able to go to next screen when account exists in validateUserRegistrationRequest on next button pressed in case of invite registration', () => __awaiter(void 0, void 0, void 0, function* () {
const validateFn = jest.fn().mockReturnValue({ codeValid: true, accountExists: true });
const { getByText, queryByText, getByTestId, findByText } = render(React.createElement(PaperProvider, null,
React.createElement(RegistrationContextProvider, Object.assign({}, {
language: 'en',
i18n: i18nRegistrationInstance,
navigate: () => { },
routeConfig: {},
actions: {
loadEula: jest.fn(),
acceptEula: jest.fn(),
requestRegistrationCode: jest.fn(),
validateUserRegistrationRequest: validateFn,
createPassword: jest.fn(),
setAccountDetails: jest.fn(),
completeRegistration: jest.fn().mockImplementation(() => Promise.resolve()),
},
}),
React.createElement(RegistrationWorkflow, { isInviteRegistration: true, initialRegistrationParams: { code: '123', email: 'emailAddress@emailAddress.com' } },
React.createElement(EulaScreen, { eulaContent: 'Hello', checkboxLabel: 'check', checkboxProps: { disabled: false } })))));
yield waitFor(() => expect(queryByText('Loading...')).toBeNull());
const checkbox = getByTestId('blui-eula-checkbox');
fireEvent.press(checkbox);
fireEvent.press(getByText('Next'));
yield waitFor(() => expect(queryByText('Loading...')).toBeNull());
expect(yield findByText('Your account has been successfully created. Please log in with your Eaton account email and password.')).toBeOnTheScreen();
expect(validateFn).toHaveBeenCalled();
}), 10000);
it('should be able to trigger error when codeValid is false in validateUserRegistrationRequest on next button pressed in case of invite registration ', () => __awaiter(void 0, void 0, void 0, function* () {
const validateFn = jest.fn().mockReturnValue({ codeValid: false, accountExists: false });
const { getByText, queryByText, getByTestId, findByText } = render(React.createElement(PaperProvider, null,
React.createElement(RegistrationContextProvider, Object.assign({}, {
language: 'en',
i18n: i18nRegistrationInstance,
navigate: () => { },
routeConfig: {},
actions: {
loadEula: jest.fn(),
acceptEula: jest.fn(),
requestRegistrationCode: jest.fn(),
validateUserRegistrationRequest: validateFn,
createPassword: jest.fn(),
setAccountDetails: jest.fn(),
completeRegistration: jest.fn().mockImplementation(() => Promise.resolve()),
},
}),
React.createElement(RegistrationWorkflow, { isInviteRegistration: true, initialRegistrationParams: { code: '123', email: 'emailAddress@emailAddress.com' } },
React.createElement(EulaScreen, { eulaContent: 'Hello', checkboxLabel: 'check', checkboxProps: { disabled: false } })))));
yield waitFor(() => expect(queryByText('Loading...')).toBeNull());
const checkbox = getByTestId('blui-eula-checkbox');
fireEvent.press(checkbox);
fireEvent.press(getByText('Next'));
yield waitFor(() => expect(queryByText('Loading...')).toBeNull());
expect(yield findByText('Error!')).toBeOnTheScreen();
expect(validateFn).toHaveBeenCalled();
}), 10000);
});