From c6e630eaf6c24ac0a77beecf0fe48929c19b3512 Mon Sep 17 00:00:00 2001 From: aliraza556 Date: Wed, 17 Jan 2024 18:45:28 +0500 Subject: [PATCH] fix(testing): unit test v22 --- .../tickets/tickets/TicketModalPage.spec.tsx | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 frontend/app/src/pages/tickets/tickets/TicketModalPage.spec.tsx diff --git a/frontend/app/src/pages/tickets/tickets/TicketModalPage.spec.tsx b/frontend/app/src/pages/tickets/tickets/TicketModalPage.spec.tsx new file mode 100644 index 000000000..99b76da6c --- /dev/null +++ b/frontend/app/src/pages/tickets/tickets/TicketModalPage.spec.tsx @@ -0,0 +1,58 @@ +import React from 'react'; +import { render, screen, fireEvent, waitFor } from '@testing-library/react'; +import '@testing-library/jest-dom'; +import { TicketModalPage } from '../TicketModalPage'; // Adjust the import path as needed +import { BrowserRouter } from 'react-router-dom'; + +// Mocks +jest.mock('react-router-dom', () => ({ + ...jest.requireActual('react-router-dom'), + useHistory: jest.fn(), + useLocation: jest.fn(), + useParams: jest.fn() +})); + +const mockedHistoryPush = jest.fn(); +const mockedUseHistory = require('react-router-dom').useHistory; +const mockedUseLocation = require('react-router-dom').useLocation; +const mockedUseParams = require('react-router-dom').useParams; + +describe('TicketModalPage', () => { + beforeEach(() => { + mockedUseHistory.mockReturnValue({ push: mockedHistoryPush }); + mockedUseLocation.mockReturnValue({ + pathname: '/bounty/1203', + search: '' + }); + mockedUseParams.mockReturnValue({ uuid: 'ck1p7l6a5fdlqdgmmnpg', bountyId: '1203' }); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it('redirects to home page on direct access to bounty page', async () => { + // Arrange: Set up conditions for a direct access + Object.defineProperty(window, 'referrer', { + configurable: true, + value: '' + }); + + // Act: Render the component and simulate closing the modal + render( + + {}} /> + + ); + const goBackButton = await waitFor(() => screen.findByTestId('testid-modal'), { + timeout: 10000 + }); + + fireEvent.click(goBackButton); + + // Assert: Expect to be redirected to the home page + expect(mockedHistoryPush).toHaveBeenCalledWith('/bounties'); + }); + + // Other tests for different scenarios... +});