generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from Arquisoft/develop
Mejora de coverage en nuevo codigo
- Loading branch information
Showing
3 changed files
with
204 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,87 @@ | ||
import React from 'react'; | ||
import { render, fireEvent, screen, waitFor } from '@testing-library/react'; | ||
import axios from 'axios'; | ||
import MockAdapter from 'axios-mock-adapter'; | ||
import { render, screen, fireEvent, waitFor } from '@testing-library/react'; | ||
import AddUser from './AddUser'; | ||
import { BrowserRouter as Router } from 'react-router-dom'; | ||
import {AuthProvider} from "../../AuthContext"; | ||
const mockAxios = new MockAdapter(axios); | ||
|
||
describe('AddUser component', () => { | ||
beforeEach(() => { | ||
mockAxios.reset(); | ||
}); | ||
|
||
it('should add user successfully', async () => { | ||
render( | ||
<Router> | ||
<AuthProvider> | ||
<AddUser /> | ||
</AuthProvider> | ||
</Router> | ||
import { BrowserRouter } from 'react-router-dom'; | ||
import { AuthContext } from '../../AuthContext'; | ||
import axios from 'axios'; | ||
|
||
jest.mock('axios'); | ||
const mockHandleLogin = jest.fn(); | ||
|
||
function renderAddUser() { | ||
return render( | ||
<BrowserRouter> | ||
<AuthContext.Provider value={{ handleLogin: mockHandleLogin }}> | ||
<AddUser /> | ||
</AuthContext.Provider> | ||
</BrowserRouter> | ||
); | ||
} | ||
|
||
const usernameInput = screen.getByLabelText(/Nombre de Usuario/i); | ||
const passwordInput = screen.getByLabelText(/Contraseña/i); | ||
const addUserButton = screen.getByRole('button', { name: /Registrarse/i }); | ||
function fillAndSubmitForm(username, password) { | ||
fireEvent.change(screen.getByLabelText(/nombre de usuario/i), { target: { value: username } }); | ||
fireEvent.change(screen.getByLabelText(/contraseña/i), { target: { value: password } }); | ||
fireEvent.click(screen.getByRole('button', { name: /registrarse/i })); | ||
} | ||
|
||
// Mock the axios.post request to simulate a successful response | ||
mockAxios.onPost('http://localhost:8000/adduser').reply(200); | ||
test('renderiza el formulario de registro', () => { | ||
renderAddUser(); | ||
expect(screen.getByLabelText(/nombre de usuario/i)).toBeInTheDocument(); | ||
expect(screen.getByLabelText(/contraseña/i)).toBeInTheDocument(); | ||
expect(screen.getByRole('button', { name: /registrarse/i })).toBeInTheDocument(); | ||
}); | ||
|
||
// Simulate user input | ||
fireEvent.change(usernameInput, { target: { value: 'testUser' } }); | ||
fireEvent.change(passwordInput, { target: { value: 'testPassword2' } }); | ||
test('Errores de validacion al crear una nueva cuenta', async () => { | ||
renderAddUser(); | ||
fillAndSubmitForm('abc', '12345678'); | ||
|
||
// Trigger the add user button click | ||
fireEvent.click(addUserButton); | ||
await waitFor(() => { | ||
expect(screen.getByText(/el nombre de usuario debe tener al menos 4 caracteres/i)).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
test('La contraseña debe tener al menos 8 caracteres', async () => { | ||
renderAddUser(); | ||
fillAndSubmitForm('testuser', 'abc'); | ||
|
||
// Wait for the Snackbar to be open | ||
await waitFor(() => { | ||
expect(screen.getByText(/Usuario añadido correctamente/i)).toBeInTheDocument(); | ||
expect(screen.getByText(/la contraseña debe tener al menos 8 caracteres/i)).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should handle error when adding user', async () => { | ||
render( | ||
<Router> | ||
<AuthProvider> | ||
<AddUser /> | ||
</AuthProvider> | ||
</Router> | ||
); | ||
}); | ||
|
||
const usernameInput = screen.getByLabelText(/Nombre de Usuario/i); | ||
const passwordInput = screen.getByLabelText(/Contraseña/i); | ||
const addUserButton = screen.getByRole('button', { name: /Registrarse/i }); | ||
test('La contraseña debe contener al menos una letra mayúscula', async () => { | ||
renderAddUser(); | ||
fillAndSubmitForm('testuser', 'abcdefgh'); | ||
|
||
// Mock the axios.post request to simulate an error response | ||
mockAxios.onPost('http://localhost:8000/adduser').reply(500, { error: 'Internal Server Error' }); | ||
await waitFor(() => { | ||
expect(screen.getByText(/la contraseña debe contener al menos una letra mayúscula/i)).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
test('La contraseña debe contener al menos un número', async () => { | ||
renderAddUser(); | ||
fillAndSubmitForm('testuser', 'Abcdefgh'); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText(/la contraseña debe contener al menos un número/i)).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
// Simulate user input | ||
fireEvent.change(usernameInput, { target: { value: 'testUser' } }); | ||
fireEvent.change(passwordInput, { target: { value: 'testPassword2' } }); | ||
test('Registro exitoso de usuario', async () => { | ||
axios.post.mockImplementation((url) => { | ||
if (url.includes('/adduser')) { | ||
return Promise.resolve({ status: 200 }); | ||
} else if (url.includes('/login')) { | ||
return Promise.resolve({ data: { token: 'fakeToken123' } }); | ||
} | ||
}); | ||
|
||
// Trigger the add user button click | ||
fireEvent.click(addUserButton); | ||
renderAddUser(); | ||
fillAndSubmitForm('validUser', 'Valid1234'); | ||
|
||
// Wait for the error Snackbar to be open | ||
await waitFor(() => { | ||
expect(screen.getByText(/Error: Internal Server Error/i)).toBeInTheDocument(); | ||
expect(screen.getByText('Usuario añadido correctamente')).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); | ||
|
||
// Verificar redireccionamiento a la página inicial | ||
expect(window.location.pathname).toBe('/'); | ||
}); |