/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. * * Any modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ /* * Licensed to Elasticsearch B.V. under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch B.V. licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import React, { FC } from 'react'; import { mount, ReactWrapper } from 'enzyme'; import { MountPoint, UnmountCallback } from 'opensearch-dashboards/public'; import { MountPointPortal } from './mount_point_portal'; import { act } from 'react-dom/test-utils'; describe('MountPointPortal', () => { let portalTarget: HTMLElement; let mountPoint: MountPoint; let setMountPoint: jest.Mock<(mountPoint: MountPoint) => void>; let dom: ReactWrapper; const refresh = () => { new Promise(async (resolve) => { if (dom) { act(() => { dom.update(); }); } setImmediate(() => resolve(dom)); // flushes any pending promises }); }; beforeEach(() => { portalTarget = document.createElement('div'); document.body.append(portalTarget); setMountPoint = jest.fn().mockImplementation((mp) => (mountPoint = mp)); }); afterEach(() => { if (portalTarget) { portalTarget.remove(); } }); it('calls the provided `setMountPoint` during render', async () => { dom = mount( portal content ); await refresh(); expect(setMountPoint).toHaveBeenCalledTimes(1); }); it('renders the portal content when calling the mountPoint ', async () => { dom = mount( portal content ); await refresh(); expect(mountPoint).toBeDefined(); act(() => { mountPoint(portalTarget); }); await refresh(); expect(portalTarget.innerHTML).toBe('portal content'); }); it('cleanup the portal content when the component is unmounted', async () => { dom = mount( portal content ); act(() => { mountPoint(portalTarget); }); await refresh(); expect(portalTarget.innerHTML).toBe('portal content'); dom.unmount(); await refresh(); expect(portalTarget.innerHTML).toBe(''); }); it('cleanup the portal content when unmounting the MountPoint from outside', async () => { dom = mount( portal content ); let unmount: UnmountCallback; act(() => { unmount = mountPoint(portalTarget); }); await refresh(); expect(portalTarget.innerHTML).toBe('portal content'); act(() => { unmount(); }); await refresh(); expect(portalTarget.innerHTML).toBe(''); }); it('updates the content of the portal element when the content of MountPointPortal changes', async () => { const Wrapper: FC<{ setMount: (mountPoint: MountPoint) => void; portalContent: string; }> = ({ setMount, portalContent }) => { return (
{portalContent}
); }; dom = mount(); act(() => { mountPoint(portalTarget); }); await refresh(); expect(portalTarget.innerHTML).toBe('
before
'); dom.setProps({ portalContent: 'after', }); await refresh(); expect(portalTarget.innerHTML).toBe('
after
'); }); it('cleanup the previous portal content when setMountPoint changes', async () => { dom = mount( portal content ); act(() => { mountPoint(portalTarget); }); await refresh(); expect(portalTarget.innerHTML).toBe('portal content'); const newSetMountPoint = jest.fn(); dom.setProps({ setMountPoint: newSetMountPoint, }); await refresh(); expect(portalTarget.innerHTML).toBe(''); }); it('intercepts errors and display an error message', async () => { const CrashTest = () => { throw new Error('crash'); }; dom = mount( ); act(() => { mountPoint(portalTarget); }); await refresh(); expect(portalTarget.innerHTML).toBe('

Error rendering portal content

'); }); });