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 tests extended #119

Merged
merged 1 commit into from
Apr 21, 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
115 changes: 96 additions & 19 deletions webapp/src/components/Question.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, fireEvent, screen, waitFor, act } from '@testing-library/react';
import { render, fireEvent, screen, waitFor, act, getByRole, getAllByRole } from '@testing-library/react';
import Question from './Question';
import useAuthUser from 'react-auth-kit/hooks/useAuthUser';
import MockAdapter from 'axios-mock-adapter';
Expand All @@ -14,33 +14,35 @@ jest.mock('react-router-dom', () => ({
}));

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

it('should render a flags question if category is flags', async () => {
it('should render a question of flag images if type is image and 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"]
images:["https://commons.wikimedia.org/wiki/File:Flag_of_Spain.svg"
,"https://commons.wikimedia.org/wiki/File:Flag_of_England.svg"
,"https://commons.wikimedia.org/wiki/File:Flag_of_Poland.svg"
,"https://commons.wikimedia.org/wiki/File:Flag_of_Germany.svg"]
});

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();
expect(screen.getByText(/Which of the following flags belongs to/i)).toBeInTheDocument();
expect(screen.getByText(/Score/i)).toBeInTheDocument();
expect(screen.getByText(/Score/i).textContent).toBe("Score: 0")
let imgs = []
imgs = screen.getAllByRole("button")
expect(imgs.length).toBe(4)
});

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


it('should render a flags question if category is foods', async () => {
it('should render a question of food images if type is image and category is foods', async () => {
useAuthUser.mockReturnValue({ username: 'testUser' });

mockAxios.onGet('http://localhost:8000/imgs/foods/question').reply(200,
Expand All @@ -52,13 +54,88 @@ describe('Question page', () => {
render(<Question type="imgs" category="foods"/>);

await waitFor(() => {
expect(screen.getByText(/Which of the following images corresponds to/i)).toBeInTheDocument();
expect(screen.getByText(/Score/i)).toBeInTheDocument();
expect(screen.getByText(/Which of the following images corresponds to/i)).toBeInTheDocument();
expect(screen.getByText(/Score/i)).toBeInTheDocument();
expect(screen.getByText(/Score/i).textContent).toBe("Score: 0")
let imgs = []
imgs = screen.getAllByRole("button")
expect(imgs.length).toBe(4)
});
});

it('should handle a fetching question error', async () => {
useAuthUser.mockReturnValue({ username: 'testUser' });

mockAxios.onGet('http://localhost:8000/imgs/foods/question').networkError();

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

it('should update the score if the answer is correct', 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:["https://commons.wikimedia.org/wiki/File:Flag_of_Spain.svg"
,"https://commons.wikimedia.org/wiki/File:Flag_of_England.svg"
,"https://commons.wikimedia.org/wiki/File:Flag_of_Poland.svg"
,"https://commons.wikimedia.org/wiki/File:Flag_of_Germany.svg"]
});

mockAxios.onPost('http://localhost:8000/imgs/answer').reply(200,
{
correct: "true"
});

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

await waitFor(() => {
expect(screen.getByText(/Score/i).textContent).toBe("Score: 0")
expect(screen.getByText(/Which of the following/i)).toBeInTheDocument();
});

await act(async () => {
fireEvent.click(screen.getAllByRole("img")[0]);
});

await waitFor(() => {
expect(screen.getByText(/Score/i).textContent).toBe("Score: 1")
})
});

it('should not update the score if the answer is incorrect', 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:["https://commons.wikimedia.org/wiki/File:Flag_of_Spain.svg"
,"https://commons.wikimedia.org/wiki/File:Flag_of_England.svg"
,"https://commons.wikimedia.org/wiki/File:Flag_of_Poland.svg"
,"https://commons.wikimedia.org/wiki/File:Flag_of_Germany.svg"]
});

mockAxios.onPost('http://localhost:8000/imgs/answer').reply(200,
{
correct: "false",
associate: "Poland"
});

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

await waitFor(() => {
expect(screen.getByText(/Score/i).textContent).toBe("Score: 0")
expect(screen.getByText(/Which of the following/i)).toBeInTheDocument();
});

let imgs = []
imgs = screen.getAllByRole("button")
expect(imgs.length).toBe(4)
await act(async () => {
fireEvent.click(screen.getAllByRole("img")[2]);
});

await waitFor(() => {
expect(screen.getByText(/Score/i).textContent).toBe("Score: 0")
})
});
});

Expand Down