@@ -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 |
@@ -255,8 +255,9 @@ describe 'ReactCoffeeScriptClass', -> |
255 | 255 | React.createElement('span', className: @state.bar) |
256 | 256 |
|
257 | 257 | test React.createElement(Foo, initialValue: 'foo'), 'SPAN', 'bar' |
258 | | - expect(renderCount).toBe 1 |
259 | | - undefined |
| 258 | + # This is broken with deferRenderPhaseUpdateToNextBatch flag on. |
| 259 | + # We can't use the gate feature here because this test is also in CoffeeScript and TypeScript. |
| 260 | + expect(renderCount).toBe(if global.__WWW__ and !global.__VARIANT__ then 2 else 1) |
260 | 261 |
|
261 | 262 | it 'should warn with non-object in the initial state property', -> |
262 | 263 | [['an array'], 'a string', 1234].forEach (state) -> |
@@ -270,7 +271,6 @@ describe 'ReactCoffeeScriptClass', -> |
270 | 271 | expect(-> |
271 | 272 | test React.createElement(Foo), 'SPAN', '' |
272 | 273 | ).toErrorDev('Foo.state: must be set to an object or null') |
273 | | - undefined |
274 | 274 |
|
275 | 275 | it 'should render with null in the initial state property', -> |
276 | 276 | class Foo extends React.Component |
@@ -281,7 +281,6 @@ describe 'ReactCoffeeScriptClass', -> |
281 | 281 | React.createElement('span') |
282 | 282 |
|
283 | 283 | test React.createElement(Foo), 'SPAN', '' |
284 | | - undefined |
285 | 284 |
|
286 | 285 | it 'setState through an event handler', -> |
287 | 286 | class Foo extends React.Component |
@@ -298,9 +297,9 @@ describe 'ReactCoffeeScriptClass', -> |
298 | 297 | ) |
299 | 298 |
|
300 | 299 | test React.createElement(Foo, initialValue: 'foo'), 'DIV', 'foo' |
301 | | - attachedListener() |
| 300 | + act -> |
| 301 | + attachedListener() |
302 | 302 | expect(renderedName).toBe 'bar' |
303 | | - undefined |
304 | 303 |
|
305 | 304 | it 'should not implicitly bind event handlers', -> |
306 | 305 | class Foo extends React.Component |
@@ -318,7 +317,6 @@ describe 'ReactCoffeeScriptClass', -> |
318 | 317 |
|
319 | 318 | test React.createElement(Foo, initialValue: 'foo'), 'DIV', 'foo' |
320 | 319 | expect(attachedListener).toThrow() |
321 | | - undefined |
322 | 320 |
|
323 | 321 | it 'renders using forceUpdate even when there is no state', -> |
324 | 322 | class Foo extends React.Component |
@@ -336,9 +334,9 @@ describe 'ReactCoffeeScriptClass', -> |
336 | 334 | ) |
337 | 335 |
|
338 | 336 | test React.createElement(Foo, initialValue: 'foo'), 'DIV', 'foo' |
339 | | - attachedListener() |
| 337 | + act -> |
| 338 | + attachedListener() |
340 | 339 | expect(renderedName).toBe 'bar' |
341 | | - undefined |
342 | 340 |
|
343 | 341 | it 'will call all the normal life cycle methods', -> |
344 | 342 | lifeCycles = [] |
@@ -387,9 +385,9 @@ describe 'ReactCoffeeScriptClass', -> |
387 | 385 | 'did-update', { value: 'foo' }, {} |
388 | 386 | ] |
389 | 387 | lifeCycles = [] # reset |
390 | | - ReactDOM.unmountComponentAtNode container |
| 388 | + act -> |
| 389 | + root.unmount() |
391 | 390 | expect(lifeCycles).toEqual ['will-unmount'] |
392 | | - undefined |
393 | 391 |
|
394 | 392 | it 'warns when classic properties are defined on the instance, |
395 | 393 | but does not invoke them.', -> |
@@ -425,7 +423,6 @@ describe 'ReactCoffeeScriptClass', -> |
425 | 423 | ]) |
426 | 424 | expect(getInitialStateWasCalled).toBe false |
427 | 425 | expect(getDefaultPropsWasCalled).toBe false |
428 | | - undefined |
429 | 426 |
|
430 | 427 | it 'does not warn about getInitialState() on class components |
431 | 428 | if state is also defined.', -> |
@@ -443,7 +440,6 @@ describe 'ReactCoffeeScriptClass', -> |
443 | 440 | ) |
444 | 441 |
|
445 | 442 | test React.createElement(Foo), 'SPAN', 'foo' |
446 | | - undefined |
447 | 443 |
|
448 | 444 | it 'should warn when misspelling shouldComponentUpdate', -> |
449 | 445 | class NamedComponent extends React.Component |
@@ -462,7 +458,6 @@ describe 'ReactCoffeeScriptClass', -> |
462 | 458 | Did you mean shouldComponentUpdate()? The name is phrased as a |
463 | 459 | question because the function is expected to return a value.' |
464 | 460 | ) |
465 | | - undefined |
466 | 461 |
|
467 | 462 | it 'should warn when misspelling componentWillReceiveProps', -> |
468 | 463 | class NamedComponent extends React.Component |
@@ -480,7 +475,6 @@ describe 'ReactCoffeeScriptClass', -> |
480 | 475 | 'Warning: NamedComponent has a method called componentWillRecieveProps(). |
481 | 476 | Did you mean componentWillReceiveProps()?' |
482 | 477 | ) |
483 | | - undefined |
484 | 478 |
|
485 | 479 | it 'should warn when misspelling UNSAFE_componentWillReceiveProps', -> |
486 | 480 | class NamedComponent extends React.Component |
@@ -498,24 +492,22 @@ describe 'ReactCoffeeScriptClass', -> |
498 | 492 | 'Warning: NamedComponent has a method called UNSAFE_componentWillRecieveProps(). |
499 | 493 | Did you mean UNSAFE_componentWillReceiveProps()?' |
500 | 494 | ) |
501 | | - undefined |
502 | 495 |
|
503 | 496 | it 'should throw AND warn when trying to access classic APIs', -> |
504 | | - instance = |
505 | | - test React.createElement(InnerComponent, name: 'foo'), 'DIV', 'foo' |
| 497 | + ref = React.createRef() |
| 498 | + test React.createElement(InnerComponent, name: 'foo', ref: ref), 'DIV', 'foo' |
506 | 499 | expect(-> |
507 | | - expect(-> instance.replaceState {}).toThrow() |
| 500 | + expect(-> ref.current.replaceState {}).toThrow() |
508 | 501 | ).toWarnDev( |
509 | 502 | 'replaceState(...) is deprecated in plain JavaScript React classes', |
510 | 503 | {withoutStack: true} |
511 | 504 | ) |
512 | 505 | expect(-> |
513 | | - expect(-> instance.isMounted()).toThrow() |
| 506 | + expect(-> ref.current.isMounted()).toThrow() |
514 | 507 | ).toWarnDev( |
515 | 508 | 'isMounted(...) is deprecated in plain JavaScript React classes', |
516 | 509 | {withoutStack: true} |
517 | 510 | ) |
518 | | - undefined |
519 | 511 |
|
520 | 512 | it 'supports this.context passed via getChildContext', -> |
521 | 513 | class Bar extends React.Component |
@@ -533,7 +525,6 @@ describe 'ReactCoffeeScriptClass', -> |
533 | 525 | React.createElement Bar |
534 | 526 |
|
535 | 527 | test React.createElement(Foo), 'DIV', 'bar-through-context' |
536 | | - undefined |
537 | 528 |
|
538 | 529 | it 'supports classic refs', -> |
539 | 530 | class Foo extends React.Component |
@@ -543,13 +534,14 @@ describe 'ReactCoffeeScriptClass', -> |
543 | 534 | ref: 'inner' |
544 | 535 | ) |
545 | 536 |
|
546 | | - instance = test(React.createElement(Foo), 'DIV', 'foo') |
547 | | - expect(instance.refs.inner.getName()).toBe 'foo' |
548 | | - undefined |
| 537 | + ref = React.createRef() |
| 538 | + test(React.createElement(Foo, ref: ref), 'DIV', 'foo') |
| 539 | + expect(ref.current.refs.inner.getName()).toBe 'foo' |
549 | 540 |
|
550 | 541 | it 'supports drilling through to the DOM using findDOMNode', -> |
551 | | - instance = test React.createElement(InnerComponent, name: 'foo'), 'DIV', 'foo' |
552 | | - node = ReactDOM.findDOMNode(instance) |
| 542 | + ref = React.createRef() |
| 543 | + test React.createElement(InnerComponent, name: 'foo', ref: ref), 'DIV', 'foo' |
| 544 | + node = ReactDOM.findDOMNode(ref.current) |
553 | 545 | expect(node).toBe container.firstChild |
554 | | - undefined |
| 546 | + |
555 | 547 | undefined |