@@ -5,21 +5,25 @@ This source code is licensed under the MIT license found in the
5
5
LICENSE file in the root directory of this source tree.
6
6
###
7
7
8
+ PropTypes = null
8
9
React = null
9
10
ReactDOM = null
10
- PropTypes = null
11
+ act = null
11
12
12
13
describe ' ReactCoffeeScriptClass' , ->
13
14
container = null
15
+ root = null
14
16
InnerComponent = null
15
17
attachedListener = null ;
16
18
renderedName = null ;
17
19
18
20
beforeEach ->
19
21
React = require ' react'
20
22
ReactDOM = require ' react-dom'
23
+ act = require (' react-dom/test-utils' ).act
21
24
PropTypes = require ' prop-types'
22
25
container = document .createElement ' div'
26
+ root = ReactDOM .createRoot container
23
27
attachedListener = null
24
28
renderedName = null
25
29
InnerComponent = class extends React.Component
@@ -30,11 +34,11 @@ describe 'ReactCoffeeScriptClass', ->
30
34
return React .createElement (' div' , className : this .props .name )
31
35
32
36
test = (element , expectedTag , expectedClassName ) ->
33
- instance = ReactDOM .render (element, container)
37
+ act ->
38
+ root .render (element)
34
39
expect (container .firstChild ).not .toBeNull ()
35
40
expect (container .firstChild .tagName ).toBe (expectedTag)
36
41
expect (container .firstChild .className ).toBe (expectedClassName)
37
- instance;
38
42
39
43
it ' preserves the name of the class for use in error messages' , ->
40
44
class Foo extends React.Component
@@ -44,14 +48,16 @@ describe 'ReactCoffeeScriptClass', ->
44
48
class Foo extends React.Component
45
49
expect (->
46
50
expect (->
47
- ReactDOM .render React .createElement (Foo), container
51
+ act ->
52
+ root .render React .createElement (Foo)
48
53
).toThrow ()
49
54
).toErrorDev ([
50
- # A failed component renders twice in DEV
55
+ # A failed component renders four times in DEV in concurrent mode
56
+ ' No `render` method found on the returned component instance' ,
57
+ ' No `render` method found on the returned component instance' ,
51
58
' No `render` method found on the returned component instance' ,
52
59
' No `render` method found on the returned component instance' ,
53
60
])
54
- undefined
55
61
56
62
it ' renders a simple stateless component with prop' , ->
57
63
class Foo extends React.Component
@@ -62,7 +68,6 @@ describe 'ReactCoffeeScriptClass', ->
62
68
63
69
test React .createElement (Foo, bar : ' foo' ), ' DIV' , ' foo'
64
70
test React .createElement (Foo, bar : ' bar' ), ' DIV' , ' bar'
65
- undefined
66
71
67
72
it ' renders based on state using initial values in this.props' , ->
68
73
class Foo extends React.Component
@@ -76,7 +81,6 @@ describe 'ReactCoffeeScriptClass', ->
76
81
)
77
82
78
83
test React .createElement (Foo, initialValue : ' foo' ), ' SPAN' , ' foo'
79
- undefined
80
84
81
85
it ' renders based on state using props in the constructor' , ->
82
86
class Foo extends React.Component
@@ -95,10 +99,10 @@ describe 'ReactCoffeeScriptClass', ->
95
99
className : @state .bar
96
100
)
97
101
98
- instance = test React .createElement (Foo, initialValue : ' foo' ), ' DIV' , ' foo'
99
- instance .changeState ()
102
+ ref = React .createRef ()
103
+ test React .createElement (Foo, initialValue : ' foo' , ref : ref), ' DIV' , ' foo'
104
+ ref .current .changeState ()
100
105
test React .createElement (Foo), ' SPAN' , ' bar'
101
- undefined
102
106
103
107
it ' sets initial state with value returned by static getDerivedStateFromProps' , ->
104
108
class Foo extends React.Component
@@ -115,7 +119,6 @@ describe 'ReactCoffeeScriptClass', ->
115
119
bar : ' bar'
116
120
}
117
121
test React .createElement (Foo, foo : ' foo' ), ' DIV' , ' foo bar'
118
- undefined
119
122
120
123
it ' warns if getDerivedStateFromProps is not static' , ->
121
124
class Foo extends React.Component
@@ -124,9 +127,9 @@ describe 'ReactCoffeeScriptClass', ->
124
127
getDerivedStateFromProps : ->
125
128
{}
126
129
expect (->
127
- ReactDOM .render (React .createElement (Foo, foo : ' foo' ), container)
130
+ act ->
131
+ root .render React .createElement (Foo, foo : ' foo' )
128
132
).toErrorDev ' Foo: getDerivedStateFromProps() is defined as an instance method and will be ignored. Instead, declare it as a static method.'
129
- undefined
130
133
131
134
it ' warns if getDerivedStateFromError is not static' , ->
132
135
class Foo extends React.Component
@@ -135,9 +138,9 @@ describe 'ReactCoffeeScriptClass', ->
135
138
getDerivedStateFromError : ->
136
139
{}
137
140
expect (->
138
- ReactDOM .render (React .createElement (Foo, foo : ' foo' ), container)
141
+ act ->
142
+ root .render React .createElement (Foo, foo : ' foo' )
139
143
).toErrorDev ' Foo: getDerivedStateFromError() is defined as an instance method and will be ignored. Instead, declare it as a static method.'
140
- undefined
141
144
142
145
it ' warns if getSnapshotBeforeUpdate is static' , ->
143
146
class Foo extends React.Component
@@ -146,9 +149,9 @@ describe 'ReactCoffeeScriptClass', ->
146
149
Foo .getSnapshotBeforeUpdate = () ->
147
150
{}
148
151
expect (->
149
- ReactDOM .render (React .createElement (Foo, foo : ' foo' ), container)
152
+ act ->
153
+ root .render React .createElement (Foo, foo : ' foo' )
150
154
).toErrorDev ' Foo: getSnapshotBeforeUpdate() is defined as a static method and will be ignored. Instead, declare it as an instance method.'
151
- undefined
152
155
153
156
it ' warns if state not initialized before static getDerivedStateFromProps' , ->
154
157
class Foo extends React.Component
@@ -162,14 +165,14 @@ describe 'ReactCoffeeScriptClass', ->
162
165
bar : ' bar'
163
166
}
164
167
expect (->
165
- ReactDOM .render (React .createElement (Foo, foo : ' foo' ), container)
168
+ act ->
169
+ root .render React .createElement (Foo, foo : ' foo' )
166
170
).toErrorDev (
167
171
' `Foo` uses `getDerivedStateFromProps` but its initial state is ' +
168
172
' undefined. This is not recommended. Instead, define the initial state by ' +
169
173
' assigning an object to `this.state` in the constructor of `Foo`. ' +
170
174
' This ensures that `getDerivedStateFromProps` arguments have a consistent shape.'
171
175
)
172
- undefined
173
176
174
177
it ' updates initial state with values returned by static getDerivedStateFromProps' , ->
175
178
class Foo extends React.Component
@@ -187,7 +190,6 @@ describe 'ReactCoffeeScriptClass', ->
187
190
foo : " not-#{ prevState .foo } "
188
191
}
189
192
test React .createElement (Foo), ' DIV' , ' not-foo bar'
190
- undefined
191
193
192
194
it ' renders updated state with values returned by static getDerivedStateFromProps' , ->
193
195
class Foo extends React.Component
@@ -207,7 +209,6 @@ describe 'ReactCoffeeScriptClass', ->
207
209
return null
208
210
test React .createElement (Foo, update : false ), ' DIV' , ' initial'
209
211
test React .createElement (Foo, update : true ), ' DIV' , ' updated'
210
- undefined
211
212
212
213
it ' renders based on context in the constructor' , ->
213
214
class Foo extends React.Component
@@ -239,7 +240,6 @@ describe 'ReactCoffeeScriptClass', ->
239
240
React .createElement Foo
240
241
241
242
test React .createElement (Outer), ' SPAN' , ' foo'
242
- undefined
243
243
244
244
it ' renders only once when setting state in componentWillMount' , ->
245
245
renderCount = 0
@@ -256,7 +256,6 @@ describe 'ReactCoffeeScriptClass', ->
256
256
257
257
test React .createElement (Foo, initialValue : ' foo' ), ' SPAN' , ' bar'
258
258
expect (renderCount).toBe 1
259
- undefined
260
259
261
260
it ' should warn with non-object in the initial state property' , ->
262
261
[[' an array' ], ' a string' , 1234 ].forEach (state) ->
@@ -270,7 +269,6 @@ describe 'ReactCoffeeScriptClass', ->
270
269
expect (->
271
270
test React .createElement (Foo), ' SPAN' , ' '
272
271
).toErrorDev (' Foo.state: must be set to an object or null' )
273
- undefined
274
272
275
273
it ' should render with null in the initial state property' , ->
276
274
class Foo extends React.Component
@@ -281,7 +279,6 @@ describe 'ReactCoffeeScriptClass', ->
281
279
React .createElement (' span' )
282
280
283
281
test React .createElement (Foo), ' SPAN' , ' '
284
- undefined
285
282
286
283
it ' setState through an event handler' , ->
287
284
class Foo extends React.Component
@@ -298,9 +295,9 @@ describe 'ReactCoffeeScriptClass', ->
298
295
)
299
296
300
297
test React .createElement (Foo, initialValue : ' foo' ), ' DIV' , ' foo'
301
- attachedListener ()
298
+ act ->
299
+ attachedListener ()
302
300
expect (renderedName).toBe ' bar'
303
- undefined
304
301
305
302
it ' should not implicitly bind event handlers' , ->
306
303
class Foo extends React.Component
@@ -318,7 +315,6 @@ describe 'ReactCoffeeScriptClass', ->
318
315
319
316
test React .createElement (Foo, initialValue : ' foo' ), ' DIV' , ' foo'
320
317
expect (attachedListener).toThrow ()
321
- undefined
322
318
323
319
it ' renders using forceUpdate even when there is no state' , ->
324
320
class Foo extends React.Component
@@ -336,9 +332,9 @@ describe 'ReactCoffeeScriptClass', ->
336
332
)
337
333
338
334
test React .createElement (Foo, initialValue : ' foo' ), ' DIV' , ' foo'
339
- attachedListener ()
335
+ act ->
336
+ attachedListener ()
340
337
expect (renderedName).toBe ' bar'
341
- undefined
342
338
343
339
it ' will call all the normal life cycle methods' , ->
344
340
lifeCycles = []
@@ -387,9 +383,9 @@ describe 'ReactCoffeeScriptClass', ->
387
383
' did-update' , { value : ' foo' }, {}
388
384
]
389
385
lifeCycles = [] # reset
390
- ReactDOM .unmountComponentAtNode container
386
+ act ->
387
+ root .unmount ()
391
388
expect (lifeCycles).toEqual [' will-unmount' ]
392
- undefined
393
389
394
390
it ' warns when classic properties are defined on the instance,
395
391
but does not invoke them.' , ->
@@ -425,7 +421,6 @@ describe 'ReactCoffeeScriptClass', ->
425
421
])
426
422
expect (getInitialStateWasCalled).toBe false
427
423
expect (getDefaultPropsWasCalled).toBe false
428
- undefined
429
424
430
425
it ' does not warn about getInitialState() on class components
431
426
if state is also defined.' , ->
@@ -443,7 +438,6 @@ describe 'ReactCoffeeScriptClass', ->
443
438
)
444
439
445
440
test React .createElement (Foo), ' SPAN' , ' foo'
446
- undefined
447
441
448
442
it ' should warn when misspelling shouldComponentUpdate' , ->
449
443
class NamedComponent extends React.Component
@@ -462,7 +456,6 @@ describe 'ReactCoffeeScriptClass', ->
462
456
Did you mean shouldComponentUpdate()? The name is phrased as a
463
457
question because the function is expected to return a value.'
464
458
)
465
- undefined
466
459
467
460
it ' should warn when misspelling componentWillReceiveProps' , ->
468
461
class NamedComponent extends React.Component
@@ -480,7 +473,6 @@ describe 'ReactCoffeeScriptClass', ->
480
473
' Warning: NamedComponent has a method called componentWillRecieveProps().
481
474
Did you mean componentWillReceiveProps()?'
482
475
)
483
- undefined
484
476
485
477
it ' should warn when misspelling UNSAFE_componentWillReceiveProps' , ->
486
478
class NamedComponent extends React.Component
@@ -498,24 +490,22 @@ describe 'ReactCoffeeScriptClass', ->
498
490
' Warning: NamedComponent has a method called UNSAFE_componentWillRecieveProps().
499
491
Did you mean UNSAFE_componentWillReceiveProps()?'
500
492
)
501
- undefined
502
493
503
494
it ' should throw AND warn when trying to access classic APIs' , ->
504
- instance =
505
- test React .createElement (InnerComponent, name : ' foo' ), ' DIV' , ' foo'
495
+ ref = React . createRef ()
496
+ test React .createElement (InnerComponent, name : ' foo' , ref : ref ), ' DIV' , ' foo'
506
497
expect (->
507
- expect (-> instance .replaceState {}).toThrow ()
498
+ expect (-> ref . current .replaceState {}).toThrow ()
508
499
).toWarnDev (
509
500
' replaceState(...) is deprecated in plain JavaScript React classes' ,
510
501
{withoutStack : true }
511
502
)
512
503
expect (->
513
- expect (-> instance .isMounted ()).toThrow ()
504
+ expect (-> ref . current .isMounted ()).toThrow ()
514
505
).toWarnDev (
515
506
' isMounted(...) is deprecated in plain JavaScript React classes' ,
516
507
{withoutStack : true }
517
508
)
518
- undefined
519
509
520
510
it ' supports this.context passed via getChildContext' , ->
521
511
class Bar extends React.Component
@@ -533,7 +523,6 @@ describe 'ReactCoffeeScriptClass', ->
533
523
React .createElement Bar
534
524
535
525
test React .createElement (Foo), ' DIV' , ' bar-through-context'
536
- undefined
537
526
538
527
it ' supports classic refs' , ->
539
528
class Foo extends React.Component
@@ -543,13 +532,14 @@ describe 'ReactCoffeeScriptClass', ->
543
532
ref : ' inner'
544
533
)
545
534
546
- instance = test ( React .createElement (Foo), ' DIV ' , ' foo ' )
547
- expect ( instance . refs . inner . getName ()). toBe ' foo'
548
- undefined
535
+ ref = React .createRef ( )
536
+ test ( React . createElement (Foo, ref : ref), ' DIV ' , ' foo' )
537
+ expect ( ref . current . refs . inner . getName ()). toBe ' foo '
549
538
550
539
it ' supports drilling through to the DOM using findDOMNode' , ->
551
- instance = test React .createElement (InnerComponent, name : ' foo' ), ' DIV' , ' foo'
552
- node = ReactDOM .findDOMNode (instance)
540
+ ref = React .createRef ()
541
+ test React .createElement (InnerComponent, name : ' foo' , ref : ref), ' DIV' , ' foo'
542
+ node = ReactDOM .findDOMNode (ref .current )
553
543
expect (node).toBe container .firstChild
554
- undefined
544
+
555
545
undefined
0 commit comments