forked from deriv-com/deriv-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request deriv-com#70 from farrah-deriv/refactor-leave-page…
…-modal refactor: LeavePageModal
- Loading branch information
Showing
2 changed files
with
90 additions
and
6 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
.../src/components/modal-manager/modals/leave-page-modal/__tests__/leave-page-modal.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { useModalManagerContext } from 'Components/modal-manager/modal-manager-context'; | ||
import { useStores } from 'Stores'; | ||
import LeavePageModal from '../leave-page-modal'; | ||
|
||
const mock_store_values: DeepPartial<ReturnType<typeof useStores>> = { | ||
buy_sell_store: { | ||
setShowFilterPaymentMethods: jest.fn(), | ||
}, | ||
}; | ||
const mock_modal_manager: Partial<ReturnType<typeof useModalManagerContext>> = { | ||
hideModal: jest.fn(), | ||
is_modal_open: true, | ||
}; | ||
|
||
jest.mock('Stores', () => ({ | ||
...jest.requireActual('Stores'), | ||
useStores: jest.fn(() => mock_store_values), | ||
})); | ||
|
||
jest.mock('Components/modal-manager/modal-manager-context', () => ({ | ||
useModalManagerContext: () => mock_modal_manager, | ||
})); | ||
|
||
describe('<LeavePageModal/>', () => { | ||
let el_modal: HTMLElement; | ||
|
||
beforeAll(() => { | ||
el_modal = document.createElement('div'); | ||
el_modal.setAttribute('id', 'modal_root'); | ||
document.body.appendChild(el_modal); | ||
}); | ||
|
||
afterAll(() => { | ||
document.body.removeChild(el_modal); | ||
}); | ||
|
||
it('should render LeavePageModal with given props', () => { | ||
const props = { | ||
onCancel: jest.fn(), | ||
onLeavePage: jest.fn(), | ||
}; | ||
|
||
render(<LeavePageModal {...props} />); | ||
|
||
expect( | ||
screen.getByText('Are you sure you want to leave this page? Changes made will not be saved.') | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it('should call onLeavePage and close the modal on click of Leave page button', () => { | ||
const props = { | ||
onLeavePage: jest.fn(), | ||
}; | ||
|
||
render(<LeavePageModal {...props} />); | ||
|
||
const leave_page_button = screen.getByRole('button', { name: 'Leave page' }); | ||
userEvent.click(leave_page_button); | ||
expect(props.onLeavePage).toHaveBeenCalled(); | ||
expect(mock_modal_manager.hideModal).toHaveBeenCalled(); | ||
expect(mock_store_values.buy_sell_store.setShowFilterPaymentMethods).toHaveBeenCalledWith(false); | ||
}); | ||
|
||
it('should call onCancel and close the modal on click of Cancel button', () => { | ||
const props = { | ||
onCancel: jest.fn(), | ||
}; | ||
|
||
render(<LeavePageModal {...props} />); | ||
|
||
const cancel_button = screen.getByRole('button', { name: 'Cancel' }); | ||
userEvent.click(cancel_button); | ||
expect(props.onCancel).toHaveBeenCalled(); | ||
expect(mock_modal_manager.hideModal).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters