[Lens] Load indexpatterns list from indexPattern Service (#91984)
* [Lens] Load lists from indexPattern Service * test that indexpattern service has not been called * Use mock data Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
parent
556a882a24
commit
bdfa9695dc
|
@ -82,7 +82,6 @@ export function getIndexPatternDatasource({
|
|||
data: DataPublicPluginStart;
|
||||
charts: ChartsPluginSetup;
|
||||
}) {
|
||||
const savedObjectsClient = core.savedObjects.client;
|
||||
const uiSettings = core.uiSettings;
|
||||
const onIndexPatternLoadError = (err: Error) =>
|
||||
core.notifications.toasts.addError(err, {
|
||||
|
@ -121,7 +120,6 @@ export function getIndexPatternDatasource({
|
|||
return loadInitialState({
|
||||
persistedState,
|
||||
references,
|
||||
savedObjectsClient: await savedObjectsClient,
|
||||
defaultIndexPatternId: core.uiSettings.get('defaultIndex'),
|
||||
storage,
|
||||
indexPatternsService,
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import { HttpHandler, SavedObjectsClientContract } from 'kibana/public';
|
||||
import { HttpHandler } from 'kibana/public';
|
||||
import _ from 'lodash';
|
||||
import {
|
||||
loadInitialState,
|
||||
|
@ -183,23 +183,24 @@ const sampleIndexPatterns = {
|
|||
'2': indexPattern2,
|
||||
};
|
||||
|
||||
function mockClient() {
|
||||
return ({
|
||||
find: jest.fn(async () => ({
|
||||
savedObjects: [
|
||||
{ id: '1', attributes: { title: sampleIndexPatterns[1].title } },
|
||||
{ id: '2', attributes: { title: sampleIndexPatterns[2].title } },
|
||||
],
|
||||
})),
|
||||
} as unknown) as Pick<SavedObjectsClientContract, 'find'>;
|
||||
}
|
||||
|
||||
function mockIndexPatternsService() {
|
||||
return ({
|
||||
get: jest.fn(async (id: '1' | '2') => {
|
||||
return { ...sampleIndexPatternsFromService[id], metaFields: [] };
|
||||
}),
|
||||
} as unknown) as Pick<IndexPatternsContract, 'get'>;
|
||||
getIdsWithTitle: jest.fn(async () => {
|
||||
return [
|
||||
{
|
||||
id: sampleIndexPatterns[1].id,
|
||||
title: sampleIndexPatterns[1].title,
|
||||
},
|
||||
{
|
||||
id: sampleIndexPatterns[2].id,
|
||||
title: sampleIndexPatterns[2].title,
|
||||
},
|
||||
];
|
||||
}),
|
||||
} as unknown) as Pick<IndexPatternsContract, 'get' | 'getIdsWithTitle'>;
|
||||
}
|
||||
|
||||
describe('loader', () => {
|
||||
|
@ -212,7 +213,8 @@ describe('loader', () => {
|
|||
get: jest.fn(() =>
|
||||
Promise.reject('mockIndexPatternService.get should not have been called')
|
||||
),
|
||||
} as unknown) as Pick<IndexPatternsContract, 'get'>,
|
||||
getIdsWithTitle: jest.fn(),
|
||||
} as unknown) as Pick<IndexPatternsContract, 'get' | 'getIdsWithTitle'>,
|
||||
});
|
||||
|
||||
expect(cache).toEqual(sampleIndexPatterns);
|
||||
|
@ -281,7 +283,11 @@ describe('loader', () => {
|
|||
},
|
||||
],
|
||||
})),
|
||||
} as unknown) as Pick<IndexPatternsContract, 'get'>,
|
||||
getIdsWithTitle: jest.fn(async () => ({
|
||||
id: 'foo',
|
||||
title: 'Foo index',
|
||||
})),
|
||||
} as unknown) as Pick<IndexPatternsContract, 'get' | 'getIdsWithTitle'>,
|
||||
});
|
||||
|
||||
expect(cache.foo.getFieldByName('bytes')!.aggregationRestrictions).toEqual({
|
||||
|
@ -333,7 +339,11 @@ describe('loader', () => {
|
|||
},
|
||||
],
|
||||
})),
|
||||
} as unknown) as Pick<IndexPatternsContract, 'get'>,
|
||||
getIdsWithTitle: jest.fn(async () => ({
|
||||
id: 'foo',
|
||||
title: 'Foo index',
|
||||
})),
|
||||
} as unknown) as Pick<IndexPatternsContract, 'get' | 'getIdsWithTitle'>,
|
||||
});
|
||||
|
||||
expect(cache.foo.getFieldByName('timestamp')!.meta).toEqual(true);
|
||||
|
@ -344,7 +354,6 @@ describe('loader', () => {
|
|||
it('should load a default state', async () => {
|
||||
const storage = createMockStorage();
|
||||
const state = await loadInitialState({
|
||||
savedObjectsClient: mockClient(),
|
||||
indexPatternsService: mockIndexPatternsService(),
|
||||
storage,
|
||||
options: { isFullEditor: true },
|
||||
|
@ -368,10 +377,9 @@ describe('loader', () => {
|
|||
|
||||
it('should load a default state without loading the indexPatterns when embedded', async () => {
|
||||
const storage = createMockStorage();
|
||||
const savedObjectsClient = mockClient();
|
||||
const indexPatternsService = mockIndexPatternsService();
|
||||
const state = await loadInitialState({
|
||||
savedObjectsClient,
|
||||
indexPatternsService: mockIndexPatternsService(),
|
||||
indexPatternsService,
|
||||
storage,
|
||||
options: { isFullEditor: false },
|
||||
});
|
||||
|
@ -384,14 +392,12 @@ describe('loader', () => {
|
|||
});
|
||||
|
||||
expect(storage.set).not.toHaveBeenCalled();
|
||||
|
||||
expect(savedObjectsClient.find).not.toHaveBeenCalled();
|
||||
expect(indexPatternsService.getIdsWithTitle).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should load a default state when lastUsedIndexPatternId is not found in indexPatternRefs', async () => {
|
||||
const storage = createMockStorage({ indexPatternId: 'c' });
|
||||
const state = await loadInitialState({
|
||||
savedObjectsClient: mockClient(),
|
||||
indexPatternsService: mockIndexPatternsService(),
|
||||
storage,
|
||||
options: { isFullEditor: true },
|
||||
|
@ -415,7 +421,6 @@ describe('loader', () => {
|
|||
|
||||
it('should load lastUsedIndexPatternId if in localStorage', async () => {
|
||||
const state = await loadInitialState({
|
||||
savedObjectsClient: mockClient(),
|
||||
indexPatternsService: mockIndexPatternsService(),
|
||||
storage: createMockStorage({ indexPatternId: '2' }),
|
||||
options: { isFullEditor: true },
|
||||
|
@ -438,7 +443,6 @@ describe('loader', () => {
|
|||
const storage = createMockStorage();
|
||||
const state = await loadInitialState({
|
||||
defaultIndexPatternId: '2',
|
||||
savedObjectsClient: mockClient(),
|
||||
indexPatternsService: mockIndexPatternsService(),
|
||||
storage,
|
||||
options: { isFullEditor: true },
|
||||
|
@ -463,7 +467,6 @@ describe('loader', () => {
|
|||
it('should use the indexPatternId of the visualize trigger field, if provided', async () => {
|
||||
const storage = createMockStorage();
|
||||
const state = await loadInitialState({
|
||||
savedObjectsClient: mockClient(),
|
||||
indexPatternsService: mockIndexPatternsService(),
|
||||
storage,
|
||||
initialContext: {
|
||||
|
@ -524,7 +527,6 @@ describe('loader', () => {
|
|||
{ name: 'indexpattern-datasource-layer-layerb', id: '2', type: 'index-pattern' },
|
||||
{ name: 'another-reference', id: 'c', type: 'index-pattern' },
|
||||
],
|
||||
savedObjectsClient: mockClient(),
|
||||
indexPatternsService: mockIndexPatternsService(),
|
||||
storage,
|
||||
options: { isFullEditor: true },
|
||||
|
@ -681,6 +683,7 @@ describe('loader', () => {
|
|||
get: jest.fn(async () => {
|
||||
throw err;
|
||||
}),
|
||||
getIdsWithTitle: jest.fn(),
|
||||
},
|
||||
onError,
|
||||
storage,
|
||||
|
@ -808,6 +811,7 @@ describe('loader', () => {
|
|||
get: jest.fn(async () => {
|
||||
throw err;
|
||||
}),
|
||||
getIdsWithTitle: jest.fn(),
|
||||
},
|
||||
onError,
|
||||
storage,
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
import _ from 'lodash';
|
||||
import { IStorageWrapper } from 'src/plugins/kibana_utils/public';
|
||||
import { SavedObjectsClientContract, HttpSetup, SavedObjectReference } from 'kibana/public';
|
||||
import { HttpSetup, SavedObjectReference } from 'kibana/public';
|
||||
import { InitializationOptions, StateSetter } from '../types';
|
||||
import {
|
||||
IndexPattern,
|
||||
|
@ -30,8 +30,7 @@ import { readFromStorage, writeToStorage } from '../settings_storage';
|
|||
import { getFieldByNameFactory } from './pure_helpers';
|
||||
|
||||
type SetState = StateSetter<IndexPatternPrivateState>;
|
||||
type SavedObjectsClient = Pick<SavedObjectsClientContract, 'find'>;
|
||||
type IndexPatternsService = Pick<IndexPatternsContract, 'get'>;
|
||||
type IndexPatternsService = Pick<IndexPatternsContract, 'get' | 'getIdsWithTitle'>;
|
||||
type ErrorHandler = (err: Error) => void;
|
||||
|
||||
export async function loadIndexPatterns({
|
||||
|
@ -186,7 +185,6 @@ export function injectReferences(
|
|||
export async function loadInitialState({
|
||||
persistedState,
|
||||
references,
|
||||
savedObjectsClient,
|
||||
defaultIndexPatternId,
|
||||
storage,
|
||||
indexPatternsService,
|
||||
|
@ -195,7 +193,6 @@ export async function loadInitialState({
|
|||
}: {
|
||||
persistedState?: IndexPatternPersistedState;
|
||||
references?: SavedObjectReference[];
|
||||
savedObjectsClient: SavedObjectsClient;
|
||||
defaultIndexPatternId?: string;
|
||||
storage: IStorageWrapper;
|
||||
indexPatternsService: IndexPatternsService;
|
||||
|
@ -203,7 +200,7 @@ export async function loadInitialState({
|
|||
options?: InitializationOptions;
|
||||
}): Promise<IndexPatternPrivateState> {
|
||||
const { isFullEditor } = options ?? {};
|
||||
const indexPatternRefs = await (isFullEditor ? loadIndexPatternRefs(savedObjectsClient) : []);
|
||||
const indexPatternRefs = await (isFullEditor ? loadIndexPatternRefs(indexPatternsService) : []);
|
||||
const lastUsedIndexPatternId = getLastUsedIndexPatternId(storage, indexPatternRefs);
|
||||
|
||||
const state =
|
||||
|
@ -334,22 +331,13 @@ export async function changeLayerIndexPattern({
|
|||
}
|
||||
|
||||
async function loadIndexPatternRefs(
|
||||
savedObjectsClient: SavedObjectsClient
|
||||
indexPatternsService: IndexPatternsService
|
||||
): Promise<IndexPatternRef[]> {
|
||||
const result = await savedObjectsClient.find<{ title: string }>({
|
||||
type: 'index-pattern',
|
||||
fields: ['title'],
|
||||
perPage: 10000,
|
||||
});
|
||||
const indexPatterns = await indexPatternsService.getIdsWithTitle();
|
||||
|
||||
return result.savedObjects
|
||||
.map((o) => ({
|
||||
id: String(o.id),
|
||||
title: (o.attributes as { title: string }).title,
|
||||
}))
|
||||
.sort((a, b) => {
|
||||
return a.title.localeCompare(b.title);
|
||||
});
|
||||
return indexPatterns.sort((a, b) => {
|
||||
return a.title.localeCompare(b.title);
|
||||
});
|
||||
}
|
||||
|
||||
export async function syncExistingFields({
|
||||
|
|
Loading…
Reference in a new issue