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

✨ Cambios finales #207

Merged
merged 17 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
🧪 Cambios en tests
  • Loading branch information
coral2742 committed May 6, 2024
commit 5ca1b36b2d294b99b18e0f00d430c2c11851fa70
19 changes: 18 additions & 1 deletion 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"axios": "^1.6.8",
"cors": "^2.8.5",
"express": "^4.19.2",
"history": "^5.3.0",
"jest": "^29.7.0",
"supertest": "^6.3.4"
},
Expand Down
12 changes: 8 additions & 4 deletions webapp/src/components/AddUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ const AddUser = () => {
const [password, setPassword] = useState('');
const [error, setError] = useState('');
const [openSnackbar, setOpenSnackbar] = useState(false);
const navigate = useNavigate(); // Move useNavigate hook outside of the addUser function
const navigate = useNavigate();

const addUser = async () => {
try {
await axios.post(`${apiEndpoint}/adduser`, { username, password });

await axios.post(`${apiEndpoint}/login`, { username, password });
localStorage.setItem('username', username);
setOpenSnackbar(true);
navigate("/MainPage");

// Redirige a la página de juego después de 3 segundos
setTimeout(() => {
navigate("/Game");
}, 3000);
} catch (error) {
setError("Error al crear usuario");
setOpenSnackbar(true); // Abre el Snackbar en caso de error
Expand Down Expand Up @@ -55,7 +59,7 @@ const AddUser = () => {
<Button variant="contained" color="primary" onClick={addUser}>
Crear usuario
</Button>
<Snackbar open={openSnackbar} autoHideDuration={6000} onClose={handleCloseSnackbar} message="User added successfully" />
<Snackbar open={openSnackbar} autoHideDuration={6000} onClose={handleCloseSnackbar} message="Usuario añadido correctamente" />
{error && (
<Snackbar open={!!error} autoHideDuration={6000} onClose={() => setError('')} message={`Error: ${error}`} />
)}
Expand Down
15 changes: 11 additions & 4 deletions webapp/src/components/AddUser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ import { render, fireEvent, screen, waitFor } from '@testing-library/react';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import AddUser from './AddUser';
import { MemoryRouter } from 'react-router-dom'; // Importa MemoryRouter en lugar de Router

const mockAxios = new MockAdapter(axios);

const renderAddUserComponent = () => {
render(<AddUser />);
return render(
<MemoryRouter> {/* Usa MemoryRouter en lugar de Router */}
<AddUser />
</MemoryRouter>
);
};

const addUser = async () => {
Expand All @@ -27,6 +32,8 @@ describe('AddUser component', () => {
jest.useFakeTimers();
});



it('should add user successfully', async () => {
renderAddUserComponent();

Expand All @@ -35,7 +42,7 @@ describe('AddUser component', () => {
await addUser();

await waitFor(() => {
expect(screen.getByText(/User added successfully/i)).toBeInTheDocument();
expect(screen.getByText(/Usuario añadido correctamente/i)).toBeInTheDocument();
});
});

Expand Down Expand Up @@ -68,10 +75,10 @@ describe('AddUser component', () => {

jest.runAllTimers();

expect(screen.queryByText(/User added successfully/i)).toBeNull();
expect(screen.queryByText(/Usuario añadido correctamente/i)).toBeNull();

await waitFor(() => {
expect(screen.getByText(/User added successfully/i)).toBeInTheDocument();
expect(screen.getByText(/Usuario añadido correctamente/i)).toBeInTheDocument();
});
});

Expand Down
5 changes: 2 additions & 3 deletions webapp/src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ const Login = () => {
setOpenSnackbar(true);
navigate("/MainPage");
} catch (error) {
setError("Error al iniciar sesión");
setOpenSnackbar(true);
setError('Error: Credenciales inválidas')
}
};

Expand Down Expand Up @@ -55,7 +54,7 @@ const Login = () => {
</Button>
<Snackbar open={openSnackbar} autoHideDuration={6000} onClose={handleCloseSnackbar} message="Inicio de sesión exitoso" />
{error && (
<Snackbar open={!!error} autoHideDuration={6000} onClose={() => setError('')} message={`Error: ${error}`} />
<Snackbar open={!!error} autoHideDuration={6000} onClose={() => setError('Error: Credenciales inválidas')} message={error} />
)}
</div>
</Container>
Expand Down
3 changes: 1 addition & 2 deletions webapp/src/components/Login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Login component', () => {
const loginButton = screen.getByRole('button', { name: /Iniciar sesión/i });

// Mock del request axios.post para simular una respuesta de error
mockAxios.onPost('http://localhost:8000/login').reply(401, { error: 'Credenciales inválidas' });
mockAxios.onPost('http://localhost:8000/login').reply(401, { error: 'Error: Credenciales inválidas' });

fireEvent.change(usernameInput, { target: { value: 'testUser' } });
fireEvent.change(passwordInput, { target: { value: 'testPassword' } });
Expand Down Expand Up @@ -80,7 +80,6 @@ describe('Login component', () => {
expect(screen.getByText(/Inicio de sesión exitoso/i)).toBeInTheDocument();
});

// Verificar si la redirección sucede después del inicio de sesión exitoso
expect(history.location.pathname).toBe('/');
});
});