Skip to content

Commit

Permalink
[Controls] Persist runPastTimeout setting (#208611)
Browse files Browse the repository at this point in the history
Closes #206459

## Summary

This PR ensures that the `runPastTimeout` setting is persisted for
options list controls and the UI reflects the value of this setting. The
root cause of this bug is that we weren't sending in the behavior
subject for `runPastTimeout$` to the data control's `editorStateManager`
when initializing the options list control, which meant that its value
could not be set by the options list's `CustomOptionsComponent` - this
PR fixes that.

**Before**


https://github.com/user-attachments/assets/2c9eeab8-67d9-46bc-938e-4d7cb91e435f

**After**


https://github.com/user-attachments/assets/d06b6ffa-b1e9-4ecd-b732-69bd69a8aee9


### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [x] The PR description includes the appropriate Release Notes section,
and the correct `release_note:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
  • Loading branch information
Heenawter authored Jan 29, 2025
1 parent 66dab0a commit da41b8b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataView> => {
if (id !== 'myDataViewId') {
throw new Error(`Simulated error: no data view found for id ${id}`);
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand All @@ -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
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,17 @@ export const getOptionsListControlFactory = (): DataControlFactory<
const totalCardinality$ = new BehaviorSubject<number>(0);

const dataControl = initializeDataControl<
Pick<OptionsListControlState, 'searchTechnique' | 'singleSelect'>
Pick<OptionsListControlState, 'searchTechnique' | 'singleSelect' | 'runPastTimeout'>
>(
uuid,
OPTIONS_LIST_CONTROL,
'optionsListDataView',
initialState,
{ searchTechnique: searchTechnique$, singleSelect: singleSelect$ },
{
searchTechnique: searchTechnique$,
singleSelect: singleSelect$,
runPastTimeout: runPastTimeout$,
},
controlGroupApi
);

Expand Down

0 comments on commit da41b8b

Please sign in to comment.