Skip to content

Commit

Permalink
duplication fix
Browse files Browse the repository at this point in the history
  • Loading branch information
andrrsin committed Apr 27, 2024
1 parent 0c0d50b commit dd711ff
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 60 deletions.
44 changes: 21 additions & 23 deletions webapp/src/components/auth/AddUser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<BrowserRouter>
<AddUser />
</BrowserRouter>);
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();
Expand All @@ -30,16 +42,10 @@ describe('AddUser component', () => {
});

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



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);
Expand All @@ -60,13 +66,9 @@ describe('AddUser component', () => {
});

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

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' });
Expand All @@ -86,13 +88,9 @@ describe('AddUser component', () => {
});

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


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' });
Expand Down
61 changes: 24 additions & 37 deletions webapp/src/components/auth/Login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,58 @@ 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(
<AuthProvider
store={store}
>
<BrowserRouter>
<Login/>
<Login />
</BrowserRouter>
</AuthProvider>);

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:"[email protected]",createdAt: '2024-01-01T12:34:56Z',token: 'testToken'});
mockAxios.onPost('http://localhost:8000/login').reply(200, { username: "testUser", email: "[email protected]", createdAt: '2024-01-01T12:34:56Z', token: 'testToken' });

// Simulate user input
await act(async () => {
fireEvent.change(usernameInput, { target: { value: 'testUser' } });
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(
<AuthProvider
store={store}
>
<BrowserRouter>
<Login/>
</BrowserRouter>
</AuthProvider>);

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' });
Expand All @@ -92,5 +79,5 @@ describe('Login component', () => {

});


});

0 comments on commit dd711ff

Please sign in to comment.