Skip to content

Commit

Permalink
fix(testing): unit test v22
Browse files Browse the repository at this point in the history
  • Loading branch information
aliraza556 committed Jan 17, 2024
1 parent dfedd45 commit c6e630e
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions frontend/app/src/pages/tickets/tickets/TicketModalPage.spec.tsx
Original file line number Diff line number Diff line change
@@ -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(
<BrowserRouter>
<TicketModalPage setConnectPerson={() => {}} />
</BrowserRouter>
);
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...
});

0 comments on commit c6e630e

Please sign in to comment.