-
Notifications
You must be signed in to change notification settings - Fork 924
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
wanglam
wants to merge
4
commits into
opensearch-project:main
Choose a base branch
from
wanglam:fix-visualization-legend-toggle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+145
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
fix: | ||
- Visualization chart not expanded after legend toggle ([#9170](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/9170)) |
139 changes: 139 additions & 0 deletions
139
src/plugins/visualizations/public/expressions/visualization_renderer.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,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'); | ||
}); | ||
}); |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ofany
whilevis.setUiState
is expectingPersistedState
.There was a problem hiding this comment.
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. Thevis.uiState
always be an empty sincevis
object recreated in every render. To address thisany
concern, how about add a type assertment in the compare condition. Refactor code like below:There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 newExpVis
object above. TheuiState
of this object should be empty too.