From 2241bbd9e7f14e752d3e785baef1d313c920ef39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fern=C3=A1ndez=20Noriega?= Date: Mon, 8 Apr 2024 01:56:02 +0200 Subject: [PATCH] Added basic test for Question.jsx --- webapp/src/components/Question.test.js | 41 ++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 webapp/src/components/Question.test.js diff --git a/webapp/src/components/Question.test.js b/webapp/src/components/Question.test.js new file mode 100644 index 0000000..dd59304 --- /dev/null +++ b/webapp/src/components/Question.test.js @@ -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(); + + 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) + }); +}); \ No newline at end of file