@samitouri / QOS-React / commits / 8c20615b06

Removed dev warnings from shallow renderer. (#12433)

Brian Vaughn committed Mar 22, 2018 at 11:32 UTC 8c20615b06d22e023ff54a30e2602f0409876441
3 files changed +1 -418
packages/react-test-renderer/src/ReactShallowRenderer.js
-183
@@ -8,33 +8,12 @@
8
9 import React from 'react';
10 import {isForwardRef} from 'react-is';
11 -import {warnAboutDeprecatedLifecycles} from 'shared/ReactFeatureFlags';
11 import describeComponentFrame from 'shared/describeComponentFrame';
12 import getComponentName from 'shared/getComponentName';
13 import emptyObject from 'fbjs/lib/emptyObject';
14 import invariant from 'fbjs/lib/invariant';
16 -import lowPriorityWarning from 'shared/lowPriorityWarning';
15 import shallowEqual from 'fbjs/lib/shallowEqual';
16 import checkPropTypes from 'prop-types/checkPropTypes';
19 -import warning from 'fbjs/lib/warning';
20 -
21 -let didWarnAboutLegacyWillMount;
22 -let didWarnAboutLegacyWillReceiveProps;
23 -let didWarnAboutLegacyWillUpdate;
24 -let didWarnAboutUndefinedDerivedState;
25 -let didWarnAboutUninitializedState;
26 -let didWarnAboutLegacyLifecyclesAndDerivedState;
27 -
28 -if (__DEV__) {
29 - if (warnAboutDeprecatedLifecycles) {
30 - didWarnAboutLegacyWillMount = {};
31 - didWarnAboutLegacyWillReceiveProps = {};
32 - didWarnAboutLegacyWillUpdate = {};
33 - }
34 - didWarnAboutUndefinedDerivedState = {};
35 - didWarnAboutUninitializedState = {};
36 - didWarnAboutLegacyLifecyclesAndDerivedState = {};
37 -}
17
18 class ReactShallowRenderer {
19 static createRenderer = function() {
@@ -106,28 +85,6 @@ class ReactShallowRenderer {
85 this._updater,
86 );
87
109 - if (__DEV__) {
110 - if (typeof element.type.getDerivedStateFromProps === 'function') {
111 - if (
112 - this._instance.state === null ||
113 - this._instance.state === undefined
114 - ) {
115 - const componentName =
116 - getName(element.type, this._instance) || 'Unknown';
117 - if (!didWarnAboutUninitializedState[componentName]) {
118 - warning(
119 - false,
120 - '%s: Did not properly initialize state during construction. ' +
121 - 'Expected state to be an object, but it was %s.',
122 - componentName,
123 - this._instance.state === null ? 'null' : 'undefined',
124 - );
125 - didWarnAboutUninitializedState[componentName] = true;
126 - }
127 - }
128 - }
129 - }
130 -
88 this._updateStateFromStaticLifecycle(element.props);
89
90 if (element.type.hasOwnProperty('contextTypes')) {
@@ -183,31 +140,6 @@ class ReactShallowRenderer {
140 const beforeState = this._newState;
141
142 if (typeof this._instance.componentWillMount === 'function') {
186 - if (__DEV__) {
187 - // Don't warn about react-lifecycles-compat polyfilled components
188 - if (
189 - warnAboutDeprecatedLifecycles &&
190 - this._instance.componentWillMount.__suppressDeprecationWarning !==
191 - true
192 - ) {
193 - const componentName = getName(element.type, this._instance);
194 - if (!didWarnAboutLegacyWillMount[componentName]) {
195 - lowPriorityWarning(
196 - false,
197 - '%s: componentWillMount() is deprecated and will be ' +
198 - 'removed in the next major version. Read about the motivations ' +
199 - 'behind this change: ' +
200 - 'https://fb.me/react-async-component-lifecycle-hooks' +
201 - '\n\n' +
202 - 'As a temporary workaround, you can rename to ' +
203 - 'UNSAFE_componentWillMount instead.',
204 - componentName,
205 - );
206 - didWarnAboutLegacyWillMount[componentName] = true;
207 - }
208 - }
209 - }
210 -
143 // In order to support react-lifecycles-compat polyfilled components,
144 // Unsafe lifecycles should not be invoked for any component with the new gDSFP.
145 if (typeof element.type.getDerivedStateFromProps !== 'function') {
@@ -242,26 +174,6 @@ class ReactShallowRenderer {
174
175 if (oldProps !== props) {
176 if (typeof this._instance.componentWillReceiveProps === 'function') {
245 - if (__DEV__) {
246 - if (warnAboutDeprecatedLifecycles) {
247 - const componentName = getName(element.type, this._instance);
248 - if (!didWarnAboutLegacyWillReceiveProps[componentName]) {
249 - lowPriorityWarning(
250 - false,
251 - '%s: componentWillReceiveProps() is deprecated and ' +
252 - 'will be removed in the next major version. Use ' +
253 - 'static getDerivedStateFromProps() instead. Read about the ' +
254 - 'motivations behind this change: ' +
255 - 'https://fb.me/react-async-component-lifecycle-hooks' +
256 - '\n\n' +
257 - 'As a temporary workaround, you can rename to ' +
258 - 'UNSAFE_componentWillReceiveProps instead.',
259 - componentName,
260 - );
261 - didWarnAboutLegacyWillReceiveProps[componentName] = true;
262 - }
263 - }
264 - }
177 // In order to support react-lifecycles-compat polyfilled components,
178 // Unsafe lifecycles should not be invoked for any component with the new gDSFP.
179 if (typeof element.type.getDerivedStateFromProps !== 'function') {
@@ -300,26 +212,6 @@ class ReactShallowRenderer {
212
213 if (shouldUpdate) {
214 if (typeof this._instance.componentWillUpdate === 'function') {
303 - if (__DEV__) {
304 - if (warnAboutDeprecatedLifecycles) {
305 - const componentName = getName(element.type, this._instance);
306 - if (!didWarnAboutLegacyWillUpdate[componentName]) {
307 - lowPriorityWarning(
308 - false,
309 - '%s: componentWillUpdate() is deprecated and will be ' +
310 - 'removed in the next major version. Read about the motivations ' +
311 - 'behind this change: ' +
312 - 'https://fb.me/react-async-component-lifecycle-hooks' +
313 - '\n\n' +
314 - 'As a temporary workaround, you can rename to ' +
315 - 'UNSAFE_componentWillUpdate instead.',
316 - componentName,
317 - );
318 - didWarnAboutLegacyWillUpdate[componentName] = true;
319 - }
320 - }
321 - }
322 -
215 // In order to support react-lifecycles-compat polyfilled components,
216 // Unsafe lifecycles should not be invoked for any component with the new gDSFP.
217 if (typeof type.getDerivedStateFromProps !== 'function') {
@@ -351,87 +243,12 @@ class ReactShallowRenderer {
243 const {type} = this._element;
244
245 if (typeof type.getDerivedStateFromProps === 'function') {
354 - if (__DEV__) {
355 - const instance = this._instance;
356 -
357 - // If getDerivedStateFromProps() is defined, "unsafe" lifecycles won't be called.
358 - // Warn about these lifecycles if they are present.
359 - // Don't warn about react-lifecycles-compat polyfilled methods though.
360 - let foundWillMountName = null;
361 - let foundWillReceivePropsName = null;
362 - let foundWillUpdateName = null;
363 - if (
364 - typeof instance.componentWillMount === 'function' &&
365 - instance.componentWillMount.__suppressDeprecationWarning !== true
366 - ) {
367 - foundWillMountName = 'componentWillMount';
368 - } else if (typeof instance.UNSAFE_componentWillMount === 'function') {
369 - foundWillMountName = 'UNSAFE_componentWillMount';
370 - }
371 - if (
372 - typeof instance.componentWillReceiveProps === 'function' &&
373 - instance.componentWillReceiveProps.__suppressDeprecationWarning !==
374 - true
375 - ) {
376 - foundWillReceivePropsName = 'componentWillReceiveProps';
377 - } else if (
378 - typeof instance.UNSAFE_componentWillReceiveProps === 'function'
379 - ) {
380 - foundWillReceivePropsName = 'UNSAFE_componentWillReceiveProps';
381 - }
382 - if (typeof instance.componentWillUpdate === 'function') {
383 - foundWillUpdateName = 'componentWillUpdate';
384 - } else if (typeof instance.UNSAFE_componentWillUpdate === 'function') {
385 - foundWillUpdateName = 'UNSAFE_componentWillUpdate';
386 - }
387 - if (
388 - foundWillMountName !== null ||
389 - foundWillReceivePropsName !== null ||
390 - foundWillUpdateName !== null
391 - ) {
392 - const componentName = getName(type, instance) || 'Component';
393 - if (!didWarnAboutLegacyLifecyclesAndDerivedState[componentName]) {
394 - warning(
395 - false,
396 - 'Unsafe legacy lifecycles will not be called for components using ' +
397 - 'the new getDerivedStateFromProps() API.\n\n' +
398 - '%s uses getDerivedStateFromProps() but also contains the following legacy lifecycles:' +
399 - '%s%s%s\n\n' +
400 - 'The above lifecycles should be removed. Learn more about this warning here:\n' +
401 - 'https://fb.me/react-async-component-lifecycle-hooks',
402 - componentName,
403 - foundWillMountName !== null ? `\n ${foundWillMountName}` : '',
404 - foundWillReceivePropsName !== null
405 - ? `\n ${foundWillReceivePropsName}`
406 - : '',
407 - foundWillUpdateName !== null ? `\n ${foundWillUpdateName}` : '',
408 - );
409 - didWarnAboutLegacyLifecyclesAndDerivedState[componentName] = true;
410 - }
411 - }
412 - }
413 -
246 const partialState = type.getDerivedStateFromProps.call(
247 null,
248 props,
249 this._instance.state,
250 );
251
420 - if (__DEV__) {
421 - if (partialState === undefined) {
422 - const componentName = getName(type, this._instance);
423 - if (!didWarnAboutUndefinedDerivedState[componentName]) {
424 - warning(
425 - false,
426 - '%s.getDerivedStateFromProps(): A valid state object (or null) must be returned. ' +
427 - 'You have returned undefined.',
428 - componentName,
429 - );
430 - didWarnAboutUndefinedDerivedState[componentName] = componentName;
431 - }
432 - }
433 - }
434 -
252 if (partialState != null) {
253 const oldState = this._newState || this._instance.state;
254 const newState = Object.assign({}, oldState, partialState);
packages/react-test-renderer/src/__tests__/ReactShallowRenderer-test.internal.js deleted
-82
@@ -1,82 +0,0 @@
1 -/**
2 - * Copyright (c) 2013-present, Facebook, Inc.
3 - *
4 - * This source code is licensed under the MIT license found in the
5 - * LICENSE file in the root directory of this source tree.
6 - *
7 - * @emails react-core
8 - * @jest-environment node
9 - */
10 -
11 -'use strict';
12 -
13 -let createRenderer;
14 -let React;
15 -let ReactFeatureFlags;
16 -
17 -describe('ReactShallowRenderer', () => {
18 - beforeEach(() => {
19 - ReactFeatureFlags = require('shared/ReactFeatureFlags');
20 - ReactFeatureFlags.warnAboutDeprecatedLifecycles = true;
21 -
22 - createRenderer = require('react-test-renderer/shallow').createRenderer;
23 - React = require('react');
24 - });
25 -
26 - afterEach(() => {
27 - jest.resetModules();
28 - });
29 -
30 - // TODO (RFC #6) Merge this back into ReactShallowRenderer-test once
31 - // the 'warnAboutDeprecatedLifecycles' feature flag has been removed.
32 - it('should warn if deprecated lifecycles exist', () => {
33 - class ComponentWithWarnings extends React.Component {
34 - componentWillReceiveProps() {}
35 - componentWillMount() {}
36 - componentWillUpdate() {}
37 - render() {
38 - return null;
39 - }
40 - }
41 -
42 - const shallowRenderer = createRenderer();
43 - expect(() =>
44 - shallowRenderer.render(<ComponentWithWarnings />),
45 - ).toLowPriorityWarnDev(
46 - 'Warning: ComponentWithWarnings: componentWillMount() is deprecated and will ' +
47 - 'be removed in the next major version.',
48 - );
49 - expect(() =>
50 - shallowRenderer.render(<ComponentWithWarnings />),
51 - ).toLowPriorityWarnDev([
52 - 'Warning: ComponentWithWarnings: componentWillReceiveProps() is deprecated ' +
53 - 'and will be removed in the next major version.',
54 - 'Warning: ComponentWithWarnings: componentWillUpdate() is deprecated and will ' +
55 - 'be removed in the next major version.',
56 - ]);
57 -
58 - // Verify no duplicate warnings
59 - shallowRenderer.render(<ComponentWithWarnings />);
60 - });
61 -
62 - describe('react-lifecycles-compat', () => {
63 - const polyfill = require('react-lifecycles-compat');
64 -
65 - it('should not warn about deprecated cWM/cWRP for polyfilled components', () => {
66 - class PolyfilledComponent extends React.Component {
67 - state = {};
68 - static getDerivedStateFromProps() {
69 - return null;
70 - }
71 - render() {
72 - return null;
73 - }
74 - }
75 -
76 - polyfill(PolyfilledComponent);
77 -
78 - const shallowRenderer = createRenderer();
79 - shallowRenderer.render(<PolyfilledComponent />);
80 - });
81 - });
82 -});
packages/react-test-renderer/src/__tests__/ReactShallowRenderer-test.js
+1 -153
@@ -125,99 +125,7 @@ describe('ReactShallowRenderer', () => {
125 }
126
127 const shallowRenderer = createRenderer();
128 - expect(() => shallowRenderer.render(<Component />)).toWarnDev(
129 - 'Unsafe legacy lifecycles will not be called for components using the new getDerivedStateFromProps() API.',
130 - );
131 - });
132 -
133 - it('should warn about deprecated lifecycles (cWM/cWRP/cWU) if new static gDSFP is present', () => {
134 - let shallowRenderer;
135 -
136 - class AllLegacyLifecycles extends React.Component {
137 - state = {};
138 - static getDerivedStateFromProps() {
139 - return null;
140 - }
141 - componentWillMount() {}
142 - UNSAFE_componentWillReceiveProps() {}
143 - componentWillUpdate() {}
144 - render() {
145 - return null;
146 - }
147 - }
148 -
149 - shallowRenderer = createRenderer();
150 - expect(() => shallowRenderer.render(<AllLegacyLifecycles />)).toWarnDev(
151 - 'Unsafe legacy lifecycles will not be called for components using the new getDerivedStateFromProps() API.\n\n' +
152 - 'AllLegacyLifecycles uses getDerivedStateFromProps() but also contains the following legacy lifecycles:\n' +
153 - ' componentWillMount\n' +
154 - ' UNSAFE_componentWillReceiveProps\n' +
155 - ' componentWillUpdate\n\n' +
156 - 'The above lifecycles should be removed. Learn more about this warning here:\n' +
157 - 'https://fb.me/react-async-component-lifecycle-hooks',
158 - );
159 -
160 - class WillMount extends React.Component {
161 - state = {};
162 - static getDerivedStateFromProps() {
163 - return null;
164 - }
165 - UNSAFE_componentWillMount() {}
166 - render() {
167 - return null;
168 - }
169 - }
170 -
171 - shallowRenderer = createRenderer();
172 - expect(() => shallowRenderer.render(<WillMount />)).toWarnDev(
173 - 'Unsafe legacy lifecycles will not be called for components using the new getDerivedStateFromProps() API.\n\n' +
174 - 'WillMount uses getDerivedStateFromProps() but also contains the following legacy lifecycles:\n' +
175 - ' UNSAFE_componentWillMount\n\n' +
176 - 'The above lifecycles should be removed. Learn more about this warning here:\n' +
177 - 'https://fb.me/react-async-component-lifecycle-hooks',
178 - );
179 -
180 - class WillMountAndUpdate extends React.Component {
181 - state = {};
182 - static getDerivedStateFromProps() {
183 - return null;
184 - }
185 - componentWillMount() {}
186 - UNSAFE_componentWillUpdate() {}
187 - render() {
188 - return null;
189 - }
190 - }
191 -
192 - shallowRenderer = createRenderer();
193 - expect(() => shallowRenderer.render(<WillMountAndUpdate />)).toWarnDev(
194 - 'Unsafe legacy lifecycles will not be called for components using the new getDerivedStateFromProps() API.\n\n' +
195 - 'WillMountAndUpdate uses getDerivedStateFromProps() but also contains the following legacy lifecycles:\n' +
196 - ' componentWillMount\n' +
197 - ' UNSAFE_componentWillUpdate\n\n' +
198 - 'The above lifecycles should be removed. Learn more about this warning here:\n' +
199 - 'https://fb.me/react-async-component-lifecycle-hooks',
200 - );
201 -
202 - class WillReceiveProps extends React.Component {
203 - state = {};
204 - static getDerivedStateFromProps() {
205 - return null;
206 - }
207 - componentWillReceiveProps() {}
208 - render() {
209 - return null;
210 - }
211 - }
212 -
213 - shallowRenderer = createRenderer();
214 - expect(() => shallowRenderer.render(<WillReceiveProps />)).toWarnDev(
215 - 'Unsafe legacy lifecycles will not be called for components using the new getDerivedStateFromProps() API.\n\n' +
216 - 'WillReceiveProps uses getDerivedStateFromProps() but also contains the following legacy lifecycles:\n' +
217 - ' componentWillReceiveProps\n\n' +
218 - 'The above lifecycles should be removed. Learn more about this warning here:\n' +
219 - 'https://fb.me/react-async-component-lifecycle-hooks',
220 - );
128 + shallowRenderer.render(<Component />);
129 });
130
131 it('should only render 1 level deep', () => {
@@ -1314,66 +1222,6 @@ describe('ReactShallowRenderer', () => {
1222 expect(shallowRenderer.getMountedInstance().state).toBeNull();
1223 });
1224
1317 - it('should warn if both componentWillReceiveProps and static getDerivedStateFromProps exist', () => {
1318 - class ComponentWithWarnings extends React.Component {
1319 - state = {};
1320 - static getDerivedStateFromProps(props, prevState) {
1321 - return null;
1322 - }
1323 - UNSAFE_componentWillReceiveProps(nextProps) {}
1324 - render() {
1325 - return null;
1326 - }
1327 - }
1328 -
1329 - const shallowRenderer = createRenderer();
1330 - expect(() => shallowRenderer.render(<ComponentWithWarnings />)).toWarnDev(
1331 - 'ComponentWithWarnings uses getDerivedStateFromProps() but also contains the following legacy lifecycles',
1332 - );
1333 -
1334 - // Should not log duplicate warning
1335 - shallowRenderer.render(<ComponentWithWarnings />);
1336 - });
1337 -
1338 - it('should warn if getDerivedStateFromProps returns undefined', () => {
1339 - class Component extends React.Component {
1340 - state = {};
1341 - static getDerivedStateFromProps() {}
1342 - render() {
1343 - return null;
1344 - }
1345 - }
1346 -
1347 - const shallowRenderer = createRenderer();
1348 - expect(() => shallowRenderer.render(<Component />)).toWarnDev(
1349 - 'Component.getDerivedStateFromProps(): A valid state object (or null) must ' +
1350 - 'be returned. You have returned undefined.',
1351 - );
1352 -
1353 - // De-duped
1354 - shallowRenderer.render(<Component />);
1355 - });
1356 -
1357 - it('should warn if state not initialized before getDerivedStateFromProps', () => {
1358 - class Component extends React.Component {
1359 - static getDerivedStateFromProps() {
1360 - return null;
1361 - }
1362 - render() {
1363 - return null;
1364 - }
1365 - }
1366 -
1367 - const shallowRenderer = createRenderer();
1368 - expect(() => shallowRenderer.render(<Component />)).toWarnDev(
1369 - 'Component: Did not properly initialize state during construction. ' +
1370 - 'Expected state to be an object, but it was undefined.',
1371 - );
1372 -
1373 - // De-duped
1374 - shallowRenderer.render(<Component />);
1375 - });
1376 -
1225 it('should invoke both deprecated and new lifecycles if both are present', () => {
1226 const log = [];
1227