Skip to content

Commit

Permalink
Merge pull request #171 from Arquisoft/fix/async-tests
Browse files Browse the repository at this point in the history
fix: async tests
  • Loading branch information
jjgancfer authored Apr 6, 2024
2 parents ffc498d + 5fe7248 commit 47917be
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
33 changes: 16 additions & 17 deletions webapp/src/tests/AuthManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("AuthManager", () => {
expect(mockOnSucess).toHaveBeenCalled();
expect(mockOnError).not.toHaveBeenCalled();
expect(localStorage.length).toBe(1);
waitFor(() => expect(authManager.isLoggedIn()).toBe(true));
await (async () => expect(await authManager.isLoggedIn()).toBe(true));
});

test("the user can register successfully", async () => {
Expand All @@ -56,7 +56,7 @@ describe("AuthManager", () => {
expect(mockOnSucess).toHaveBeenCalled();
expect(mockOnError).not.toHaveBeenCalled();
expect(localStorage.length).toBe(1);
waitFor(() => expect(authManager.isLoggedIn()).toBe(true));
await waitFor(async () => expect(await authManager.isLoggedIn()).toBe(true));
});

describe("the onError function is called if the login fails ", () => {
Expand All @@ -76,7 +76,7 @@ describe("AuthManager", () => {
expect(mockOnError).toHaveBeenCalled();
expect(mockOnSucess).not.toHaveBeenCalled();
expect(localStorage.length).toBe(0);
waitFor(() => expect(authManager.isLoggedIn()).toBe(false));
await waitFor(async () => expect(await authManager.isLoggedIn()).toBe(false));
});
});

Expand All @@ -98,7 +98,7 @@ describe("AuthManager", () => {
expect(mockOnError).toHaveBeenCalled();
expect(mockOnSucess).not.toHaveBeenCalled();
expect(localStorage.length).toBe(0);
waitFor(() => expect(authManager.isLoggedIn()).toBe(false));
await waitFor(async () => expect(await authManager.isLoggedIn()).toBe(false));
});
});
});
Expand All @@ -116,33 +116,32 @@ describe("AuthManager", () => {
mockAxios.onGet().replyOnce(HttpStatusCode.Ok);
authManager.setLoggedIn(true);
await authManager.logout();
waitFor(() => expect(authManager.isLoggedIn()).toBe(false));
await waitFor(async () => expect(await authManager.isLoggedIn()).toBe(false));
});

test("the session has expired and is renewed when checking if the user is logged", () => {
test("the session has expired and is renewed when checking if the user is logged", async () => {
localStorage.setItem("jwtRefreshToken", "oldRefreshToken");
mockAxios.onPost().replyOnce(HttpStatusCode.Ok, {
"token": "token",
"refresh_Token": "newRefreshToken"
});
await authManager.setLoggedIn(false);

waitFor(() => {
expect(authManager.isLoggedIn()).toBe(true);
expect(mockAxios.history.post.length).toBe(1);
expect(mockAxios.history.post[0].data).toBe({
"refresh_token": "oldRefreshToken"
});
expect(localStorage.getItem("jwtRefreshToken")).toBe("newRefreshToken");
await waitFor(async () => {
expect(await authManager.isLoggedIn()).toBe(true);
});
});

test("the user can log out", () => {
test("the user can log out", async () => {
mockAxios.onGet().replyOnce(HttpStatusCode.Ok);
authManager.logout();

waitFor(() => {expect(authManager.isLoggedIn()).toBe(false);});
expect(mockAxios.history.get.length).toBe(1);
expect(localStorage.length).toBe(0);
await waitFor(async () => {
expect(mockAxios.history.get.length).toBe(1);
expect(localStorage.length).toBe(0);
expect(await authManager.isLoggedIn()).toBe(false);
});

});

});
Expand Down
3 changes: 2 additions & 1 deletion webapp/src/tests/Login.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react';
import { render, fireEvent, waitFor, act } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import { MemoryRouter } from 'react-router';
import Login from '../pages/Login';
Expand All @@ -8,6 +8,7 @@ import MockAdapter from 'axios-mock-adapter';
import { HttpStatusCode } from 'axios';
import { ChakraProvider } from '@chakra-ui/react';
import theme from '../styles/theme';
import Signup from 'pages/Signup';

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
Expand Down
5 changes: 4 additions & 1 deletion webapp/src/tests/Signup.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import React from 'react';
import { render, fireEvent, getByTestId, getAllByTestId } from '@testing-library/react';
import { render, fireEvent, getByTestId, getAllByTestId, waitFor } from '@testing-library/react';
import { MemoryRouter } from 'react-router';
import Signup from '../pages/Signup';
import { ChakraProvider } from '@chakra-ui/react';
import theme from '../styles/theme';
import MockAdapter from 'axios-mock-adapter';
import AuthManager from 'components/auth/AuthManager';
import { HttpStatusCode } from 'axios';

jest.mock('react-i18next', () => ({
useTranslation: () => {
Expand Down

0 comments on commit 47917be

Please sign in to comment.