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