diff --git a/webapp/src/pages/Ranking/Ranking.test.js b/webapp/src/pages/Ranking/Ranking.test.js
index 4938293e..25da9631 100644
--- a/webapp/src/pages/Ranking/Ranking.test.js
+++ b/webapp/src/pages/Ranking/Ranking.test.js
@@ -4,100 +4,75 @@ import { BrowserRouter as Router } from 'react-router-dom';
import '@testing-library/jest-dom/extend-expect';
import Ranking from './Ranking';
-describe('Ranking component', () => {
- test('renders loading state initially', () => {
- render(
-
-
-
- );
- expect(screen.getByText('Cargando ...')).toBeInTheDocument();
- });
-
- test('renders error message if fetching fails', async () => {
- // Mock fetch function to simulate failure
- jest.spyOn(global, 'fetch').mockImplementation(() =>
- Promise.reject(new Error('Failed to fetch'))
- );
+const mockData = [
+ { username: 'user1', avgPoints: 50 },
+ { username: 'user2', avgPoints: 60 }
+];
- render(
-
-
-
- );
-
- // Wait for error message to be displayed
- await waitFor(() => {
- expect(screen.getByText('Error: Failed to fetch')).toBeInTheDocument();
+describe('Ranking component', () => {
+ beforeEach(() => {
+ jest.spyOn(global, 'fetch').mockResolvedValue({
+ json: () => Promise.resolve([]),
});
+ });
- // Restore fetch
- global.fetch.mockRestore();
+ afterEach(() => {
+ jest.restoreAllMocks();
});
- test('renders ranking table with data after successful fetch', async () => {
- // Mock successful fetch response
- const mockData = [{ username: 'user1', avgPoints: 50 }, { username: 'user2', avgPoints: 60 }];
- jest.spyOn(global, 'fetch').mockImplementation(() =>
- Promise.resolve({
- json: () => Promise.resolve(mockData),
- })
- );
-
+ const renderComponent = () => {
render(
);
-
- // Wait for table to be rendered with data
+ };
+
+ const assertRankingTableWithData = async () => {
await waitFor(() => {
expect(screen.getByText('Usuario')).toBeInTheDocument();
- expect(screen.getAllByText('Puntos promedio')[1]).toBeInTheDocument();
expect(screen.getByText('user1')).toBeInTheDocument();
expect(screen.getByText('50')).toBeInTheDocument();
expect(screen.getByText('user2')).toBeInTheDocument();
expect(screen.getByText('60')).toBeInTheDocument();
});
-
- // Restore fetch
- global.fetch.mockRestore();
+ };
+
+ test('renders loading state initially', () => {
+ renderComponent();
+ expect(screen.getByText('Cargando ...')).toBeInTheDocument();
+ });
+
+ test('renders error message if fetching fails', async () => {
+ global.fetch.mockRejectedValueOnce(new Error('Failed to fetch'));
+ renderComponent();
+
+ await waitFor(() => {
+ expect(screen.getByText('Error: Failed to fetch')).toBeInTheDocument();
+ });
+ });
+
+ test('renders ranking table with data after successful fetch', async () => {
+ global.fetch.mockResolvedValueOnce({ json: () => Promise.resolve(mockData) });
+
+ renderComponent();
+
+ await assertRankingTableWithData();
});
-
test('changes gamemode when clicking on mode buttons', async () => {
- const mockData = [{ username: 'user1', avgPoints: 50 }, { username: 'user2', avgPoints: 60 }];
- jest.spyOn(global, 'fetch').mockImplementation(() =>
- Promise.resolve({
- json: () => Promise.resolve(mockData),
- })
- );
-
- render(
-
-
-
- );
-
- // Esperar a que se renderice la tabla con los datos
- await waitFor(() => {
- expect(screen.getByText('Usuario')).toBeInTheDocument();
- expect(screen.getAllByText('Puntos promedio')[1]).toBeInTheDocument();
- expect(screen.getByText('user1')).toBeInTheDocument();
- expect(screen.getByText('50')).toBeInTheDocument();
- expect(screen.getByText('user2')).toBeInTheDocument();
- expect(screen.getByText('60')).toBeInTheDocument();
- });
-
- const bateriaButton = screen.getByRole('button', { name: 'Batería de sabios' });
-
- fireEvent.click(bateriaButton);
-
- await waitFor(() => {
- expect(screen.getByText('Ranking - modo Batería de sabios')).toBeInTheDocument();
- });
-
- // Restaurar fetch
- global.fetch.mockRestore();
+ global.fetch.mockResolvedValueOnce({ json: () => Promise.resolve(mockData) });
+
+ renderComponent();
+
+ await assertRankingTableWithData();
+
+ const bateriaButton = screen.getByRole('button', { name: 'Batería de sabios' });
+ fireEvent.click(bateriaButton);
+
+ await waitFor(() => {
+ expect(screen.getByText('Ranking - modo Batería de sabios')).toBeInTheDocument();
+ });
});
});
+