-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dashboard): fix styling for component palette and add tests for d…
…rag and drop
- Loading branch information
Showing
3 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
} | ||
|
||
.palette-component-draggable { | ||
cursor: pointer; | ||
cursor: grab; | ||
display: flex; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
packages/dashboard/src/components/palette/index.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import React from 'react'; | ||
|
||
import { screen } from '@testing-library/dom'; | ||
import { render } from '@testing-library/react'; | ||
|
||
import { act } from 'react-dom/test-utils'; | ||
|
||
import { fireEvent } from '@testing-library/react'; | ||
import { Provider } from 'react-redux'; | ||
import { DndProvider } from 'react-dnd'; | ||
import { HTML5Backend } from 'react-dnd-html5-backend'; | ||
import { RecursivePartial } from '@iot-app-kit/table'; | ||
|
||
import { DASHBOARD_CONTAINER_ID } from '../grid/getDashboardPosition'; | ||
import InternalDashboard from '../internalDashboard'; | ||
import { DefaultDashboardMessages } from '../../messages'; | ||
|
||
import { configureDashboardStore } from '../../store'; | ||
|
||
import { DashboardState } from '../../store/state'; | ||
import { setupDashboardPlugins } from '../../customization/api'; | ||
import pluginsConfiguration from '../../customization/pluginsConfiguration'; | ||
|
||
const renderDashboard = (state?: RecursivePartial<DashboardState>) => { | ||
setupDashboardPlugins(pluginsConfiguration); | ||
|
||
const store = configureDashboardStore(state); | ||
|
||
const { container } = render( | ||
<Provider store={store}> | ||
<DndProvider | ||
backend={HTML5Backend} | ||
options={{ | ||
enableMouseEvents: true, | ||
enableKeyboardEvents: true, | ||
}} | ||
> | ||
<InternalDashboard hasEditPermission={false} query={undefined} messageOverrides={DefaultDashboardMessages} /> | ||
</DndProvider> | ||
</Provider> | ||
); | ||
|
||
return { container, store }; | ||
}; | ||
|
||
describe('Component Palette', () => { | ||
it('can drag widgets onto the grid', function () { | ||
const { container, store } = renderDashboard(); | ||
|
||
const draggableLine = screen.getByText('Line').previousElementSibling; | ||
const draggableText = screen.getByText('Text').previousElementSibling; | ||
|
||
const grid = container.querySelector(`#${DASHBOARD_CONTAINER_ID}`); | ||
|
||
if (!draggableLine || !draggableText || !grid) throw new Error('could not get draggable elements for test'); | ||
|
||
expect(store.getState().dashboardConfiguration.widgets).toHaveLength(0); | ||
|
||
act(() => { | ||
fireEvent.dragStart(draggableLine); | ||
fireEvent.dragEnter(grid); | ||
fireEvent.dragOver(grid); | ||
fireEvent.drop(grid); | ||
}); | ||
|
||
expect(store.getState().dashboardConfiguration.widgets).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ | ||
type: 'iot-line', | ||
}), | ||
]) | ||
); | ||
|
||
act(() => { | ||
fireEvent.dragStart(draggableText); | ||
fireEvent.dragEnter(grid); | ||
fireEvent.dragOver(grid); | ||
fireEvent.drop(grid); | ||
}); | ||
|
||
expect(store.getState().dashboardConfiguration.widgets).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ | ||
type: 'iot-line', | ||
}), | ||
expect.objectContaining({ | ||
type: 'text', | ||
}), | ||
]) | ||
); | ||
}); | ||
|
||
it('does not add a widget if dropped outside of grid', function () { | ||
const { container, store } = renderDashboard(); | ||
|
||
const draggable = screen.getByText('Line').previousElementSibling; | ||
|
||
const componentPalette = container.querySelector('.component-palette'); | ||
|
||
if (!draggable || !componentPalette) throw new Error('could not get draggable elements for test'); | ||
|
||
expect(store.getState().dashboardConfiguration.widgets).toHaveLength(0); | ||
|
||
act(() => { | ||
fireEvent.dragStart(draggable); | ||
fireEvent.dragEnter(componentPalette); | ||
fireEvent.dragOver(componentPalette); | ||
fireEvent.drop(componentPalette); | ||
}); | ||
|
||
expect(store.getState().dashboardConfiguration.widgets).toHaveLength(0); | ||
}); | ||
}); |