Skip to content

Commit

Permalink
simplify tests
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Li <joshuali925@gmail.com>
  • Loading branch information
joshuali925 committed Jul 29, 2024
1 parent 2e27301 commit a397108
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const createSetupContractMock = () => {
setDataSet: jest.fn(),
getUpdates$: jest.fn(),
getDefaultDataSet: jest.fn(),
fetchDefaultDataSet: jest.fn(),
initWithIndexPattern: jest.fn(),
};
return dataSetManagerMock;
};
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/public/query/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const createSetupContractMock = () => {
filterManager: createFilterManagerMock(),
timefilter: timefilterServiceMock.createSetupContract(),
queryString: queryStringManagerMock.createSetupContract(),
dataSet: dataSetManagerMock.createSetupContract(),
dataSetManager: dataSetManagerMock.createSetupContract(),
state$: new Observable(),
};

Expand All @@ -57,7 +57,7 @@ const createStartContractMock = () => {
savedQueries: jest.fn() as any,
state$: new Observable(),
timefilter: timefilterServiceMock.createStartContract(),
dataSet: dataSetManagerMock.createStartContract(),
dataSetManager: dataSetManagerMock.createStartContract(),
getOpenSearchQuery: jest.fn(),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function createDataSetNavigator(
dataSetManager: DataSetContract
) {
// Return a function that takes props, omitting the dependencies from the props type
return (props: Omit<DataSetNavigatorProps, 'savedObjectsClient' | 'http' | 'dataSet'>) => (
return (props: Omit<DataSetNavigatorProps, 'savedObjectsClient' | 'http' | 'dataSetManager'>) => (
<DataSetNavigator
{...props}
savedObjectsClient={savedObjectsClient}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,6 @@ import { DataSetContract } from '../../../../data/public/query';
import { ConfigSchema } from '../../../common/config';
import { createQueryAssistExtension } from './create_extension';

const mockSimpleDataSet = {
id: 'mock-data-set-id',
title: 'mock-title',
dataSourceRef: {
id: 'mock-data-source-id',
},
} as SimpleDataSet;

const mockDataSetManager: jest.Mocked<DataSetContract> = {
getUpdates$: jest.fn().mockReturnValue(of(mockSimpleDataSet)),
getDataSet: jest.fn().mockReturnValue(mockSimpleDataSet),
setDataSet: jest.fn(),
getDefaultDataSet: jest.fn().mockReturnValue(mockSimpleDataSet),
fetchDefaultDataSet: jest.fn().mockResolvedValue(mockSimpleDataSet),
init: jest.fn(),
initWithIndexPattern: jest.fn(),
};

const coreSetupMock = coreMock.createSetup({
pluginStartDeps: {
data: {
Expand All @@ -41,13 +23,19 @@ const coreSetupMock = coreMock.createSetup({
},
});
const httpMock = coreSetupMock.http;
const dataMock = {
...dataPluginMock.createSetupContract(),
query: {
...dataPluginMock.createSetupContract().query,
dataSetManager: mockDataSetManager,
const dataMock = dataPluginMock.createSetupContract();
const dataSetMock = (dataMock.query.dataSetManager as unknown) as jest.Mocked<DataSetContract>;

const mockSimpleDataSet = {
id: 'mock-data-set-id',
title: 'mock-title',
dataSourceRef: {
id: 'mock-data-source-id',
},
};
} as SimpleDataSet;

dataSetMock.getDataSet.mockReturnValue(mockSimpleDataSet);
dataSetMock.getUpdates$.mockReturnValue(of(mockSimpleDataSet));

jest.mock('../components', () => ({
QueryAssistBar: jest.fn(() => <div>QueryAssistBar</div>),
Expand All @@ -71,17 +59,7 @@ describe('CreateExtension', () => {

it('should be enabled if at least one language is configured', async () => {
httpMock.get.mockResolvedValueOnce({ configuredLanguages: ['PPL'] });
const extension = createQueryAssistExtension(
httpMock,
{
...dataMock,
query: {
...dataMock.query,
dataSetManager: mockDataSetManager,
},
},
config
);
const extension = createQueryAssistExtension(httpMock, dataMock, config);
const isEnabled = await firstValueFrom(extension.isEnabled$(dependencies));
expect(isEnabled).toBeTruthy();
expect(httpMock.get).toBeCalledWith('/api/enhancements/assist/languages', {
Expand Down

0 comments on commit a397108

Please sign in to comment.