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 #109 from Arquisoft/develop
v1.0.1
- Loading branch information
Showing
2 changed files
with
77 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,53 +13,74 @@ describe("Auth Utils tests", () => { | |
}); | ||
|
||
it("does not have a stored token", () => { | ||
expect(isUserLogged()).not.toBe(true); | ||
expect(isUserLogged()).toBe(false); | ||
}); | ||
|
||
it("can log in", async () => { | ||
|
||
// Mock axios and the onSuccess and onError functions | ||
it("can log in successfully", async () => { | ||
mockAxios.onPost().replyOnce(HttpStatusCode.Ok, { | ||
"token": "token", | ||
"refresh_Token": "refreshToken" | ||
}); | ||
const mockOnSucess = jest.fn(); | ||
const mockOnError = jest.fn(); | ||
|
||
// Test | ||
const loginData = { | ||
"email": "[email protected]", | ||
"password": "test" | ||
}; | ||
|
||
await login(loginData, mockOnSucess, mockOnError); | ||
|
||
//Check the user is now logged in | ||
expect(mockOnSucess).toHaveBeenCalled(); | ||
expect(mockOnError).not.toHaveBeenCalled(); | ||
expect(isUserLogged()).toBe(true); | ||
}); | ||
|
||
it("handles login error", async () => { | ||
mockAxios.onPost().replyOnce(HttpStatusCode.BadRequest); | ||
|
||
const mockOnSucess = jest.fn(); | ||
const mockOnError = jest.fn(); | ||
|
||
const loginData = { | ||
"email": "[email protected]", | ||
"password": "test" | ||
}; | ||
|
||
await login(loginData, mockOnSucess, mockOnError); | ||
|
||
expect(mockOnSucess).not.toHaveBeenCalled(); | ||
expect(mockOnError).toHaveBeenCalled(); | ||
expect(isUserLogged()).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("when the user is authenticated", () => { | ||
|
||
beforeAll(() => { | ||
sessionStorage.setItem("jwtToken", "token"); | ||
}) | ||
}); | ||
|
||
afterEach(() => { | ||
sessionStorage.clear(); | ||
}) | ||
}); | ||
|
||
it("has a stored token", () => { | ||
expect(isUserLogged()).toBe(true); | ||
}); | ||
|
||
it("can log out successfully", () => { | ||
sessionStorage.clear(); | ||
expect(isUserLogged()).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("saving the token", () => { | ||
beforeEach(() => { | ||
sessionStorage.clear(); | ||
}); | ||
|
||
it ("is saved", () => { | ||
it("saves the token and refresh token", () => { | ||
let mockResponse = { | ||
"data": { | ||
"token": "token", | ||
|
@@ -74,3 +95,4 @@ describe("Auth Utils tests", () => { | |
}); | ||
|
||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React from 'react'; | ||
import { render, fireEvent, waitFor, screen } from '@testing-library/react'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import { BrowserRouter, MemoryRouter } from 'react-router-dom'; | ||
import Results from '../pages/Results'; | ||
|
||
jest.mock('react-router-dom', () => ({ | ||
...jest.requireActual('react-router-dom'), | ||
useLocation: () => ({ | ||
state: { correctAnswers: 3 }, | ||
}), | ||
})); | ||
|
||
describe('Results Component', () => { | ||
test('renders results with correct answers', () => { | ||
const { getByText, getByTestId } = render( | ||
<BrowserRouter> | ||
<Results /> | ||
</BrowserRouter> | ||
); | ||
|
||
expect(getByText('Results')).toBeInTheDocument(); | ||
expect(getByText('Correct answers: 3')).toBeInTheDocument(); | ||
expect(getByTestId('GoBack')).toBeInTheDocument(); | ||
expect(getByTestId('GoBack')).toHaveTextContent('common.finish'); | ||
}); | ||
|
||
it('navigates to dashboard on button click', async () => { | ||
const navigateMock = jest.fn(); | ||
jest.spyOn(require('react-router-dom'), 'useNavigate').mockReturnValue(navigateMock); | ||
|
||
const { getByTestId } = render( | ||
<BrowserRouter> | ||
<Results /> | ||
</BrowserRouter> | ||
); | ||
|
||
const goBackButton = getByTestId('GoBack'); | ||
fireEvent.click(goBackButton); | ||
|
||
await waitFor(() => { | ||
expect(navigateMock).toHaveBeenCalled(); | ||
expect(navigateMock).toHaveBeenCalledWith('/dashboard'); | ||
}); | ||
}); | ||
}); |