|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 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 | + */ |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +let React; |
| 13 | +let ReactDOM; |
| 14 | + |
| 15 | +let ReactFeatureFlags = require('shared/ReactFeatureFlags'); |
| 16 | + |
| 17 | +// NOTE: We're explicitly not using JSX here. This is intended to test |
| 18 | +// a new React.jsx api which does not have a JSX transformer yet. |
| 19 | +// A lot of these tests are pulled from ReactElement-test because |
| 20 | +// this api is meant to be backwards compatible. |
| 21 | +describe('ReactElementJSXNewWarnings', () => { |
| 22 | + let originalSymbol; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + jest.resetModules(); |
| 26 | + |
| 27 | + // Delete the native Symbol if we have one to ensure we test the |
| 28 | + // unpolyfilled environment. |
| 29 | + originalSymbol = global.Symbol; |
| 30 | + global.Symbol = undefined; |
| 31 | + |
| 32 | + ReactFeatureFlags = require('shared/ReactFeatureFlags'); |
| 33 | + ReactFeatureFlags.warnAboutSpreadingKeyToJSX = true; |
| 34 | + |
| 35 | + React = require('react'); |
| 36 | + ReactDOM = require('react-dom'); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + global.Symbol = originalSymbol; |
| 41 | + }); |
| 42 | + |
| 43 | + if (!__EXPERIMENTAL__) { |
| 44 | + it("empty test so Jest doesn't complain", () => {}); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + it('should warn when keys are passed as part of props', () => { |
| 49 | + const container = document.createElement('div'); |
| 50 | + class Child extends React.Component { |
| 51 | + render() { |
| 52 | + return React.jsx('div', {}); |
| 53 | + } |
| 54 | + } |
| 55 | + class Parent extends React.Component { |
| 56 | + render() { |
| 57 | + return React.jsx('div', { |
| 58 | + children: [React.jsx(Child, {key: '0'})], |
| 59 | + }); |
| 60 | + } |
| 61 | + } |
| 62 | + expect(() => ReactDOM.render(React.jsx(Parent, {}), container)).toErrorDev( |
| 63 | + 'Warning: React.jsx: Spreading a key to JSX is a deprecated pattern. ' + |
| 64 | + 'Explicitly pass a key after spreading props in your JSX call. ' + |
| 65 | + 'E.g. <Child {...props} key={key} />', |
| 66 | + ); |
| 67 | + }); |
| 68 | +}); |
0 commit comments