Skip to content

Commit

Permalink
feat: Adding tests to the Results.jsx
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiorodriguezgarcia committed Mar 11, 2024
1 parent e91dc9f commit da1c267
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions webapp/src/tests/Results.test.js
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');
});
});
});

0 comments on commit da1c267

Please sign in to comment.