|
| 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 ReactTestRenderer; |
| 14 | +let act; |
| 15 | +let useState; |
| 16 | +let useTransition; |
| 17 | + |
| 18 | +const SUSPICIOUS_NUMBER_OF_FIBERS_UPDATED = 10; |
| 19 | + |
| 20 | +describe('ReactStartTransition', () => { |
| 21 | + beforeEach(() => { |
| 22 | + jest.resetModules(); |
| 23 | + React = require('react'); |
| 24 | + ReactTestRenderer = require('react-test-renderer'); |
| 25 | + act = require('jest-react').act; |
| 26 | + useState = React.useState; |
| 27 | + useTransition = React.useTransition; |
| 28 | + }); |
| 29 | + |
| 30 | + // @gate warnOnSubscriptionInsideStartTransition || !__DEV__ |
| 31 | + it('Warns if a suspicious number of fibers are updated inside startTransition', () => { |
| 32 | + const subs = new Set(); |
| 33 | + const useUserSpaceSubscription = () => { |
| 34 | + const setState = useState(0)[1]; |
| 35 | + subs.add(setState); |
| 36 | + }; |
| 37 | + |
| 38 | + let triggerHookTransition; |
| 39 | + |
| 40 | + const Component = ({level}) => { |
| 41 | + useUserSpaceSubscription(); |
| 42 | + if (level === 0) { |
| 43 | + triggerHookTransition = useTransition()[1]; |
| 44 | + } |
| 45 | + if (level < SUSPICIOUS_NUMBER_OF_FIBERS_UPDATED) { |
| 46 | + return <Component level={level + 1} />; |
| 47 | + } |
| 48 | + return null; |
| 49 | + }; |
| 50 | + |
| 51 | + act(() => { |
| 52 | + ReactTestRenderer.create(<Component level={0} />, { |
| 53 | + unstable_isConcurrent: true, |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + expect(() => { |
| 58 | + act(() => { |
| 59 | + React.startTransition(() => { |
| 60 | + subs.forEach(setState => { |
| 61 | + setState(state => state + 1); |
| 62 | + }); |
| 63 | + }); |
| 64 | + }); |
| 65 | + }).toWarnDev( |
| 66 | + [ |
| 67 | + 'Detected a large number of updates inside startTransition. ' + |
| 68 | + 'If this is due to a subscription please re-write it to use React provided hooks. ' + |
| 69 | + 'Otherwise concurrent mode guarantees are off the table.', |
| 70 | + ], |
| 71 | + {withoutStack: true}, |
| 72 | + ); |
| 73 | + |
| 74 | + expect(() => { |
| 75 | + act(() => { |
| 76 | + triggerHookTransition(() => { |
| 77 | + subs.forEach(setState => { |
| 78 | + setState(state => state + 1); |
| 79 | + }); |
| 80 | + }); |
| 81 | + }); |
| 82 | + }).toWarnDev( |
| 83 | + [ |
| 84 | + 'Detected a large number of updates inside startTransition. ' + |
| 85 | + 'If this is due to a subscription please re-write it to use React provided hooks. ' + |
| 86 | + 'Otherwise concurrent mode guarantees are off the table.', |
| 87 | + ], |
| 88 | + {withoutStack: true}, |
| 89 | + ); |
| 90 | + }); |
| 91 | +}); |
0 commit comments