diff --git a/src/plugins/data/public/query/query_string/dataset_service/dataset_service.mock.ts b/src/plugins/data/public/query/query_string/dataset_service/dataset_service.mock.ts index df5521078feb..ba491cb51191 100644 --- a/src/plugins/data/public/query/query_string/dataset_service/dataset_service.mock.ts +++ b/src/plugins/data/public/query/query_string/dataset_service/dataset_service.mock.ts @@ -43,6 +43,9 @@ const createSetupDatasetServiceMock = (): jest.Mocked => fetchOptions: jest.fn(), getRecentDatasets: jest.fn(), addRecentDataset: jest.fn(), + clearCache: jest.fn(), + getLastCacheTime: jest.fn(), + removeFromRecentDatasets: jest.fn(), }; }; diff --git a/src/plugins/data/public/ui/dataset_selector/configurator.test.tsx b/src/plugins/data/public/ui/dataset_selector/configurator.test.tsx index 462c6298a0a3..38d4e4e12183 100644 --- a/src/plugins/data/public/ui/dataset_selector/configurator.test.tsx +++ b/src/plugins/data/public/ui/dataset_selector/configurator.test.tsx @@ -3,14 +3,14 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { render, screen, fireEvent, waitFor } from '@testing-library/react'; -import { Configurator } from './configurator'; import '@testing-library/jest-dom'; +import { fireEvent, render, screen, waitFor } from '@testing-library/react'; import React from 'react'; -import { setQueryService, setIndexPatterns } from '../../services'; import { IntlProvider } from 'react-intl'; -import { Query } from '../../../../data/public'; import { Dataset } from 'src/plugins/data/common'; +import { Query } from '../../../../data/public'; +import { setIndexPatterns, setQueryService } from '../../services'; +import { Configurator } from './configurator'; const getQueryMock = jest.fn().mockReturnValue({ query: '', @@ -358,4 +358,68 @@ describe('Configurator Component', () => { expect(submitButton).toBeEnabled(); }); }); + + it('should show the date picker if supportsTimeFilter is undefined', async () => { + const mockDataset = { + ...mockBaseDataset, + timeFieldName: undefined, + type: 'index', + }; + const { container } = render( + + + + ); + + expect( + container.querySelector(`[data-test-subj="advancedSelectorTimeFieldSelect"]`) + ).toBeTruthy(); + }); + + it('should hide the date picker if supportsTimeFilter is false', async () => { + const mockDataset = { + ...mockBaseDataset, + timeFieldName: undefined, + type: 'index', + }; + const datasetTypeConfig = mockServices + .getQueryService() + .queryString.getDatasetService() + .getType(); + mockServices + .getQueryService() + .queryString.getDatasetService() + .getType.mockReturnValue({ + ...datasetTypeConfig, + meta: { + supportsTimeFilter: false, + }, + }); + const { container } = render( + + + + ); + + expect( + container.querySelector(`[data-test-subj="advancedSelectorTimeFieldSelect"]`) + ).toBeFalsy(); + + mockServices + .getQueryService() + .queryString.getDatasetService() + .getType.mockReturnValue(datasetTypeConfig); + }); }); diff --git a/src/plugins/data/public/ui/query_editor/query_editor_top_row.test.tsx b/src/plugins/data/public/ui/query_editor/query_editor_top_row.test.tsx index 62fe653bfd45..284cbe8d4ff0 100644 --- a/src/plugins/data/public/ui/query_editor/query_editor_top_row.test.tsx +++ b/src/plugins/data/public/ui/query_editor/query_editor_top_row.test.tsx @@ -3,15 +3,16 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { Query, UI_SETTINGS } from '../../../common'; -import { coreMock } from '../../../../../core/public/mocks'; -import { dataPluginMock } from '../../mocks'; -import React from 'react'; import { I18nProvider } from '@osd/i18n/react'; -import { createEditor, DQLBody, QueryEditorTopRow, SingleLineInput } from '../'; -import { OpenSearchDashboardsContextProvider } from 'src/plugins/opensearch_dashboards_react/public'; import { cleanup, render, waitFor } from '@testing-library/react'; -import { LanguageConfig } from '../../query'; +import React from 'react'; +import { OpenSearchDashboardsContextProvider } from 'src/plugins/opensearch_dashboards_react/public'; +import { createEditor, DQLBody, QueryEditorTopRow, SingleLineInput } from '../'; +import { coreMock } from '../../../../../core/public/mocks'; +import { Query, UI_SETTINGS } from '../../../common'; +import { dataPluginMock } from '../../mocks'; +import { DatasetTypeConfig, LanguageConfig } from '../../query'; +import { datasetServiceMock } from '../../query/query_string/dataset_service/dataset_service.mock'; import { getQueryService } from '../../services'; const startMock = coreMock.createStart(); @@ -66,6 +67,7 @@ const createMockStorage = () => ({ }); const dataPlugin = dataPluginMock.createStartContract(true); +const datasetService = datasetServiceMock.createStartContract(); function wrapQueryEditorTopRowInContext(testProps: any) { const defaultOptions = { @@ -111,6 +113,7 @@ describe('QueryEditorTopRow', () => { beforeEach(() => { jest.clearAllMocks(); (getQueryService as jest.Mock).mockReturnValue(dataPlugin.query); + dataPlugin.query.queryString.getDatasetService = jest.fn().mockReturnValue(datasetService); }); afterEach(() => { @@ -155,4 +158,49 @@ describe('QueryEditorTopRow', () => { await waitFor(() => expect(container.querySelector(QUERY_EDITOR)).toBeTruthy()); expect(container.querySelector(DATE_PICKER)).toBeFalsy(); }); + + it('Should not render date picker if dataset type does not support time field', async () => { + const query: Query = { + query: 'test query', + dataset: datasetService.getDefault(), + language: 'test-language', + }; + dataPlugin.query.queryString.getQuery = jest.fn().mockReturnValue(query); + datasetService.getType.mockReturnValue({ + meta: { supportsTimeFilter: false }, + } as DatasetTypeConfig); + + const { container } = render( + wrapQueryEditorTopRowInContext({ + query, + showQueryEditor: false, + showDatePicker: true, + }) + ); + await waitFor(() => expect(container.querySelector(QUERY_EDITOR)).toBeTruthy()); + expect(container.querySelector(DATE_PICKER)).toBeFalsy(); + }); + + it('Should render date picker if dataset overrides hideDatePicker to false', async () => { + const query: Query = { + query: 'test query', + dataset: datasetService.getDefault(), + language: 'test-language', + }; + dataPlugin.query.queryString.getQuery = jest.fn().mockReturnValue(query); + datasetService.getType.mockReturnValue(({ + meta: { supportsTimeFilter: true }, + languageOverrides: { 'test-language': { hideDatePicker: false } }, + } as unknown) as DatasetTypeConfig); + + const { container } = render( + wrapQueryEditorTopRowInContext({ + query, + showQueryEditor: false, + showDatePicker: true, + }) + ); + await waitFor(() => expect(container.querySelector(QUERY_EDITOR)).toBeTruthy()); + expect(container.querySelector(DATE_PICKER)).toBeTruthy(); + }); });