|
| 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 | + * @jest-environment node |
| 9 | + */ |
| 10 | + |
| 11 | +let React; |
| 12 | +let ReactNoop; |
| 13 | +let Scheduler; |
| 14 | +let act; |
| 15 | + |
| 16 | +let useState; |
| 17 | +let startTransition; |
| 18 | + |
| 19 | +describe('ReactInteractionTracing', () => { |
| 20 | + beforeEach(() => { |
| 21 | + jest.resetModules(); |
| 22 | + const ReactFeatureFlags = require('shared/ReactFeatureFlags'); |
| 23 | + ReactFeatureFlags.enableTransitionTracing = true; |
| 24 | + |
| 25 | + React = require('react'); |
| 26 | + ReactNoop = require('react-noop-renderer'); |
| 27 | + Scheduler = require('scheduler'); |
| 28 | + |
| 29 | + act = require('jest-react').act; |
| 30 | + |
| 31 | + useState = React.useState; |
| 32 | + startTransition = React.startTransition; |
| 33 | + }); |
| 34 | + |
| 35 | + function Text({text}) { |
| 36 | + Scheduler.unstable_yieldValue(text); |
| 37 | + return text; |
| 38 | + } |
| 39 | + |
| 40 | + function advanceTimers(ms) { |
| 41 | + // Note: This advances Jest's virtual time but not React's. Use |
| 42 | + // ReactNoop.expire for that. |
| 43 | + if (typeof ms !== 'number') { |
| 44 | + throw new Error('Must specify ms'); |
| 45 | + } |
| 46 | + jest.advanceTimersByTime(ms); |
| 47 | + // Wait until the end of the current tick |
| 48 | + // We cannot use a timer since we're faking them |
| 49 | + return Promise.resolve().then(() => {}); |
| 50 | + } |
| 51 | + |
| 52 | + it('should correctly trace basic interaction', async () => { |
| 53 | + const transitionCallbacks = { |
| 54 | + onTransitionStart: (name, startTime) => { |
| 55 | + Scheduler.unstable_yieldValue( |
| 56 | + `onTransitionStart(${name}, ${startTime})`, |
| 57 | + ); |
| 58 | + }, |
| 59 | + onTransitionComplete: (name, startTime, endTime) => { |
| 60 | + Scheduler.unstable_yieldValue( |
| 61 | + `onTransitionComplete(${name}, ${startTime}, ${endTime})`, |
| 62 | + ); |
| 63 | + }, |
| 64 | + }; |
| 65 | + |
| 66 | + let navigateToPageTwo; |
| 67 | + function App() { |
| 68 | + const [navigate, setNavigate] = useState(false); |
| 69 | + navigateToPageTwo = () => { |
| 70 | + setNavigate(true); |
| 71 | + }; |
| 72 | + |
| 73 | + return ( |
| 74 | + <div> |
| 75 | + {navigate ? <Text text="Page Two" /> : <Text text="Page One" />} |
| 76 | + </div> |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + const root = ReactNoop.createRoot({transitionCallbacks}); |
| 81 | + await act(async () => { |
| 82 | + root.render(<App />); |
| 83 | + ReactNoop.expire(1000); |
| 84 | + await advanceTimers(1000); |
| 85 | + |
| 86 | + expect(Scheduler).toFlushAndYield(['Page One']); |
| 87 | + |
| 88 | + await act(async () => { |
| 89 | + startTransition(() => navigateToPageTwo(), {name: 'page transition'}); |
| 90 | + |
| 91 | + ReactNoop.expire(1000); |
| 92 | + await advanceTimers(1000); |
| 93 | + |
| 94 | + expect(Scheduler).toFlushAndYield([ |
| 95 | + 'Page Two', |
| 96 | + 'onTransitionStart(page transition, 1000)', |
| 97 | + 'onTransitionComplete(page transition, 1000, 2000)', |
| 98 | + ]); |
| 99 | + }); |
| 100 | + }); |
| 101 | + }); |
| 102 | +}); |
0 commit comments