Skip to content

Commit

Permalink
Added basic test for Question.jsx
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianFN2 committed Apr 7, 2024
1 parent ab327ad commit 2241bbd
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions webapp/src/components/Question.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react';
import { render, fireEvent, screen, waitFor, act } from '@testing-library/react';
import Question from './Question';
import useAuthUser from 'react-auth-kit/hooks/useAuthUser';
import MockAdapter from 'axios-mock-adapter';
import axios from 'axios';
const mockAxios = new MockAdapter(axios);

const mock = jest.fn();

jest.mock('react-auth-kit/hooks/useAuthUser');
jest.mock('react-router-dom', () => ({
useNavigate: () => mock,
}));

describe('Question page', () => {
beforeEach(() => {
mockAxios.reset();
});

it('should render a flags question if category is flags', async () => {
useAuthUser.mockReturnValue({ username: 'testUser' });

mockAxios.onGet('http://localhost:8000/imgs/flags/question').reply(200,
{
question: "Which of the following flags belongs to Spain?",
images:["SpainImage","EnglandImage","PolandImage","GermanyImage"]
});

render(<Question type="imgs" category="flags"/>);

await waitFor(() => {
expect(screen.getByText(/Which of the following flags belongs to/i)).toBeInTheDocument();
expect(screen.getByText(/Score/i)).toBeInTheDocument();
});

let imgs = []
imgs = screen.getAllByRole("button")
expect(imgs.length).toBe(4)
});
});

0 comments on commit 2241bbd

Please sign in to comment.