From 0de905d528105bb71469672f5ac23452caceb28f Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Sat, 12 Nov 2022 16:34:15 +0100 Subject: [PATCH 1/2] Block Editor: Cleanup after MediaReplaceFlow tests --- .../src/components/media-replace-flow/test/index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/components/media-replace-flow/test/index.js b/packages/block-editor/src/components/media-replace-flow/test/index.js index 8d6bc6ca43378..7bc082e74e862 100644 --- a/packages/block-editor/src/components/media-replace-flow/test/index.js +++ b/packages/block-editor/src/components/media-replace-flow/test/index.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { render, screen } from '@testing-library/react'; +import { cleanup, render, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; /** @@ -33,6 +33,10 @@ function TestWrapper() { } describe( 'General media replace flow', () => { + afterEach( () => { + cleanup(); + } ); + it( 'renders successfully', () => { render( ); From c823760c0b9bff5d411902ec59d5e54b9c97cddd Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 17 Nov 2022 11:13:56 +0200 Subject: [PATCH 2/2] Unmount instead of cleanup() --- .../media-replace-flow/test/index.js | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/block-editor/src/components/media-replace-flow/test/index.js b/packages/block-editor/src/components/media-replace-flow/test/index.js index 7bc082e74e862..de4bcf7e748fe 100644 --- a/packages/block-editor/src/components/media-replace-flow/test/index.js +++ b/packages/block-editor/src/components/media-replace-flow/test/index.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { cleanup, render, screen } from '@testing-library/react'; +import { render, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; /** @@ -33,12 +33,8 @@ function TestWrapper() { } describe( 'General media replace flow', () => { - afterEach( () => { - cleanup(); - } ); - it( 'renders successfully', () => { - render( ); + const { unmount } = render( ); expect( screen.getByRole( 'button', { @@ -46,6 +42,10 @@ describe( 'General media replace flow', () => { name: 'Replace', } ) ).toBeVisible(); + + // Unmount the UI synchronously so that any async effects, like the on-mount focus + // that shows and positions a tooltip, are cancelled right away and never run. + unmount(); } ); it( 'renders replace menu', async () => { @@ -53,7 +53,7 @@ describe( 'General media replace flow', () => { advanceTimers: jest.advanceTimersByTime, } ); - render( ); + const { unmount } = render( ); await user.click( screen.getByRole( 'button', { @@ -66,6 +66,10 @@ describe( 'General media replace flow', () => { expect( uploadMenu ).toBeInTheDocument(); expect( uploadMenu ).not.toBeVisible(); + + // Unmount the UI synchronously so that any async effects, like the on-mount focus + // that shows and positions a tooltip, are cancelled right away and never run. + unmount(); } ); it( 'displays media URL', async () => { @@ -73,7 +77,7 @@ describe( 'General media replace flow', () => { advanceTimers: jest.advanceTimersByTime, } ); - render( ); + const { unmount } = render( ); await user.click( screen.getByRole( 'button', { @@ -87,6 +91,10 @@ describe( 'General media replace flow', () => { name: 'example.media (opens in a new tab)', } ) ).toHaveAttribute( 'href', 'https://example.media' ); + + // Unmount the UI synchronously so that any async effects, like the on-mount focus + // that shows and positions a tooltip, are cancelled right away and never run. + unmount(); } ); it( 'edits media URL', async () => { @@ -94,7 +102,7 @@ describe( 'General media replace flow', () => { advanceTimers: jest.advanceTimersByTime, } ); - render( ); + const { unmount } = render( ); await user.click( screen.getByRole( 'button', { @@ -128,5 +136,9 @@ describe( 'General media replace flow', () => { name: 'new.example.media (opens in a new tab)', } ) ).toHaveAttribute( 'href', 'https://new.example.media' ); + + // Unmount the UI synchronously so that any async effects, like the on-mount focus + // that shows and positions a tooltip, are cancelled right away and never run. + unmount(); } ); } );