@@ -2,21 +2,27 @@ import { mount } from 'enzyme';
2
2
import { createElement } from 'preact' ;
3
3
import { useRef } from 'preact/hooks' ;
4
4
import { act } from 'preact/test-utils' ;
5
- import propTypes from 'prop-types' ;
6
5
7
6
import useElementShouldClose from '../use-element-should-close' ;
8
7
9
- describe ( 'hooks. useElementShouldClose' , ( ) => {
8
+ describe ( 'useElementShouldClose' , ( ) => {
10
9
let handleClose ;
11
- let e ;
10
+
11
+ const createEvent = ( name , props ) => {
12
+ const event = new Event ( name ) ;
13
+ Object . assign ( event , props ) ;
14
+ return event ;
15
+ } ;
16
+
12
17
const events = [
13
18
new Event ( 'mousedown' ) ,
14
19
new Event ( 'click' ) ,
15
- ( ( e = new Event ( 'keydown' ) ) , ( e . key = 'Escape' ) , e ) ,
20
+ createEvent ( 'keydown' , { key : 'Escape' } ) ,
16
21
new Event ( 'focus' ) ,
17
22
] ;
18
23
19
24
// Create a fake component to mount in tests that uses the hook
25
+ // eslint-disable-next-line react/prop-types
20
26
function FakeComponent ( { isOpen = true } ) {
21
27
const myRef = useRef ( ) ;
22
28
useElementShouldClose ( myRef , isOpen , handleClose ) ;
@@ -27,103 +33,62 @@ describe('hooks.useElementShouldClose', () => {
27
33
) ;
28
34
}
29
35
30
- FakeComponent . propTypes = {
31
- isOpen : propTypes . bool ,
32
- } ;
33
-
34
- // Tests useElementShouldClose on a custom component directly
35
- function FakeCompoundComponent ( { isOpen = true } ) {
36
- function FakeCustomComponent ( ) {
37
- return (
38
- < div >
39
- < button > Hi</ button >
40
- </ div >
41
- ) ;
42
- }
43
- const myRef = useRef ( ) ;
44
- useElementShouldClose ( myRef , isOpen , handleClose ) ;
45
- return < FakeCustomComponent ref = { myRef } /> ;
46
- }
47
-
48
- FakeCompoundComponent . propTypes = {
49
- isOpen : propTypes . bool ,
50
- } ;
51
-
52
36
function createComponent ( props ) {
53
37
return mount ( < FakeComponent isOpen = { true } { ...props } /> ) ;
54
38
}
55
39
56
- function createCompoundComponent ( props ) {
57
- return mount ( < FakeCompoundComponent isOpen = { true } { ...props } /> ) ;
58
- }
59
-
60
40
beforeEach ( ( ) => {
61
41
handleClose = sinon . stub ( ) ;
62
42
} ) ;
63
43
64
- // Run each set of tests twice, once for a regular node and a second
65
- // time for a custom preact component
66
- [
67
- {
68
- createWrapper : createComponent ,
69
- description : 'useElementShouldClose attached to a html node' ,
70
- } ,
71
- {
72
- createWrapper : createCompoundComponent ,
73
- description : 'useElementShouldClose attached to a preact component' ,
74
- } ,
75
- ] . forEach ( test => {
76
- context ( test . description , ( ) => {
77
- events . forEach ( event => {
78
- it ( `should invoke close callback once for events outside of element (${ event . type } )` , ( ) => {
79
- const wrapper = test . createWrapper ( ) ;
80
-
81
- act ( ( ) => {
82
- document . body . dispatchEvent ( event ) ;
83
- } ) ;
84
- wrapper . update ( ) ;
85
-
86
- assert . calledOnce ( handleClose ) ;
87
-
88
- // Update the component to change it and re-execute the hook
89
- wrapper . setProps ( { isOpen : false } ) ;
90
-
91
- act ( ( ) => {
92
- document . body . dispatchEvent ( event ) ;
93
- } ) ;
94
-
95
- // Cleanup of hook should have removed eventListeners, so the callback
96
- // is not called again
97
- assert . calledOnce ( handleClose ) ;
98
- } ) ;
44
+ events . forEach ( event => {
45
+ it ( `should invoke close callback once for events outside of element (${ event . type } )` , ( ) => {
46
+ const wrapper = createComponent ( ) ;
47
+
48
+ act ( ( ) => {
49
+ document . body . dispatchEvent ( event ) ;
99
50
} ) ;
51
+ wrapper . update ( ) ;
100
52
101
- events . forEach ( event => {
102
- it ( `should not invoke close callback on events outside of element if element closed (${ event . type } )` , ( ) => {
103
- const wrapper = test . createWrapper ( { isOpen : false } ) ;
53
+ assert . calledOnce ( handleClose ) ;
104
54
105
- act ( ( ) => {
106
- document . body . dispatchEvent ( event ) ;
107
- } ) ;
108
- wrapper . update ( ) ;
55
+ // Update the component to change it and re-execute the hook
56
+ wrapper . setProps ( { isOpen : false } ) ;
109
57
110
- assert . equal ( handleClose . callCount , 0 ) ;
111
- } ) ;
58
+ act ( ( ) => {
59
+ document . body . dispatchEvent ( event ) ;
112
60
} ) ;
113
61
114
- events . forEach ( event => {
115
- it ( `should not invoke close callback on events inside of element (${ event . type } )` , ( ) => {
116
- const wrapper = test . createWrapper ( ) ;
117
- const button = wrapper . find ( 'button' ) ;
62
+ // Cleanup of hook should have removed eventListeners, so the callback
63
+ // is not called again
64
+ assert . calledOnce ( handleClose ) ;
65
+ } ) ;
66
+ } ) ;
118
67
119
- act ( ( ) => {
120
- button . getDOMNode ( ) . dispatchEvent ( event ) ;
121
- } ) ;
122
- wrapper . update ( ) ;
68
+ events . forEach ( event => {
69
+ it ( `should not invoke close callback on events outside of element if element closed (${ event . type } )` , ( ) => {
70
+ const wrapper = createComponent ( { isOpen : false } ) ;
123
71
124
- assert . equal ( handleClose . callCount , 0 ) ;
125
- } ) ;
72
+ act ( ( ) => {
73
+ document . body . dispatchEvent ( event ) ;
126
74
} ) ;
75
+ wrapper . update ( ) ;
76
+
77
+ assert . equal ( handleClose . callCount , 0 ) ;
78
+ } ) ;
79
+ } ) ;
80
+
81
+ events . forEach ( event => {
82
+ it ( `should not invoke close callback on events inside of element (${ event . type } )` , ( ) => {
83
+ const wrapper = createComponent ( ) ;
84
+ const button = wrapper . find ( 'button' ) ;
85
+
86
+ act ( ( ) => {
87
+ button . getDOMNode ( ) . dispatchEvent ( event ) ;
88
+ } ) ;
89
+ wrapper . update ( ) ;
90
+
91
+ assert . equal ( handleClose . callCount , 0 ) ;
127
92
} ) ;
128
93
} ) ;
129
94
} ) ;
0 commit comments