Skip to content

Commit

Permalink
Merge branch 'master' into aum/WALL-297/deposit-fiat-module
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-hosseini-deriv authored Jun 18, 2023
2 parents d0d184c + b27de39 commit 770a43b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React from 'react';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import LeaveConfirm, { TransitionBlockerWithRouter } from '../leave-confirm';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Formik } from 'formik';
import { Router } from 'react-router';
import { createBrowserHistory } from 'history';
import { Router } from 'react-router';
import { isMobile } from '@deriv/shared';
import LeaveConfirm, { TransitionBlocker } from '../leave-confirm';

let modal_root_el;
let modal_root_el: HTMLElement;

jest.mock('@deriv/shared', () => ({
...jest.requireActual('@deriv/shared'),
Expand All @@ -23,12 +24,34 @@ afterAll(() => {
document.body.removeChild(modal_root_el);
});

const mock_set_show = jest.fn();
const mock_set_next_location = jest.fn();

const get_leave_confirm_states = () => {
return jest
.spyOn(React, 'useState')
.mockImplementationOnce(() => [null, jest.fn()])
.mockImplementationOnce(() => [true, mock_set_show])
.mockImplementationOnce(() => [{ pathname: '/' }, mock_set_next_location]);
};

const get_transition_blocker_states = ({ pathname }: { pathname: string | null }) => {
return jest
.spyOn(React, 'useState')
.mockImplementationOnce(() => [true, mock_set_show])
.mockImplementationOnce(() => [pathname ? { pathname: '/' } : null, mock_set_next_location]);
};

const on_dirty = jest.fn();

const LeaveConfirmComponent = () => {
const history = createBrowserHistory();
return (
<Router history={history}>
<Formik>
<LeaveConfirm onDirty={jest.fn()} />
<Formik initialValues={{ field: '' }} enableReinitialize validate={jest.fn()} onSubmit={jest.fn()}>
{() => {
return <LeaveConfirm onDirty={on_dirty()} />;
}}
</Formik>
</Router>
);
Expand All @@ -45,25 +68,24 @@ const withRouter = Component => {
return WrapperComponent;
};

const TransitionBlockerComponent = withRouter(TransitionBlockerWithRouter);
const TransitionBlockerComponent = withRouter(TransitionBlocker);

describe('LeaveConfirm', () => {
it('should render LeaveConfirm component in desktop mode', () => {
jest.spyOn(React, 'useState').mockReturnValueOnce([true, () => null]);
get_leave_confirm_states();
render(<LeaveConfirmComponent />);
expect(
screen.getByText('You have unsaved changes. Are you sure you want to discard changes and leave this page?')
).toBeInTheDocument();
});
it('should render LeaveConfirm component in mobile mode', () => {
jest.spyOn(React, 'useState').mockImplementationOnce(() => React.useState(true));
isMobile.mockReturnValueOnce(true);
get_leave_confirm_states();
(isMobile as jest.Mock).mockReturnValueOnce(true);
render(<LeaveConfirmComponent />);
expect(screen.getByText('Unsaved changes')).toBeInTheDocument();
});
it('should show proper icon', () => {
jest.spyOn(React, 'useState').mockImplementationOnce(() => React.useState(true));

get_leave_confirm_states();
render(<LeaveConfirmComponent />);
expect(screen.getByTestId('unsaved_changes_icon')).toBeInTheDocument();
expect(screen.getByText('Unsaved changes')).toBeInTheDocument();
Expand All @@ -74,28 +96,46 @@ describe('LeaveConfirm', () => {
expect(screen.getByRole('button', { name: 'Leave Settings' })).toBeInTheDocument();
});
it('should trigger onclick callback when the user clicks cancel button', async () => {
jest.spyOn(React, 'useState').mockImplementationOnce(() => React.useState(true));
get_leave_confirm_states();
render(<LeaveConfirmComponent />);
const el_cancel_btn = screen.getByRole('button', { name: 'Cancel' });
fireEvent.click(el_cancel_btn);
await waitFor(() => {
expect(screen.queryByText('Unsaved changes')).not.toBeInTheDocument();
});
await userEvent.click(el_cancel_btn);

expect(mock_set_show).toHaveBeenCalled();
expect(mock_set_next_location).toHaveBeenCalled();
});
it('should sehow modal when value is dirty and trigger unblock function', () => {
get_transition_blocker_states({ pathname: '/' });
render(<TransitionBlockerComponent dirty onDirty={on_dirty} />);
const el_leave_settings_btn = screen.getByRole('button', { name: 'Leave Settings' });
userEvent.click(el_leave_settings_btn);

expect(on_dirty).toHaveBeenCalled();
expect(mock_set_show).toHaveBeenCalled();
expect(mock_set_next_location).toHaveBeenCalled();
});

it('should set values as dirty when the user leaves modal', () => {
jest.spyOn(React, 'useState').mockImplementationOnce(() => React.useState(true));
render(<TransitionBlockerComponent onDirty={jest.fn()} />);
get_transition_blocker_states({ pathname: '/' });
render(<TransitionBlockerComponent onDirty={on_dirty} />);
const el_cancel_btn = screen.getByRole('button', { name: 'Cancel' });
fireEvent.click(el_cancel_btn);
expect(screen.getByRole('button', { name: 'Leave Settings' })).toBeInTheDocument();
userEvent.click(el_cancel_btn);

expect(on_dirty).toHaveBeenCalled();
});
it('should change pathname when user leaves form', () => {
jest.spyOn(React, 'useState')
.mockReturnValueOnce([true, () => null])
.mockReturnValueOnce([{ pathname: '/' }, () => null]);
get_transition_blocker_states({ pathname: '/' });
render(<TransitionBlockerComponent />);
const el_leave_settings_btn = screen.getByRole('button', { name: 'Leave Settings' });
userEvent.click(el_leave_settings_btn);
expect(screen.getByRole('button', { name: 'Leave Settings' })).toBeInTheDocument();
});

it('should not change pathname when user leaves form', () => {
get_transition_blocker_states({ pathname: null });
render(<TransitionBlockerComponent />);
const el_leave_settings_btn = screen.getByRole('button', { name: 'Leave Settings' });
fireEvent.click(el_leave_settings_btn);
userEvent.click(el_leave_settings_btn);
expect(screen.getByRole('button', { name: 'Leave Settings' })).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ const AcuityDownloadModal = ({
};

const getLink = (language: string): string => {
if (language === 'PT') {
return getUrlBase('/public/pdf/Acuity_tool_user_guide_Portuguese.pdf');
} else if (language === 'ES') {
if (language === 'ES') {
return getUrlBase('/public/pdf/Acuity_tool_user_guide_Spanish.pdf');
}
if (is_eu) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const languages = {
ID: 'Indonesian',
IT: 'Italiano',
PL: 'Polish',
PT: 'Português',
RU: 'Русский',
VI: 'Tiếng Việt',
ZH_CN: '简体中文',
Expand Down
2 changes: 0 additions & 2 deletions packages/translations/src/i18next/i18next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const ALL_LANGUAGES = Object.freeze({
ID: 'Indonesian',
IT: 'Italiano',
PL: 'Polish',
PT: 'Português',
RU: 'Русский',
VI: 'Tiếng Việt',
ZH_CN: '简体中文',
Expand All @@ -24,7 +23,6 @@ const ALL_LANGUAGES = Object.freeze({
export const getAllowedLanguages = () => {
const allowed_languages = {
EN: 'English',
PT: 'Português',
ES: 'Español',
RU: 'Русский',
FR: 'Français',
Expand Down

0 comments on commit 770a43b

Please sign in to comment.