Skip to content

Commit

Permalink
Merge pull request #91 from Arquisoft/stats-calculadora
Browse files Browse the repository at this point in the history
Tests de las vistas de stats, ranking y perfil para calculadora
  • Loading branch information
iyanfdezz authored Apr 5, 2024
2 parents 824d94d + e85dab2 commit ef4458d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
28 changes: 28 additions & 0 deletions webapp/src/pages/Perfil/Perfil.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,34 @@ describe('Perfil Component', () => {
expect(screen.getByText('No hay partidas recientes.')).toBeInTheDocument();
});
});

test('displays recent calculator game data', async () => {
const mockUserData = {
username: 'testUser',
createdAt: new Date(),
games: [
{ gamemode: 'calculadora', points: 15, avgTime: 8.5 }
]
};

jest.spyOn(global, 'fetch').mockResolvedValueOnce({
json: jest.fn().mockResolvedValueOnce(mockUserData),
});

render(
<MemoryRouter>
<Perfil />
</MemoryRouter>
);

await waitFor(() => {
expect(screen.getByText('calculadora')).toBeInTheDocument();
const dashCells = screen.getAllByText('-');
expect(dashCells.length).toBe(2);
expect(screen.getByText('15')).toBeInTheDocument();
expect(screen.getByText('8.50 segundos')).toBeInTheDocument();
});
});


});
16 changes: 16 additions & 0 deletions webapp/src/pages/Ranking/Ranking.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,21 @@ describe('Ranking component', () => {
expect(screen.getByText('Ranking - modo Batería de sabios')).toBeInTheDocument();
});
});

test('changes gamemode when clicking on mode buttons, clicks on Human Calculator button', async () => {
global.fetch.mockResolvedValueOnce({ json: () => Promise.resolve(mockData) });

renderComponent();

await assertRankingTableWithData();

const calculatorButton = screen.getByRole('button', { name: 'Calculadora humana' });
fireEvent.click(calculatorButton);

await waitFor(() => {
expect(screen.getByText('Ranking - modo Calculadora humana')).toBeInTheDocument();
expect(screen.queryByText('Ratio de aciertos (%)')).toBeNull(); // Asegura que no se muestra la opción de filtrar por ratio de aciertos
});
});
});

43 changes: 43 additions & 0 deletions webapp/src/pages/Stats/Stats.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,47 @@ describe('Stats component', () => {
expect(screen.queryByText('Estadísticas de testUser - modo Batería de sabios')).toBeInTheDocument();
});
});

test('fetches and displays user statistics for Human Calculator mode', async () => {
localStorage.setItem('username', 'testUser');

userData.ratioCorrect=0;
userData.totalCorrectQuestions=0;
userData.totalIncorrectQuestions=0;

global.fetch = jest.fn().mockResolvedValue({
json: jest.fn().mockResolvedValue(userData)
});

renderComponentWithRouter();

await waitFor(() => {
expect(screen.queryByText('Cargando ...')).not.toBeInTheDocument();
});

const modeButton = screen.getByRole('button', { name: /Calculadora humana/i });
userEvent.click(modeButton);

const table = await screen.findByRole('table');
expect(table).toBeInTheDocument();

const columnHeaders = ['Partidas jugadas', 'Puntos por partida', 'Puntos totales', 'Tiempo por pregunta (s):'];
columnHeaders.forEach(headerText => {
const headerElement = screen.getByText(headerText);
expect(headerElement).toBeInTheDocument();
});

Object.entries(userData).forEach(([key, value]) => {
if (key !== 'username') {
if (key === 'avgPoints' || key === 'avgTime') {
const valueElements = screen.getAllByText(value.toFixed(2));
expect(valueElements.length).toBeGreaterThan(0);
} else {
const valueElements = screen.getAllByText(value.toString());
expect(valueElements.length).toBeGreaterThan(0);
}
}
});
});

});

0 comments on commit ef4458d

Please sign in to comment.