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 4177284 commit 8899ce1
Showing 1 changed file with 7 additions and 29 deletions.
36 changes: 7 additions & 29 deletions webapp/src/components/tests/AddUser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import AddUser from '../AddUser';

const mockAxios = new MockAdapter(axios);
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 });

describe('AddUser component', () => {
beforeEach(() => {
Expand All @@ -13,11 +19,6 @@ describe('AddUser component', () => {

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

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

// Mock the axios.post request to simulate a successful response
mockAxios.onPost('http://localhost:8000/adduser').reply(200);

Expand All @@ -36,11 +37,6 @@ describe('AddUser component', () => {

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

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

// Mock the axios.post request to simulate an error response
mockAxios.onPost('http://localhost:8000/adduser').reply(500, { error: 'Internal Server Error' });

Expand All @@ -59,15 +55,6 @@ describe('AddUser component', () => {

it('should show error of password too short', async () => {
render(<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 });

// Mock the axios.post request to simulate a successful response
mockAxios.onPost('http://localhost:8000/adduser').reply(200);

Expand All @@ -86,15 +73,6 @@ describe('AddUser component', () => {

it('should show error of repeated user', async () => {
render(<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 });

// Mock the axios.post request to simulate a successful response
mockAxios.onPost('http://localhost:8000/adduser').reply(200);

Expand Down

0 comments on commit 8899ce1

Please sign in to comment.