Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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 () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Good call to switch to waitFor here.

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 @@ -93,13 +93,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