From 4e16086a9bf3fa0d7ec1e987a684fd99dd2f4f7f Mon Sep 17 00:00:00 2001 From: Maxime Beauchemin Date: Fri, 10 Apr 2026 12:30:38 -0700 Subject: [PATCH] fix(tests): improve ShareMenuItems test isolation to fix intermittent suite failure (#39280) Co-authored-by: Claude Opus 4.6 (cherry picked from commit 98146251c4fc16d26e9cb49a80c0d72a25263107) --- .../ShareMenuItems/ShareMenuItems.test.tsx | 34 ++++++++----------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/superset-frontend/src/dashboard/components/menu/ShareMenuItems/ShareMenuItems.test.tsx b/superset-frontend/src/dashboard/components/menu/ShareMenuItems/ShareMenuItems.test.tsx index 3e137d2c874..476487563a3 100644 --- a/superset-frontend/src/dashboard/components/menu/ShareMenuItems/ShareMenuItems.test.tsx +++ b/superset-frontend/src/dashboard/components/menu/ShareMenuItems/ShareMenuItems.test.tsx @@ -45,13 +45,16 @@ const createProps = () => ({ submenuKey: 'share', }); -const { location } = window; +const originalLocation = window.location; const postDashboardPermalinkMockUrl = `http://localhost/api/v1/dashboard/${DASHBOARD_ID}/permalink`; -beforeAll((): void => { +beforeEach(() => { + jest.clearAllMocks(); // @ts-expect-error delete window.location; + window.location = { href: '' } as any; + fetchMock.clearHistory().removeRoutes(); fetchMock.post( postDashboardPermalinkMockUrl, { key: '123', url: 'http://localhost/superset/dashboard/p/123/' }, @@ -59,15 +62,10 @@ beforeAll((): void => { ); }); -beforeEach(() => { - jest.clearAllMocks(); - window.location = { - href: '', - } as any; -}); - -afterAll((): void => { - window.location = location; +afterEach(() => { + window.location = originalLocation; + window.featureFlags = {}; + fetchMock.clearHistory().removeRoutes(); }); const MenuWrapper = ( @@ -113,7 +111,7 @@ test('Click on "Copy dashboard URL" and succeed', async () => { expect(props.addDangerToast).toHaveBeenCalledTimes(0); }); - userEvent.click(screen.getByText('Copy dashboard URL')); + await userEvent.click(screen.getByText('Copy dashboard URL')); await waitFor(async () => { expect(spy).toHaveBeenCalledTimes(1); @@ -145,7 +143,7 @@ test('Click on "Copy dashboard URL" and fail', async () => { expect(props.addDangerToast).toHaveBeenCalledTimes(0); }); - userEvent.click(screen.getByText('Copy dashboard URL')); + await userEvent.click(screen.getByText('Copy dashboard URL')); await waitFor(async () => { expect(spy).toHaveBeenCalledTimes(1); @@ -177,7 +175,7 @@ test('Click on "Share dashboard by email" and succeed', async () => { expect(window.location.href).toBe(''); }); - userEvent.click(screen.getByText('Share dashboard by email')); + await userEvent.click(screen.getByText('Share dashboard by email')); await waitFor(() => { expect(props.addDangerToast).toHaveBeenCalledTimes(0); @@ -189,11 +187,7 @@ test('Click on "Share dashboard by email" and succeed', async () => { test('Click on "Share dashboard by email" and fail', async () => { fetchMock.removeRoute(postDashboardPermalinkMockUrl); - fetchMock.post( - `http://localhost/api/v1/dashboard/${DASHBOARD_ID}/permalink`, - { status: 404 }, - { name: postDashboardPermalinkMockUrl }, - ); + fetchMock.post(postDashboardPermalinkMockUrl, { status: 404 }); const props = createProps(); render( { expect(window.location.href).toBe(''); }); - userEvent.click(screen.getByText('Share dashboard by email')); + await userEvent.click(screen.getByText('Share dashboard by email')); await waitFor(() => { expect(window.location.href).toBe('');