Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Li <[email protected]>
  • Loading branch information
joshuali925 committed Nov 26, 2024
1 parent 0ff21eb commit a0ea032
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const createSetupDatasetServiceMock = (): jest.Mocked<DatasetServiceContract> =>
fetchOptions: jest.fn(),
getRecentDatasets: jest.fn(),
addRecentDataset: jest.fn(),
clearCache: jest.fn(),
getLastCacheTime: jest.fn(),
removeFromRecentDatasets: jest.fn(),
};
};

Expand Down
72 changes: 68 additions & 4 deletions src/plugins/data/public/ui/dataset_selector/configurator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand Down Expand Up @@ -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(
<IntlProvider locale="en" messages={messages}>
<Configurator
services={mockServices}
baseDataset={mockDataset}
onConfirm={mockOnConfirm}
onCancel={mockOnCancel}
onPrevious={mockOnPrevious}
/>
</IntlProvider>
);

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(
<IntlProvider locale="en" messages={messages}>
<Configurator
services={mockServices}
baseDataset={mockDataset}
onConfirm={mockOnConfirm}
onCancel={mockOnCancel}
onPrevious={mockOnPrevious}
/>
</IntlProvider>
);

expect(
container.querySelector(`[data-test-subj="advancedSelectorTimeFieldSelect"]`)
).toBeFalsy();

mockServices
.getQueryService()
.queryString.getDatasetService()
.getType.mockReturnValue(datasetTypeConfig);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -66,6 +67,7 @@ const createMockStorage = () => ({
});

const dataPlugin = dataPluginMock.createStartContract(true);
const datasetService = datasetServiceMock.createStartContract();

function wrapQueryEditorTopRowInContext(testProps: any) {
const defaultOptions = {
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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();
});
});

0 comments on commit a0ea032

Please sign in to comment.