generated from Arquisoft/wiq_0
-
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.
feat: Adding tests to the Results.jsx
- Loading branch information
1 parent
e91dc9f
commit da1c267
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
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,46 @@ | ||
import React from 'react'; | ||
import { render, fireEvent, waitFor, screen } from '@testing-library/react'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import { BrowserRouter, MemoryRouter } from 'react-router-dom'; | ||
import Results from '../pages/Results'; | ||
|
||
jest.mock('react-router-dom', () => ({ | ||
...jest.requireActual('react-router-dom'), | ||
useLocation: () => ({ | ||
state: { correctAnswers: 3 }, | ||
}), | ||
})); | ||
|
||
describe('Results Component', () => { | ||
test('renders results with correct answers', () => { | ||
const { getByText, getByTestId } = render( | ||
<BrowserRouter> | ||
<Results /> | ||
</BrowserRouter> | ||
); | ||
|
||
expect(getByText('Results')).toBeInTheDocument(); | ||
expect(getByText('Correct answers: 3')).toBeInTheDocument(); | ||
expect(getByTestId('GoBack')).toBeInTheDocument(); | ||
expect(getByTestId('GoBack')).toHaveTextContent('common.finish'); | ||
}); | ||
|
||
it('navigates to dashboard on button click', async () => { | ||
const navigateMock = jest.fn(); | ||
jest.spyOn(require('react-router-dom'), 'useNavigate').mockReturnValue(navigateMock); | ||
|
||
const { getByTestId } = render( | ||
<BrowserRouter> | ||
<Results /> | ||
</BrowserRouter> | ||
); | ||
|
||
const goBackButton = getByTestId('GoBack'); | ||
fireEvent.click(goBackButton); | ||
|
||
await waitFor(() => { | ||
expect(navigateMock).toHaveBeenCalled(); | ||
expect(navigateMock).toHaveBeenCalledWith('/dashboard'); | ||
}); | ||
}); | ||
}); |