Skip to content

Commit

Permalink
Quitando codigo repetido
Browse files Browse the repository at this point in the history
  • Loading branch information
uo276026 committed Apr 28, 2024
1 parent 8899ce1 commit 0bceae4
Showing 1 changed file with 44 additions and 56 deletions.
100 changes: 44 additions & 56 deletions webapp/src/components/tests/AddUser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,91 +4,79 @@ import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import AddUser from '../AddUser';

const mockAxios = new MockAdapter(axios);
const mockedUsernames = ['existingUser'];
mockAxios.onGet(`http://localhost:8000/getUsernames`).reply(200, { usernames: mockedUsernames });

const usernameInput = screen.getByLabelText(/Usuario/i);
const passwordInput = screen.getByLabelText(/Contraseña/i);
const addUserButton = screen.getByRole('button', { name: /Crear/i });
const mockAxios = new MockAdapter(axios);

describe('AddUser component', () => {
beforeEach(() => {
mockAxios.reset();
});

it('should add user successfully', async () => {
render(<AddUser />);
// Mock the axios.post request to simulate a successful response
const renderComponent = () => render(<AddUser />);

const setupMockedUsernames = (usernames) => {
mockAxios.onGet(`http://localhost:8000/getUsernames`).reply(200, { usernames });
};

const setupSuccessfulResponse = () => {
mockAxios.onPost('http://localhost:8000/adduser').reply(200);
};

// Simulate user input
fireEvent.change(usernameInput, { target: { value: 'testUser' } });
fireEvent.change(passwordInput, { target: { value: 'testPassword' } });
const setupErrorResponse = (statusCode, errorMessage) => {
mockAxios.onPost('http://localhost:8000/adduser').reply(statusCode, { error: errorMessage });
};

// Trigger the add user button click
fireEvent.click(addUserButton);
const typeUsernameAndPassword = (username, password) => {
const usernameInput = screen.getByLabelText(/Usuario/i);
const passwordInput = screen.getByLabelText(/Contraseña/i);
fireEvent.change(usernameInput, { target: { value: username } });
fireEvent.change(passwordInput, { target: { value: password } });
};

const clickCreateUserButton = () => {
const createUserButton = screen.getByRole('button', { name: /Crear/i });
fireEvent.click(createUserButton);
};

// Wait for the Snackbar to be open
it('should add user successfully', async () => {
renderComponent();
setupSuccessfulResponse();
typeUsernameAndPassword('testUser', 'testPassword');
clickCreateUserButton();
await waitFor(() => {
expect(screen.getByText(/Usuario creado correctamente/i)).toBeInTheDocument();
});
});

it('should handle error when adding user', async () => {
render(<AddUser />);
// Mock the axios.post request to simulate an error response
mockAxios.onPost('http://localhost:8000/adduser').reply(500, { error: 'Internal Server Error' });

// Simulate user input
fireEvent.change(usernameInput, { target: { value: 'testUser' } });
fireEvent.change(passwordInput, { target: { value: 'testPassword' } });

// Trigger the add user button click
fireEvent.click(addUserButton);

// Wait for the error Snackbar to be open
renderComponent();
setupErrorResponse(500, 'Internal Server Error');
typeUsernameAndPassword('testUser', 'testPassword');
clickCreateUserButton();
await waitFor(() => {
expect(screen.getByText(/Error: Internal Server Error/i)).toBeInTheDocument();
});
});

it('should show error of password too short', async () => {
render(<AddUser />);
// Mock the axios.post request to simulate a successful response
mockAxios.onPost('http://localhost:8000/adduser').reply(200);

// Simulate user input
fireEvent.change(usernameInput, { target: { value: 'testUser' } });
fireEvent.change(passwordInput, { target: { value: 'sh' } });

// Trigger the add user button click
fireEvent.click(addUserButton);

// Wait for the Snackbar to be open
it('should show error message for short password', async () => {
renderComponent();
setupMockedUsernames([]);
setupSuccessfulResponse();
typeUsernameAndPassword('testUser', 'sh');
clickCreateUserButton();
await waitFor(() => {
expect(screen.getByText(/Error: Credenciales incorrectas. La contraseña debe contener al menos 8 caracteres/i)).toBeInTheDocument();
});
});

it('should show error of repeated user', async () => {
render(<AddUser />);
// Mock the axios.post request to simulate a successful response
mockAxios.onPost('http://localhost:8000/adduser').reply(200);

// Simulate user input
fireEvent.change(usernameInput, { target: { value: 'existingUser' } });
fireEvent.change(passwordInput, { target: { value: 'passwordCorrect' } });

// Trigger the add user button click
fireEvent.click(addUserButton);

// Wait for the Snackbar to be open
it('should show error message for repeated username', async () => {
renderComponent();
setupMockedUsernames(['existingUser']);
setupSuccessfulResponse();
typeUsernameAndPassword('existingUser', 'passwordCorrect');
clickCreateUserButton();
await waitFor(() => {
expect(screen.getByText(/Error: Credenciales incorrectas. El nombre de usuario esta en uso/i)).toBeInTheDocument();
});
});


});

0 comments on commit 0bceae4

Please sign in to comment.