@openmrs/esm-react-utils 3.2.1-pre.1082 → 3.2.1-pre.1084
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openmrs/esm-react-utils",
|
|
3
|
-
"version": "3.2.1-pre.
|
|
3
|
+
"version": "3.2.1-pre.1084",
|
|
4
4
|
"license": "MPL-2.0",
|
|
5
5
|
"description": "React utilities for OpenMRS.",
|
|
6
6
|
"browser": "dist/openmrs-esm-react-utils.js",
|
|
@@ -55,11 +55,11 @@
|
|
|
55
55
|
"react-i18next": "11.x"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@openmrs/esm-api": "^3.2.1-pre.
|
|
59
|
-
"@openmrs/esm-config": "^3.2.1-pre.
|
|
60
|
-
"@openmrs/esm-error-handling": "^3.2.1-pre.
|
|
61
|
-
"@openmrs/esm-extensions": "^3.2.1-pre.
|
|
62
|
-
"@openmrs/esm-globals": "^3.2.1-pre.
|
|
58
|
+
"@openmrs/esm-api": "^3.2.1-pre.1084",
|
|
59
|
+
"@openmrs/esm-config": "^3.2.1-pre.1084",
|
|
60
|
+
"@openmrs/esm-error-handling": "^3.2.1-pre.1084",
|
|
61
|
+
"@openmrs/esm-extensions": "^3.2.1-pre.1084",
|
|
62
|
+
"@openmrs/esm-globals": "^3.2.1-pre.1084",
|
|
63
63
|
"dayjs": "^1.10.8",
|
|
64
64
|
"i18next": "^19.6.0",
|
|
65
65
|
"react": "^16.13.1",
|
|
@@ -68,5 +68,5 @@
|
|
|
68
68
|
"rxjs": "^6.5.3",
|
|
69
69
|
"unistore": "^3.5.2"
|
|
70
70
|
},
|
|
71
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "c2a5b8ed52049b3e302df8043f42ac57ec3321b1"
|
|
72
72
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { render, fireEvent } from "@testing-library/react";
|
|
3
|
+
import { useOnClickOutside } from "./useOnClickOutside";
|
|
4
|
+
|
|
5
|
+
describe("useOnClickOutside", () => {
|
|
6
|
+
const handler: (e: Event) => void = jest.fn();
|
|
7
|
+
afterEach(() => (handler as jest.Mock).mockClear());
|
|
8
|
+
|
|
9
|
+
it("should call the handler when clicking outside", () => {
|
|
10
|
+
// setup
|
|
11
|
+
const Component: React.FC = ({ children }) => {
|
|
12
|
+
const ref = useOnClickOutside<HTMLDivElement>(handler);
|
|
13
|
+
return <div ref={ref}>{children}</div>;
|
|
14
|
+
};
|
|
15
|
+
const ref = render(<Component />);
|
|
16
|
+
|
|
17
|
+
// act
|
|
18
|
+
fireEvent.click(ref.container);
|
|
19
|
+
|
|
20
|
+
// verify
|
|
21
|
+
expect(handler).toHaveBeenCalledTimes(1);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should not call the handler when clicking on the element", () => {
|
|
25
|
+
// setup
|
|
26
|
+
const Component: React.FC = ({ children }) => {
|
|
27
|
+
const ref = useOnClickOutside<HTMLDivElement>(handler);
|
|
28
|
+
return <div ref={ref}>{children}</div>;
|
|
29
|
+
};
|
|
30
|
+
const mutableRef: { current: HTMLDivElement } = { current: undefined };
|
|
31
|
+
render(
|
|
32
|
+
<Component>
|
|
33
|
+
<div ref={mutableRef}></div>
|
|
34
|
+
</Component>
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
// act
|
|
38
|
+
fireEvent.click(mutableRef.current);
|
|
39
|
+
|
|
40
|
+
// verify
|
|
41
|
+
expect(handler).not.toHaveBeenCalled();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("should unregister the event listener when unmounted", () => {
|
|
45
|
+
// setup
|
|
46
|
+
const Component: React.FC = ({ children }) => {
|
|
47
|
+
const ref = useOnClickOutside<HTMLDivElement>(handler);
|
|
48
|
+
return <div ref={ref}>{children}</div>;
|
|
49
|
+
};
|
|
50
|
+
const ref = render(<Component />);
|
|
51
|
+
const spy = jest.spyOn(window, "removeEventListener");
|
|
52
|
+
|
|
53
|
+
// act
|
|
54
|
+
ref.unmount();
|
|
55
|
+
|
|
56
|
+
// verify
|
|
57
|
+
expect(spy).toHaveBeenCalledWith("click", expect.any(Function));
|
|
58
|
+
});
|
|
59
|
+
});
|