Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ccb94fb

Browse files
authoredDec 16, 2024
(test) Add some missing test cases and improve typing in test files (openmrs#1239)
This PR improves test coverage and standardizes test descriptions across Core. Key changes include: - Adding coverage for missing test cases in the Devtools, Help Menu, and Implementer Tools test suites among others. - Standardizing test case descriptions across all modules. - Improving TypeScript types in test files. - Using `jest.mocked()` instead of direct type assertions for mocks as suggested in our [mocking patterns](https://o3-docs.openmrs.org/docs/frontend-modules/unit-and-integration-testing#mocking-patterns) guide. - Adding proper type annotations for mock data and responses - Cleaning up redundant type casts and improving mock implementations - Maintaining consistent naming conventions for mock variables (mock* prefix) - Adding missing type information to test utilities and helpers
1 parent 7269bbf commit ccb94fb

File tree

47 files changed

+943
-385
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+943
-385
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,75 @@
11
import React from 'react';
2+
import '@testing-library/jest-dom';
3+
import userEvent from '@testing-library/user-event';
4+
import { type AppProps } from 'single-spa';
5+
import { render, screen } from '@testing-library/react';
26
import Root from './devtools.component';
3-
import { render } from '@testing-library/react';
47

5-
describe(`<Root />`, () => {
6-
it(`renders without dying`, () => {
7-
render(<Root />);
8+
jest.mock('./import-map.component', () => ({
9+
__esModule: true,
10+
default: () => <div role="dialog">Mock Import Map</div>,
11+
importMapOverridden: false,
12+
}));
13+
14+
const defaultProps: AppProps = {
15+
name: '@openmrs/esm-devtools-app-page-0',
16+
singleSpa: {},
17+
mountParcel: jest.fn(),
18+
};
19+
20+
describe('DevTools', () => {
21+
beforeEach(() => {
22+
localStorage.clear();
23+
delete window.spaEnv;
24+
jest.resetModules();
25+
});
26+
27+
describe('Root component', () => {
28+
it('should not render DevTools in production without the devtools localStorage flag', () => {
29+
window.spaEnv = 'production';
30+
31+
const { container } = render(<Root {...defaultProps} />);
32+
expect(container).toBeEmptyDOMElement();
33+
});
34+
35+
it('should render DevTools in development environments', () => {
36+
window.spaEnv = 'development';
37+
38+
render(<Root {...defaultProps} />);
39+
40+
expect(screen.getByRole('button', { name: '{···}' })).toBeInTheDocument();
41+
});
42+
43+
it('should render DevTools when the devtools localStorage flag is set', () => {
44+
localStorage.setItem('openmrs:devtools', 'true');
45+
46+
render(<Root {...defaultProps} />);
47+
48+
expect(screen.getByRole('button', { name: '{···}' })).toBeInTheDocument();
49+
});
50+
});
51+
52+
describe('DevTools component', () => {
53+
const user = userEvent.setup();
54+
55+
beforeEach(() => {
56+
window.spaEnv = 'development';
57+
});
58+
59+
it('should toggle DevToolsPopup when clicking trigger button', async () => {
60+
render(<Root {...defaultProps} />);
61+
62+
const triggerButton = screen.getByRole('button', { name: '{···}' });
63+
// Initially, popup should not be present
64+
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
65+
66+
// Click to open
67+
await user.click(triggerButton);
68+
expect(screen.getByRole('dialog')).toBeInTheDocument();
69+
70+
// Click to close
71+
await user.click(triggerButton);
72+
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
73+
});
874
});
975
});

‎packages/apps/esm-devtools-app/src/devtools/devtools.component.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import React, { useState } from 'react';
22
import classNames from 'classnames';
3+
import { type AppProps } from 'single-spa';
34
import { importMapOverridden } from './import-map.component';
45
import DevToolsPopup from './devtools-popup.component';
56
import styles from './devtools.styles.css';
67

7-
export default function Root(props) {
8+
export default function Root(props: AppProps) {
89
return window.spaEnv === 'development' || Boolean(localStorage.getItem('openmrs:devtools')) ? (
910
<DevTools {...props} />
1011
) : null;
1112
}
1213

13-
function DevTools() {
14+
function DevTools(props: AppProps) {
1415
const [devToolsOpen, setDevToolsOpen] = useState(false);
1516
const [isOverridden, setIsOverridden] = useState(importMapOverridden);
17+
1618
return (
1719
<>
1820
<div

0 commit comments

Comments
 (0)
Please sign in to comment.