From 5f8f9f4a88b59dec35bf2657d206f7051f273f7a Mon Sep 17 00:00:00 2001 From: Lin Wang Date: Tue, 21 May 2024 17:03:39 +0800 Subject: [PATCH] Remove history listen in renderImportSampleDataApp Signed-off-by: Lin Wang --- .../public/application/application.test.tsx | 27 ++++--------------- .../home/public/application/application.tsx | 14 +--------- src/plugins/home/public/plugin.ts | 2 +- 3 files changed, 7 insertions(+), 36 deletions(-) diff --git a/src/plugins/home/public/application/application.test.tsx b/src/plugins/home/public/application/application.test.tsx index 46974ac6e5d9..2bf8eb66f583 100644 --- a/src/plugins/home/public/application/application.test.tsx +++ b/src/plugins/home/public/application/application.test.tsx @@ -4,8 +4,8 @@ */ import React, { useEffect, useRef } from 'react'; -import { render, waitFor } from '@testing-library/react'; -import { coreMock, scopedHistoryMock } from '../../../../core/public/mocks'; +import { render } from '@testing-library/react'; +import { coreMock } from '../../../../core/public/mocks'; import { renderImportSampleDataApp } from './application'; jest.mock('./components/home_app', () => ({ @@ -15,21 +15,16 @@ jest.mock('./components/home_app', () => ({ const coreStartMocks = coreMock.createStart(); -const ComponentForRender = (props: { - renderFn: typeof renderImportSampleDataApp; - historyMock?: ReturnType; -}) => { +const ComponentForRender = (props: { renderFn: typeof renderImportSampleDataApp }) => { const container = useRef(null); - const historyMock = props.historyMock || scopedHistoryMock.create(); - historyMock.listen.mockReturnValueOnce(() => () => null); useEffect(() => { if (container.current) { - const destroyFn = props.renderFn(container.current, coreStartMocks, historyMock); + const destroyFn = props.renderFn(container.current, coreStartMocks); return () => { destroyFn.then((res) => res()); }; } - }, [historyMock, props]); + }, [props]); return
; }; @@ -45,16 +40,4 @@ describe('renderImportSampleDataApp', () => {
`); }); - it('should clean up history listeners after unmount', async () => { - const historyMock = scopedHistoryMock.create(); - const unlistenMock = jest.fn(() => null); - historyMock.listen.mockImplementationOnce(() => unlistenMock); - const { unmount } = render( - - ); - unmount(); - await waitFor(() => { - expect(unlistenMock).toHaveBeenCalled(); - }); - }); }); diff --git a/src/plugins/home/public/application/application.tsx b/src/plugins/home/public/application/application.tsx index bd5b3c161ff8..7b383b53ae92 100644 --- a/src/plugins/home/public/application/application.tsx +++ b/src/plugins/home/public/application/application.tsx @@ -78,18 +78,7 @@ export const renderApp = async ( }; }; -export const renderImportSampleDataApp = async ( - element: HTMLElement, - coreStart: CoreStart, - history: ScopedHistory -) => { - // dispatch synthetic hash change event to update hash history objects - // this is necessary because hash updates triggered by using popState won't trigger this event naturally. - // This must be called before the app is mounted to avoid call this after the redirect to default app logic kicks in - const unlisten = history.listen((location) => { - window.dispatchEvent(new HashChangeEvent('hashchange')); - }); - +export const renderImportSampleDataApp = async (element: HTMLElement, coreStart: CoreStart) => { render( @@ -99,6 +88,5 @@ export const renderImportSampleDataApp = async ( return () => { unmountComponentAtNode(element); - unlisten(); }; }; diff --git a/src/plugins/home/public/plugin.ts b/src/plugins/home/public/plugin.ts index 06a8ac9a9cfb..a9e4cb263e88 100644 --- a/src/plugins/home/public/plugin.ts +++ b/src/plugins/home/public/plugin.ts @@ -164,7 +164,7 @@ export class HomePublicPlugin }) ); const { renderImportSampleDataApp } = await import('./application'); - return await renderImportSampleDataApp(params.element, coreStart, params.history); + return await renderImportSampleDataApp(params.element, coreStart); }, }); urlForwarding.forwardApp('home', 'home');