Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Maps] fix Do not allow opening multiple tooltips for single feature #149254

Merged
merged 5 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions x-pack/plugins/maps/public/actions/tooltip_actions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { TooltipState } from '../../common/descriptor_types';
import { openOnClickTooltip } from './tooltip_actions';
import { MapStoreState } from '../reducers/store';

describe('openOnClickTooltip', () => {
const newTooltip = {
features: [
{
id: 'feature1',
layerId: 'layer1',
},
],
id: 'tooltip1',
isLocked: true,
location: [0, 0],
} as unknown as TooltipState;

test('should add tooltip to open tooltips', () => {
const openTooltip = {
features: [
{
id: 'feature2',
layerId: 'layer1',
},
],
id: 'tooltip2',
isLocked: true,
location: [1, 1],
} as unknown as TooltipState;
const action = openOnClickTooltip(newTooltip);
const dispatchMock = jest.fn();
action(dispatchMock, () => {
return {
map: {
openTooltips: [openTooltip],
},
} as unknown as MapStoreState;
});
expect(dispatchMock.mock.calls[0][0]).toEqual({
openTooltips: [openTooltip, newTooltip],
type: 'SET_OPEN_TOOLTIPS',
});
});

test('should remove existing mouseover tooltips when adding locked tooltips', () => {
const action = openOnClickTooltip(newTooltip);
const dispatchMock = jest.fn();
action(dispatchMock, () => {
return {
map: {
openTooltips: [
{
features: [
{
id: 'feature2',
layerId: 'layer1',
},
],
id: 'tooltip2',
isLocked: false, // mouseover tooltip
location: [1, 1],
},
],
},
} as unknown as MapStoreState;
});
expect(dispatchMock.mock.calls[0][0]).toEqual({
openTooltips: [newTooltip],
type: 'SET_OPEN_TOOLTIPS',
});
});

test('should remove existing tooltip when adding new tooltip at same location', () => {
const action = openOnClickTooltip(newTooltip);
const dispatchMock = jest.fn();
action(dispatchMock, () => {
return {
map: {
openTooltips: [
{
features: [
{
id: 'feature2',
layerId: 'layer1',
},
],
id: 'tooltip2',
isLocked: true,
location: [0, 0], // same location as newTooltip
},
],
},
} as unknown as MapStoreState;
});
expect(dispatchMock.mock.calls[0][0]).toEqual({
openTooltips: [newTooltip],
type: 'SET_OPEN_TOOLTIPS',
});
});

test('should remove existing tooltip when adding new tooltip with same features', () => {
const action = openOnClickTooltip(newTooltip);
const dispatchMock = jest.fn();
action(dispatchMock, () => {
return {
map: {
openTooltips: [
{
features: [
{
id: 'feature1',
layerId: 'layer1',
// ensure new props do not break equality check
newProp: 'someValue',
},
],
id: 'tooltip2',
isLocked: true,
location: [1, 1],
},
],
},
} as unknown as MapStoreState;
});
expect(dispatchMock.mock.calls[0][0]).toEqual({
openTooltips: [newTooltip],
type: 'SET_OPEN_TOOLTIPS',
});
});
});
9 changes: 8 additions & 1 deletion x-pack/plugins/maps/public/actions/tooltip_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ export function openOnClickTooltip(tooltipState: TooltipState) {
return (
isLocked &&
!_.isEqual(location, tooltipState.location) &&
!_.isEqual(features, tooltipState.features)
!_.isEqual(
features.map(({ id, layerId }) => {
return { id, layerId };
}),
tooltipState.features.map(({ id, layerId }) => {
return { id, layerId };
})
)
);
});

Expand Down