|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import { |
| 4 | + OpenedPanelMap, |
| 5 | + PanelComponent, |
| 6 | + PanelManager, |
| 7 | +} from '@deephaven/dashboard'; |
| 8 | +import GoldenLayout, { Config } from '@deephaven/golden-layout'; |
| 9 | +import { TypeValue as FilterTypeValue } from '@deephaven/filters'; |
| 10 | +import ToolType from './ToolType'; |
| 11 | +import { Linker } from './Linker'; |
| 12 | +import { Link, LinkPoint, LinkType } from './LinkerUtils'; |
| 13 | + |
| 14 | +// Disable CSSTransition delays to make testing simpler |
| 15 | +jest.mock('react-transition-group', () => ({ |
| 16 | + // eslint-disable-next-line react/display-name, react/prop-types |
| 17 | + Transition: ({ children, in: inProp }) => |
| 18 | + inProp !== false ? children : null, |
| 19 | + // eslint-disable-next-line react/display-name, react/prop-types |
| 20 | + CSSTransition: ({ children, in: inProp }) => |
| 21 | + inProp !== false ? children : null, |
| 22 | +})); |
| 23 | + |
| 24 | +function makeLayout() { |
| 25 | + return new GoldenLayout({} as Config, undefined); |
| 26 | +} |
| 27 | + |
| 28 | +function makePanelManager(layout = makeLayout()) { |
| 29 | + const PANEL_ID_A = 'PANEL_ID_A'; |
| 30 | + const PANEL_ID_B = 'PANEL_ID_B'; |
| 31 | + const openedMap: OpenedPanelMap = new Map([ |
| 32 | + [ |
| 33 | + PANEL_ID_A, |
| 34 | + { |
| 35 | + getCoordinateForColumn: jest.fn(() => { |
| 36 | + const coordinate = [5, 5]; |
| 37 | + return coordinate; // make coordinates here |
| 38 | + }), |
| 39 | + } as PanelComponent, |
| 40 | + ], |
| 41 | + [ |
| 42 | + PANEL_ID_B, |
| 43 | + { |
| 44 | + getCoordinateForColumn: jest.fn(() => { |
| 45 | + const coordinate = [50, 50]; |
| 46 | + return coordinate; // make coordinates here |
| 47 | + }), |
| 48 | + } as PanelComponent, |
| 49 | + ], |
| 50 | + ]); |
| 51 | + return new PanelManager(layout, undefined, undefined, openedMap); |
| 52 | +} |
| 53 | + |
| 54 | +function makeLinkPoint( |
| 55 | + panelId: string | string[], |
| 56 | + columnName: string, |
| 57 | + columnType: string | null, |
| 58 | + panelComponent?: string | null |
| 59 | +): LinkPoint { |
| 60 | + return { panelId, panelComponent, columnName, columnType }; |
| 61 | +} |
| 62 | + |
| 63 | +function makeLink( |
| 64 | + start: LinkPoint, |
| 65 | + end: LinkPoint, |
| 66 | + id: string, |
| 67 | + type: LinkType, |
| 68 | + isReversed?: boolean | undefined, |
| 69 | + operator?: FilterTypeValue | undefined |
| 70 | +): Link { |
| 71 | + return { start, end, id, isReversed, type, operator }; |
| 72 | +} |
| 73 | + |
| 74 | +function mountLinker({ |
| 75 | + links = [] as Link[], |
| 76 | + timeZone = 'TIMEZONE', |
| 77 | + activeTool = ToolType.LINKER, |
| 78 | + localDashboardId = 'TEST_ID', |
| 79 | + layout = makeLayout(), |
| 80 | + panelManager = makePanelManager(), |
| 81 | + setActiveTool = jest.fn(), |
| 82 | + setDashboardLinks = jest.fn(), |
| 83 | + addDashboardLinks = jest.fn(), |
| 84 | + deleteDashboardLinks = jest.fn(), |
| 85 | + setDashboardIsolatedLinkerPanelId = jest.fn(), |
| 86 | + setDashboardColumnSelectionValidator = jest.fn(), |
| 87 | +} = {}) { |
| 88 | + return render( |
| 89 | + <Linker |
| 90 | + links={links} |
| 91 | + timeZone={timeZone} |
| 92 | + activeTool={activeTool} |
| 93 | + localDashboardId={localDashboardId} |
| 94 | + layout={layout} |
| 95 | + panelManager={panelManager} |
| 96 | + setActiveTool={setActiveTool} |
| 97 | + setDashboardLinks={setDashboardLinks} |
| 98 | + addDashboardLinks={addDashboardLinks} |
| 99 | + deleteDashboardLinks={deleteDashboardLinks} |
| 100 | + setDashboardIsolatedLinkerPanelId={setDashboardIsolatedLinkerPanelId} |
| 101 | + setDashboardColumnSelectionValidator={ |
| 102 | + setDashboardColumnSelectionValidator |
| 103 | + } |
| 104 | + /> |
| 105 | + ); |
| 106 | +} |
| 107 | + |
| 108 | +it('closes Linker when escape key or Done button is pressed', async () => { |
| 109 | + const setActiveTool = jest.fn(); |
| 110 | + mountLinker({ setActiveTool }); |
| 111 | + const dialog = screen.getByTestId('linker-toast-dialog'); |
| 112 | + const buttons = await screen.findAllByRole('button'); |
| 113 | + expect(buttons).toHaveLength(3); |
| 114 | + |
| 115 | + const doneButton = screen.getByRole('button', { name: 'Done' }); |
| 116 | + fireEvent.click(doneButton); |
| 117 | + expect(setActiveTool).toHaveBeenCalledWith(ToolType.DEFAULT); |
| 118 | + |
| 119 | + fireEvent.keyDown(dialog, { key: 'Escape' }); |
| 120 | + expect(setActiveTool).toHaveBeenCalledWith(ToolType.DEFAULT); |
| 121 | +}); |
| 122 | + |
| 123 | +describe('tests link operations', () => { |
| 124 | + const deleteDashboardLinks = jest.fn(); |
| 125 | + const setDashboardLinks = jest.fn(); |
| 126 | + let linkPaths: HTMLElement[] = []; |
| 127 | + |
| 128 | + beforeEach(async () => { |
| 129 | + const links: Link[] = []; |
| 130 | + for (let i = 0; i < 4; i += 1) { |
| 131 | + const start = makeLinkPoint( |
| 132 | + 'PANEL_ID_A', |
| 133 | + `COLUMN_A`, |
| 134 | + 'int', |
| 135 | + 'PANEL_COMPONENT' |
| 136 | + ); |
| 137 | + const end = makeLinkPoint( |
| 138 | + 'PANEL_ID_B', |
| 139 | + `COLUMN_B_${i}`, |
| 140 | + 'long', |
| 141 | + 'PANEL_COMPONENT' |
| 142 | + ); |
| 143 | + const link = makeLink(start, end, `LINK_ID_${i}`, 'tableLink'); |
| 144 | + links.push(link); |
| 145 | + } |
| 146 | + mountLinker({ links, deleteDashboardLinks, setDashboardLinks }); |
| 147 | + linkPaths = screen.getAllByTestId('link-select'); |
| 148 | + expect(linkPaths).toHaveLength(4); |
| 149 | + }); |
| 150 | + |
| 151 | + it('deletes correct link with alt+click', async () => { |
| 152 | + expect(linkPaths).toHaveLength(4); |
| 153 | + fireEvent.click(linkPaths[0], { altKey: true }); |
| 154 | + expect(deleteDashboardLinks).toHaveBeenCalledWith('TEST_ID', ['LINK_ID_0']); |
| 155 | + }); |
| 156 | + |
| 157 | + it('deletes all links when Clear All is clicked', async () => { |
| 158 | + const clearAllButton = screen.getByRole('button', { name: 'Clear All' }); |
| 159 | + fireEvent.click(clearAllButton); |
| 160 | + expect(setDashboardLinks).toHaveBeenCalledWith('TEST_ID', []); |
| 161 | + }); |
| 162 | +}); |
0 commit comments