From c11760e73ace04b5494751ab55e69ff4411dc480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fern=C3=A1ndez=20Noriega?= Date: Thu, 25 Apr 2024 18:03:43 +0200 Subject: [PATCH] Game tests include checking if the question categories render when clicked and the case in which user is not authenticated --- webapp/src/components/Game.test.js | 45 ++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/webapp/src/components/Game.test.js b/webapp/src/components/Game.test.js index 1255a21..4b98fe2 100644 --- a/webapp/src/components/Game.test.js +++ b/webapp/src/components/Game.test.js @@ -1,9 +1,12 @@ import React from 'react'; -import { render, screen } from '@testing-library/react'; +import { fireEvent, act, render, screen, waitFor } from '@testing-library/react'; import Game from './Game'; import { jest } from '@jest/globals'; import useIsAuthenticated from 'react-auth-kit/hooks/useIsAuthenticated'; import useAuthUser from 'react-auth-kit/hooks/useAuthUser'; +import axios from 'axios'; +import MockAdapter from 'axios-mock-adapter'; +const mockAxios = new MockAdapter(axios); const mock = jest.fn(); @@ -13,6 +16,14 @@ jest.mock('react-router-dom', () => ({ useNavigate: () => mock, })); +mockAxios.onGet(/\/imgs\/([^\/]+)\/question/).reply(config => { + const category = config.url.match(/\/imgs\/([^\/]+)\/question/)[1]; + return [200, { + question: `${category} question`, + images: ["img1", "img2", "img3", "img4"] + }]; +}); + describe('Game page', () => { it('should render play message for authenticated user', async () => { useIsAuthenticated.mockReturnValue(() => true); @@ -31,5 +42,35 @@ describe('Game page', () => { throw new Error('Unauthenticated user was able to see Game page'); } catch (err) {} }); - + + it('should render the corresponding question depending on the option clicked', async () => { + useIsAuthenticated.mockReturnValue(() => true); + useAuthUser.mockReturnValue({ username: 'testUser' }); + + let gameOptions = ["Flag","City","Monument","Tourist attraction","Food"] + let gameCategories = ["flags","cities","monuments","tourist_attractions","foods"] + + for(let i=0;i); + //Click game option + await act(async ()=>{ + fireEvent.click(screen.getByText(gameOptions[i])) + }) + + //Should have rendered question category + await waitFor(()=>{ + expect(screen.getByText(gameCategories[i]+" question")).toBeInTheDocument() + }) + } + }); + + it('should navigate out of the game if the user is not authenticated', async () => { + useIsAuthenticated.mockReturnValue(() => false); + useAuthUser.mockReturnValue({ username: 'testUser' }); + + render(); + await waitFor(()=>{ + expect(screen.queryByText("Let's Play!")).not.toBeInTheDocument(); + }) + }); }); \ No newline at end of file