Skip to content

Commit

Permalink
Fix up some more of the tests, clean them up
Browse files Browse the repository at this point in the history
  • Loading branch information
mofojed committed Mar 21, 2024
1 parent 1a8976d commit 26dae55
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 17 deletions.
1 change: 1 addition & 0 deletions plugins/ui/src/js/__mocks__/@deephaven/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
...DashboardActual,
LayoutUtils: {
getComponentName: jest.fn(),
getContentItemInStack: jest.fn(),
getStackForConfig: jest.fn(),
getIdFromContainer: DashboardActual.LayoutUtils.getIdFromContainer,
openComponent: jest.fn(),
Expand Down
154 changes: 137 additions & 17 deletions plugins/ui/src/js/src/layout/ReactPanel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,37 @@ import {
} from './ReactPanelManager';
import { ReactPanelProps } from './LayoutUtils';
import PortalPanelManager from './PortalPanelManager';
import PortalPanelManagerContext from './PortalPanelManagerContext';

const mockPanelId = 'test-panel-id';

beforeEach(() => {
jest.clearAllMocks();
});

function makeReactPanel({
function makeReactPanelManager({
children,
metadata = { name: 'test-name', type: 'test-type' },
onClose = jest.fn(),
onOpen = jest.fn(),
getPanelId = jest.fn(() => mockPanelId),
title = 'test title',
}: Partial<ReactPanelProps> & Partial<ReactPanelManager> = {}) {
return (
<ReactPanelManagerContext.Provider
value={{
getPanelId,
metadata,
onClose,
onOpen,
}}
>
<ReactPanel title={title}>{children}</ReactPanel>
</ReactPanelManagerContext.Provider>
);
}

function makeTestComponent({
children,
metadata = { name: 'test-name', type: 'test-type' },
onClose = jest.fn(),
Expand All @@ -25,16 +48,14 @@ function makeReactPanel({
}: Partial<ReactPanelProps> & Partial<ReactPanelManager> = {}) {
return (
<PortalPanelManager>
<ReactPanelManagerContext.Provider
value={{
getPanelId,
metadata,
onClose,
onOpen,
}}
>
<ReactPanel title={title}>{children}</ReactPanel>
</ReactPanelManagerContext.Provider>
{makeReactPanelManager({
children,
metadata,
onClose,
onOpen,
getPanelId,
title,
})}
</PortalPanelManager>
);
}
Expand All @@ -49,7 +70,7 @@ function simulatePanelClosed() {
it('opens panel on mount, and closes panel on unmount', () => {
const onOpen = jest.fn();
const onClose = jest.fn();
const { unmount } = render(makeReactPanel({ onOpen, onClose }));
const { unmount } = render(makeTestComponent({ onOpen, onClose }));
expect(LayoutUtils.openComponent).toHaveBeenCalledTimes(1);
expect(LayoutUtils.closeComponent).not.toHaveBeenCalled();
expect(onOpen).toHaveBeenCalledTimes(1);
Expand All @@ -69,14 +90,14 @@ it('only calls open once if the panel has not closed and only children change',
const metadata = { type: 'bar' };
const children = 'hello';
const { rerender } = render(
makeReactPanel({ children, onOpen, onClose, metadata })
makeTestComponent({ children, onOpen, onClose, metadata })
);
expect(LayoutUtils.openComponent).toHaveBeenCalledTimes(1);
expect(LayoutUtils.closeComponent).not.toHaveBeenCalled();
expect(onOpen).toHaveBeenCalledTimes(1);
expect(onClose).not.toHaveBeenCalled();

rerender(makeReactPanel({ children: 'world', onOpen, onClose, metadata }));
rerender(makeTestComponent({ children: 'world', onOpen, onClose, metadata }));

expect(LayoutUtils.openComponent).toHaveBeenCalledTimes(1);
expect(LayoutUtils.closeComponent).not.toHaveBeenCalled();
Expand All @@ -90,7 +111,7 @@ it('calls openComponent again after panel is closed only if the metadata changes
const metadata = { type: 'bar' };
const children = 'hello';
const { rerender } = render(
makeReactPanel({
makeTestComponent({
children,
onOpen,
onClose,
Expand All @@ -112,7 +133,7 @@ it('calls openComponent again after panel is closed only if the metadata changes

// Should not re-open if just the children change but the metadata stays the same
rerender(
makeReactPanel({
makeTestComponent({
children: 'world',
onOpen,
onClose,
Expand All @@ -127,7 +148,7 @@ it('calls openComponent again after panel is closed only if the metadata changes

// Should re-open after the metadata change
rerender(
makeReactPanel({
makeTestComponent({
children,
onOpen,
onClose,
Expand All @@ -140,3 +161,102 @@ it('calls openComponent again after panel is closed only if the metadata changes
expect(onOpen).toHaveBeenCalledTimes(2);
expect(onClose).toHaveBeenCalledTimes(1);
});

// Case when rehydrating a widget
it('does not call openComponent or setActiveContentItem if panel already exists when created', () => {
const onOpen = jest.fn();
const onClose = jest.fn();
const mockStack = {
setActiveContentItem: jest.fn(),
};
const mockContentItem = {};
(LayoutUtils.getStackForConfig as jest.Mock).mockReturnValue(mockStack);
(LayoutUtils.getContentItemInStack as jest.Mock).mockReturnValue(
mockContentItem
);
const portal = document.createElement('div');
const portals = new Map([[mockPanelId, portal]]);

const metadata = { type: 'bar' };
const children = 'hello';
const { rerender } = render(
<PortalPanelManagerContext.Provider value={portals}>
{makeReactPanelManager({
children,
onOpen,
onClose,
metadata,
})}
</PortalPanelManagerContext.Provider>
);
expect(LayoutUtils.openComponent).not.toHaveBeenCalled();
expect(LayoutUtils.closeComponent).not.toHaveBeenCalled();
expect(LayoutUtils.getStackForConfig).toHaveBeenCalled();
expect(mockStack.setActiveContentItem).not.toHaveBeenCalled();

expect(onOpen).toHaveBeenCalledTimes(1);
expect(onClose).not.toHaveBeenCalled();

// Now check that it focuses it if it's called after the metadat changes
rerender(
<PortalPanelManagerContext.Provider value={portals}>
{makeReactPanelManager({
children: 'world',
onOpen,
onClose,
metadata: { type: 'baz' },
})}
</PortalPanelManagerContext.Provider>
);

expect(LayoutUtils.openComponent).not.toHaveBeenCalled();
expect(LayoutUtils.closeComponent).not.toHaveBeenCalled();
expect(onOpen).toHaveBeenCalledTimes(1);
expect(onClose).not.toHaveBeenCalled();

expect(mockStack.setActiveContentItem).toHaveBeenCalledTimes(1);
expect(mockStack.setActiveContentItem).toHaveBeenCalledWith(mockContentItem);
});

it('calls setActiveContentItem if metadata changed while the panel already exists', () => {
const onOpen = jest.fn();
const onClose = jest.fn();
const metadata = { type: 'bar' };
const children = 'hello';
const { rerender } = render(
makeTestComponent({
children,
onOpen,
onClose,
metadata,
})
);
expect(LayoutUtils.openComponent).not.toHaveBeenCalled();
expect(LayoutUtils.closeComponent).not.toHaveBeenCalled();
expect(onOpen).toHaveBeenCalledTimes(1);
expect(onClose).not.toHaveBeenCalled();
expect(useListener).toHaveBeenCalledTimes(1);

const mockStack = {
setActiveContentItem: jest.fn(),
};
const mockContentItem = {};
(LayoutUtils.getStackForConfig as jest.Mock).mockReturnValue(mockStack);
(LayoutUtils.getContentItemInStack as jest.Mock).mockReturnValue(
mockContentItem
);
rerender(
makeTestComponent({
children: 'world',
onOpen,
onClose,
metadata: { type: 'baz' },
})
);

expect(LayoutUtils.openComponent).not.toHaveBeenCalled();
expect(LayoutUtils.closeComponent).not.toHaveBeenCalled();
expect(onOpen).toHaveBeenCalledTimes(1);
expect(onClose).not.toHaveBeenCalled();
expect(mockStack.setActiveContentItem).toHaveBeenCalledTimes(1);
});

0 comments on commit 26dae55

Please sign in to comment.