Skip to content

Commit

Permalink
Merge pull request #146 from Arquisoft/Ruben
Browse files Browse the repository at this point in the history
Nuevos tests index y Ranking y solucionados errores menores
  • Loading branch information
UO289337 authored Apr 26, 2024
2 parents 828fb6e + 5014a48 commit 306c6e3
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 31 deletions.
26 changes: 2 additions & 24 deletions questiongenerator/text_questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,7 @@ queries["es"]= {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],es". }
}
LIMIT 100
`, "¿Que lenguaje de programación fue creado por "],
// pregunta = empresa tecnologica, opcion = pais de origen
[
`
SELECT ?question ?questionLabel ?option ?optionLabel
WHERE {
?question wdt:P31 wd:Q4830453.
?question wdt:P17 ?option.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],es". }
}
LIMIT 100
`, "¿De que pais procede "]
`, "¿Que lenguaje de programación fue creado por "]
]
};

Expand Down Expand Up @@ -258,18 +247,7 @@ queries["en"] = {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
LIMIT 100
`, "What programming language was created by "],
// pregunta = empresa tecnologica, opcion = pais de origen
[
`
SELECT ?question ?questionLabel ?option ?optionLabel
WHERE {
?question wdt:P31 wd:Q4830453.
?question wdt:P17 ?option.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
LIMIT 100
`, "What country does the following company come from"]
`, "What programming language was created by "]
]
}

Expand Down
9 changes: 9 additions & 0 deletions webapp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@testing-library/react": "^14.1.2",
"@testing-library/user-event": "^14.5.2",
"axios": "^1.6.5",
"history": "^5.3.0",
"i18n": "^0.15.1",
"i18next": "^23.11.2",
"i18next-browser-languagedetector": "^7.2.1",
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const theme = createTheme({

function App() {

const [t, i18n] = useTranslation("global");
const [t] = useTranslation("global");

const [showLogin, setShowLogin] = useState(true);

Expand Down
4 changes: 2 additions & 2 deletions webapp/src/components/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const apiEndpoint = process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000

const Game = () => {

const {t,i18n} = useTranslation("global");
const [t, i18n] = useTranslation("global");

const { usernameGlobal } = useUser();
const [question, setQuestion] = useState('');
Expand Down Expand Up @@ -58,7 +58,7 @@ const Game = () => {
console.log("Error: " + error.response.data.error);
setError(error.response.data.error);
}
}, [usernameGlobal, MAX_TIME, THEMATIC]);
}, [usernameGlobal, MAX_TIME, THEMATIC, i18n.language]);

const saveGameHistory = useCallback(async () => {
try {
Expand Down
7 changes: 3 additions & 4 deletions webapp/src/components/Ranking.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
TableRow,
Snackbar
} from '@mui/material';
import { useUser } from './UserContext';
import '../App.css';
import { useTranslation } from 'react-i18next';

Expand Down Expand Up @@ -45,7 +44,9 @@ const Ranking = () => {
const getRankingGlobal = useCallback(async () => {
try {
const response = await axios.get(`${apiEndpoint}/ranking`, {params: {sortBy, userLimit}});
console.log(response.data);
setRankingTable(response.data);
console.log(rankingTable);
} catch (error) {
setError(error.response.data.error);
}
Expand Down Expand Up @@ -138,7 +139,6 @@ const Ranking = () => {
onChange={handleSortByChange}
labelId="sortBy-label"
label="Ordenar por"
sx
>
<MenuItem value="ratio">Ratio</MenuItem>
<MenuItem value="totalRightQuestions">Aciertos</MenuItem>
Expand All @@ -157,7 +157,6 @@ const Ranking = () => {
type="number"
step="1"
label="Número de usuarios"
sx
inputProps={{
inputMode: 'numeric',
min: 1,
Expand Down Expand Up @@ -191,7 +190,7 @@ const Ranking = () => {
</TableRow>
</TableHead>
<TableBody>
{rankingTable.map((user, index) => (
{Array.isArray(rankingTable) && rankingTable.map((user, index) => (
<TableRow key={index}>
<TableCell align="center">{index + 1}</TableCell>
<TableCell align="center">{user.userId}</TableCell>
Expand Down
61 changes: 61 additions & 0 deletions webapp/src/components/Ranking.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from 'react';
import { render, waitFor, act } from '@testing-library/react';
import Ranking from './Ranking';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { BrowserRouter as Router } from 'react-router-dom';
import { UserProvider } from './UserContext';
import { I18nextProvider } from "react-i18next";
import i18n from "../translations/i18n";

const mock = new MockAdapter(axios);

describe('Ranking test', () => {
it('renders correctly', async () => {
mock.onGet('http://localhost:8000/topUsers').reply(200, {
topUsers: [
{ userId: 'usuario1', ratio: 80 },
{ userId: 'usuario2', ratio: 75 },
{ userId: 'usuario3', ratio: 70 },
],
});

mock.onGet('http://localhost:8000/ranking').reply(200,
[
{ userId: 'usuario1', totalGamesPlayed: 10, totalQuestionsAnswered: 50, totalRightQuestions: 40, ratio: '80%', totalTime: '100s' },
{ userId: 'usuario2', totalGamesPlayed: 12, totalQuestionsAnswered: 60, totalRightQuestions: 45, ratio: '75%', totalTime: '120s' },
]);

const { getByText, getByLabelText } = render(
<I18nextProvider i18n={i18n}>
<UserProvider>
<Router>
<Ranking />
</Router>
</UserProvider>
</I18nextProvider>);

await waitFor(() => {
expect(getByLabelText('Número de usuarios')).toBeInTheDocument();
expect(getByText('Posición')).toBeInTheDocument();
});
});

it('can handle errors', async () => {
mock.onGet('http://localhost:8000/ranking').reply(500, {error: "Error al obtener el ranking"});

const { getByText } = render(
<I18nextProvider i18n={i18n}>
<UserProvider>
<Router>
<Ranking />
</Router>
</UserProvider>
</I18nextProvider>);

await waitFor(() => {
let error = getByText("Error: Error al obtener el ranking");
expect(error).toBeInTheDocument();
});
});
});
54 changes: 54 additions & 0 deletions webapp/src/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React from 'react';
import { render } from '@testing-library/react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import { I18nextProvider } from 'react-i18next';
import { UserProvider } from './components/UserContext';
import i18next from './translations/i18n';
import NavigationBar from './components/fragments/NavigationBar';
import Footer from './components/fragments/Footer';
import PantallaInicio from './components/PantallaInicio';
import PantallaInicioAdmin from './components/PantallaInicioAdmin';
import Login from './components/Login';
import AddUser from './components/AddUser';
import App from './App';
import Game from './components/Game';
import EndGame from './components/EndGame';
import Gamehistory from './components/Gamehistory';
import Perfil from './components/Perfil';
import AllUsers from './components/AllUsers';
import AllQuestions from './components/AllQuestions';
import Ranking from './components/Ranking';
import GameConfiguration from './components/GameConfiguration';

describe('Root component', () => {
it('renders all routes correctly', () => {
render(
<Router>
<React.StrictMode>
<I18nextProvider i18n={i18next}>
<UserProvider>
<NavigationBar />
<Routes>
<Route path="/" element={<App />} />
<Route path="/PantallaInicio" element={<PantallaInicio />} />
<Route path="/PantallaInicioAdmin" element={<PantallaInicioAdmin />} />
<Route path="/Login" element={<Login />} />
<Route path="/AddUser" element={<AddUser />} />
<Route path="/App" element={<App />} />
<Route path="/Game" element={<Game />} />
<Route path="/EndGame" element={<EndGame />} />
<Route path="/Gamehistory" element={<Gamehistory />} />
<Route path="/Perfil" element={<Perfil />} />
<Route path="/AllUsers" element={<AllUsers />} />
<Route path="/AllQuestions" element={<AllQuestions />} />
<Route path="/Ranking" element={<Ranking />} />
<Route path="/GameConfiguration" element={<GameConfiguration />} />
</Routes>
<Footer />
</UserProvider>
</I18nextProvider>
</React.StrictMode>
</Router>
);
});
});

0 comments on commit 306c6e3

Please sign in to comment.