fix(tests): improve ShareMenuItems test isolation to fix intermittent suite failure (#39280)

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
(cherry picked from commit 98146251c4)
This commit is contained in:
Maxime Beauchemin
2026-04-10 12:30:38 -07:00
committed by Michael S. Molina
parent c78a599ca6
commit 4e16086a9b

View File

@@ -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(
<MenuWrapper
@@ -211,7 +205,7 @@ test('Click on "Share dashboard by email" and fail', 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(window.location.href).toBe('');