diff --git a/packages/cashier/src/Components/__tests__/error-component.spec.js b/packages/cashier/src/Components/__tests__/error-component.spec.js new file mode 100644 index 000000000000..2a56dfc0e694 --- /dev/null +++ b/packages/cashier/src/Components/__tests__/error-component.spec.js @@ -0,0 +1,81 @@ +import React, { useEffect } from 'react'; +import { screen, render, fireEvent } from '@testing-library/react'; +import ErrorComponent from '../error-component'; +import { Router } from 'react-router-dom'; +import { createBrowserHistory } from 'history'; +import { unmountComponentAtNode } from 'react-dom'; +describe('', () => { + let history; + const renderWithRouter = component => { + history = createBrowserHistory(); + return render({component}); + }; + const reloadFn = () => { + window.location.reload(true); + }; + beforeAll(() => { + Object.defineProperty(window, 'location', { + configurable: true, + value: { reload: jest.fn() }, + }); + }); + const props = { + redirect_to: ['/testurl'], + redirect_label: ['testlabel'], + }; + it('should show the default message when message is not passed', () => { + const message = ''; + renderWithRouter(); + expect(screen.getByText('Sorry, an error occured while processing your request.')).toBeInTheDocument(); + }); + it('should show the actual message when message is passed', () => { + const message = 'This is the error message'; + renderWithRouter(); + expect(screen.getByText(message)).toBeInTheDocument(); + }); + it('should show refresh message when should_show_refresh is true', () => { + renderWithRouter(); + expect(screen.getByText('Please refresh this page to continue.')).toBeInTheDocument(); + }); + it('do not show refresh message when should_show_refresh is false', () => { + const refreshRequestText = screen.queryByText('Please refresh this page to continue.'); + renderWithRouter(); + expect(refreshRequestText).toBeNull(); + }); + it('should show default message when header message is not passed', () => { + const header = ''; + renderWithRouter(); + expect(screen.getByText('Something’s not right')).toBeInTheDocument(); + }); + it('should show actual message when header message is passed', () => { + const header = 'Header Text'; + renderWithRouter(); + expect(screen.getByText(header)).toBeInTheDocument(); + }); + it('should refresh the page when redirectOnClick is not passed or empty', () => { + const redirectOnClick = ''; + renderWithRouter(); + reloadFn(); // as defined above.. + expect(window.location.reload).toHaveBeenCalled(); + }); + it('should show the redirect button label as refresh when there is no redirect_label', () => { + const redirectOnClick = ''; + const redirect_to = ['/testurl']; + renderWithRouter(); + expect(screen.getByText('Refresh')).toBeInTheDocument(); + }); + it('should trigger the history.listen and call the setError function when redirect button get clicked', () => { + const redirectOnClick = jest.fn(); + const history = createBrowserHistory(); + const setError = jest.fn(); + render( + + + + ); + fireEvent.click(screen.getByText('testlabel')); + if (typeof setError === 'function') { + expect(setError).toHaveBeenCalledTimes(1); + } + }); +}); diff --git a/packages/cashier/src/Components/error-component.jsx b/packages/cashier/src/Components/error-component.jsx index 45d0da6c6b39..dd261587c734 100644 --- a/packages/cashier/src/Components/error-component.jsx +++ b/packages/cashier/src/Components/error-component.jsx @@ -46,8 +46,8 @@ const ErrorComponent = ({ refresh_message, ] } - redirect_url={redirect_to} - redirect_label={redirect_label || } + redirect_urls={[redirect_to]} + redirect_labels={[redirect_label || ]} buttonOnClick={redirectOnClick || (() => location.reload())} should_clear_error_on_click={should_clear_error_on_click} setError={setError} diff --git a/packages/cashier/src/Containers/__tests__/routes.spec.js b/packages/cashier/src/Containers/__tests__/routes.spec.js index 247d963214f6..552ad54ac3b2 100644 --- a/packages/cashier/src/Containers/__tests__/routes.spec.js +++ b/packages/cashier/src/Containers/__tests__/routes.spec.js @@ -15,10 +15,19 @@ jest.mock('Components/Routes/binary-routes', () => () =>
BinaryRoutes
describe('', () => { it('should show error messages when "has_error = true"', () => { const history = createBrowserHistory(); - + const error = { + header:'', + message:'', + redirect_label:['test label'], + redirectOnClick:jest.fn(), + should_clear_error_on_click:true, + setError:jest.fn(), + redirect_to:['/testurl'], + should_show_refresh:true, + } render( - + );