@@ -13,32 +13,71 @@ jest.mock('../events/isEventSupported');
13
13
14
14
describe ( 'InvalidEventListeners' , ( ) => {
15
15
let React ;
16
- let ReactTestUtils ;
16
+ let ReactDOM ;
17
+ let container ;
17
18
18
19
beforeEach ( ( ) => {
19
20
jest . resetModules ( ) ;
20
21
React = require ( 'react' ) ;
21
- ReactTestUtils = require ( 'react-dom/test-utils' ) ;
22
+ ReactDOM = require ( 'react-dom' ) ;
23
+
24
+ container = document . createElement ( 'div' ) ;
25
+ document . body . appendChild ( container ) ;
26
+ } ) ;
27
+
28
+ afterEach ( ( ) => {
29
+ document . body . removeChild ( container ) ;
30
+ container = null ;
22
31
} ) ;
23
32
24
33
it ( 'should prevent non-function listeners, at dispatch' , ( ) => {
25
34
let node ;
26
35
expect ( ( ) => {
27
- node = ReactTestUtils . renderIntoDocument (
28
- < div onClick = "not a function" /> ,
29
- ) ;
36
+ node = ReactDOM . render ( < div onClick = "not a function" /> , container ) ;
30
37
} ) . toErrorDev (
31
38
'Expected `onClick` listener to be a function, instead got a value of `string` type.' ,
32
39
) ;
33
- expect ( ( ) => ReactTestUtils . SimulateNative . click ( node ) ) . toThrowError (
34
- 'Expected `onClick` listener to be a function, instead got a value of `string` type.' ,
40
+
41
+ spyOnProd ( console , 'error' ) ;
42
+
43
+ const uncaughtErrors = [ ] ;
44
+ function handleWindowError ( e ) {
45
+ uncaughtErrors . push ( e . error ) ;
46
+ }
47
+ window . addEventListener ( 'error' , handleWindowError ) ;
48
+ try {
49
+ node . dispatchEvent (
50
+ new MouseEvent ( 'click' , {
51
+ bubbles : true ,
52
+ } ) ,
53
+ ) ;
54
+ } finally {
55
+ window . removeEventListener ( 'error' , handleWindowError ) ;
56
+ }
57
+ expect ( uncaughtErrors . length ) . toBe ( 1 ) ;
58
+ expect ( uncaughtErrors [ 0 ] ) . toEqual (
59
+ expect . objectContaining ( {
60
+ message :
61
+ 'Expected `onClick` listener to be a function, ' +
62
+ 'instead got a value of `string` type.' ,
63
+ } ) ,
35
64
) ;
65
+
66
+ if ( ! __DEV__ ) {
67
+ expect ( console . error ) . toHaveBeenCalledTimes ( 1 ) ;
68
+ expect ( console . error . calls . argsFor ( 0 ) [ 0 ] ) . toMatch (
69
+ 'Expected `onClick` listener to be a function, ' +
70
+ 'instead got a value of `string` type.' ,
71
+ ) ;
72
+ }
36
73
} ) ;
37
74
38
75
it ( 'should not prevent null listeners, at dispatch' , ( ) => {
39
- const node = ReactTestUtils . renderIntoDocument ( < div onClick = { null } /> ) ;
40
- expect ( function ( ) {
41
- ReactTestUtils . SimulateNative . click ( node ) ;
42
- } ) . not . toThrow ( ) ;
76
+ const node = ReactDOM . render ( < div onClick = { null } /> , container ) ;
77
+ node . dispatchEvent (
78
+ new MouseEvent ( 'click' , {
79
+ bubbles : true ,
80
+ } ) ,
81
+ ) ;
43
82
} ) ;
44
83
} ) ;
0 commit comments