From dd711ff8fd419ea50b3df79cf289c5172025b682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cadenas?= <77900120+andrrsin@users.noreply.github.com> Date: Sat, 27 Apr 2024 23:32:11 +0200 Subject: [PATCH] duplication fix --- webapp/src/components/auth/AddUser.test.js | 44 ++++++++-------- webapp/src/components/auth/Login.test.js | 61 +++++++++------------- 2 files changed, 45 insertions(+), 60 deletions(-) diff --git a/webapp/src/components/auth/AddUser.test.js b/webapp/src/components/auth/AddUser.test.js index 1c82124..460111d 100644 --- a/webapp/src/components/auth/AddUser.test.js +++ b/webapp/src/components/auth/AddUser.test.js @@ -13,15 +13,27 @@ const mockAxios = new MockAdapter(axios); describe('AddUser component', () => { + let usernameInput = 0; + let emailInput = 0; + let passwordInput = 0; + let cpasswordInput = 0; + let addUserButton = 0; + beforeEach(() => { mockAxios.reset(); - }); - - it('renders correctly', () => { render( ); + usernameInput = screen.getByLabelText(/Username/i); + emailInput = screen.getByLabelText(/Email/i); + passwordInput = screen.getByLabelText("Password"); + cpasswordInput = screen.getByLabelText(/Confirm Password/i); + addUserButton = screen.getByRole('button', { name: /Register/i }); + }); + + it('renders correctly', () => { + expect(screen.getByLabelText('Username')).toBeInTheDocument(); expect(screen.getByLabelText('Email')).toBeInTheDocument(); @@ -30,16 +42,10 @@ describe('AddUser component', () => { }); it('should add user successfully', async () => { - render( - - ); + - const usernameInput = screen.getByLabelText(/Username/i); - const emailInput = screen.getByLabelText(/Email/i); - const passwordInput = screen.getByLabelText("Password"); - const cpasswordInput = screen.getByLabelText(/Confirm Password/i); - const addUserButton = screen.getByRole('button', { name: /Register/i }); + // Mock the axios.post request to simulate a successful response mockAxios.onPost('http://localhost:8000/adduser').reply(200); @@ -60,13 +66,9 @@ describe('AddUser component', () => { }); it('should handle wrong passwords when adding user', async () => { - render( - - ); - const usernameInput = screen.getByLabelText(/Username/i); - const passwordInput = screen.getByLabelText("Password"); - const addUserButton = screen.getByRole('button', { name: /Register/i }); + + // Mock the axios.post request to simulate an error response mockAxios.onPost('http://localhost:8000/adduser').reply(500, { error: 'Internal Server Error' }); @@ -86,13 +88,9 @@ describe('AddUser component', () => { }); it('should handle error when adding user', async () => { - render( - - ); + - const usernameInput = screen.getByLabelText(/Username/i); - const passwordInput = screen.getByLabelText("Password"); - const addUserButton = screen.getByRole('button', { name: /Register/i }); + // Mock the axios.post request to simulate an error response mockAxios.onPost('http://localhost:8000/adduser').reply(500, { error: 'Internal Server Error' }); diff --git a/webapp/src/components/auth/Login.test.js b/webapp/src/components/auth/Login.test.js index 5eba7a5..c35d2bf 100644 --- a/webapp/src/components/auth/Login.test.js +++ b/webapp/src/components/auth/Login.test.js @@ -9,36 +9,41 @@ import createStore from 'react-auth-kit/createStore'; const mockAxios = new MockAdapter(axios); describe('Login component', () => { + let usernameInput = 0; + let passwordInput = 0; + let loginButton = 0; + const store = createStore({ + authName: '_auth', + authType: 'cookie', + cookieDomain: window.location.hostname, + cookieSecure: window.location.protocol === 'https:', + }); beforeEach(() => { mockAxios.reset(); - }); - - it('should log in successfully', async () => { - const store = createStore({ - authName: '_auth', - authType: 'cookie', - cookieDomain: window.location.hostname, - cookieSecure: window.location.protocol === 'https:', - }); + render( - + ); - - const usernameInput = screen.getByLabelText(/Username/i); - const passwordInput = screen.getByLabelText(/Password/i); - const loginButton = screen.getByRole('button', { name: /Log In/i }); + + usernameInput = screen.getByLabelText(/Username/i); + passwordInput = screen.getByLabelText(/Password/i); + loginButton = screen.getByRole('button', { name: /Log In/i }); + }); + + it('should log in successfully', async () => { + const mock = jest.fn(); jest.mock('react-router-dom', () => ({ useNavigate: () => mock, })); // Mock the axios.post request to simulate a successful response - mockAxios.onPost('http://localhost:8000/login').reply(200, { username:"testUser",email:"test@test.com",createdAt: '2024-01-01T12:34:56Z',token: 'testToken'}); + mockAxios.onPost('http://localhost:8000/login').reply(200, { username: "testUser", email: "test@test.com", createdAt: '2024-01-01T12:34:56Z', token: 'testToken' }); // Simulate user input await act(async () => { @@ -46,34 +51,16 @@ describe('Login component', () => { fireEvent.change(passwordInput, { target: { value: 'testPassword' } }); fireEvent.click(loginButton); }); - + const linkElement = screen.getByText(/Error: Error: There was a problem.../i); expect(linkElement).toBeInTheDocument(); - + }); it('should handle error when logging in', async () => { - const store = createStore({ - authName: '_auth', - authType: 'cookie', - cookieDomain: window.location.hostname, - cookieSecure: window.location.protocol === 'https:', - }); - - render( - - - - - ); - - const usernameInput = screen.getByLabelText(/Username/i); - const passwordInput = screen.getByLabelText(/Password/i); - const loginButton = screen.getByRole('button', { name: /Log In/i }); + // Mock the axios.post request to simulate an error response mockAxios.onPost('http://localhost:8000/login').reply(401, { error: 'Unauthorized' }); @@ -92,5 +79,5 @@ describe('Login component', () => { }); - + });