|
| 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 | +/* eslint-disable no-func-assign */ |
| 12 | + |
| 13 | +'use strict'; |
| 14 | + |
| 15 | +let React; |
| 16 | +let ReactNoop; |
| 17 | +let Scheduler; |
| 18 | +let useState; |
| 19 | +let useEffect; |
| 20 | +let useLayoutEffect; |
| 21 | +let act; |
| 22 | + |
| 23 | +describe('ReactHooksWithNoopRenderer', () => { |
| 24 | + beforeEach(() => { |
| 25 | + jest.resetModules(); |
| 26 | + jest.useFakeTimers(); |
| 27 | + |
| 28 | + React = require('react'); |
| 29 | + ReactNoop = require('react-noop-renderer'); |
| 30 | + Scheduler = require('scheduler'); |
| 31 | + useState = React.useState; |
| 32 | + useEffect = React.useEffect; |
| 33 | + useLayoutEffect = React.useLayoutEffect; |
| 34 | + act = ReactNoop.act; |
| 35 | + }); |
| 36 | + |
| 37 | + test('layout unmmouts on deletion are fired in parent -> child order', async () => { |
| 38 | + const root = ReactNoop.createRoot(); |
| 39 | + |
| 40 | + function Parent() { |
| 41 | + useLayoutEffect(() => { |
| 42 | + return () => Scheduler.unstable_yieldValue('Unmount parent'); |
| 43 | + }); |
| 44 | + return <Child />; |
| 45 | + } |
| 46 | + |
| 47 | + function Child() { |
| 48 | + useLayoutEffect(() => { |
| 49 | + return () => Scheduler.unstable_yieldValue('Unmount child'); |
| 50 | + }); |
| 51 | + return 'Child'; |
| 52 | + } |
| 53 | + |
| 54 | + await ReactNoop.act(async () => { |
| 55 | + root.render(<Parent />); |
| 56 | + }); |
| 57 | + expect(root).toMatchRenderedOutput('Child'); |
| 58 | + await ReactNoop.act(async () => { |
| 59 | + root.render(null); |
| 60 | + }); |
| 61 | + expect(Scheduler).toHaveYielded(['Unmount parent', 'Unmount child']); |
| 62 | + }); |
| 63 | + |
| 64 | + test('passive unmmouts on deletion are fired in parent -> child order', async () => { |
| 65 | + const root = ReactNoop.createRoot(); |
| 66 | + |
| 67 | + function Parent() { |
| 68 | + useEffect(() => { |
| 69 | + return () => Scheduler.unstable_yieldValue('Unmount parent'); |
| 70 | + }); |
| 71 | + return <Child />; |
| 72 | + } |
| 73 | + |
| 74 | + function Child() { |
| 75 | + useEffect(() => { |
| 76 | + return () => Scheduler.unstable_yieldValue('Unmount child'); |
| 77 | + }); |
| 78 | + return 'Child'; |
| 79 | + } |
| 80 | + |
| 81 | + await ReactNoop.act(async () => { |
| 82 | + root.render(<Parent />); |
| 83 | + }); |
| 84 | + expect(root).toMatchRenderedOutput('Child'); |
| 85 | + await ReactNoop.act(async () => { |
| 86 | + root.render(null); |
| 87 | + }); |
| 88 | + expect(Scheduler).toHaveYielded(['Unmount parent', 'Unmount child']); |
| 89 | + }); |
| 90 | +}); |
0 commit comments