Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

[chore] Export effects types/utils and incapsulate dnd logic into new hooks (#2384)

Igor Dykhta08492a8a6b16
diff --git a/effects.js b/effects.js
new file mode 100644
index 000000000..7372ce141
--- /dev/null
+++ b/effects.js
@@ -0,0 +1,21 @@
+// Copyright (c) 2023 Uber Technologies, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+module.exports = require('./dist/effects');
diff --git a/package.json b/package.json
index b484785b1..9807fdff6 100644
--- a/package.json
+++ b/package.json
@@ -150,7 +150,6 @@
"@testing-library/dom": "^9.0.1",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
- "@testing-library/react-hooks": "^3.4.2",
"@testing-library/user-event": "^14.4.3",
"@types/d3-array": "^2.0.0",
"@types/d3-scale": "^3.2.2",
diff --git a/src/components/src/dnd-context.tsx b/src/components/src/dnd-context.tsx
index ba441dd59..1a35dc2b1 100644
--- a/src/components/src/dnd-context.tsx
+++ b/src/components/src/dnd-context.tsx
@@ -1,32 +1,21 @@
-import React, {useCallback, useMemo, useState, PropsWithChildren} from 'react';
+import React, {useCallback, useMemo, PropsWithChildren} from 'react';
import styled from 'styled-components';
import {DndContext as DndKitContext, DragOverlay} from '@dnd-kit/core';
import Console from 'global/console';
-import {useDispatch} from 'react-redux';
+
import {
DND_EMPTY_MODIFIERS,
DND_MODIFIERS,
- DROPPABLE_MAP_CONTAINER_TYPE,
SORTABLE_LAYER_TYPE,
- SORTABLE_SIDE_PANEL_TYPE,
- SORTABLE_EFFECT_TYPE,
- SORTABLE_EFFECT_PANEL_TYPE
+ SORTABLE_EFFECT_TYPE
} from '@kepler.gl/constants';
-import {
- layerConfigChange,
- reorderLayer,
- toggleLayerForMap,
- updateEffect,
- reorderEffect
-} from '@kepler.gl/actions';
-import LayerPanelHeaderFactory from './side-panel/layer-panel/layer-panel-header';
-import {withState} from './injector';
import {visStateLens} from '@kepler.gl/reducers';
-import {reorderLayerOrder} from '@kepler.gl/reducers';
import {VisState} from '@kepler.gl/schemas';
-import {Layer} from '@kepler.gl/layers';
-import {Effect} from '@kepler.gl/types';
-import {reorderEffectOrder} from '@kepler.gl/utils';
+
+import LayerPanelHeaderFactory from './side-panel/layer-panel/layer-panel-header';
+import {withState} from './injector';
+import useDndLayers from './hooks/use-dnd-layers';
+import useDndEffects from './hooks/use-dnd-effects';
export type DndContextProps = PropsWithChildren<{
visState: VisState;
@@ -77,48 +66,18 @@ function DndContextFactory(
const DndContext = ({children, visState}: DndContextProps) => {
const {datasets, layerOrder, layers, effects, effectOrder, splitMaps} = visState;
- const [activeLayer, setActiveLayer]: [
- Layer | undefined,
- (l: Layer | undefined) => void
- ] = useState();
-
- const [activeEffect, setActiveEffect]: [
- Effect | undefined,
- (l: Effect | undefined) => void
- ] = useState();
+ const {activeLayer, onDragStart: onLayerDragStart, onDragEnd: onLayerDragEnd} = useDndLayers(
+ layers,
+ layerOrder
+ );
+ const {onDragStart: onEffectDragStart, onDragEnd: onEffectDragEnd} = useDndEffects(
+ effects,
+ effectOrder
+ );
- const dispatch = useDispatch();
const isSplit = useMemo(() => splitMaps?.length > 1, [splitMaps]);
const dndModifiers = useMemo(() => (isSplit ? DND_EMPTY_MODIFIERS : DND_MODIFIERS), [isSplit]);
- const onLayerDragStart = useCallback(
- event => {
- const {active} = event;
- const newActiveLayer = layers.find(layer => layer.id === active.id);
- if (newActiveLayer) {
- setActiveLayer(newActiveLayer);
- if (newActiveLayer?.config.isConfigActive) {
- dispatch(layerConfigChange(newActiveLayer, {isConfigActive: false}));
- }
- }
- },
- [dispatch, layers]
- );
-
- const onEffectDragStart = useCallback(
- event => {
- const {active} = event;
- const newActiveEffect = effects.find(effect => effect.id === active.id);
- if (newActiveEffect) {
- setActiveEffect(newActiveEffect);
- if (newActiveEffect.config.isConfigActive) {
- dispatch(updateEffect(newActiveEffect.id, {isConfigActive: false}));
- }
- }
- },
- [dispatch, effects]
- );
-
const onDragStart = useCallback(
event => {
const activeType = event.active.data?.current?.type;
@@ -136,88 +95,21 @@ function DndContextFactory(
[onLayerDragStart, onEffectDragStart]
);
- const onLayerDragEnd = useCallback(
+ const onDragEnd = useCallback(
event => {
- const {active, over} = event;
-
- const {id: activeLayerId} = active;
- const overType = over?.data.current?.type;
-
- if (!overType) {
- setActiveLayer(undefined);
- return;
- }
-
- switch (overType) {
- // moving layers into maps
- case DROPPABLE_MAP_CONTAINER_TYPE:
- const mapIndex = over.data.current?.index ?? 0;
- dispatch(toggleLayerForMap(mapIndex, activeLayerId));
- break;
- // swaping layers
+ const activeType = event.active.data?.current?.type;
+ switch (activeType) {
case SORTABLE_LAYER_TYPE:
- const newLayerOrder = reorderLayerOrder(layerOrder, activeLayerId, over.id);
- dispatch(reorderLayer(newLayerOrder));
- break;
- // moving layers within side panel
- case SORTABLE_SIDE_PANEL_TYPE:
- // move layer to the end of the list
- dispatch(
- reorderLayer(
- reorderLayerOrder(layerOrder, activeLayerId, layerOrder[layerOrder.length - 1])
- )
- );
+ onLayerDragEnd(event);
break;
- default:
- break;
- }
-
- setActiveLayer(undefined);
- },
- [dispatch, layerOrder]
- );
-
- const onEffectDragEnd = useCallback(
- event => {
- const {active, over} = event;
-
- const {id: activeEffectId} = active;
- const overType = over?.data.current?.type;
-
- if (!overType) {
- setActiveEffect(undefined);
- return;
- }
-
- switch (overType) {
- // swaping effects
case SORTABLE_EFFECT_TYPE:
- dispatch(reorderEffect(reorderEffectOrder(effectOrder, activeEffectId, over.id)));
- break;
- // moving effects within side panel
- case SORTABLE_EFFECT_PANEL_TYPE:
- // move effect to the end of the list
- dispatch(
- reorderEffect(
- reorderEffectOrder(effectOrder, activeEffectId, effectOrder[effectOrder.length - 1])
- )
- );
+ onEffectDragEnd(event);
break;
default:
- break;
+ Console.log(`activeType ${activeType} unknown`);
}
-
- setActiveEffect(undefined);
- },
- [dispatch, effectOrder]
- );
-
- const onDragEnd = useCallback(
- event => {
- if (activeLayer) onLayerDragEnd(event);
- if (activeEffect) onEffectDragEnd(event);
},
- [activeLayer, activeEffect, onLayerDragEnd, onEffectDragEnd]
+ [onLayerDragEnd, onEffectDragEnd]
);
return (
diff --git a/src/components/src/hooks/use-dnd-effects.tsx b/src/components/src/hooks/use-dnd-effects.tsx
new file mode 100644
index 000000000..e93e1055b
--- /dev/null
+++ b/src/components/src/hooks/use-dnd-effects.tsx
@@ -0,0 +1,74 @@
+import {useCallback, useState} from 'react';
+import {useDispatch} from 'react-redux';
+import {DragEndEvent, DragStartEvent} from '@dnd-kit/core';
+
+import {reorderEffect, updateEffect} from '@kepler.gl/actions';
+import {SORTABLE_EFFECT_PANEL_TYPE, SORTABLE_EFFECT_TYPE} from '@kepler.gl/constants';
+import {reorderEffectOrder} from '@kepler.gl/utils';
+import {Effect} from '@kepler.gl/types';
+
+type DndEffectsHook = {
+ activeEffect: Effect | undefined;
+ onDragStart: (event: DragStartEvent) => void;
+ onDragEnd: (event: DragEndEvent) => void;
+};
+
+const useDndEffects: (effects: Effect[], effectOrder: string[]) => DndEffectsHook = (
+ effects,
+ effectOrder
+) => {
+ const dispatch = useDispatch();
+ const [activeEffect, setActiveEffect]: [activeEffect: Effect | undefined, setActiveEffect: (effect: Effect | undefined) => void] = useState();
+ const onEffectDragStart = useCallback(
+ event => {
+ const {active} = event;
+ const newActiveEffect = effects.find(effect => effect.id === active.id);
+ if (newActiveEffect) {
+ setActiveEffect(newActiveEffect);
+ if (newActiveEffect.config.isConfigActive) {
+ dispatch(updateEffect(newActiveEffect.id, {isConfigActive: false}));
+ }
+ }
+ },
+ [dispatch, effects]
+ );
+
+ const onEffectDragEnd = useCallback(
+ event => {
+ const {active, over} = event;
+
+ const {id: activeEffectId} = active;
+ const overType = over?.data?.current?.type;
+
+ if (!overType) {
+ setActiveEffect(undefined);
+ return;
+ }
+
+ switch (overType) {
+ // swaping effects
+ case SORTABLE_EFFECT_TYPE:
+ dispatch(reorderEffect(reorderEffectOrder(effectOrder, activeEffectId, over.id)));
+ break;
+ // moving effects within side panel
+ case SORTABLE_EFFECT_PANEL_TYPE:
+ // move effect to the end of the list
+ dispatch(
+ reorderEffect(
+ reorderEffectOrder(effectOrder, activeEffectId, effectOrder[effectOrder.length - 1])
+ )
+ );
+ break;
+ default:
+ break;
+ }
+
+ setActiveEffect(undefined);
+ },
+ [dispatch, effectOrder]
+ );
+
+ return {activeEffect, onDragStart: onEffectDragStart, onDragEnd: onEffectDragEnd};
+};
+
+export default useDndEffects;
diff --git a/src/components/src/hooks/use-dnd-layers.tsx b/src/components/src/hooks/use-dnd-layers.tsx
new file mode 100644
index 000000000..6c7b2b72e
--- /dev/null
+++ b/src/components/src/hooks/use-dnd-layers.tsx
@@ -0,0 +1,82 @@
+import {useCallback, useState} from 'react';
+import {useDispatch} from 'react-redux';
+import {DragEndEvent, DragStartEvent} from '@dnd-kit/core';
+import {layerConfigChange, reorderLayer, toggleLayerForMap} from '@kepler.gl/actions';
+import {
+ DROPPABLE_MAP_CONTAINER_TYPE,
+ SORTABLE_LAYER_TYPE,
+ SORTABLE_SIDE_PANEL_TYPE
+} from '@kepler.gl/constants';
+import {reorderLayerOrder} from '@kepler.gl/reducers';
+import {Layer} from '@kepler.gl/layers';
+
+type DndEffectsHook = {
+ activeLayer: Layer | undefined;
+ onDragStart: (event: DragStartEvent) => void;
+ onDragEnd: (event: DragEndEvent) => void;
+};
+
+const useDndLayers: (layers: Layer[], layerOrder: string[]) => DndEffectsHook = (layers, layerOrder) => {
+ const dispatch = useDispatch();
+
+ const [activeLayer, setActiveLayer]: [activeEffect: Layer | undefined, setActiveEffect: (effect: Layer | undefined) => void] = useState();
+
+ const onDragStart = useCallback(
+ event => {
+ const {active} = event;
+ const newActiveLayer = layers.find(layer => layer.id === active.id);
+ if (newActiveLayer) {
+ setActiveLayer(newActiveLayer);
+ if (newActiveLayer?.config.isConfigActive) {
+ dispatch(layerConfigChange(newActiveLayer, {isConfigActive: false}));
+ }
+ }
+ },
+ [dispatch, layers]
+ );
+
+ const onDragEnd = useCallback(
+ event => {
+ const {active, over} = event;
+
+ const {id: activeLayerId} = active;
+ const overType = over?.data?.current?.type;
+
+ if (!overType) {
+ setActiveLayer(undefined);
+ return;
+ }
+
+ switch (overType) {
+ // moving layers into maps
+ case DROPPABLE_MAP_CONTAINER_TYPE:
+ const mapIndex = over.data.current?.index ?? 0;
+ dispatch(toggleLayerForMap(mapIndex, activeLayerId));
+ break;
+ // swaping layers
+ case SORTABLE_LAYER_TYPE:
+ const newLayerOrder = reorderLayerOrder(layerOrder, activeLayerId, over.id);
+ dispatch(reorderLayer(newLayerOrder));
+ break;
+ // moving layers within side panel
+ case SORTABLE_SIDE_PANEL_TYPE:
+ // move layer to the end of the list
+ dispatch(
+ reorderLayer(
+ reorderLayerOrder(layerOrder, activeLayerId, layerOrder[layerOrder.length - 1])
+ )
+ );
+ break;
+ default:
+ break;
+ }
+
+ setActiveLayer(undefined);
+ },
+ [dispatch, layerOrder]
+ );
+
+ return {activeLayer, onDragStart, onDragEnd};
+};
+
+export default useDndLayers;
diff --git a/src/components/src/hooks/use-feature-flags.tsx b/src/components/src/hooks/use-feature-flags.tsx
new file mode 100644
index 000000000..699027bc4
--- /dev/null
+++ b/src/components/src/hooks/use-feature-flags.tsx
@@ -0,0 +1,12 @@
+import {useContext} from 'react';
+import {FeatureFlagsContext} from '../context';
+
+type FeatureFlags = {
+ // Define your feature flags here
+ [flagName: string]: boolean;
+};
+
+// @ts-expect-error FeatureFlagsContext is typed as object
+const useFeatureFlags = () => useContext<FeatureFlags>(FeatureFlagsContext);
+
+export default useFeatureFlags;
diff --git a/src/components/src/hooks/useFeatureFlags.ts b/src/components/src/hooks/useFeatureFlags.ts
deleted file mode 100644
index 0b781053f..000000000
--- a/src/components/src/hooks/useFeatureFlags.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-import {useContext} from 'react';
-import {FeatureFlagsContext} from '../context';
-
-const useFeatureFlags = () => useContext(FeatureFlagsContext);
-
-export default useFeatureFlags;
diff --git a/src/components/src/index.ts b/src/components/src/index.ts
index 8d577e98d..86b43d23e 100644
--- a/src/components/src/index.ts
+++ b/src/components/src/index.ts
@@ -383,3 +383,8 @@ export {
LayerColorRangeSelectorFactory,
ArcLayerColorSelectorFactory
};
+
+// hooks
+export {default as useFeatureFlags} from './hooks/use-feature-flags';
+export {default as useDndLayers} from './hooks/use-dnd-layers';
+export {default as useDndEffects} from './hooks/use-dnd-effects';
\ No newline at end of file
diff --git a/test/browser/components/hooks/use-dnd-effects.spec.js b/test/browser/components/hooks/use-dnd-effects.spec.js
new file mode 100644
index 000000000..e2f5627f4
--- /dev/null
+++ b/test/browser/components/hooks/use-dnd-effects.spec.js
@@ -0,0 +1,201 @@
+// Copyright (c) 2023 Uber Technologies, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+import React from 'react';
+import {renderHook, act} from '@testing-library/react';
+import {useDispatch} from 'react-redux';
+import {useDndEffects} from '@kepler.gl/components';
+import {reorderEffect, updateEffect} from '@kepler.gl/actions';
+import {SORTABLE_EFFECT_PANEL_TYPE, SORTABLE_EFFECT_TYPE} from '@kepler.gl/constants';
+import {reorderEffectOrder} from '@kepler.gl/utils';
+
+// Mock useDispatch hook
+jest.mock('react-redux', () => ({
+ ...jest.requireActual('react-redux'),
+ useDispatch: jest.fn()
+}));
+
+// Mock dependencies
+jest.mock('@kepler.gl/actions', () => ({
+ ...jest.requireActual('@kepler.gl/actions'),
+ reorderEffect: jest.fn(),
+ updateEffect: jest.fn()
+}));
+
+jest.mock('@kepler.gl/utils', () => ({
+ ...jest.requireActual('@kepler.gl/utils'),
+ reorderEffectOrder: jest.fn()
+}));
+
+describe('useDndEffects', () => {
+ const effects = [
+ {id: 1, name: 'Effect 1', config: {isConfigActive: false}},
+ {id: 2, name: 'Effect 2', config: {isConfigActive: false}},
+ {id: 3, name: 'Effect 3', config: {isConfigActive: false}}
+ ];
+
+ const effectOrder = [2, 1, 3];
+
+ const dispatchMock = jest.fn();
+
+ beforeEach(() => {
+ useDispatch.mockReturnValue(dispatchMock);
+ });
+
+ afterEach(() => {
+ useDispatch.mockReset();
+ dispatchMock.mockReset();
+ reorderEffect.mockReset();
+ updateEffect.mockReset();
+ reorderEffectOrder.mockReset();
+ });
+
+ test('should initialize with correct initial state', () => {
+ const {result} = renderHook(() => useDndEffects(effects, effectOrder));
+
+ expect(result.current.activeEffect).toBeUndefined();
+ expect(result.current.onDragStart).toBeInstanceOf(Function);
+ expect(result.current.onDragEnd).toBeInstanceOf(Function);
+ });
+
+ test('onDragStart should update activeEffect and dispatch updateEffect if config is active', () => {
+ const currentEffects = [
+ {id: 1, name: 'Effect 1', config: {isConfigActive: false}},
+ {id: 2, name: 'Effect 2', config: {isConfigActive: true}},
+ {id: 3, name: 'Effect 3', config: {isConfigActive: false}}
+ ];
+ const {result} = renderHook(() => useDndEffects(currentEffects, effectOrder));
+
+ const event = {
+ active: {id: 2}
+ };
+
+ act(() => {
+ result.current.onDragStart(event);
+ });
+
+ expect(result.current.activeEffect).toEqual(currentEffects[1]);
+ expect(dispatchMock).toHaveBeenCalledTimes(1);
+ expect(updateEffect).toHaveBeenCalledWith(currentEffects[1].id, {isConfigActive: false});
+ });
+
+ test('onDragStart should not dispatch updateEffect if config is not active', () => {
+ const {result} = renderHook(() => useDndEffects(effects, effectOrder));
+
+ const event = {
+ active: {id: 1}
+ };
+
+ act(() => {
+ result.current.onDragStart(event);
+ });
+
+ expect(result.current.activeEffect).toEqual(effects[0]);
+ expect(dispatchMock).not.toHaveBeenCalled();
+ expect(updateEffect).not.toHaveBeenCalled();
+ });
+
+ test('onDragEnd should reset activeEffect if overType is not defined', () => {
+ const {result} = renderHook(() => useDndEffects(effects, effectOrder));
+
+ const event = {
+ active: {id: 2},
+ over: {}
+ };
+
+ act(() => {
+ result.current.onDragEnd(event);
+ });
+
+ expect(result.current.activeEffect).toBeUndefined();
+ });
+
+ test('onDragEnd should dispatch reorderEffect when overType is SORTABLE_EFFECT_TYPE', () => {
+ const {result} = renderHook(() => useDndEffects(effects, effectOrder));
+
+ const event = {
+ active: {id: 2},
+ over: {
+ data: {
+ current: {
+ type: SORTABLE_EFFECT_TYPE
+ }
+ }
+ }
+ };
+
+ const newEffectOrder = [1, 2, 3];
+ reorderEffectOrder.mockReturnValue(newEffectOrder);
+
+ act(() => {
+ result.current.onDragEnd(event);
+ });
+
+ expect(dispatchMock).toHaveBeenCalledTimes(1);
+ expect(reorderEffect).toHaveBeenCalledWith(newEffectOrder);
+ });
+
+ test('onDragEnd should dispatch reorderEffect when overType is SORTABLE_EFFECT_PANEL_TYPE', () => {
+ const {result} = renderHook(() => useDndEffects(effects, effectOrder));
+
+ const event = {
+ active: {id: 2},
+ over: {
+ data: {
+ current: {
+ type: SORTABLE_EFFECT_PANEL_TYPE
+ }
+ }
+ }
+ };
+
+ const newEffectOrder = [1, 3, 2];
+ reorderEffectOrder.mockReturnValue(newEffectOrder);
+
+ act(() => {
+ result.current.onDragEnd(event);
+ });
+
+ expect(dispatchMock).toHaveBeenCalledTimes(1);
+ expect(reorderEffect).toHaveBeenCalledWith(newEffectOrder);
+ });
+
+ test('onDragEnd should do nothing if overType does not match any case', () => {
+ const {result} = renderHook(() => useDndEffects(effects, effectOrder));
+
+ const event = {
+ active: {id: 2},
+ over: {
+ data: {
+ current: {
+ type: 'UNKNOWN_TYPE'
+ }
+ }
+ }
+ };
+
+ act(() => {
+ result.current.onDragEnd(event);
+ });
+
+ expect(dispatchMock).not.toHaveBeenCalled();
+ expect(reorderEffect).not.toHaveBeenCalled();
+ });
+});
diff --git a/test/browser/components/hooks/use-dnd-layers.spec.js b/test/browser/components/hooks/use-dnd-layers.spec.js
new file mode 100644
index 000000000..386ed1068
--- /dev/null
+++ b/test/browser/components/hooks/use-dnd-layers.spec.js
@@ -0,0 +1,230 @@
+// Copyright (c) 2023 Uber Technologies, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+import React from 'react';
+import {renderHook, act} from '@testing-library/react';
+import {useDispatch} from 'react-redux';
+import {useDndLayers} from '@kepler.gl/components';
+import {layerConfigChange, reorderLayer, toggleLayerForMap} from '@kepler.gl/actions';
+import {
+ DROPPABLE_MAP_CONTAINER_TYPE,
+ SORTABLE_LAYER_TYPE,
+ SORTABLE_SIDE_PANEL_TYPE
+} from '@kepler.gl/constants';
+import {reorderLayerOrder} from '@kepler.gl/reducers';
+
+// Mock useDispatch hook
+jest.mock('react-redux', () => ({
+ ...jest.requireActual('react-redux'),
+ useDispatch: jest.fn()
+}));
+
+// Mock dependencies
+jest.mock('@kepler.gl/actions', () => ({
+ ...jest.requireActual('@kepler.gl/actions'),
+ layerConfigChange: jest.fn(),
+ reorderLayer: jest.fn(),
+ toggleLayerForMap: jest.fn()
+}));
+
+jest.mock('@kepler.gl/reducers', () => ({
+ reorderLayerOrder: jest.fn()
+}));
+
+describe('useDndLayers', () => {
+ const layers = [
+ {id: 1, name: 'Layer 1', config: {isConfigActive: false}},
+ {id: 2, name: 'Layer 2', config: {isConfigActive: false}},
+ {id: 3, name: 'Layer 3', config: {isConfigActive: false}}
+ ];
+
+ const layerOrder = [2, 1, 3];
+
+ const dispatchMock = jest.fn();
+
+ beforeEach(() => {
+ useDispatch.mockReturnValue(dispatchMock);
+ });
+
+ afterEach(() => {
+ useDispatch.mockReset();
+ dispatchMock.mockReset();
+ layerConfigChange.mockReset();
+ reorderLayer.mockReset();
+ toggleLayerForMap.mockReset();
+ reorderLayerOrder.mockReset();
+ });
+
+ test('should initialize with correct initial state', () => {
+ const {result} = renderHook(() => useDndLayers(layers, layerOrder));
+
+ expect(result.current.activeLayer).toBeUndefined();
+ expect(result.current.onDragStart).toBeInstanceOf(Function);
+ expect(result.current.onDragEnd).toBeInstanceOf(Function);
+ });
+
+ test('onDragStart should update activeLayer and dispatch layerConfigChange if config is active', () => {
+ const currentLayers = [
+ {id: 1, name: 'Layer 1', config: {isConfigActive: false}},
+ {id: 2, name: 'Layer 2', config: {isConfigActive: true}},
+ {id: 3, name: 'Layer 3', config: {isConfigActive: false}}
+ ];
+ const {result} = renderHook(() => useDndLayers(currentLayers, layerOrder));
+
+ const event = {
+ active: {id: 2}
+ };
+
+ act(() => {
+ result.current.onDragStart(event);
+ });
+
+ expect(result.current.activeLayer).toEqual(currentLayers[1]);
+ expect(dispatchMock).toHaveBeenCalledTimes(1);
+ expect(layerConfigChange).toHaveBeenCalledWith(currentLayers[1], {isConfigActive: false});
+ });
+
+ test('onDragStart should not dispatch layerConfigChange if config is not active', () => {
+ const {result} = renderHook(() => useDndLayers(layers, layerOrder));
+
+ const event = {
+ active: {id: 1}
+ };
+
+ act(() => {
+ result.current.onDragStart(event);
+ });
+
+ expect(result.current.activeLayer).toEqual(layers[0]);
+ expect(dispatchMock).not.toHaveBeenCalled();
+ expect(layerConfigChange).not.toHaveBeenCalled();
+ });
+
+ test('onDragEnd should reset activeLayer if overType is not defined', () => {
+ const {result} = renderHook(() => useDndLayers(layers, layerOrder));
+
+ const event = {
+ active: {id: 2},
+ over: {}
+ };
+
+ act(() => {
+ result.current.onDragEnd(event);
+ });
+
+ expect(result.current.activeLayer).toBeUndefined();
+ });
+
+ test('onDragEnd should dispatch toggleLayerForMap when overType is DROPPABLE_MAP_CONTAINER_TYPE', () => {
+ const {result} = renderHook(() => useDndLayers(layers, layerOrder));
+
+ const event = {
+ active: {id: 1},
+ over: {
+ data: {
+ current: {
+ type: DROPPABLE_MAP_CONTAINER_TYPE,
+ index: 1
+ }
+ }
+ }
+ };
+
+ act(() => {
+ result.current.onDragEnd(event);
+ });
+
+ expect(dispatchMock).toHaveBeenCalledTimes(1);
+ expect(toggleLayerForMap).toHaveBeenCalledWith(1, 1);
+ });
+
+ test('onDragEnd should dispatch reorderLayer when overType is SORTABLE_LAYER_TYPE', () => {
+ const {result} = renderHook(() => useDndLayers(layers, layerOrder));
+
+ const event = {
+ active: {id: 2},
+ over: {
+ data: {
+ current: {
+ type: SORTABLE_LAYER_TYPE
+ }
+ }
+ }
+ };
+
+ const newLayerOrder = [1, 2, 3];
+ reorderLayerOrder.mockReturnValue(newLayerOrder);
+
+ act(() => {
+ result.current.onDragEnd(event);
+ });
+
+ expect(dispatchMock).toHaveBeenCalledTimes(1);
+ expect(reorderLayer).toHaveBeenCalledWith(newLayerOrder);
+ });
+
+ test('onDragEnd should dispatch reorderLayer when overType is SORTABLE_SIDE_PANEL_TYPE', () => {
+ const {result} = renderHook(() => useDndLayers(layers, layerOrder));
+
+ const event = {
+ active: {id: 2},
+ over: {
+ data: {
+ current: {
+ type: SORTABLE_SIDE_PANEL_TYPE
+ }
+ }
+ }
+ };
+
+ const newLayerOrder = [1, 3, 2];
+ reorderLayerOrder.mockReturnValue(newLayerOrder);
+
+ act(() => {
+ result.current.onDragEnd(event);
+ });
+
+ expect(dispatchMock).toHaveBeenCalledTimes(1);
+ expect(reorderLayer).toHaveBeenCalledWith(newLayerOrder);
+ });
+
+ test('onDragEnd should do nothing if overType does not match any case', () => {
+ const {result} = renderHook(() => useDndLayers(layers, layerOrder));
+
+ const event = {
+ active: {id: 2},
+ over: {
+ data: {
+ current: {
+ type: 'UNKNOWN_TYPE'
+ }
+ }
+ }
+ };
+
+ act(() => {
+ result.current.onDragEnd(event);
+ });
+
+ expect(dispatchMock).not.toHaveBeenCalled();
+ expect(reorderLayer).not.toHaveBeenCalled();
+ expect(toggleLayerForMap).not.toHaveBeenCalled();
+ });
+});
diff --git a/yarn.lock b/yarn.lock
index f52df80b0..1fd6b0183 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -2861,14 +2861,6 @@
lodash "^4.17.15"
redent "^3.0.0"
-"@testing-library/react-hooks@^3.4.2":
- version "3.7.0"
- resolved "https://registry.yarnpkg.com/@testing-library/react-hooks/-/react-hooks-3.7.0.tgz#6d75c5255ef49bce39b6465bf6b49e2dac84919e"
- integrity sha512-TwfbY6BWtWIHitjT05sbllyLIProcysC0dF0q1bbDa7OHLC6A6rJOYJwZ13hzfz3O4RtOuInmprBozJRyyo7/g==
- dependencies:
- "@babel/runtime" "^7.12.5"
- "@types/testing-library__react-hooks" "^3.4.0"
-
"@testing-library/react@^14.0.0":
version "14.0.0"
resolved "https://registry.yarnpkg.com/@testing-library/react/-/react-14.0.0.tgz#59030392a6792450b9ab8e67aea5f3cc18d6347c"
@@ -3781,13 +3773,6 @@
hoist-non-react-statics "^3.3.0"
redux "^4.0.0"
-"@types/react-test-renderer@*":
- version "17.0.1"
- resolved "https://registry.yarnpkg.com/@types/react-test-renderer/-/react-test-renderer-17.0.1.tgz#3120f7d1c157fba9df0118dae20cb0297ee0e06b"
- integrity sha512-3Fi2O6Zzq/f3QR9dRnlnHso9bMl7weKCviFmfF6B4LS1Uat6Hkm15k0ZAQuDz+UBq6B3+g+NM6IT2nr5QgPzCw==
- dependencies:
- "@types/react" "*"
-
"@types/react-virtualized@^9.21.23":
version "9.21.23"
resolved "https://registry.yarnpkg.com/@types/react-virtualized/-/react-virtualized-9.21.23.tgz#62c4b20b796351eca8e2f816c263b39b69ed97d0"
@@ -3865,13 +3850,6 @@
dependencies:
"@types/jest" "*"
-"@types/testing-library__react-hooks@^3.4.0":
- version "3.4.1"
- resolved "https://registry.yarnpkg.com/@types/testing-library__react-hooks/-/testing-library__react-hooks-3.4.1.tgz#b8d7311c6c1f7db3103e94095fe901f8fef6e433"
- integrity sha512-G4JdzEcq61fUyV6wVW9ebHWEiLK2iQvaBuCHHn9eMSbZzVh4Z4wHnUGIvQOYCCYeu5DnUtFyNYuAAgbSaO/43Q==
- dependencies:
- "@types/react-test-renderer" "*"
-
"@types/tough-cookie@*":
version "4.0.3"
resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-4.0.3.tgz#3d06b6769518450871fbc40770b7586334bdfd90"