Skip to content

Commit

Permalink
Game tests include checking if the question categories render when cl…
Browse files Browse the repository at this point in the history
…icked and

the case in which user is not authenticated
  • Loading branch information
ChristianFN2 committed Apr 25, 2024
1 parent db8ff22 commit c11760e
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions webapp/src/components/Game.test.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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);
Expand All @@ -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<gameOptions.length;i++){
render(<Game />);
//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(<Game />);
await waitFor(()=>{
expect(screen.queryByText("Let's Play!")).not.toBeInTheDocument();
})
});
});

0 comments on commit c11760e

Please sign in to comment.