Shallow renderer passes context to componentWillReceiveProps (#10342)
This parameter was accidentally omitted before. Leland reported it because it impacts Enzyme. I also added a basic lifecycle parameter test for shallow renderer.
Brian Vaughn committed
Aug 2, 2017 at 08:18 UTC
2da37a8a449bfa38ef1ea9c2f3e750dac37dae9a
2 files changed
+78
-1
src/renderers/testing/ReactShallowRendererEntry.js
+1
-1
@@ -182,7 +182,7 @@ class ReactShallowRenderer {
182
oldProps !== props &&
183
typeof this._instance.componentWillReceiveProps === 'function'
184
) {
185
- this._instance.componentWillReceiveProps(props);
185
+ this._instance.componentWillReceiveProps(props, context);
186
}
187
188
// Read state after cWRP in case it calls setState
src/renderers/testing/__tests__/ReactShallowRenderer-test.js
+77
@@ -333,6 +333,83 @@ describe('ReactShallowRenderer', () => {
333
expect(result).toEqual(<div />);
334
});
335
336
+ it('passes expected params to component lifecycle methods', () => {
337
+ const componentDidUpdateParams = [];
338
+ const componentWillReceivePropsParams = [];
339
+ const componentWillUpdateParams = [];
340
+ const setStateParams = [];
341
+ const shouldComponentUpdateParams = [];
342
+
343
+ const initialProp = {prop: 'init prop'};
344
+ const initialState = {state: 'init state'};
345
+ const initialContext = {context: 'init context'};
346
+ const updatedState = {state: 'updated state'};
347
+ const updatedProp = {prop: 'updated prop'};
348
+ const updatedContext = {context: 'updated context'};
349
+
350
+ class SimpleComponent extends React.Component {
351
+ constructor(props, context) {
352
+ super(props, context);
353
+ this.state = initialState;
354
+ }
355
+ componentDidUpdate(...args) {
356
+ componentDidUpdateParams.push(...args);
357
+ }
358
+ componentWillReceiveProps(...args) {
359
+ componentWillReceivePropsParams.push(...args);
360
+ this.setState((...innerArgs) => {
361
+ setStateParams.push(...innerArgs);
362
+ return updatedState;
363
+ });
364
+ }
365
+ componentWillUpdate(...args) {
366
+ componentWillUpdateParams.push(...args);
367
+ }
368
+ shouldComponentUpdate(...args) {
369
+ shouldComponentUpdateParams.push(...args);
370
+ return true;
371
+ }
372
+ render() {
373
+ return null;
374
+ }
375
+ }
376
+
377
+ const shallowRenderer = createRenderer();
378
+
379
+ // No lifecycle hooks should be invoked on initial render
380
+ shallowRenderer.render(
381
+ React.createElement(SimpleComponent, initialProp),
382
+ initialContext,
383
+ );
384
+ expect(componentDidUpdateParams).toEqual([]);
385
+ expect(componentWillReceivePropsParams).toEqual([]);
386
+ expect(componentWillUpdateParams).toEqual([]);
387
+ expect(setStateParams).toEqual([]);
388
+ expect(shouldComponentUpdateParams).toEqual([]);
389
+
390
+ // Lifecycle hooks should be invoked with the correct prev/next params on update.
391
+ shallowRenderer.render(
392
+ React.createElement(SimpleComponent, updatedProp),
393
+ updatedContext,
394
+ );
395
+ expect(componentWillReceivePropsParams).toEqual([
396
+ updatedProp,
397
+ updatedContext,
398
+ ]);
399
+ expect(setStateParams).toEqual([initialState, initialProp]);
400
+ expect(shouldComponentUpdateParams).toEqual([
401
+ updatedProp,
402
+ updatedState,
403
+ updatedContext,
404
+ ]);
405
+ expect(componentWillUpdateParams).toEqual([
406
+ updatedProp,
407
+ updatedState,
408
+ updatedContext,
409
+ ]);
410
+ expect(componentDidUpdateParams).toEqual([initialProp, initialState]);
411
+ });
412
+
413
it('can shallowly render components with ref as function', () => {
414
class SimpleComponent extends React.Component {
415
state = {clicked: false};