Skip to content

Commit ab72773

Browse files
authored
use testing-library (#216)
1 parent 4204f44 commit ab72773

File tree

3 files changed

+225
-61
lines changed

3 files changed

+225
-61
lines changed

package-lock.json

Lines changed: 183 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"@mui/lab": "5.0.0-alpha.169",
8080
"@mui/material": "^5.15.14",
8181
"@react-hook/window-size": "^3.1.1",
82+
"@testing-library/react": "^16.0.0",
8283
"@types/autosuggest-highlight": "^3.2.3",
8384
"@types/eslint": "^8.56.7",
8485
"@types/eslint-config-prettier": "^6.11.3",

src/components/TopBar/TopBar.test.tsx

Lines changed: 41 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,17 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77

8-
import { createRoot } from 'react-dom/client';
9-
import { act } from 'react-dom/test-utils';
8+
import { render } from '@testing-library/react';
109
import { IntlProvider } from 'react-intl';
1110

1211
import { red } from '@mui/material/colors';
1312
import { createTheme, ThemeProvider } from '@mui/material';
14-
import { afterEach, beforeEach, expect, it } from '@jest/globals';
13+
import { expect, it } from '@jest/globals';
1514
import TopBar, { LANG_ENGLISH } from './TopBar';
1615
import { CommonMetadata, top_bar_en } from '../..';
1716

1817
import PowsyblLogo from '../images/powsybl_logo.svg?react';
1918

20-
let container: Element;
21-
22-
beforeEach(() => {
23-
// setup a DOM element as a render target
24-
container = document.createElement('div');
25-
document.body.appendChild(container);
26-
});
27-
28-
afterEach(() => {
29-
// cleanup on exiting
30-
container?.remove();
31-
});
32-
3319
const apps: CommonMetadata[] = [
3420
{
3521
name: 'App1',
@@ -54,50 +40,44 @@ const theme = createTheme({
5440
});
5541

5642
it('renders', () => {
57-
const root = createRoot(container);
58-
act(() => {
59-
root.render(
60-
<ThemeProvider theme={theme}>
61-
<IntlProvider locale="en" messages={top_bar_en}>
62-
<TopBar
63-
appName="Demo"
64-
appColor="#808080"
65-
appLogo={<PowsyblLogo />}
66-
onParametersClick={() => {}}
67-
onLogoutClick={() => {}}
68-
onLogoClick={() => {}}
69-
user={{
70-
profile: {
71-
name: 'John Doe',
72-
iss: 'issuer',
73-
sub: 'sub',
74-
aud: 'aud',
75-
exp: 213443,
76-
iat: 3214324,
77-
},
78-
id_token: 'id_token',
79-
access_token: 'access_token',
80-
token_type: 'code',
81-
scope: 'scope',
82-
expires_at: 123343,
83-
scopes: ['code', 'token'],
84-
expired: false,
85-
state: null,
86-
toStorageString: () => 'stored',
87-
expires_in: 1232,
88-
}}
89-
appsAndUrls={apps}
90-
language={LANG_ENGLISH}
91-
onLanguageClick={() => {}}
92-
>
93-
<p>testchild</p>
94-
</TopBar>
95-
</IntlProvider>
96-
</ThemeProvider>
97-
);
98-
});
43+
const { container } = render(
44+
<ThemeProvider theme={theme}>
45+
<IntlProvider locale="en" messages={top_bar_en}>
46+
<TopBar
47+
appName="Demo"
48+
appColor="#808080"
49+
appLogo={<PowsyblLogo />}
50+
onParametersClick={() => {}}
51+
onLogoutClick={() => {}}
52+
onLogoClick={() => {}}
53+
user={{
54+
profile: {
55+
name: 'John Doe',
56+
iss: 'issuer',
57+
sub: 'sub',
58+
aud: 'aud',
59+
exp: 213443,
60+
iat: 3214324,
61+
},
62+
id_token: 'id_token',
63+
access_token: 'access_token',
64+
token_type: 'code',
65+
scope: 'scope',
66+
expires_at: 123343,
67+
scopes: ['code', 'token'],
68+
expired: false,
69+
state: null,
70+
toStorageString: () => 'stored',
71+
expires_in: 1232,
72+
}}
73+
appsAndUrls={apps}
74+
language={LANG_ENGLISH}
75+
onLanguageClick={() => {}}
76+
>
77+
<p>testchild</p>
78+
</TopBar>
79+
</IntlProvider>
80+
</ThemeProvider>
81+
);
9982
expect(container.textContent).toContain('GridDemotestchildJD');
100-
act(() => {
101-
root.unmount();
102-
});
10383
});

0 commit comments

Comments
 (0)