Skip to content

Commit

Permalink
Remove history listen in renderImportSampleDataApp
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Wang <wonglam@amazon.com>
  • Loading branch information
wanglam committed May 21, 2024
1 parent b82fcec commit 5f8f9f4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 36 deletions.
27 changes: 5 additions & 22 deletions src/plugins/home/public/application/application.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => ({
Expand All @@ -15,21 +15,16 @@ jest.mock('./components/home_app', () => ({

const coreStartMocks = coreMock.createStart();

const ComponentForRender = (props: {
renderFn: typeof renderImportSampleDataApp;
historyMock?: ReturnType<typeof scopedHistoryMock.create>;
}) => {
const ComponentForRender = (props: { renderFn: typeof renderImportSampleDataApp }) => {
const container = useRef<HTMLDivElement>(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 <div ref={container} />;
};
Expand All @@ -45,16 +40,4 @@ describe('renderImportSampleDataApp', () => {
</div>
`);
});
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(
<ComponentForRender renderFn={renderImportSampleDataApp} historyMock={historyMock} />
);
unmount();
await waitFor(() => {
expect(unlistenMock).toHaveBeenCalled();
});
});
});
14 changes: 1 addition & 13 deletions src/plugins/home/public/application/application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<OpenSearchDashboardsContextProvider services={{ ...coreStart }}>
<ImportSampleDataApp />
Expand All @@ -99,6 +88,5 @@ export const renderImportSampleDataApp = async (

return () => {
unmountComponentAtNode(element);
unlisten();
};
};
2 changes: 1 addition & 1 deletion src/plugins/home/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Check warning on line 167 in src/plugins/home/public/plugin.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/home/public/plugin.ts#L166-L167

Added lines #L166 - L167 were not covered by tests
},
});
urlForwarding.forwardApp('home', 'home');
Expand Down

0 comments on commit 5f8f9f4

Please sign in to comment.