From c71c9c0158639ad966f763d0fc6f7baf7f3332b9 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Thu, 30 Jan 2025 03:37:51 +1100 Subject: [PATCH] [8.17] [Controls] Persist `runPastTimeout` setting (#208611) (#208764) # Backport This will backport the following commits from `main` to `8.17`: - [[Controls] Persist `runPastTimeout` setting (#208611)](https://github.com/elastic/kibana/pull/208611) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Hannah Mudge --- .../options_list_editor_options.test.tsx | 29 ++++++--- .../get_options_list_control_factory.test.tsx | 62 +++++++++++++++---- .../get_options_list_control_factory.tsx | 8 ++- 3 files changed, 77 insertions(+), 22 deletions(-) diff --git a/src/plugins/controls/public/controls/data_controls/options_list_control/components/options_list_editor_options.test.tsx b/src/plugins/controls/public/controls/data_controls/options_list_control/components/options_list_editor_options.test.tsx index 079a857f7c090..62bf7a1819aca 100644 --- a/src/plugins/controls/public/controls/data_controls/options_list_control/components/options_list_editor_options.test.tsx +++ b/src/plugins/controls/public/controls/data_controls/options_list_control/components/options_list_editor_options.test.tsx @@ -52,16 +52,27 @@ describe('Options list sorting button', () => { return component; }; - test('run past timeout', async () => { - const component = mountComponent({ - initialState: getMockedState({ runPastTimeout: false }), - field: { type: 'string' } as DataViewField, + describe('run past timeout', () => { + test('can toggle the setting', async () => { + const component = mountComponent({ + initialState: getMockedState({ runPastTimeout: false }), + field: { type: 'string' } as DataViewField, + }); + const toggle = component.getByTestId('optionsListControl__runPastTimeoutAdditionalSetting'); + expect(toggle.getAttribute('aria-checked')).toBe('false'); + await userEvent.click(toggle); + expect(updateState).toBeCalledWith({ runPastTimeout: true }); + expect(toggle.getAttribute('aria-checked')).toBe('true'); + }); + + test('setting is persisted', async () => { + const component = mountComponent({ + initialState: getMockedState({ runPastTimeout: true }), + field: { type: 'string' } as DataViewField, + }); + const toggle = component.getByTestId('optionsListControl__runPastTimeoutAdditionalSetting'); + expect(toggle.getAttribute('aria-checked')).toBe('true'); }); - const toggle = component.getByTestId('optionsListControl__runPastTimeoutAdditionalSetting'); - expect(toggle.getAttribute('aria-checked')).toBe('false'); - await userEvent.click(toggle); - expect(updateState).toBeCalledWith({ runPastTimeout: true }); - expect(toggle.getAttribute('aria-checked')).toBe('true'); }); test('selection options', async () => { diff --git a/src/plugins/controls/public/controls/data_controls/options_list_control/get_options_list_control_factory.test.tsx b/src/plugins/controls/public/controls/data_controls/options_list_control/get_options_list_control_factory.test.tsx index 20aad3e260983..cf5539a0a2680 100644 --- a/src/plugins/controls/public/controls/data_controls/options_list_control/get_options_list_control_factory.test.tsx +++ b/src/plugins/controls/public/controls/data_controls/options_list_control/get_options_list_control_factory.test.tsx @@ -11,19 +11,18 @@ import React from 'react'; import { DataView } from '@kbn/data-views-plugin/common'; import { createStubDataView } from '@kbn/data-views-plugin/common/data_view.stub'; -import { act, render, waitFor } from '@testing-library/react'; +import { render, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { coreServices, dataViewsService } from '../../../services/kibana_services'; import { getMockedBuildApi, getMockedControlGroupApi } from '../../mocks/control_mocks'; +import * as initializeControl from '../initialize_data_control'; import { getOptionsListControlFactory } from './get_options_list_control_factory'; describe('Options List Control Api', () => { const uuid = 'myControl1'; const controlGroupApi = getMockedControlGroupApi(); - const waitOneTick = () => act(() => new Promise((resolve) => setTimeout(resolve, 0))); - dataViewsService.get = jest.fn().mockImplementation(async (id: string): Promise => { if (id !== 'myDataViewId') { throw new Error(`Simulated error: no data view found for id ${id}`); @@ -198,10 +197,13 @@ describe('Options List Control Api', () => { expect(control.getByTestId('optionsList-control-selection-exists')).toBeChecked(); const option = control.getByTestId('optionsList-control-selection-woof'); + await userEvent.click(option); - await waitOneTick(); + await waitFor(async () => { + expect(option).toBeChecked(); + }); + expect(control.getByTestId('optionsList-control-selection-exists')).not.toBeChecked(); - expect(option).toBeChecked(); }); test('clicking "Exists" unselects all other selections', async () => { @@ -229,8 +231,10 @@ describe('Options List Control Api', () => { expect(control.getByTestId('optionsList-control-selection-meow')).not.toBeChecked(); await userEvent.click(existsOption); - await waitOneTick(); - expect(existsOption).toBeChecked(); + await waitFor(async () => { + expect(existsOption).toBeChecked(); + }); + expect(control.getByTestId('optionsList-control-selection-woof')).not.toBeChecked(); expect(control.getByTestId('optionsList-control-selection-bark')).not.toBeChecked(); expect(control.getByTestId('optionsList-control-selection-meow')).not.toBeChecked(); @@ -260,8 +264,10 @@ describe('Options List Control Api', () => { expect(control.queryByTestId('optionsList-control-selection-meow')).toBeNull(); await userEvent.click(control.getByTestId('optionsList-control-selection-bark')); - await waitOneTick(); - expect(control.getByTestId('optionsList-control-selection-woof')).toBeChecked(); + await waitFor(async () => { + expect(control.getByTestId('optionsList-control-selection-woof')).toBeChecked(); + }); + expect(control.queryByTestId('optionsList-control-selection-bark')).toBeNull(); expect(control.queryByTestId('optionsList-control-selection-meow')).toBeNull(); @@ -316,9 +322,12 @@ describe('Options List Control Api', () => { expect(control.getByTestId('optionsList-control-selection-woof')).toBeChecked(); expect(control.queryByTestId('optionsList-control-selection-bark')).not.toBeChecked(); expect(control.queryByTestId('optionsList-control-selection-meow')).not.toBeChecked(); + await userEvent.click(control.getByTestId('optionsList-control-selection-bark')); - await waitOneTick(); - expect(control.getByTestId('optionsList-control-selection-woof')).not.toBeChecked(); + await waitFor(async () => { + expect(control.getByTestId('optionsList-control-selection-woof')).not.toBeChecked(); + }); + expect(control.queryByTestId('optionsList-control-selection-bark')).toBeChecked(); expect(control.queryByTestId('optionsList-control-selection-meow')).not.toBeChecked(); @@ -337,4 +346,35 @@ describe('Options List Control Api', () => { ]); }); }); + + test('ensure initialize data control contains all editor state', async () => { + const initializeSpy = jest.spyOn(initializeControl, 'initializeDataControl'); + const initialState = { + dataViewId: 'myDataViewId', + fieldName: 'myFieldName', + runPastTimeout: true, + }; + await factory.buildControl( + { + dataViewId: 'myDataViewId', + fieldName: 'myFieldName', + runPastTimeout: true, + }, + getMockedBuildApi(uuid, factory, controlGroupApi), + uuid, + controlGroupApi + ); + expect(initializeSpy).toHaveBeenCalledWith( + uuid, + 'optionsListControl', + 'optionsListDataView', + initialState, + { + searchTechnique: expect.anything(), + singleSelect: expect.anything(), + runPastTimeout: expect.anything(), + }, + controlGroupApi + ); + }); }); diff --git a/src/plugins/controls/public/controls/data_controls/options_list_control/get_options_list_control_factory.tsx b/src/plugins/controls/public/controls/data_controls/options_list_control/get_options_list_control_factory.tsx index ab077248ad840..bc344039acbda 100644 --- a/src/plugins/controls/public/controls/data_controls/options_list_control/get_options_list_control_factory.tsx +++ b/src/plugins/controls/public/controls/data_controls/options_list_control/get_options_list_control_factory.tsx @@ -85,13 +85,17 @@ export const getOptionsListControlFactory = (): DataControlFactory< const totalCardinality$ = new BehaviorSubject(0); const dataControl = initializeDataControl< - Pick + Pick >( uuid, OPTIONS_LIST_CONTROL, 'optionsListDataView', initialState, - { searchTechnique: searchTechnique$, singleSelect: singleSelect$ }, + { + searchTechnique: searchTechnique$, + singleSelect: singleSelect$, + runPastTimeout: runPastTimeout$, + }, controlGroupApi );