From da1c2671ed8398b484f0e314f009aaa2668521f1 Mon Sep 17 00:00:00 2001 From: sergiorodriguezgarcia <113514397+sergiorodriguezgarcia@users.noreply.github.com> Date: Mon, 11 Mar 2024 14:32:42 +0100 Subject: [PATCH 1/2] feat: Adding tests to the Results.jsx --- webapp/src/tests/Results.test.js | 46 ++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 webapp/src/tests/Results.test.js 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'); + }); + }); +}); From fcc0ac8a279232c4353b9f4c200edfdded604f5f Mon Sep 17 00:00:00 2001 From: sergiorodriguezgarcia <113514397+sergiorodriguezgarcia@users.noreply.github.com> Date: Mon, 11 Mar 2024 14:32:42 +0100 Subject: [PATCH 2/2] feat: Adding more tests to the AuthUtils.js --- webapp/src/tests/AuthUtils.test.js | 40 ++++++++++++++++++++------ webapp/src/tests/Results.test.js | 46 ++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 webapp/src/tests/Results.test.js 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'); + }); + }); +});