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

Fix visualization chart not expanded after legend toggle #9170

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions changelogs/fragments/9170.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Visualization chart not expanded after legend toggle ([#9170](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/9170))
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import { act } from 'react-dom/test-utils';
import { unmountComponentAtNode } from 'react-dom';
import { setTypes } from '../services';
import { TypesService } from '../vis_types';
import { PersistedState } from '../persisted_state';
import { ExprVis } from './vis';
import { visualization } from './visualization_renderer';

jest.mock('../components', () => ({
Visualization: ({ visData, visParams, uiState, listenOnChange }) => (
<div>
<span>Visualization</span>
<span>{JSON.stringify(visData)}</span>
<span>{JSON.stringify(visParams)}</span>
<span>{JSON.stringify(uiState)}</span>
<span>{listenOnChange.toString()}</span>
</div>
),
}));

describe('visualization', () => {
let domNode: HTMLDivElement;
let handlers: Parameters<ReturnType<typeof visualization>['render']>[2];

beforeAll(() => {
const typeService = new TypesService();
const typeSetup = typeService.setup();
const typeStart = typeService.start();
typeSetup.createReactVisualization({
name: 'pie',
title: 'Controls',
icon: 'controlsHorizontal',
stage: 'experimental',
visConfig: {
defaults: {
controls: [],
updateFiltersOnChange: false,
useTimeFilter: false,
pinFilters: false,
},
component: () => <div />,
},
inspectorAdapters: {},
requestHandler: 'none',
responseHandler: 'none',
});
setTypes(typeStart);
});

beforeEach(() => {
domNode = document.createElement('div');
handlers = {
event: jest.fn(),
done: jest.fn(),
onDestroy: jest.fn(),
reload: jest.fn(),
update: jest.fn(),
};
document.body.appendChild(domNode);
});

afterEach(() => {
jest.clearAllMocks();
unmountComponentAtNode(domNode);
document.body.removeChild(domNode);
});

it('should render the Visualization component', async () => {
const config = {
visType: '',
visData: { data: [1, 2, 3] },
visConfig: { type: 'pie' },
params: { listenOnChange: true },
};

await act(async () => {
await visualization().render(domNode, config, handlers);
});

expect(domNode.textContent).toContain('Visualization');
expect(domNode.textContent).toContain('{"data":[1,2,3]}');
expect(domNode.textContent).toContain('true');
});

it('should call setUiState if vis uiState not matched', async () => {
const config = {
visType: 'pie',
visData: { data: [1, 2, 3] },
visConfig: { type: 'pie' },
params: { listenOnChange: false },
};
const uiState = new PersistedState({ vis: { colors: { brand: '#000' } } });
handlers.uiState = uiState;

const getUiStateMock = jest.spyOn(ExprVis.prototype, 'getUiState').mockReturnValueOnce(uiState);
const setUiStateMock = jest.spyOn(ExprVis.prototype, 'setUiState');

await act(async () => {
await visualization().render(domNode, config, handlers);
});
const newUIState = new PersistedState();

handlers.uiState = newUIState;
await act(async () => {
await visualization().render(domNode, config, handlers);
});
getUiStateMock.mockReturnValueOnce(newUIState);
expect(setUiStateMock).toHaveBeenCalledWith(newUIState);
});

it('should unmount the component on destroy', async () => {
let destroyFn: Function;
jest.spyOn(handlers, 'onDestroy').mockImplementationOnce((fn) => {
destroyFn = fn;
});
const config = {
visType: '',
visData: { data: [1, 2, 3] },
visConfig: { type: 'pie' },
params: { listenOnChange: false },
};

await act(async () => {
await visualization().render(domNode, config, handlers);
});

await act(async () => {
destroyFn();
});

expect(domNode.textContent).not.toContain('Visualization');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export const visualization = (): ExpressionRenderDefinition<VisRenderValue> => (
unmountComponentAtNode(domNode);
});

if (vis.getUiState() !== uiState) {
vis.setUiState(uiState);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am concerned that handlers.uiState has a type of any while vis.setUiState is expecting PersistedState.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, but I think it's not a big issue. According the implementation in the Visualization component, the uiState has already been assigned to the vis object. The vis.uiState always be an empty since vis object recreated in every render. To address this any concern, how about add a type assertment in the compare condition. Refactor code like below:

if (uiState instanceof PersistedState && vis.getUiState() !== uiState) {
      vis.setUiState(uiState);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Should we do a deep compare?

Copy link
Contributor Author

@wanglam wanglam Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vis.getUiState() should always be an empty object in this place. Since we construct a new ExpVis object above. The uiState of this object should be empty too.

}

const listenOnChange = params ? params.listenOnChange : false;
render(
<Visualization
Expand Down
Loading