Skip to content

Commit

Permalink
Added tests to pass sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
uo289029 committed Apr 7, 2024
1 parent b96d2b5 commit 4266414
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
1 change: 0 additions & 1 deletion questionsgenerator/questions-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const bodyParser = require('body-parser');
const QuestionGenerator = require('./questionGenerator.js');
const Question = require('./question-model')
const mongoURI = process.env.MONGODB_URI;
//|| 'mongodb+srv://wiq_es01b_admin:[email protected]/wiq?retryWrites=true&w=majority&appName=WIQ';

const app = express();
app.disable('x-powered-by');
Expand Down
17 changes: 16 additions & 1 deletion webapp/src/components/GameFinale.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { BrowserRouter as Router } from 'react-router-dom';
import { render, screen, waitFor } from '@testing-library/react';
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import GameFinale from './GameFinale';
Expand Down Expand Up @@ -39,4 +39,19 @@ describe('GameFinale component', () => {
expect(buttons.length).toBe(1);
});

it('should render button succesfully', async () => {

mockAxios.onPost('http://localhost:8000/saveGameRecord').reply(200);

render(
<Router>
<GameFinale />
</Router>);

const saveRecordButton = screen.getByTestId('saveRecordButton');
expect(saveRecordButton).toBeInTheDocument();
fireEvent.click(saveRecordButton);
expect(await screen.findByText('Record saved successfully!')).toBeInTheDocument();
});

});
38 changes: 38 additions & 0 deletions webapp/src/components/Home.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,43 @@ describe('Home component', () => {
expect(history.location.pathname).toBe('/');

});

it('should render all buttons successfully', async () => {
const history = createMemoryHistory();
render(
<Router history={history}>
<Home />
</Router>
);

// Make sure all the buttons are rendered
expect(screen.getByText('New Full Random Game')).toBeInTheDocument();
expect(screen.getByText('New Images Game')).toBeInTheDocument();
expect(screen.getByText('New Geography Game')).toBeInTheDocument();
expect(screen.getByText('New Science Game')).toBeInTheDocument();
});

it('should render all configuration buttons successfully', async () => {
const history = createMemoryHistory();
render(
<Router history={history}>
<Home />
</Router>
);

const configButton = screen.getByText('Configuration of the game');
fireEvent.click(configButton);

const questionsButton = screen.getByText(/Press to change nº of questions:/);
fireEvent.click(questionsButton);
expect(await screen.findByText('5')).toBeInTheDocument();
expect(await screen.findByText('10')).toBeInTheDocument();
expect(await screen.findByText('15')).toBeInTheDocument();

const timeLimitButton = screen.getByText(/Press to change the time limit:/);
fireEvent.click(timeLimitButton);
expect(await screen.findByText('20')).toBeInTheDocument();

});

});
2 changes: 1 addition & 1 deletion webapp/src/components/NavigationBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const NavigationBar = () => {
};

const showApiDoc = () => {
window.location.href = 'http://20.26.114.153:8000/api-doc/';
window.location.href = 'http://20.26.114.153:8000/api-doc/'; //NOSONAR
};

return (
Expand Down
16 changes: 16 additions & 0 deletions webapp/src/components/Record.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,20 @@ describe('Record component', () => {

});

it('should render the home button succesfully', async () => {

mockAxios.onPost('http://localhost:8000/getGameRecord').reply(200, {games: []});

render(
<Router>
<Record />
</Router>);

await waitFor(() => screen.getByText(/Here you can see your record! All about your past games and all!/i));

const homeButton = screen.getByText('Home');
expect(homeButton).toBeInTheDocument();

});

});

0 comments on commit 4266414

Please sign in to comment.