Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Arquisoft/wiq_en2a
Browse files Browse the repository at this point in the history
  • Loading branch information
pelazas committed Apr 30, 2024
2 parents 2690a50 + 1211a81 commit 4714353
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 10 deletions.
4 changes: 2 additions & 2 deletions gatewayservice/monitoring/prometheus/prometheus.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
global:
scrape_interval: 5s
scrape_configs:
- job_name: "example-nodejs-app"
- job_name: "conoceryvencer-nodejs-app"
static_configs:
- targets: ["gatewayservice:8000"]
- targets: ["gatewayservice:8000","userservice:8001","queryservice:8002","gatewayservice:8003","gameservice:8004"]
6 changes: 3 additions & 3 deletions webapp/e2e/singlePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ function delay(time) {
// Esperar a que aparezca el botón de inicio de sesión y hacer clic en él
await page.waitForSelector('button.MuiButtonBase-root:nth-child(1)');
await page.click('button.MuiButtonBase-root:nth-child(1)');

// Esperar a que aparezca el botón de juego individual y hacer clic en él
await page.waitForSelector('button.game-page-button:nth-child(1)');
await page.click('button.game-page-button:nth-child(1)');
await page.waitForSelector('a.game-page-button:nth-child(1)');
await page.click('a.game-page-button:nth-child(1)');

// Esperar a que aparezca el botón "add bot" y hacer clic en él
await page.waitForSelector('.add-bot-button');
Expand Down
73 changes: 73 additions & 0 deletions webapp/src/components/game/PlayingGame.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react';
import PlayingGame from './PlayingGame';

jest.useFakeTimers();

describe('PlayingGame component', () => {
it('should handle answering questions and end game correctly', async () => {
const questions = [
{ uuid: '1',
question: 'What is the capital of France?',
correctAnswer: '1',
incorrectAnswer1: '2',
incorrectAnswer2: '3',
incorrectAnswer3: '4', },
{ uuid: '2',
question: 'What is the capital of Italy?',
correctAnswer: '1',
incorrectAnswer1: '2',
incorrectAnswer2: '3',
incorrectAnswer3: '4', },
{ uuid: '2',
question: 'What is the capital of Spain?',
correctAnswer: '1',
incorrectAnswer1: '2',
incorrectAnswer2: '3',
incorrectAnswer3: '4', },
];

const setCurrentStageMock = jest.fn();
const { getByTestId, queryByTestId } = render(
<PlayingGame
questions={questions}
setCurrentStage={setCurrentStageMock}
/>
);

// Ensure initial rendering
expect(getByTestId('question-container')).toBeInTheDocument();
expect(getByTestId('question-title')).toBeInTheDocument();
expect(getByTestId('question')).toBeInTheDocument();
expect(getByTestId('seconds')).toBeInTheDocument();

// Simulate answering question 1
fireEvent.click(getByTestId('answer-1'));
jest.advanceTimersByTime(10000);
await waitFor(() => {
expect(getByTestId('question-title')).toBeInTheDocument();
expect(getByTestId('question')).toBeInTheDocument();
expect(getByTestId('seconds')).toHaveTextContent('10');

});

// Simulate answering question 2
fireEvent.click(getByTestId('answer-2'));
jest.advanceTimersByTime(10000);
await waitFor(() => {
expect(getByTestId('question')).toBeInTheDocument();
expect(getByTestId('seconds')).toHaveTextContent('10');
});

// Simulate answering question 3
fireEvent.click(getByTestId('answer-3'));
jest.advanceTimersByTime(10000);
await waitFor(() => {
expect(queryByTestId('question-container')).toBeNull();
expect(getByTestId('result')).toBeInTheDocument();
expect(getByTestId('points')).toHaveTextContent('playing_single_player_you_earned50playing_single_player_points');
});
});

// Add more test cases for edge cases and multiplayer scenarios if applicable
});
41 changes: 39 additions & 2 deletions webapp/src/components/group/GroupCreationModal.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { render, fireEvent } from '@testing-library/react';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import userEvent from "@testing-library/user-event";
import { CreationModal } from './GroupCreationModal';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';

jest.mock('axios');
const mockAxios = new MockAdapter(axios);

describe('CreationModal component', () => {
test('renders the modal properly', async () => {
Expand Down Expand Up @@ -29,4 +32,38 @@ describe('CreationModal component', () => {

});

it('should render input fields and handle changes correctly', async () => {
const nowHasGroupMock = jest.fn();
const setErrorMock = jest.fn();
const toggleCreateModalMock = jest.fn();

render(
<CreationModal
nowHasGroup={nowHasGroupMock}
setError={setErrorMock}
toggleCreateModal={toggleCreateModalMock}
/>
);

const groupName = screen.getByTestId('group-name-input');
const description = screen.getByTestId('description-input');

// Simulate changing input values
await userEvent.type(groupName, "a")
await userEvent.type(description, "a")
fireEvent.click(screen.getByTestId('no-button'));
fireEvent.change(screen.getByTestId('max-members-input'), { target: { value: '5' } });

// Ensure input values are updated correctly
expect(screen.getByTestId('max-members-input')).toHaveValue(5);

mockAxios.onPost('http://localhost:8000/createGroup').reply(200, {group: "1"});


fireEvent.click(screen.getByTestId('create-button'));
await waitFor(() => {
expect(nowHasGroupMock).toHaveBeenCalled();
});
});

});
13 changes: 11 additions & 2 deletions webapp/src/components/group/GroupCreationModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ export const CreationModal: FC<ActionProps> = ({ nowHasGroup, setError, toggleCr
nowHasGroup();
});
} catch (error: any) {
setError(error.response?.data.error);
if (error.response && error.response.data && error.response.data.error) {
setError(error.response.data.error);
} else {
// Handle other types of errors
console.error('An error occurred:', error);
}
}
};

Expand Down Expand Up @@ -81,7 +86,11 @@ export const CreationModal: FC<ActionProps> = ({ nowHasGroup, setError, toggleCr
<Grid item xs={6} ><p>{t('create_group_max_members')}</p></Grid>
<Grid item xs={1} ><input
data-testid="max-members-input"
style={{ width: '37px' }} type="number" step={1} value={maxMembers} onChange={handleChange} max={200} min={2} /></Grid>
style={{ width: '37px' }}
type="number"
step={1}
value={maxMembers}
onChange={handleChange} max={200} min={2} /></Grid>
</Grid>
<Grid container padding={2} sx={{ display: 'flex', width: '400px', justifyContent: 'space-evenly', alignItems: 'center' }}>
<Grid item xs={5} ><p>{t('create_group_description')}</p></Grid>
Expand Down
46 changes: 46 additions & 0 deletions webapp/src/components/register/Register.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ describe('Register component', () => {
});

it('should add user successfully', async () => {
var localStorageMock = (function() {
var store = {};
return {
getItem: function(key) {
return store[key];
},
setItem: function(key, value) {
store[key] = value.toString();
},
clear: function() {
store = {};
},
removeItem: function(key) {
delete store[key];
}
};
})();
Object.defineProperty(window, 'localStorage', { value: localStorageMock });
render(
<Router>
<Register />
Expand All @@ -26,6 +44,12 @@ describe('Register component', () => {

// Mock the axios.post request to simulate a successful response
mockAxios.onPost('http://localhost:8000/adduser').reply(200);
mockAxios.onPost('http://localhost:8000/login').reply(200, {
username: 'testUser',
totalScore: 0,
nWins: 0,
uuid: '123'
});

// Simulate user input
fireEvent.change(usernameInput, { target: { value: 'testUser' } });
Expand All @@ -38,6 +62,14 @@ describe('Register component', () => {
await waitFor(() => {
expect(screen.getByTestId('register-successfull-snackbar')).toBeInTheDocument();
});

await waitFor(() => {
expect(localStorageMock.getItem('username')).toBe('testUser');
expect(localStorageMock.getItem('score')).toBe('0');
expect(localStorageMock.getItem('isAuthenticated')).toBe('true');
expect(localStorageMock.getItem('userUUID')).toBe('123');

});
});

it('should handle error when adding user', async () => {
Expand Down Expand Up @@ -66,4 +98,18 @@ describe('Register component', () => {
expect(screen.getByTestId('register-error-snackbar')).toBeInTheDocument();
});
});

it('should handle return button click', () => {
const goBackMock = jest.fn();
const { getByTestId } = render(
<Router>
<Register goBack={goBackMock}/>
</Router>);

// Click on the return button
fireEvent.click(getByTestId("return-button"));

expect(goBackMock).toHaveBeenCalled();
});

});
2 changes: 1 addition & 1 deletion webapp/src/components/register/Register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const Register = (props:ActionProps) => {
<Button variant='contained' color="primary" onClick={addUser}>
{t('register')}
</Button>
<Button variant='contained' color="primary" onClick={handleReturnButtonClick}>
<Button data-testid="return-button" variant='contained' color="primary" onClick={handleReturnButtonClick}>
{t('return')}
</Button>
</Stack>
Expand Down

0 comments on commit 4714353

Please sign in to comment.