diff --git a/webapp/src/tests/AuthUtils.test.js b/webapp/src/tests/AuthUtils.test.js
index 51565bab..bc315bf2 100644
--- a/webapp/src/tests/AuthUtils.test.js
+++ b/webapp/src/tests/AuthUtils.test.js
@@ -13,12 +13,10 @@ 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"
@@ -26,7 +24,6 @@ describe("Auth Utils tests", () => {
const mockOnSucess = jest.fn();
const mockOnError = jest.fn();
- // Test
const loginData = {
"email": "test@email.com",
"password": "test"
@@ -34,24 +31,48 @@ describe("Auth Utils tests", () => {
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": "test@email.com",
+ "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", () => {
@@ -59,7 +80,7 @@ describe("Auth Utils tests", () => {
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", () => {
});
+
diff --git a/webapp/src/tests/Results.test.js b/webapp/src/tests/Results.test.js
new file mode 100644
index 00000000..47de497f
--- /dev/null
+++ b/webapp/src/tests/Results.test.js
@@ -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(
+
+
+
+ );
+
+ 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(
+
+
+
+ );
+
+ const goBackButton = getByTestId('GoBack');
+ fireEvent.click(goBackButton);
+
+ await waitFor(() => {
+ expect(navigateMock).toHaveBeenCalled();
+ expect(navigateMock).toHaveBeenCalledWith('/dashboard');
+ });
+ });
+});