|
| 1 | +import React from 'react'; |
| 2 | +import { Formik } from 'formik'; |
| 3 | +import { createBrowserHistory } from 'history'; |
| 4 | +import { Router, useHistory } from 'react-router-dom'; |
| 5 | +import { Modal } from '@deriv-com/ui'; |
| 6 | +import { act, render, screen, waitFor } from '@testing-library/react'; |
| 7 | +import userEvent from '@testing-library/user-event'; |
| 8 | +import { LeaveConfirm } from '../LeaveConfirm'; |
| 9 | + |
| 10 | +jest.mock('@deriv-com/ui', () => ({ |
| 11 | + ...jest.requireActual('@deriv-com/ui'), |
| 12 | + useDevice: jest.fn(() => ({ isMobile: false })), |
| 13 | +})); |
| 14 | + |
| 15 | +afterEach(() => { |
| 16 | + jest.clearAllMocks(); |
| 17 | +}); |
| 18 | + |
| 19 | +let history: ReturnType<typeof useHistory>; |
| 20 | + |
| 21 | +beforeEach(() => { |
| 22 | + history = createBrowserHistory(); |
| 23 | +}); |
| 24 | + |
| 25 | +describe('LeaveConfirm', () => { |
| 26 | + const onCancelMock = jest.fn(); |
| 27 | + const onLeaveMock = jest.fn(); |
| 28 | + |
| 29 | + const wrapper = ({ children }: { children: JSX.Element }) => { |
| 30 | + return ( |
| 31 | + <Router history={history}> |
| 32 | + <Formik initialValues={{ name: '' }} onSubmit={jest.fn()}> |
| 33 | + {({ handleBlur, handleChange, handleSubmit, values }) => ( |
| 34 | + <form onSubmit={handleSubmit}> |
| 35 | + {children} |
| 36 | + <input |
| 37 | + aria-label='name' |
| 38 | + name='name' |
| 39 | + onBlur={handleBlur} |
| 40 | + onChange={handleChange} |
| 41 | + value={values.name} |
| 42 | + /> |
| 43 | + </form> |
| 44 | + )} |
| 45 | + </Formik> |
| 46 | + </Router> |
| 47 | + ); |
| 48 | + }; |
| 49 | + |
| 50 | + const renderComponent = () => { |
| 51 | + render(<LeaveConfirm onCancel={onCancelMock} onLeave={onLeaveMock} />, { wrapper }); |
| 52 | + const inputField = screen.getByRole('textbox'); |
| 53 | + userEvent.type(inputField, 'Hello'); |
| 54 | + |
| 55 | + act(() => { |
| 56 | + history.push('/something'); |
| 57 | + }); |
| 58 | + }; |
| 59 | + |
| 60 | + Modal.setAppElement('body'); |
| 61 | + |
| 62 | + const unsavedChangesText = 'Unsaved Changes'; |
| 63 | + |
| 64 | + it('should render the component', async () => { |
| 65 | + renderComponent(); |
| 66 | + await waitFor(() => { |
| 67 | + expect(screen.getByText(unsavedChangesText)).toBeInTheDocument(); |
| 68 | + expect( |
| 69 | + screen.getByText( |
| 70 | + 'You have unsaved changes. Are you sure you want to discard changes and leave this page?' |
| 71 | + ) |
| 72 | + ).toBeInTheDocument(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should call onCancel when Cancel button is clicked', async () => { |
| 77 | + renderComponent(); |
| 78 | + |
| 79 | + const cancelButton = screen.getByLabelText('Cancel'); |
| 80 | + userEvent.click(cancelButton); |
| 81 | + |
| 82 | + await waitFor(() => { |
| 83 | + expect(screen.queryByText(unsavedChangesText)).not.toBeInTheDocument(); |
| 84 | + expect(onCancelMock).toHaveBeenCalled(); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should call onLeave when Leave Settings button is clicked', async () => { |
| 89 | + renderComponent(); |
| 90 | + |
| 91 | + const leaveButton = screen.getByLabelText('Leave Settings'); |
| 92 | + userEvent.click(leaveButton); |
| 93 | + |
| 94 | + await waitFor(() => { |
| 95 | + expect(screen.queryByText(unsavedChangesText)).not.toBeInTheDocument(); |
| 96 | + expect(onLeaveMock).toHaveBeenCalled(); |
| 97 | + }); |
| 98 | + }); |
| 99 | +}); |
0 commit comments