Skip to content
Snippets Groups Projects
Commit 0573eb9b authored by mateusz-winiarczyk's avatar mateusz-winiarczyk
Browse files

Merge branch 'MIN-257-center-map-button-behaviour-when-nothing-selected' into 'development'

fix(map): add map centering when polygonCoordinates does not exist (MIN-257)

See merge request !121
parents 46189b69 af9eaab1
No related branches found
No related tags found
2 merge requests!223reset the pin numbers before search results are fetch (so the results will be...,!121fix(map): add map centering when polygonCoordinates does not exist (MIN-257)
Pipeline #85296 passed
...@@ -6,6 +6,8 @@ import { getReduxStoreWithActionsListener } from '@/utils/testing/getReduxStoreA ...@@ -6,6 +6,8 @@ import { getReduxStoreWithActionsListener } from '@/utils/testing/getReduxStoreA
import { getReduxWrapperWithStore } from '@/utils/testing/getReduxWrapperWithStore'; import { getReduxWrapperWithStore } from '@/utils/testing/getReduxWrapperWithStore';
import { renderHook } from '@testing-library/react'; import { renderHook } from '@testing-library/react';
import Map from 'ol/Map'; import Map from 'ol/Map';
import { initialMapDataFixture } from '@/redux/map/map.fixtures';
import { modelsFixture } from '@/models/fixtures/modelsFixture';
import { useAddtionalActions } from './useAdditionalActions'; import { useAddtionalActions } from './useAdditionalActions';
import { useVisibleBioEntitiesPolygonCoordinates } from './useVisibleBioEntitiesPolygonCoordinates'; import { useVisibleBioEntitiesPolygonCoordinates } from './useVisibleBioEntitiesPolygonCoordinates';
...@@ -122,8 +124,54 @@ describe('useAddtionalActions - hook', () => { ...@@ -122,8 +124,54 @@ describe('useAddtionalActions - hook', () => {
useVisibleBioEntitiesPolygonCoordinatesMock.mockImplementation(() => undefined); useVisibleBioEntitiesPolygonCoordinatesMock.mockImplementation(() => undefined);
}); });
it('should return undefined', () => { it('should return undefined and center map by calculating coordinates when default center coordinates do not exist', () => {
const { Wrapper } = getReduxStoreWithActionsListener(INITIAL_STORE_STATE_MOCK); const MAP_CONFIG = {
size: {
width: 3500,
height: 2000,
},
zoom: {
minZoom: 2,
maxZoom: 9,
},
position: {
x: 1300,
y: 1900,
z: 7,
},
};
const { Wrapper, store } = getReduxStoreWithActionsListener({
...INITIAL_STORE_STATE_MOCK,
models: {
...INITIAL_STORE_STATE_MOCK.models,
data: [
{
...modelsFixture[0],
...MAP_CONFIG.size,
...MAP_CONFIG.zoom,
defaultCenterX: null,
defaultCenterY: null,
defaultZoomLevel: null,
},
],
},
map: {
...INITIAL_STORE_STATE_MOCK.map,
data: {
...initialMapDataFixture,
position: {
last: MAP_CONFIG.position,
initial: MAP_CONFIG.position,
},
size: {
...MAP_CONFIG.size,
...MAP_CONFIG.zoom,
tileSize: 256,
},
modelId: modelsFixture[0].idObject,
},
},
});
const { const {
result: { result: {
current: { zoomInToBioEntities }, current: { zoomInToBioEntities },
...@@ -131,8 +179,81 @@ describe('useAddtionalActions - hook', () => { ...@@ -131,8 +179,81 @@ describe('useAddtionalActions - hook', () => {
} = renderHook(() => useAddtionalActions(), { } = renderHook(() => useAddtionalActions(), {
wrapper: Wrapper, wrapper: Wrapper,
}); });
const result = zoomInToBioEntities();
expect(result).toBeUndefined();
const actions = store.getActions();
const position = store.getState().map?.data.position;
expect(position?.last).toEqual(MAP_CONFIG.position);
expect(actions[0]).toEqual({
payload: { x: 1750, y: 1000, z: 5 },
type: 'map/setMapPosition',
});
});
expect(zoomInToBioEntities()).toBeUndefined(); it('should return undefined and center map using default center coordinates if exist', () => {
const MAP_CONFIG = {
size: {
width: 5000,
height: 10000,
},
zoom: {
minZoom: 2,
maxZoom: 9,
},
position: {
x: 1300,
y: 1900,
z: 7,
},
};
const { Wrapper, store } = getReduxStoreWithActionsListener({
...INITIAL_STORE_STATE_MOCK,
models: {
...INITIAL_STORE_STATE_MOCK.models,
data: [
{
...modelsFixture[0],
...MAP_CONFIG.size,
...MAP_CONFIG.zoom,
defaultCenterX: 2500,
defaultCenterY: 5000,
defaultZoomLevel: 3,
},
],
},
map: {
...INITIAL_STORE_STATE_MOCK.map,
data: {
...initialMapDataFixture,
position: {
last: MAP_CONFIG.position,
initial: MAP_CONFIG.position,
},
size: {
...MAP_CONFIG.size,
...MAP_CONFIG.zoom,
tileSize: 256,
},
modelId: modelsFixture[0].idObject,
},
},
});
const {
result: {
current: { zoomInToBioEntities },
},
} = renderHook(() => useAddtionalActions(), {
wrapper: Wrapper,
});
const result = zoomInToBioEntities();
expect(result).toBeUndefined();
const actions = store.getActions();
const position = store.getState().map?.data.position;
expect(position?.last).toEqual(MAP_CONFIG.position);
expect(actions[0]).toEqual({
payload: { x: 2500, y: 5000, z: 3 },
type: 'map/setMapPosition',
});
}); });
}); });
}); });
......
import { varyPositionZoom } from '@/redux/map/map.slice'; import { setMapPosition, varyPositionZoom } from '@/redux/map/map.slice';
import { SetBoundsResult, useSetBounds } from '@/utils/map/useSetBounds'; import { SetBoundsResult, useSetBounds } from '@/utils/map/useSetBounds';
import { useCallback } from 'react'; import { useCallback } from 'react';
import { useDispatch } from 'react-redux'; import { useDispatch } from 'react-redux';
import { MAP_ZOOM_IN_DELTA, MAP_ZOOM_OUT_DELTA } from '../MappAdditionalActions.constants'; import { useAppSelector } from '@/redux/hooks/useAppSelector';
import { currentModelIdSelector, modelByIdSelector } from '@/redux/models/models.selectors';
import { DEFAULT_ZOOM } from '@/constants/map';
import { useVisibleBioEntitiesPolygonCoordinates } from './useVisibleBioEntitiesPolygonCoordinates'; import { useVisibleBioEntitiesPolygonCoordinates } from './useVisibleBioEntitiesPolygonCoordinates';
import { MAP_ZOOM_IN_DELTA, MAP_ZOOM_OUT_DELTA } from '../MappAdditionalActions.constants';
interface UseAddtionalActionsResult { interface UseAddtionalActionsResult {
zoomIn(): void; zoomIn(): void;
...@@ -15,13 +18,26 @@ export const useAddtionalActions = (): UseAddtionalActionsResult => { ...@@ -15,13 +18,26 @@ export const useAddtionalActions = (): UseAddtionalActionsResult => {
const dispatch = useDispatch(); const dispatch = useDispatch();
const setBounds = useSetBounds(); const setBounds = useSetBounds();
const polygonCoordinates = useVisibleBioEntitiesPolygonCoordinates(); const polygonCoordinates = useVisibleBioEntitiesPolygonCoordinates();
const currentMapModelId = useAppSelector(currentModelIdSelector);
const currentModel = useAppSelector(state => modelByIdSelector(state, currentMapModelId));
const zoomInToBioEntities = (): SetBoundsResult | undefined => { const zoomInToBioEntities = (): SetBoundsResult | undefined => {
if (!polygonCoordinates) { if (polygonCoordinates) {
return undefined; return setBounds(polygonCoordinates);
}
if (currentModel) {
const HALF = 2;
const defaultPosition = {
x: currentModel?.defaultCenterX ?? currentModel.width / HALF,
y: currentModel?.defaultCenterY ?? currentModel.height / HALF,
z: currentModel?.defaultZoomLevel ?? DEFAULT_ZOOM,
};
dispatch(setMapPosition(defaultPosition));
} }
return setBounds(polygonCoordinates); return undefined;
}; };
const varyZoomByDelta = useCallback( const varyZoomByDelta = useCallback(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment