|
| 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 | +/* eslint-disable no-func-assign */ |
| 12 | + |
| 13 | +'use strict'; |
| 14 | + |
| 15 | +let React; |
| 16 | +let ReactFeatureFlags; |
| 17 | +let ReactTestRenderer; |
| 18 | + |
| 19 | +// Additional tests can be found in ReactHooksWithNoopRenderer. Plan is to |
| 20 | +// gradually migrate those to this file. |
| 21 | +describe('ReactHooks', () => { |
| 22 | + beforeEach(() => { |
| 23 | + jest.resetModules(); |
| 24 | + |
| 25 | + ReactFeatureFlags = require('shared/ReactFeatureFlags'); |
| 26 | + ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false; |
| 27 | + ReactFeatureFlags.enableHooks = true; |
| 28 | + React = require('react'); |
| 29 | + ReactTestRenderer = require('react-test-renderer'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('warns about variable number of dependencies', () => { |
| 33 | + const {useLayoutEffect} = React; |
| 34 | + function App(props) { |
| 35 | + useLayoutEffect(() => { |
| 36 | + ReactTestRenderer.unstable_yield( |
| 37 | + 'Did commit: ' + props.dependencies.join(', '), |
| 38 | + ); |
| 39 | + }, props.dependencies); |
| 40 | + return props.dependencies; |
| 41 | + } |
| 42 | + const root = ReactTestRenderer.create(<App dependencies={['A']} />); |
| 43 | + expect(ReactTestRenderer).toHaveYielded(['Did commit: A']); |
| 44 | + expect(() => { |
| 45 | + root.update(<App dependencies={['A', 'B']} />); |
| 46 | + }).toWarnDev([ |
| 47 | + 'Warning: Detected a variable number of hook dependencies. The length ' + |
| 48 | + 'of the dependencies array should be constant between renders.\n\n' + |
| 49 | + 'Previous: A, B\n' + |
| 50 | + 'Incoming: A', |
| 51 | + ]); |
| 52 | + expect(ReactTestRenderer).toHaveYielded(['Did commit: A, B']); |
| 53 | + }); |
| 54 | +}); |
0 commit comments