Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question component basic test #107

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
});
});