Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: async tests #171

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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