Skip to content

Commit

Permalink
Más test de perfil
Browse files Browse the repository at this point in the history
  • Loading branch information
iyanfdezz committed Apr 1, 2024
1 parent 4d988c6 commit 662e1f4
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 60 deletions.
123 changes: 64 additions & 59 deletions webapp/src/pages/Perfil/Perfil.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Perfil = () => {
const gatewayUrl = process.env.GATEWAY_SERVICE_URL || "http://localhost:8000";
const [userData, setUserData] = useState(null);
const [loading, setLoading] = useState(true);
const [username,setUsername]=useState(localStorage.username);
const [username,setUsername]=useState(localStorage.username || 'error');
const [error, setError] = useState(null);

useEffect(() => {
Expand All @@ -27,64 +27,69 @@ const Perfil = () => {

return (
<>
<Nav/>
<Center py={8}>
<Box w="xl" borderWidth="1px" borderRadius="lg" overflow="hidden" boxShadow="lg" width="100%">
<VStack p={8} align="start" spacing={6}>
<Heading as="h1" size="lg">
Perfil del usuario
</Heading>
{loading ? (
<Center>
<Spinner />
</Center>
) : (
<>
<Text>
<strong>Nombre de usuario:</strong> {userData.username}
</Text>
<Text>
<strong>Fecha de creación de la cuenta:</strong>{" "}
{new Date(userData.createdAt).toLocaleString()}
</Text>
<Heading as="h2" size="md">
Partidas Recientes
</Heading>
<div style={{ width: '100%' }}>
{userData.games.length > 0 ? (
<Table variant="simple">
<Thead>
<Tr>
<Th>Modo de juego</Th>
<Th>Respuestas correctas</Th>
<Th>Respuestas incorrectas</Th>
<Th>Puntos</Th>
<Th>Tiempo promedio</Th>
</Tr>
</Thead>
<Tbody>
{userData.games.slice(0, 10).map((game, index) => (
<Tr key={index}>
<Td>{game.gamemode}</Td>
<Td>{game.correctAnswers}</Td>
<Td>{game.incorrectAnswers}</Td>
<Td>{game.points}</Td>
<Td>{game.avgTime} segundos</Td>
</Tr>
))}
</Tbody>
</Table>
) : (
<Text>No hay partidas recientes.</Text>
)}
</div>

</>
)}
</VStack>
</Box>
</Center>
<Footer/>
<Nav />
<Center py={8}>
<Box w="xl" borderWidth="1px" borderRadius="lg" overflow="hidden" boxShadow="lg" width="100%">
<VStack p={8} align="start" spacing={6}>
<Heading as="h1" size="lg">
Perfil del usuario
</Heading>
{loading ? (
<Center>
<Spinner />
</Center>
) : error ? (
<Text>Error: {error}</Text>
) : (
<>
{userData && (
<>
<Text>
<strong>Nombre de usuario:</strong> {userData.username}
</Text>
<Text>
<strong>Fecha de creación de la cuenta:</strong>{" "}
{new Date(userData.createdAt).toLocaleString()}
</Text>
<Heading as="h2" size="md">
Partidas Recientes
</Heading>
<div style={{ width: '100%' }}>
{userData.games.length > 0 ? (
<Table variant="simple">
<Thead>
<Tr>
<Th>Modo de juego</Th>
<Th>Respuestas correctas</Th>
<Th>Respuestas incorrectas</Th>
<Th>Puntos</Th>
<Th>Tiempo promedio</Th>
</Tr>
</Thead>
<Tbody>
{userData.games.slice(0, 10).map((game, index) => (
<Tr key={index}>
<Td>{game.gamemode}</Td>
<Td>{game.correctAnswers}</Td>
<Td>{game.incorrectAnswers}</Td>
<Td>{game.points}</Td>
<Td>{game.avgTime} segundos</Td>
</Tr>
))}
</Tbody>
</Table>
) : (
<Text>No hay partidas recientes.</Text>
)}
</div>
</>
)}
</>
)}
</VStack>
</Box>
</Center>
<Footer />
</>
);
};
Expand Down
38 changes: 38 additions & 0 deletions webapp/src/pages/Perfil/Perfil.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,42 @@ describe('Perfil Component', () => {
expect(screen.getByText('35')).toBeInTheDocument();
});
});

test('displays an error message when profile loading fails', async () => {
jest.spyOn(global, 'fetch').mockRejectedValueOnce(new Error('Failed to fetch'));

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

await waitFor(() => {
expect(screen.getByText('Error: Failed to fetch')).toBeInTheDocument();
});
});

test('displays a message when there are no recent games', async () => {
const mockUserData = {
username: 'testUser',
createdAt: new Date(),
games: []
};

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

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

await waitFor(() => {
expect(screen.getByText('No hay partidas recientes.')).toBeInTheDocument();
});
});


});
2 changes: 1 addition & 1 deletion webapp/src/pages/Stats/Stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Footer from "../../components/Footer/Footer.js";
const Stats = () => {
const gatewayUrl = process.env.GATEWAY_SERVICE_URL || "http://localhost:8000";

const [username, setUsername] = useState(localStorage.username || 'test');
const [username, setUsername] = useState(localStorage.username || 'error');
const [stats, setStats] = useState(null);
const [gamemode, setGamemode] = useState("clasico");
const [isLoading, setIsLoading] = useState(false);
Expand Down

0 comments on commit 662e1f4

Please sign in to comment.