|
| 1 | +import React from 'react'; |
| 2 | +import { MY_ADS_URL } from '@/constants'; |
| 3 | +import { useQueryString } from '@/hooks'; |
| 4 | +import { render, screen } from '@testing-library/react'; |
| 5 | +import userEvent from '@testing-library/user-event'; |
| 6 | +import AdCancelCreateEditModal from '../AdCancelCreateEditModal'; |
| 7 | + |
| 8 | +jest.mock('@deriv-com/ui', () => ({ |
| 9 | + ...jest.requireActual('@deriv-com/ui'), |
| 10 | + useDevice: () => ({ isMobile: false }), |
| 11 | +})); |
| 12 | + |
| 13 | +jest.mock('@/hooks', () => ({ |
| 14 | + ...jest.requireActual('@/hooks'), |
| 15 | + useQueryString: jest.fn().mockReturnValue({ queryString: { advertId: '' } }), |
| 16 | +})); |
| 17 | + |
| 18 | +const mockUseQueryString = useQueryString as jest.Mock; |
| 19 | + |
| 20 | +const mockFn = jest.fn(); |
| 21 | +jest.mock('react-router-dom', () => ({ |
| 22 | + ...jest.requireActual('react-router-dom'), |
| 23 | + useHistory: () => ({ push: mockFn }), |
| 24 | +})); |
| 25 | + |
| 26 | +const mockProps = { |
| 27 | + isModalOpen: true, |
| 28 | + onRequestClose: jest.fn(), |
| 29 | +}; |
| 30 | + |
| 31 | +describe('AdCancelCreateEditModal', () => { |
| 32 | + it('should render the component as expected', () => { |
| 33 | + render(<AdCancelCreateEditModal {...mockProps} />); |
| 34 | + expect(screen.getByText('Cancel ad creation?')).toBeInTheDocument(); |
| 35 | + }); |
| 36 | + it('should render the component as expected when isEdit is true', () => { |
| 37 | + mockUseQueryString.mockReturnValueOnce({ queryString: { advertId: '123' } }); |
| 38 | + render(<AdCancelCreateEditModal {...mockProps} />); |
| 39 | + expect(screen.getByText('Cancel your edits?')).toBeInTheDocument(); |
| 40 | + }); |
| 41 | + it('should redirect to my ads page on clicking cancel button', () => { |
| 42 | + render(<AdCancelCreateEditModal {...mockProps} />); |
| 43 | + const button = screen.getByRole('button', { name: 'Cancel' }); |
| 44 | + userEvent.click(button); |
| 45 | + expect(mockFn).toHaveBeenCalledWith(MY_ADS_URL); |
| 46 | + }); |
| 47 | + it("should close the modal on clicking don't cancel button", () => { |
| 48 | + render(<AdCancelCreateEditModal {...mockProps} />); |
| 49 | + const button = screen.getByRole('button', { name: 'Don’t cancel' }); |
| 50 | + userEvent.click(button); |
| 51 | + expect(mockProps.onRequestClose).toHaveBeenCalledTimes(1); |
| 52 | + }); |
| 53 | +}); |
0 commit comments