Skip to content

Commit

Permalink
Refactor: Upgrade testing library, react types and use React's "act" …
Browse files Browse the repository at this point in the history
…instead of the deprecated act from the testing library
  • Loading branch information
drikusroor committed Jun 28, 2024
1 parent c1ea03f commit 1de3a67
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 311 deletions.
256 changes: 99 additions & 157 deletions frontend/.pnp.cjs

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@
"@storybook/react": "^8.1.11",
"@storybook/react-vite": "^8.1.11",
"@storybook/test": "^8.1.11",
"@testing-library/react": "^14.1.2",
"@testing-library/dom": "^10.2.0",
"@testing-library/react": "^16.0.0",
"@testing-library/user-event": "^14.5.1",
"@types/react": "^18.2.73",
"@types/react-dom": "^18.2.23",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@types/react-helmet": "^6",
"@types/react-router-dom": "^5.3.3",
"@vitest/coverage-istanbul": "^1.4.0",
Expand Down
102 changes: 50 additions & 52 deletions frontend/src/components/FontLoader/FontLoader.test.jsx
Original file line number Diff line number Diff line change
@@ -1,71 +1,69 @@
import React from 'react';
import React, { act } from 'react';
import { createRoot } from 'react-dom/client';
import { act } from 'react-dom/test-utils';
import FontLoader from './FontLoader';


describe('FontLoader', () => {
let container = null;
let root = null;

beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
document.head.querySelectorAll('link, style').forEach(el => el.remove());
container.remove();
container = null;
root.unmount();
});
let container = null;
let root = null;

beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});

it('renders without crashing', () => {
act(() => {
root = createRoot(container);
root.render(<FontLoader />);
afterEach(() => {
document.head.querySelectorAll('link, style').forEach(el => el.remove());
container.remove();
container = null;
root.unmount();
});
expect(container.innerHTML).toBe('');
});

it('loads the font and sets the global font style', async () => {
const fontUrl = 'https://fonts.googleapis.com/css?family=Roboto';
const fontType = 'heading';

act(() => {
root = createRoot(container);
root.render(<FontLoader fontUrl={fontUrl} fontType={fontType} />);
it('renders without crashing', () => {
act(() => {
root = createRoot(container);
root.render(<FontLoader />);
});
expect(container.innerHTML).toBe('');
});

const linkElement = document.querySelector('link[href="' + fontUrl + '"]');
const styleElement = document.querySelector('style');
it('loads the font and sets the global font style', async () => {
const fontUrl = 'https://fonts.googleapis.com/css?family=Roboto';
const fontType = 'heading';

expect(linkElement).not.toBeNull();
expect(styleElement).not.toBeNull();
expect(styleElement.innerHTML).toContain('font-family: "Roboto", sans-serif;');
act(() => {
root = createRoot(container);
root.render(<FontLoader fontUrl={fontUrl} fontType={fontType} />);
});

act(() => {
root.unmount();
});
const linkElement = document.querySelector('link[href="' + fontUrl + '"]');
const styleElement = document.querySelector('style');

expect(linkElement).not.toBeNull();
expect(styleElement).not.toBeNull();
expect(styleElement.innerHTML).toContain('font-family: "Roboto", sans-serif;');

const removedLinkElement = document.querySelector('link[href="' + fontUrl + '"]');
const removedStyleElement = document.querySelector('style');
act(() => {
root.unmount();
});

expect(removedLinkElement).toBeNull();
expect(removedStyleElement).toBeNull();
});
const removedLinkElement = document.querySelector('link[href="' + fontUrl + '"]');
const removedStyleElement = document.querySelector('style');

it('does not load the font if fontUrl is not provided', async () => {
act(() => {
root = createRoot(container);
root.render(<FontLoader />);
expect(removedLinkElement).toBeNull();
expect(removedStyleElement).toBeNull();
});

const linkElement = document.querySelector('link');
const styleElement = document.querySelector('style');
it('does not load the font if fontUrl is not provided', async () => {
act(() => {
root = createRoot(container);
root.render(<FontLoader />);
});

expect(linkElement).toBeNull();
expect(styleElement).toBeNull();
});
});
const linkElement = document.querySelector('link');
const styleElement = document.querySelector('style');

expect(linkElement).toBeNull();
expect(styleElement).toBeNull();
});
});
4 changes: 2 additions & 2 deletions frontend/src/components/Score/Score.test.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { act, fireEvent, render, screen } from '@testing-library/react';
import React, { act } from 'react';
import { fireEvent, render, screen } from '@testing-library/react';
import Score from './Score';
import makeDefaultScoreProps from '../../util/testUtils/makeDefaultScoreProps';
import { vi } from 'vitest';
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/hooks/useResultHandler.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { renderHook, act } from "@testing-library/react";
import { act } from "react";
import { renderHook, } from "@testing-library/react";
import useResultHandler from "./useResultHandler";
import { vi } from 'vitest';

Expand Down
129 changes: 33 additions & 96 deletions frontend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5318,19 +5318,19 @@ __metadata:
languageName: node
linkType: hard

"@testing-library/dom@npm:^9.0.0":
version: 9.3.3
resolution: "@testing-library/dom@npm:9.3.3"
"@testing-library/dom@npm:^10.2.0":
version: 10.2.0
resolution: "@testing-library/dom@npm:10.2.0"
dependencies:
"@babel/code-frame": "npm:^7.10.4"
"@babel/runtime": "npm:^7.12.5"
"@types/aria-query": "npm:^5.0.1"
aria-query: "npm:5.1.3"
aria-query: "npm:5.3.0"
chalk: "npm:^4.1.0"
dom-accessibility-api: "npm:^0.5.9"
lz-string: "npm:^1.5.0"
pretty-format: "npm:^27.0.2"
checksum: 10c0/c3bbd67503634fd955233dc172531640656701fe35ecb9a83f85e5965874b786452f5e7c26b4f8b3b4fc4379f3a80193c74425b57843ba191f4845e22b0ac483
checksum: 10c0/de582dfbeb632436547a0ca5851b5a714a4a17f8e96ab3dc4fb4e454eef52c912b648b7cb6e9fdf477f3eeef97e698f3250f0ce50846f39d04677a44169209f2
languageName: node
linkType: hard

Expand Down Expand Up @@ -5367,17 +5367,23 @@ __metadata:
languageName: node
linkType: hard

"@testing-library/react@npm:^14.1.2":
version: 14.1.2
resolution: "@testing-library/react@npm:14.1.2"
"@testing-library/react@npm:^16.0.0":
version: 16.0.0
resolution: "@testing-library/react@npm:16.0.0"
dependencies:
"@babel/runtime": "npm:^7.12.5"
"@testing-library/dom": "npm:^9.0.0"
"@types/react-dom": "npm:^18.0.0"
peerDependencies:
"@testing-library/dom": ^10.0.0
"@types/react": ^18.0.0
"@types/react-dom": ^18.0.0
react: ^18.0.0
react-dom: ^18.0.0
checksum: 10c0/b5b0990d3aa0ea8b37c55804e0d5d584fc638a5c7d4df90da9a0fdb00bc981b27b6991468b2dc719982a5d0b0107a41596063ce51ad519eeab47b22bc04d6779
peerDependenciesMeta:
"@types/react":
optional: true
"@types/react-dom":
optional: true
checksum: 10c0/297f97bf4722dad05f11d9cafd47d387dbdb096fea4b79b876c7466460f0f2e345b55b81b3e37fc81ed8185c528cb53dd8455ca1b6b019b229edf6c796f11c9f
languageName: node
linkType: hard

Expand Down Expand Up @@ -5758,21 +5764,12 @@ __metadata:
languageName: node
linkType: hard

"@types/react-dom@npm:^18.0.0":
version: 18.2.17
resolution: "@types/react-dom@npm:18.2.17"
"@types/react-dom@npm:^18.3.0":
version: 18.3.0
resolution: "@types/react-dom@npm:18.3.0"
dependencies:
"@types/react": "npm:*"
checksum: 10c0/33b53078ed7e9e0cfc4dc691e938f7db1cc06353bc345947b41b581c3efe2b980c9e4eb6460dbf5ddc521dd91959194c970221a2bd4bfad9d23ebce338e12938
languageName: node
linkType: hard

"@types/react-dom@npm:^18.2.23":
version: 18.2.23
resolution: "@types/react-dom@npm:18.2.23"
dependencies:
"@types/react": "npm:*"
checksum: 10c0/9348e93558aa67b4b237bd0eab62e72e85f3e17a1c45fde04d874476269730f7c671b3d62390c4fca588da2a026e90cc74148abc349dbfd4ee5535a82ccdf38e
checksum: 10c0/6c90d2ed72c5a0e440d2c75d99287e4b5df3e7b011838cdc03ae5cd518ab52164d86990e73246b9d812eaf02ec351d74e3b4f5bd325bf341e13bf980392fd53b
languageName: node
linkType: hard

Expand Down Expand Up @@ -5837,13 +5834,13 @@ __metadata:
languageName: node
linkType: hard

"@types/react@npm:^18.2.73":
version: 18.2.73
resolution: "@types/react@npm:18.2.73"
"@types/react@npm:^18.3.3":
version: 18.3.3
resolution: "@types/react@npm:18.3.3"
dependencies:
"@types/prop-types": "npm:*"
csstype: "npm:^3.0.2"
checksum: 10c0/b6645ab3c20efa41cfccf58ce0be45419517a0ba4594e323dd400342fb1c1f9589d169cf9bfa85b5b0605e9097fe9de7734b6d0c533f5b9bc32aaadb624537a4
checksum: 10c0/fe455f805c5da13b89964c3d68060cebd43e73ec15001a68b34634604a78140e6fc202f3f61679b9d809dde6d7a7c2cb3ed51e0fd1462557911db09879b55114
languageName: node
linkType: hard

Expand Down Expand Up @@ -6343,10 +6340,11 @@ __metadata:
"@storybook/react": "npm:^8.1.11"
"@storybook/react-vite": "npm:^8.1.11"
"@storybook/test": "npm:^8.1.11"
"@testing-library/react": "npm:^14.1.2"
"@testing-library/dom": "npm:^10.2.0"
"@testing-library/react": "npm:^16.0.0"
"@testing-library/user-event": "npm:^14.5.1"
"@types/react": "npm:^18.2.73"
"@types/react-dom": "npm:^18.2.23"
"@types/react": "npm:^18.3.3"
"@types/react-dom": "npm:^18.3.0"
"@types/react-helmet": "npm:^6"
"@types/react-router-dom": "npm:^5.3.3"
"@vitejs/plugin-react": "npm:^4.2.1"
Expand Down Expand Up @@ -6465,15 +6463,6 @@ __metadata:
languageName: node
linkType: hard

"aria-query@npm:5.1.3":
version: 5.1.3
resolution: "aria-query@npm:5.1.3"
dependencies:
deep-equal: "npm:^2.0.5"
checksum: 10c0/edcbc8044c4663d6f88f785e983e6784f98cb62b4ba1e9dd8d61b725d0203e4cfca38d676aee984c31f354103461102a3d583aa4fbe4fd0a89b679744f4e5faf
languageName: node
linkType: hard

"aria-query@npm:5.3.0, aria-query@npm:^5.0.0, aria-query@npm:^5.1.3":
version: 5.3.0
resolution: "aria-query@npm:5.3.0"
Expand Down Expand Up @@ -7544,32 +7533,6 @@ __metadata:
languageName: node
linkType: hard

"deep-equal@npm:^2.0.5":
version: 2.2.2
resolution: "deep-equal@npm:2.2.2"
dependencies:
array-buffer-byte-length: "npm:^1.0.0"
call-bind: "npm:^1.0.2"
es-get-iterator: "npm:^1.1.3"
get-intrinsic: "npm:^1.2.1"
is-arguments: "npm:^1.1.1"
is-array-buffer: "npm:^3.0.2"
is-date-object: "npm:^1.0.5"
is-regex: "npm:^1.1.4"
is-shared-array-buffer: "npm:^1.0.2"
isarray: "npm:^2.0.5"
object-is: "npm:^1.1.5"
object-keys: "npm:^1.1.1"
object.assign: "npm:^4.1.4"
regexp.prototype.flags: "npm:^1.5.0"
side-channel: "npm:^1.0.4"
which-boxed-primitive: "npm:^1.0.2"
which-collection: "npm:^1.0.1"
which-typed-array: "npm:^1.1.9"
checksum: 10c0/07b46a9a848efdab223abc7e3ba612ef9168d88970c3400df185d5840a30ca384749c996ae5d7af844d6b27c42587fb73a4445c63e38aac77c2d0ed9a63faa87
languageName: node
linkType: hard

"deep-is@npm:^0.1.3":
version: 0.1.4
resolution: "deep-is@npm:0.1.4"
Expand Down Expand Up @@ -7964,23 +7927,6 @@ __metadata:
languageName: node
linkType: hard

"es-get-iterator@npm:^1.1.3":
version: 1.1.3
resolution: "es-get-iterator@npm:1.1.3"
dependencies:
call-bind: "npm:^1.0.2"
get-intrinsic: "npm:^1.1.3"
has-symbols: "npm:^1.0.3"
is-arguments: "npm:^1.1.1"
is-map: "npm:^2.0.2"
is-set: "npm:^2.0.2"
is-string: "npm:^1.0.7"
isarray: "npm:^2.0.5"
stop-iteration-iterator: "npm:^1.0.0"
checksum: 10c0/ebd11effa79851ea75d7f079405f9d0dc185559fd65d986c6afea59a0ff2d46c2ed8675f19f03dce7429d7f6c14ff9aede8d121fbab78d75cfda6a263030bac0
languageName: node
linkType: hard

"es-iterator-helpers@npm:^1.0.12":
version: 1.0.14
resolution: "es-iterator-helpers@npm:1.0.14"
Expand Down Expand Up @@ -9565,7 +9511,7 @@ __metadata:
languageName: node
linkType: hard

"internal-slot@npm:^1.0.4, internal-slot@npm:^1.0.5":
"internal-slot@npm:^1.0.5":
version: 1.0.5
resolution: "internal-slot@npm:1.0.5"
dependencies:
Expand Down Expand Up @@ -9606,7 +9552,7 @@ __metadata:
languageName: node
linkType: hard

"is-arguments@npm:^1.0.4, is-arguments@npm:^1.1.1":
"is-arguments@npm:^1.0.4":
version: 1.1.1
resolution: "is-arguments@npm:1.1.1"
dependencies:
Expand Down Expand Up @@ -9781,7 +9727,7 @@ __metadata:
languageName: node
linkType: hard

"is-map@npm:^2.0.1, is-map@npm:^2.0.2":
"is-map@npm:^2.0.1":
version: 2.0.2
resolution: "is-map@npm:2.0.2"
checksum: 10c0/119ff9137a37fd131a72fab3f4ab8c9d6a24b0a1ee26b4eff14dc625900d8675a97785eea5f4174265e2006ed076cc24e89f6e57ebd080a48338d914ec9168a5
Expand Down Expand Up @@ -9854,7 +9800,7 @@ __metadata:
languageName: node
linkType: hard

"is-set@npm:^2.0.1, is-set@npm:^2.0.2":
"is-set@npm:^2.0.1":
version: 2.0.2
resolution: "is-set@npm:2.0.2"
checksum: 10c0/5f8bd1880df8c0004ce694e315e6e1e47a3452014be792880bb274a3b2cdb952fdb60789636ca6e084c7947ca8b7ae03ccaf54c93a7fcfed228af810559e5432
Expand Down Expand Up @@ -12827,15 +12773,6 @@ __metadata:
languageName: node
linkType: hard

"stop-iteration-iterator@npm:^1.0.0":
version: 1.0.0
resolution: "stop-iteration-iterator@npm:1.0.0"
dependencies:
internal-slot: "npm:^1.0.4"
checksum: 10c0/c4158d6188aac510d9e92925b58709207bd94699e9c31186a040c80932a687f84a51356b5895e6dc72710aad83addb9411c22171832c9ae0e6e11b7d61b0dfb9
languageName: node
linkType: hard

"store2@npm:^2.14.2":
version: 2.14.2
resolution: "store2@npm:2.14.2"
Expand Down

0 comments on commit 1de3a67

Please sign in to comment.