Skip to content

Commit

Permalink
arreglar duplicacion de codigo
Browse files Browse the repository at this point in the history
  • Loading branch information
UO290054 committed Apr 28, 2024
1 parent 74bc474 commit 4cafb84
Showing 1 changed file with 24 additions and 39 deletions.
63 changes: 24 additions & 39 deletions webapp/src/components/AddUser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,65 @@ import AddUser from './AddUser';

const mockAxios = new MockAdapter(axios);

const renderAddUserComponent = () => {
render(<AddUser />);
};

const addUser = async () => {
const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText(/Password/i);
const addUserButton = screen.getByRole('button', { name: /Crear usuario/i });

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

fireEvent.click(addUserButton);
};

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

it('should add user successfully', async () => {
render(<AddUser />);

const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText(/Password/i);
const addUserButton = screen.getByRole('button', { name: /Crear usuario/i });
renderAddUserComponent();

mockAxios.onPost('http://localhost:8000/adduser').reply(200);

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

fireEvent.click(addUserButton);
await addUser();

await waitFor(() => {
expect(screen.getByText(/User added successfully/i)).toBeInTheDocument();
});
});

it('should handle error when adding user', async () => {
render(<AddUser />);

const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText(/Password/i);
const addUserButton = screen.getByRole('button', { name: /Crear usuario/i });
renderAddUserComponent();

mockAxios.onPost('http://localhost:8000/adduser').reply(500, { error: 'Internal Server Error' });

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

fireEvent.click(addUserButton);
await addUser();

await waitFor(() => {
expect(screen.getByText(/Error: Internal Server Error/i)).toBeInTheDocument();
});
});



it('should display proper labels and inputs', () => {
render(<AddUser />);
renderAddUserComponent();

expect(screen.getByLabelText(/Username/i)).toBeInTheDocument();
expect(screen.getByLabelText(/Password/i)).toBeInTheDocument();
expect(screen.getByRole('button', { name: /Crear usuario/i })).toBeInTheDocument();
});

it('should display success Snackbar with autoHideDuration', async () => {
render(<AddUser />);

const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText(/Password/i);
const addUserButton = screen.getByRole('button', { name: /Crear usuario/i });
renderAddUserComponent();

mockAxios.onPost('http://localhost:8000/adduser').reply(200);

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

fireEvent.click(addUserButton);
await addUser();

jest.runAllTimers();

Expand All @@ -84,18 +76,11 @@ describe('AddUser component', () => {
});

it('should display error Snackbar with autoHideDuration', async () => {
render(<AddUser />);

const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText(/Password/i);
const addUserButton = screen.getByRole('button', { name: /Crear usuario/i });
renderAddUserComponent();

mockAxios.onPost('http://localhost:8000/adduser').reply(500, { error: 'Internal Server Error' });

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

fireEvent.click(addUserButton);
await addUser();

jest.runAllTimers();

Expand Down

0 comments on commit 4cafb84

Please sign in to comment.