Skip to content

Commit

Permalink
Merge pull request #109 from Arquisoft/develop
Browse files Browse the repository at this point in the history
v1.0.1
  • Loading branch information
Toto-hitori authored Mar 11, 2024
2 parents 507023b + 492b9aa commit e1f8819
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 9 deletions.
40 changes: 31 additions & 9 deletions webapp/src/tests/AuthUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -74,3 +95,4 @@ describe("Auth Utils tests", () => {
});



46 changes: 46 additions & 0 deletions webapp/src/tests/Results.test.js
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');
});
});
});

0 comments on commit e1f8819

Please sign in to comment.