generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from Arquisoft/duplication-testing
duplication fix
- Loading branch information
Showing
2 changed files
with
45 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' }); | ||
|
@@ -92,5 +79,5 @@ describe('Login component', () => { | |
|
||
}); | ||
|
||
|
||
}); |