Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leaderboard tests #231

Merged
merged 1 commit into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions webapp/src/components/leaderboard/LeaderBoard.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React from 'react';
import { render, screen,fireEvent } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import LeaderBoard from './LeaderBoard';
import { getAllUsers } from "../../services/auth-service";
import { act } from 'react-dom/test-utils'; // Importa act

jest.mock('../../services/auth-service');

describe('Leaderboard', () => {
beforeEach(() => {
// Mock de los datos de usuarios
(getAllUsers as jest.Mock).mockResolvedValue({
user1: { username: 'user1', correctly_answered_questions: 5, questions_answered: 10 },
user2: { username: 'user2', correctly_answered_questions: 3, questions_answered: 10 },
user3: { username: 'user3', correctly_answered_questions: 7, questions_answered: 10 },
});
});

it('renders leaderboard with normal table by default', async () => {
await act(async () => { // Envuelve render en act
render(<LeaderBoard />);
});

// Verifica que inicialmente se muestre la tabla normal
const normalTable = screen.getByText('% CORRECT ANSWERS');
expect(normalTable).toBeInTheDocument();
});

it('switches to trivial table when trivial tab is clicked', async () => {
await act(async () => { // Envuelve render en act
render(<LeaderBoard />);
});

// Haz clic en la pestaña Trivial
const trivialTab = screen.getByRole('tab', { name: /trivial/i });
userEvent.click(trivialTab);

// Verifica que se muestre la tabla trivial después de hacer clic en la pestaña
const trivialTable = screen.getByText("CHEESES");
expect(trivialTable).toBeInTheDocument();
});

it('applies correct class to tabs when clicked', () => {
render(<LeaderBoard />);

// Verifica que la clase inicialmente seleccionada sea "chakra-tabs-tab-selected"
const selectedTab = screen.getByRole('tab', { name: /NORMAL/i });
expect(selectedTab).toHaveClass('chakra-tabs-tab-selected');

// Verifica que la clase inicial no seleccionada sea "chakra-tabs-tab"
const unselectedTab = screen.getByRole('tab', { name: /TRIVIAL/i });
expect(unselectedTab).toHaveClass('chakra-tabs-tab');

// Haz clic en la pestaña Trivial
fireEvent.click(unselectedTab);

// Verifica que la clase seleccionada cambió a "chakra-tabs-tab-selected" después de hacer clic
expect(unselectedTab).toHaveClass('chakra-tabs-tab-selected');

// Verifica que la clase no seleccionada cambió a "chakra-tabs-tab" después de hacer clic
expect(selectedTab).toHaveClass('chakra-tabs-tab');
});
});
20 changes: 0 additions & 20 deletions webapp/src/components/leaderboard/LeaderBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,6 @@ export default function LeaderBoard() {
</TabPanels>
</Tabs>




</>
//<RankingTable />

/*
<Tabs isFitted variant='enclosed'>
<TabList mb='1em'>
<Tab>One</Tab>
<Tab>Two</Tab>
</TabList>
<TabPanels>
<TabPanel>
<p>one!</p>
</TabPanel>
<TabPanel>
<p>two!</p>
</TabPanel>
</TabPanels>
</Tabs>*/
)
}