Skip to content

Commit

Permalink
login test extend
Browse files Browse the repository at this point in the history
  • Loading branch information
uo276976 committed Apr 7, 2024
1 parent f8ac409 commit d21efa0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 28 deletions.
7 changes: 2 additions & 5 deletions webapp/src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@ const Login = ({ goTo }) => {
setOpenSnackbar(false);
};
useEffect(() => {
if (sessionData && sessionData.token) {
autologin();
}
sessionData?.token && autologin();
//eslint-disable-next-line
}, []);

}, []);
useEffect(() => {
if (loginSuccess) {
goTo(1);
Expand Down
49 changes: 31 additions & 18 deletions webapp/src/test/AddUser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,39 +136,52 @@ describe('AddUser component', () => {

});
it('should handle successful user registration and login', async () => {
// Mock the response for successful user registration
mockAxios.onPost('http://localhost:8000/adduser').reply(200);

// Mock the response for successful user login
mockAxios.onPost('http://localhost:8000/login').reply(200, {
createdAt: '2022-01-01',
username: 'testUser',
token: 'testToken',
profileImage: 'testProfileImage',
userId: 'testUserId',
});

const goToMock = jest.fn();

// Render the component with mocked goTo function
render(
<SessionContext.Provider value={{ saveSessionData: () => {},clearSessionData: jest.fn() }}>
<AddUser goTo={(parameter) => {}}/>
<SessionContext.Provider value={{ saveSessionData: () => {}, clearSessionData: jest.fn() }}>
<AddUser goTo={goToMock} />
</SessionContext.Provider>
);


// Find input fields and submit button
const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText('Password');
const confirmPasswordInput = screen.getByLabelText(/Confirm/i);
const addUserButton = screen.getByRole('button', { name: /SIGN UP/i });

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

// Mock the axios.post request to simulate a successful response
mockAxios.onPost('http://localhost:8000/adduser').reply(200);
mockAxios.onPost(`http://localhost:8000/login`).reply(200, {
createdAt: '2022-01-01',
username: 'testUser',
token: 'testToken',
profileImage: 'testProfileImage',
userId: 'testUserId',
});


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

// Wait for the Snackbar to be open
await waitFor(() => {
expect(screen.queryByText(/Passwords do not match/i)).toBeNull();
});
});


// Ensure that loginSuccess is set to true
await waitFor(() => {
expect(screen.getByText(/User added successfully/i)).toBeInTheDocument();
});

// Verify that the goTo function is called with 1
expect(goToMock).toHaveBeenCalledWith(1);
});
});
43 changes: 38 additions & 5 deletions webapp/src/test/Login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ describe('Login component', () => {
});

it('should log in successfully', async () => {

render(
<SessionContext.Provider value={mockValue}>
<Login goTo={(parameter) => {}} />
<Login goTo={() => {}} />
</SessionContext.Provider>
);

Expand All @@ -37,13 +36,17 @@ describe('Login component', () => {
fireEvent.change(passwordInput, { target: { value: 'testPassword' } });
fireEvent.click(loginButton);
});

// Verify that login was successful
await waitFor(() => {
expect(screen.getByText(/Login successful/i)).toBeInTheDocument();
});
});

it('should handle error when logging in', async () => {

render(
<SessionContext.Provider value={mockValue}>
<Login goTo={(parameter) => {}} />
<Login goTo={() => {}} />
</SessionContext.Provider>
);

Expand All @@ -54,7 +57,7 @@ describe('Login component', () => {
// Mock the axios.post request to simulate an error response
mockAxios.onPost('http://localhost:8000/login').reply(401, { error: 'Unauthorized' });

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

Expand All @@ -70,6 +73,7 @@ describe('Login component', () => {
expect(screen.queryByText(/Hello testUser!/i)).toBeNull();
expect(screen.queryByText(/Your account was created on/i)).toBeNull();
});

it('should call autologin function when sessionData has token', async () => {
const goToMock = jest.fn();
const sessionData = { token: 'testToken' };
Expand Down Expand Up @@ -103,4 +107,33 @@ describe('Login component', () => {

expect(goToMock).not.toHaveBeenCalled();
});

it('should call goTo function when response status is 200', async () => {
const goToMock = jest.fn();

render(
<SessionContext.Provider value={mockValue}>
<Login goTo={goToMock} />
</SessionContext.Provider>
);

const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText(/Password/i);
const loginButton = screen.getByRole('button', { name: /Login/i });

// Mock the axios.post request to simulate a successful response
mockAxios.onPost('http://localhost:8000/login').reply(200, { createdAt: '2024-01-01T12:34:56Z' });

// Simulate user input and login button click
await act(async () => {
fireEvent.change(usernameInput, { target: { value: 'testUser' } });
fireEvent.change(passwordInput, { target: { value: 'testPassword' } });
fireEvent.click(loginButton);
});

// Wait for the goTo function to be called
await waitFor(() => {
expect(goToMock).toHaveBeenCalledWith(1);
});
});
});

0 comments on commit d21efa0

Please sign in to comment.