-
Notifications
You must be signed in to change notification settings - Fork 885
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backport 2.x] [VisBuilder] 2 way communication using UI actions and …
…bug fixes (#3857) * [VisBuilder] 2 way communication using UI actions and bug fixes (#3732) * adds uiActions to visBuilder * prevents multiple errors on load * fixes visbuilder type errors * fixes save * updates changelog --------- Signed-off-by: Ashwin P Chandran <ashwinpc@amazon.com> Co-authored-by: Josh Romero <rmerqg@amazon.com> (cherry picked from commit 1edb195) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> # Conflicts: # CHANGELOG.md * add changelog Signed-off-by: Josh Romero <rmerqg@amazon.com> --------- Signed-off-by: Josh Romero <rmerqg@amazon.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Josh Romero <rmerqg@amazon.com>
- Loading branch information
1 parent
b7af56f
commit 9eb8eb9
Showing
18 changed files
with
359 additions
and
138 deletions.
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
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
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
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
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
19 changes: 0 additions & 19 deletions
19
src/plugins/vis_builder/public/application/utils/get_saved_vis_builder_vis.ts
This file was deleted.
Oops, something went wrong.
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
102 changes: 102 additions & 0 deletions
102
src/plugins/vis_builder/public/application/utils/handle_vis_event.test.ts
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,102 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { ExpressionRendererEvent } from '../../../../expressions/public'; | ||
import { VIS_EVENT_TO_TRIGGER } from '../../../../visualizations/public'; | ||
import { handleVisEvent } from './handle_vis_event'; | ||
import { uiActionsPluginMock } from '../../../../ui_actions/public/mocks'; | ||
import { Action, ActionType, createAction } from '../../../../ui_actions/public'; | ||
|
||
const executeFn = jest.fn(); | ||
|
||
function createTestAction<C extends object>( | ||
type: string, | ||
checkCompatibility: (context: C) => boolean, | ||
autoExecutable = true | ||
): Action<object> { | ||
return createAction({ | ||
type: type as ActionType, | ||
id: type, | ||
isCompatible: (context: C) => Promise.resolve(checkCompatibility(context)), | ||
execute: (context) => { | ||
return executeFn(context); | ||
}, | ||
shouldAutoExecute: () => Promise.resolve(autoExecutable), | ||
}); | ||
} | ||
|
||
let uiActions: ReturnType<typeof uiActionsPluginMock.createPlugin>; | ||
|
||
describe('handleVisEvent', () => { | ||
beforeEach(() => { | ||
uiActions = uiActionsPluginMock.createPlugin(); | ||
|
||
executeFn.mockClear(); | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
test('should trigger the correct event', async () => { | ||
const event: ExpressionRendererEvent = { | ||
name: 'filter', | ||
data: {}, | ||
}; | ||
const action = createTestAction('test1', () => true); | ||
const timeFieldName = 'test-timefeild-name'; | ||
uiActions.setup.addTriggerAction(VIS_EVENT_TO_TRIGGER.filter, action); | ||
|
||
await handleVisEvent(event, uiActions.doStart(), timeFieldName); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(executeFn).toBeCalledTimes(1); | ||
expect(executeFn).toBeCalledWith( | ||
expect.objectContaining({ | ||
data: { timeFieldName }, | ||
}) | ||
); | ||
}); | ||
|
||
test('should trigger the default trigger when not found', async () => { | ||
const event: ExpressionRendererEvent = { | ||
name: 'test', | ||
data: {}, | ||
}; | ||
const action = createTestAction('test2', () => true); | ||
const timeFieldName = 'test-timefeild-name'; | ||
uiActions.setup.addTriggerAction(VIS_EVENT_TO_TRIGGER.filter, action); | ||
|
||
await handleVisEvent(event, uiActions.doStart(), timeFieldName); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(executeFn).toBeCalledTimes(1); | ||
expect(executeFn).toBeCalledWith( | ||
expect.objectContaining({ | ||
data: { timeFieldName }, | ||
}) | ||
); | ||
}); | ||
|
||
test('should have the correct context for `applyfilter`', async () => { | ||
const event: ExpressionRendererEvent = { | ||
name: 'applyFilter', | ||
data: {}, | ||
}; | ||
const action = createTestAction('test3', () => true); | ||
const timeFieldName = 'test-timefeild-name'; | ||
uiActions.setup.addTriggerAction(VIS_EVENT_TO_TRIGGER.applyFilter, action); | ||
|
||
await handleVisEvent(event, uiActions.doStart(), timeFieldName); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(executeFn).toBeCalledTimes(1); | ||
expect(executeFn).toBeCalledWith( | ||
expect.objectContaining({ | ||
timeFieldName, | ||
}) | ||
); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
src/plugins/vis_builder/public/application/utils/handle_vis_event.ts
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,24 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { ExpressionRendererEvent } from '../../../../expressions/public'; | ||
import { VIS_EVENT_TO_TRIGGER } from '../../../../visualizations/public'; | ||
import { UiActionsStart } from '../../../../ui_actions/public'; | ||
|
||
export const handleVisEvent = async ( | ||
event: ExpressionRendererEvent, | ||
uiActions: UiActionsStart, | ||
timeFieldName?: string | ||
) => { | ||
const triggerId = VIS_EVENT_TO_TRIGGER[event.name] ?? VIS_EVENT_TO_TRIGGER.filter; | ||
const isApplyFilter = triggerId === VIS_EVENT_TO_TRIGGER.applyFilter; | ||
const dataContext = { | ||
timeFieldName, | ||
...event.data, | ||
}; | ||
const context = isApplyFilter ? dataContext : { data: dataContext }; | ||
|
||
await uiActions.getTrigger(triggerId).exec(context); | ||
}; |
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.