Skip to content

Commit

Permalink
Merge branch 'webapp_interface' of https://github.com/Arquisoft/wiq_es6c
Browse files Browse the repository at this point in the history
 into webapp_interface
  • Loading branch information
alegarman2002 committed Apr 28, 2024
2 parents 149ff6a + c2fc32e commit 2dfba98
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 22 deletions.
32 changes: 32 additions & 0 deletions webapp/src/components/Button.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import Button from './Button';


describe('Button component', () => {

test('renders button', () => {
const { getByText } = render(<Button text="Click me" />);

//Comprobamos que tenemos el texto
expect(getByText('Click me')).toBeInTheDocument();
});

test('clicamos el boton', () => {
const handleClick = jest.fn();

const { getByText } = render(<Button text="Click me" onClick={handleClick} />);

//intentamos clicar el botón con una tecla que no sea enter
fireEvent.keyDown(getByText('Click me'), { key: 'Space' });
expect(handleClick).not.toHaveBeenCalled();
//hacemos click normal
fireEvent.click(getByText('Click me'));
expect(handleClick).toHaveBeenCalled();
//clicamos el boton con la tecla enter
fireEvent.keyDown(getByText('Click me'), { key: 'Enter' });
expect(handleClick).toHaveBeenCalled();

});

});
33 changes: 18 additions & 15 deletions webapp/src/components/FirstGame.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { render, fireEvent, act } from '@testing-library/react';
import { render, screen, fireEvent } from '@testing-library/react';
import { ContextFun } from './Context';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import {MemoryRouter} from 'react-router-dom';
import { BrowserRouter as Router } from 'react-router-dom';
import FirstGame from './FirstGame';
import Menu from './Menu';
import GameConfiguration from './game/GameConfiguration';

const mockAxios = new MockAdapter(axios);

Expand All @@ -15,21 +16,23 @@ describe("First game component", () => {

test("one question -> 4 possible answers",async () => {
render(
<MemoryRouter>
<Menu />
</MemoryRouter>
<ContextFun>
<Router>
<GameConfiguration />
</Router>
</ContextFun>
);

const classicGame = document.querySelector('.modes > div > button');
// Simulate new Classic Game
await act(async () => {
fireEvent.click(classicGame);
});

console.log(document.querySelector('h1'))

//Marcar la primera temática
let topic1 = document.getElementById('t0');
fireEvent.click(topic1);
expect(topic1).toBeChecked();
//Iniciamos el juego
let bt = screen.getByText('Comenzar Juego');
fireEvent.click(bt);
//comprobamos que haya 2 botones para responder
const gamesBT = document.getElementsByClassName('allAnswers');
//expect(gamesBT).toHaveLength(4);
//expect(gamesBT).toHaveLength(2);
});

});
Empty file.
3 changes: 0 additions & 3 deletions webapp/src/components/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ function shuffleArray(array) {
}

function secureRandomNumber(max) {
if (!window.crypto) {
return Math.floor(Math.random() * max);
}
const randomBytes = new Uint32Array(1);
window.crypto.getRandomValues(randomBytes);
return randomBytes[0] % max;
Expand Down
48 changes: 48 additions & 0 deletions webapp/src/components/Util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { shuffleArray, secureRandomNumber } from './Util';

jest.mock('axios');

describe('shuffleArray function', () => {
// Mocking window.crypto.getRandomValues
beforeEach(() => {
const max = 100;
global.crypto = {
getRandomValues: jest.fn().mockImplementation((array) => {
for (let i = 0; i < array.length; i++) {
array[i] = i;
}
}),
};
});

afterEach(() => {
jest.clearAllMocks();
});

test('shuffles the array', () => {
const array = [1, 2, 3, 4, 5];
const shuffledArray = shuffleArray(array);
expect(shuffledArray).not.toEqual(array);
});

test('does not mutate the original array', () => {
const array = [1, 2, 3, 4, 5];
const originalArray = [...array];
shuffleArray(array);
expect(array).toEqual(originalArray);
});
});

describe('secureRandomNumber function', () => {

test('generates a secure random number', () => {
const max = 100;
const randomNumber = secureRandomNumber(max);

//comprobamos que el numero obtenido es < que el máximo
expect(randomNumber).toBeLessThan(max);
//comprobamos que el numero obtenido es >= 0
expect(randomNumber).toBeGreaterThanOrEqual(0);
});

});
8 changes: 4 additions & 4 deletions webapp/src/components/game/Calculator.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { useState, useEffect } from 'react';
import React from 'react';
import { shuffleArray, secureRandomNumber } from '../Util';
import { Container, Typography, Box, LinearProgress} from '@mui/material';
import { Container, Typography } from '@mui/material';
import { Footer } from '../footer/Footer';
import { Nav } from '../nav/Nav';
import Button from '../Button';

var questions = []
var questionIndex = -1
let questions = []
let questionIndex = -1

const Calculator = () => {

Expand Down

0 comments on commit 2dfba98

Please sign in to comment.