2019-10-09 21:20:55 +02:00
|
|
|
/*
|
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
|
|
* or more contributor license agreements. Licensed under the Elastic License;
|
|
|
|
* you may not use this file except in compliance with the Elastic License.
|
|
|
|
*/
|
|
|
|
|
2020-07-01 10:08:37 +02:00
|
|
|
import { HttpHandler, SavedObjectsClientContract } from 'kibana/public';
|
2019-10-14 18:46:33 +02:00
|
|
|
import _ from 'lodash';
|
2019-10-09 21:20:55 +02:00
|
|
|
import {
|
|
|
|
loadInitialState,
|
|
|
|
loadIndexPatterns,
|
|
|
|
changeIndexPattern,
|
|
|
|
changeLayerIndexPattern,
|
2019-10-14 18:46:33 +02:00
|
|
|
syncExistingFields,
|
2020-08-21 18:08:25 +02:00
|
|
|
extractReferences,
|
|
|
|
injectReferences,
|
2019-10-09 21:20:55 +02:00
|
|
|
} from './loader';
|
2020-08-21 11:33:42 +02:00
|
|
|
import { IndexPatternsContract } from '../../../../../src/plugins/data/public';
|
|
|
|
import {
|
|
|
|
IndexPatternPersistedState,
|
|
|
|
IndexPatternPrivateState,
|
|
|
|
IndexPatternField,
|
|
|
|
IndexPattern,
|
|
|
|
} from './types';
|
|
|
|
import { createMockedRestrictedIndexPattern, createMockedIndexPattern } from './mocks';
|
2019-10-29 21:36:45 +01:00
|
|
|
import { documentField } from './document_field';
|
2019-10-09 21:20:55 +02:00
|
|
|
|
2020-06-26 16:33:09 +02:00
|
|
|
const createMockStorage = (lastData?: Record<string, string>) => {
|
|
|
|
return {
|
|
|
|
get: jest.fn().mockImplementation(() => lastData),
|
|
|
|
set: jest.fn(),
|
|
|
|
remove: jest.fn(),
|
|
|
|
clear: jest.fn(),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-08-21 11:33:42 +02:00
|
|
|
const indexPattern1 = ({
|
|
|
|
id: '1',
|
|
|
|
title: 'my-fake-index-pattern',
|
|
|
|
timeFieldName: 'timestamp',
|
2020-08-26 09:09:40 +02:00
|
|
|
hasRestrictions: false,
|
2020-08-21 11:33:42 +02:00
|
|
|
fields: [
|
|
|
|
{
|
|
|
|
name: 'timestamp',
|
|
|
|
displayName: 'timestampLabel',
|
|
|
|
type: 'date',
|
|
|
|
aggregatable: true,
|
|
|
|
searchable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'start_date',
|
|
|
|
displayName: 'start_date',
|
|
|
|
type: 'date',
|
|
|
|
aggregatable: true,
|
|
|
|
searchable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'bytes',
|
|
|
|
displayName: 'bytes',
|
|
|
|
type: 'number',
|
|
|
|
aggregatable: true,
|
|
|
|
searchable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'memory',
|
|
|
|
displayName: 'memory',
|
|
|
|
type: 'number',
|
|
|
|
aggregatable: true,
|
|
|
|
searchable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'source',
|
|
|
|
displayName: 'source',
|
|
|
|
type: 'string',
|
|
|
|
aggregatable: true,
|
|
|
|
searchable: true,
|
|
|
|
esTypes: ['keyword'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'unsupported',
|
|
|
|
displayName: 'unsupported',
|
|
|
|
type: 'geo',
|
|
|
|
aggregatable: true,
|
|
|
|
searchable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'dest',
|
|
|
|
displayName: 'dest',
|
|
|
|
type: 'string',
|
|
|
|
aggregatable: true,
|
|
|
|
searchable: true,
|
|
|
|
esTypes: ['keyword'],
|
|
|
|
},
|
2020-09-08 23:29:26 +02:00
|
|
|
{
|
|
|
|
name: 'scripted',
|
|
|
|
displayName: 'Scripted',
|
|
|
|
type: 'string',
|
|
|
|
searchable: true,
|
|
|
|
aggregatable: true,
|
|
|
|
scripted: true,
|
|
|
|
lang: 'painless',
|
|
|
|
script: '1234',
|
|
|
|
},
|
2020-08-21 11:33:42 +02:00
|
|
|
documentField,
|
|
|
|
],
|
|
|
|
} as unknown) as IndexPattern;
|
|
|
|
|
|
|
|
const sampleIndexPatternsFromService = {
|
|
|
|
'1': createMockedIndexPattern(),
|
|
|
|
'2': createMockedRestrictedIndexPattern(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const indexPattern2 = ({
|
|
|
|
id: '2',
|
|
|
|
title: 'my-fake-restricted-pattern',
|
|
|
|
timeFieldName: 'timestamp',
|
2020-08-26 09:09:40 +02:00
|
|
|
hasRestrictions: true,
|
2020-10-27 18:08:01 +01:00
|
|
|
fieldFormatMap: { bytes: { id: 'bytes', params: { pattern: '0.0' } } },
|
2020-08-21 11:33:42 +02:00
|
|
|
fields: [
|
|
|
|
{
|
|
|
|
name: 'timestamp',
|
|
|
|
displayName: 'timestampLabel',
|
|
|
|
type: 'date',
|
|
|
|
aggregatable: true,
|
|
|
|
searchable: true,
|
|
|
|
aggregationRestrictions: {
|
|
|
|
date_histogram: {
|
|
|
|
agg: 'date_histogram',
|
|
|
|
fixed_interval: '1d',
|
|
|
|
delay: '7d',
|
|
|
|
time_zone: 'UTC',
|
2019-10-09 21:20:55 +02:00
|
|
|
},
|
|
|
|
},
|
2020-08-21 11:33:42 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'bytes',
|
|
|
|
displayName: 'bytes',
|
|
|
|
type: 'number',
|
|
|
|
aggregatable: true,
|
|
|
|
searchable: true,
|
|
|
|
aggregationRestrictions: {
|
|
|
|
// Ignored in the UI
|
|
|
|
histogram: {
|
|
|
|
agg: 'histogram',
|
|
|
|
interval: 1000,
|
2019-10-09 21:20:55 +02:00
|
|
|
},
|
2020-08-21 11:33:42 +02:00
|
|
|
avg: {
|
|
|
|
agg: 'avg',
|
|
|
|
},
|
|
|
|
max: {
|
|
|
|
agg: 'max',
|
|
|
|
},
|
|
|
|
min: {
|
|
|
|
agg: 'min',
|
|
|
|
},
|
|
|
|
sum: {
|
|
|
|
agg: 'sum',
|
2019-10-09 21:20:55 +02:00
|
|
|
},
|
|
|
|
},
|
2020-08-21 11:33:42 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'source',
|
|
|
|
displayName: 'source',
|
|
|
|
type: 'string',
|
|
|
|
aggregatable: true,
|
|
|
|
searchable: true,
|
|
|
|
scripted: true,
|
2020-09-08 23:29:26 +02:00
|
|
|
lang: 'painless',
|
|
|
|
script: '1234',
|
2020-08-21 11:33:42 +02:00
|
|
|
aggregationRestrictions: {
|
|
|
|
terms: {
|
|
|
|
agg: 'terms',
|
|
|
|
},
|
2019-12-09 19:09:41 +01:00
|
|
|
},
|
2019-10-09 21:20:55 +02:00
|
|
|
},
|
2020-08-21 11:33:42 +02:00
|
|
|
documentField,
|
|
|
|
],
|
|
|
|
} as unknown) as IndexPattern;
|
|
|
|
|
|
|
|
const sampleIndexPatterns = {
|
|
|
|
'1': indexPattern1,
|
|
|
|
'2': indexPattern2,
|
|
|
|
};
|
2019-10-09 21:20:55 +02:00
|
|
|
|
|
|
|
function mockClient() {
|
|
|
|
return ({
|
|
|
|
find: jest.fn(async () => ({
|
|
|
|
savedObjects: [
|
2020-08-21 11:33:42 +02:00
|
|
|
{ id: '1', attributes: { title: sampleIndexPatterns[1].title } },
|
|
|
|
{ id: '2', attributes: { title: sampleIndexPatterns[2].title } },
|
2019-10-09 21:20:55 +02:00
|
|
|
],
|
|
|
|
})),
|
2020-08-21 11:33:42 +02:00
|
|
|
} as unknown) as Pick<SavedObjectsClientContract, 'find'>;
|
|
|
|
}
|
|
|
|
|
|
|
|
function mockIndexPatternsService() {
|
|
|
|
return ({
|
|
|
|
get: jest.fn(async (id: '1' | '2') => {
|
2020-09-24 09:55:32 +02:00
|
|
|
return { ...sampleIndexPatternsFromService[id], metaFields: [] };
|
2020-08-21 11:33:42 +02:00
|
|
|
}),
|
|
|
|
} as unknown) as Pick<IndexPatternsContract, 'get'>;
|
2019-10-09 21:20:55 +02:00
|
|
|
}
|
|
|
|
|
2020-06-27 08:20:29 +02:00
|
|
|
describe('loader', () => {
|
2019-10-09 21:20:55 +02:00
|
|
|
describe('loadIndexPatterns', () => {
|
|
|
|
it('should not load index patterns that are already loaded', async () => {
|
|
|
|
const cache = await loadIndexPatterns({
|
|
|
|
cache: sampleIndexPatterns,
|
2020-08-21 11:33:42 +02:00
|
|
|
patterns: ['1', '2'],
|
|
|
|
indexPatternsService: ({
|
|
|
|
get: jest.fn(() =>
|
|
|
|
Promise.reject('mockIndexPatternService.get should not have been called')
|
|
|
|
),
|
|
|
|
} as unknown) as Pick<IndexPatternsContract, 'get'>,
|
2019-10-09 21:20:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(cache).toEqual(sampleIndexPatterns);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should load index patterns that are not loaded', async () => {
|
|
|
|
const cache = await loadIndexPatterns({
|
|
|
|
cache: {
|
2020-08-21 11:33:42 +02:00
|
|
|
'2': sampleIndexPatterns['2'],
|
2019-10-09 21:20:55 +02:00
|
|
|
},
|
2020-08-21 11:33:42 +02:00
|
|
|
patterns: ['1', '2'],
|
|
|
|
indexPatternsService: mockIndexPatternsService(),
|
2019-10-09 21:20:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(cache).toMatchObject(sampleIndexPatterns);
|
|
|
|
});
|
|
|
|
|
2020-01-06 20:55:51 +01:00
|
|
|
it('should allow scripted, but not full text fields', async () => {
|
2019-12-09 19:09:41 +01:00
|
|
|
const cache = await loadIndexPatterns({
|
|
|
|
cache: {},
|
2020-08-21 11:33:42 +02:00
|
|
|
patterns: ['1', '2'],
|
|
|
|
indexPatternsService: mockIndexPatternsService(),
|
2019-12-09 19:09:41 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(cache).toMatchObject(sampleIndexPatterns);
|
|
|
|
});
|
|
|
|
|
2019-10-09 21:20:55 +02:00
|
|
|
it('should apply field restrictions from typeMeta', async () => {
|
|
|
|
const cache = await loadIndexPatterns({
|
|
|
|
cache: {},
|
|
|
|
patterns: ['foo'],
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternsService: ({
|
|
|
|
get: jest.fn(async () => ({
|
|
|
|
id: 'foo',
|
|
|
|
title: 'Foo index',
|
2020-09-24 09:55:32 +02:00
|
|
|
metaFields: [],
|
2020-08-21 11:33:42 +02:00
|
|
|
typeMeta: {
|
|
|
|
aggs: {
|
|
|
|
date_histogram: {
|
|
|
|
timestamp: {
|
|
|
|
agg: 'date_histogram',
|
|
|
|
fixed_interval: 'm',
|
2019-10-09 21:20:55 +02:00
|
|
|
},
|
|
|
|
},
|
2020-08-21 11:33:42 +02:00
|
|
|
sum: {
|
|
|
|
bytes: {
|
|
|
|
agg: 'sum',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fields: [
|
|
|
|
{
|
|
|
|
name: 'timestamp',
|
|
|
|
displayName: 'timestampLabel',
|
|
|
|
type: 'date',
|
|
|
|
aggregatable: true,
|
|
|
|
searchable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'bytes',
|
|
|
|
displayName: 'bytes',
|
|
|
|
type: 'number',
|
|
|
|
aggregatable: true,
|
|
|
|
searchable: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
})),
|
|
|
|
} as unknown) as Pick<IndexPatternsContract, 'get'>,
|
2019-10-09 21:20:55 +02:00
|
|
|
});
|
|
|
|
|
2020-11-10 14:31:04 +01:00
|
|
|
expect(cache.foo.getFieldByName('bytes')!.aggregationRestrictions).toEqual({
|
2019-10-09 21:20:55 +02:00
|
|
|
sum: { agg: 'sum' },
|
|
|
|
});
|
2020-11-10 14:31:04 +01:00
|
|
|
expect(cache.foo.getFieldByName('timestamp')!.aggregationRestrictions).toEqual({
|
2020-08-21 11:33:42 +02:00
|
|
|
date_histogram: { agg: 'date_histogram', fixed_interval: 'm' },
|
|
|
|
});
|
2019-10-09 21:20:55 +02:00
|
|
|
});
|
2020-09-24 09:55:32 +02:00
|
|
|
|
|
|
|
it('should map meta flag', async () => {
|
|
|
|
const cache = await loadIndexPatterns({
|
|
|
|
cache: {},
|
|
|
|
patterns: ['foo'],
|
|
|
|
indexPatternsService: ({
|
|
|
|
get: jest.fn(async () => ({
|
|
|
|
id: 'foo',
|
|
|
|
title: 'Foo index',
|
|
|
|
metaFields: ['timestamp'],
|
|
|
|
typeMeta: {
|
|
|
|
aggs: {
|
|
|
|
date_histogram: {
|
|
|
|
timestamp: {
|
|
|
|
agg: 'date_histogram',
|
|
|
|
fixed_interval: 'm',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
sum: {
|
|
|
|
bytes: {
|
|
|
|
agg: 'sum',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fields: [
|
|
|
|
{
|
|
|
|
name: 'timestamp',
|
|
|
|
displayName: 'timestampLabel',
|
|
|
|
type: 'date',
|
|
|
|
aggregatable: true,
|
|
|
|
searchable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'bytes',
|
|
|
|
displayName: 'bytes',
|
|
|
|
type: 'number',
|
|
|
|
aggregatable: true,
|
|
|
|
searchable: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
})),
|
|
|
|
} as unknown) as Pick<IndexPatternsContract, 'get'>,
|
|
|
|
});
|
|
|
|
|
2020-11-10 14:31:04 +01:00
|
|
|
expect(cache.foo.getFieldByName('timestamp')!.meta).toEqual(true);
|
2020-09-24 09:55:32 +02:00
|
|
|
});
|
2019-10-09 21:20:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('loadInitialState', () => {
|
|
|
|
it('should load a default state', async () => {
|
2020-06-26 16:33:09 +02:00
|
|
|
const storage = createMockStorage();
|
2019-10-09 21:20:55 +02:00
|
|
|
const state = await loadInitialState({
|
|
|
|
savedObjectsClient: mockClient(),
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternsService: mockIndexPatternsService(),
|
2020-06-26 16:33:09 +02:00
|
|
|
storage,
|
2019-10-09 21:20:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(state).toMatchObject({
|
2020-08-21 11:33:42 +02:00
|
|
|
currentIndexPatternId: '1',
|
2019-10-09 21:20:55 +02:00
|
|
|
indexPatternRefs: [
|
2020-08-21 11:33:42 +02:00
|
|
|
{ id: '1', title: sampleIndexPatterns['1'].title },
|
|
|
|
{ id: '2', title: sampleIndexPatterns['2'].title },
|
2019-10-09 21:20:55 +02:00
|
|
|
],
|
|
|
|
indexPatterns: {
|
2020-08-21 11:33:42 +02:00
|
|
|
'1': sampleIndexPatterns['1'],
|
2019-10-09 21:20:55 +02:00
|
|
|
},
|
|
|
|
layers: {},
|
|
|
|
});
|
2020-06-26 16:33:09 +02:00
|
|
|
expect(storage.set).toHaveBeenCalledWith('lens-settings', {
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternId: '1',
|
2020-06-26 16:33:09 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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(),
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternsService: mockIndexPatternsService(),
|
2020-06-26 16:33:09 +02:00
|
|
|
storage,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(state).toMatchObject({
|
2020-08-21 11:33:42 +02:00
|
|
|
currentIndexPatternId: '1',
|
2020-06-26 16:33:09 +02:00
|
|
|
indexPatternRefs: [
|
2020-08-21 11:33:42 +02:00
|
|
|
{ id: '1', title: sampleIndexPatterns['1'].title },
|
|
|
|
{ id: '2', title: sampleIndexPatterns['2'].title },
|
2020-06-26 16:33:09 +02:00
|
|
|
],
|
|
|
|
indexPatterns: {
|
2020-08-21 11:33:42 +02:00
|
|
|
'1': sampleIndexPatterns['1'],
|
2020-06-26 16:33:09 +02:00
|
|
|
},
|
|
|
|
layers: {},
|
|
|
|
});
|
|
|
|
expect(storage.set).toHaveBeenCalledWith('lens-settings', {
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternId: '1',
|
2020-06-26 16:33:09 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should load lastUsedIndexPatternId if in localStorage', async () => {
|
|
|
|
const state = await loadInitialState({
|
|
|
|
savedObjectsClient: mockClient(),
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternsService: mockIndexPatternsService(),
|
|
|
|
storage: createMockStorage({ indexPatternId: '2' }),
|
2020-06-26 16:33:09 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(state).toMatchObject({
|
2020-08-21 11:33:42 +02:00
|
|
|
currentIndexPatternId: '2',
|
2020-06-26 16:33:09 +02:00
|
|
|
indexPatternRefs: [
|
2020-08-21 11:33:42 +02:00
|
|
|
{ id: '1', title: sampleIndexPatterns['1'].title },
|
|
|
|
{ id: '2', title: sampleIndexPatterns['2'].title },
|
2020-06-26 16:33:09 +02:00
|
|
|
],
|
|
|
|
indexPatterns: {
|
2020-08-21 11:33:42 +02:00
|
|
|
'2': sampleIndexPatterns['2'],
|
2020-06-26 16:33:09 +02:00
|
|
|
},
|
|
|
|
layers: {},
|
|
|
|
});
|
2019-10-09 21:20:55 +02:00
|
|
|
});
|
|
|
|
|
2020-01-06 20:55:51 +01:00
|
|
|
it('should use the default index pattern id, if provided', async () => {
|
2020-06-26 16:33:09 +02:00
|
|
|
const storage = createMockStorage();
|
2020-01-06 20:55:51 +01:00
|
|
|
const state = await loadInitialState({
|
2020-08-21 11:33:42 +02:00
|
|
|
defaultIndexPatternId: '2',
|
2020-01-06 20:55:51 +01:00
|
|
|
savedObjectsClient: mockClient(),
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternsService: mockIndexPatternsService(),
|
2020-06-26 16:33:09 +02:00
|
|
|
storage,
|
2020-01-06 20:55:51 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(state).toMatchObject({
|
2020-08-21 11:33:42 +02:00
|
|
|
currentIndexPatternId: '2',
|
2020-01-06 20:55:51 +01:00
|
|
|
indexPatternRefs: [
|
2020-08-21 11:33:42 +02:00
|
|
|
{ id: '1', title: sampleIndexPatterns['1'].title },
|
|
|
|
{ id: '2', title: sampleIndexPatterns['2'].title },
|
2020-01-06 20:55:51 +01:00
|
|
|
],
|
|
|
|
indexPatterns: {
|
2020-08-21 11:33:42 +02:00
|
|
|
'2': sampleIndexPatterns['2'],
|
2020-01-06 20:55:51 +01:00
|
|
|
},
|
|
|
|
layers: {},
|
|
|
|
});
|
2020-06-26 16:33:09 +02:00
|
|
|
expect(storage.set).toHaveBeenCalledWith('lens-settings', {
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternId: '2',
|
2020-06-26 16:33:09 +02:00
|
|
|
});
|
2020-01-06 20:55:51 +01:00
|
|
|
});
|
|
|
|
|
2020-10-06 10:15:41 +02:00
|
|
|
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: {
|
|
|
|
indexPatternId: '1',
|
|
|
|
fieldName: '',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(state).toMatchObject({
|
|
|
|
currentIndexPatternId: '1',
|
|
|
|
indexPatternRefs: [
|
|
|
|
{ id: '1', title: sampleIndexPatterns['1'].title },
|
|
|
|
{ id: '2', title: sampleIndexPatterns['2'].title },
|
|
|
|
],
|
|
|
|
indexPatterns: {
|
|
|
|
'1': sampleIndexPatterns['1'],
|
|
|
|
},
|
|
|
|
layers: {},
|
|
|
|
});
|
|
|
|
expect(storage.set).toHaveBeenCalledWith('lens-settings', {
|
|
|
|
indexPatternId: '1',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-09 21:20:55 +02:00
|
|
|
it('should initialize from saved state', async () => {
|
|
|
|
const savedState: IndexPatternPersistedState = {
|
|
|
|
layers: {
|
|
|
|
layerb: {
|
|
|
|
columnOrder: ['col1', 'col2'],
|
|
|
|
columns: {
|
|
|
|
col1: {
|
|
|
|
dataType: 'date',
|
|
|
|
isBucketed: true,
|
|
|
|
label: 'My date',
|
|
|
|
operationType: 'date_histogram',
|
|
|
|
params: {
|
|
|
|
interval: 'm',
|
|
|
|
},
|
|
|
|
sourceField: 'timestamp',
|
|
|
|
},
|
|
|
|
col2: {
|
|
|
|
dataType: 'number',
|
|
|
|
isBucketed: false,
|
|
|
|
label: 'Sum of bytes',
|
|
|
|
operationType: 'sum',
|
|
|
|
sourceField: 'bytes',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2020-08-21 11:33:42 +02:00
|
|
|
const storage = createMockStorage({ indexPatternId: '1' });
|
2019-10-09 21:20:55 +02:00
|
|
|
const state = await loadInitialState({
|
2020-08-21 18:08:25 +02:00
|
|
|
persistedState: savedState,
|
|
|
|
references: [
|
|
|
|
{ name: 'indexpattern-datasource-current-indexpattern', id: '2', type: 'index-pattern' },
|
|
|
|
{ name: 'indexpattern-datasource-layer-layerb', id: '2', type: 'index-pattern' },
|
|
|
|
{ name: 'another-reference', id: 'c', type: 'index-pattern' },
|
|
|
|
],
|
2019-10-09 21:20:55 +02:00
|
|
|
savedObjectsClient: mockClient(),
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternsService: mockIndexPatternsService(),
|
2020-06-26 16:33:09 +02:00
|
|
|
storage,
|
2019-10-09 21:20:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(state).toMatchObject({
|
2020-08-21 11:33:42 +02:00
|
|
|
currentIndexPatternId: '2',
|
2019-10-09 21:20:55 +02:00
|
|
|
indexPatternRefs: [
|
2020-08-21 11:33:42 +02:00
|
|
|
{ id: '1', title: sampleIndexPatterns['1'].title },
|
|
|
|
{ id: '2', title: sampleIndexPatterns['2'].title },
|
2019-10-09 21:20:55 +02:00
|
|
|
],
|
|
|
|
indexPatterns: {
|
2020-08-21 11:33:42 +02:00
|
|
|
'2': sampleIndexPatterns['2'],
|
2019-10-09 21:20:55 +02:00
|
|
|
},
|
2020-08-21 18:08:25 +02:00
|
|
|
layers: { layerb: { ...savedState.layers.layerb, indexPatternId: '2' } },
|
2019-10-09 21:20:55 +02:00
|
|
|
});
|
2020-06-26 16:33:09 +02:00
|
|
|
|
|
|
|
expect(storage.set).toHaveBeenCalledWith('lens-settings', {
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternId: '2',
|
2020-06-26 16:33:09 +02:00
|
|
|
});
|
2019-10-09 21:20:55 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-21 18:08:25 +02:00
|
|
|
describe('saved object references', () => {
|
|
|
|
const state: IndexPatternPrivateState = {
|
|
|
|
currentIndexPatternId: 'b',
|
|
|
|
indexPatternRefs: [],
|
|
|
|
indexPatterns: {},
|
|
|
|
existingFields: {},
|
|
|
|
layers: {
|
|
|
|
a: {
|
|
|
|
indexPatternId: 'id-index-pattern-a',
|
|
|
|
columnOrder: ['col1'],
|
|
|
|
columns: {
|
|
|
|
col1: {
|
|
|
|
dataType: 'number',
|
|
|
|
isBucketed: false,
|
|
|
|
label: '',
|
|
|
|
operationType: 'avg',
|
|
|
|
sourceField: 'myfield',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
b: {
|
|
|
|
indexPatternId: 'id-index-pattern-b',
|
|
|
|
columnOrder: ['col2'],
|
|
|
|
columns: {
|
|
|
|
col2: {
|
|
|
|
dataType: 'number',
|
|
|
|
isBucketed: false,
|
|
|
|
label: '',
|
|
|
|
operationType: 'avg',
|
|
|
|
sourceField: 'myfield2',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
isFirstExistenceFetch: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
it('should create a reference for each layer and for current index pattern', () => {
|
|
|
|
const { savedObjectReferences } = extractReferences(state);
|
|
|
|
expect(savedObjectReferences).toMatchInlineSnapshot(`
|
|
|
|
Array [
|
|
|
|
Object {
|
|
|
|
"id": "b",
|
|
|
|
"name": "indexpattern-datasource-current-indexpattern",
|
|
|
|
"type": "index-pattern",
|
|
|
|
},
|
|
|
|
Object {
|
|
|
|
"id": "id-index-pattern-a",
|
|
|
|
"name": "indexpattern-datasource-layer-a",
|
|
|
|
"type": "index-pattern",
|
|
|
|
},
|
|
|
|
Object {
|
|
|
|
"id": "id-index-pattern-b",
|
|
|
|
"name": "indexpattern-datasource-layer-b",
|
|
|
|
"type": "index-pattern",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should restore layers', () => {
|
|
|
|
const { savedObjectReferences, state: persistedState } = extractReferences(state);
|
|
|
|
expect(injectReferences(persistedState, savedObjectReferences).layers).toEqual(state.layers);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should restore current index pattern', () => {
|
|
|
|
const { savedObjectReferences, state: persistedState } = extractReferences(state);
|
|
|
|
expect(injectReferences(persistedState, savedObjectReferences).currentIndexPatternId).toEqual(
|
|
|
|
state.currentIndexPatternId
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-09 21:20:55 +02:00
|
|
|
describe('changeIndexPattern', () => {
|
|
|
|
it('loads the index pattern and then sets it as current', async () => {
|
|
|
|
const setState = jest.fn();
|
|
|
|
const state: IndexPatternPrivateState = {
|
2020-08-21 11:33:42 +02:00
|
|
|
currentIndexPatternId: '2',
|
2019-10-09 21:20:55 +02:00
|
|
|
indexPatternRefs: [],
|
|
|
|
indexPatterns: {},
|
2019-10-14 18:46:33 +02:00
|
|
|
existingFields: {},
|
2019-10-09 21:20:55 +02:00
|
|
|
layers: {},
|
2020-07-01 10:08:37 +02:00
|
|
|
isFirstExistenceFetch: false,
|
2019-10-09 21:20:55 +02:00
|
|
|
};
|
2020-08-21 11:33:42 +02:00
|
|
|
const storage = createMockStorage({ indexPatternId: '2' });
|
2019-10-09 21:20:55 +02:00
|
|
|
|
|
|
|
await changeIndexPattern({
|
|
|
|
state,
|
|
|
|
setState,
|
2020-08-21 11:33:42 +02:00
|
|
|
id: '1',
|
|
|
|
indexPatternsService: mockIndexPatternsService(),
|
2019-10-09 21:20:55 +02:00
|
|
|
onError: jest.fn(),
|
2020-06-26 16:33:09 +02:00
|
|
|
storage,
|
2019-10-09 21:20:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(setState).toHaveBeenCalledTimes(1);
|
|
|
|
expect(setState.mock.calls[0][0](state)).toMatchObject({
|
2020-08-21 11:33:42 +02:00
|
|
|
currentIndexPatternId: '1',
|
2019-10-09 21:20:55 +02:00
|
|
|
indexPatterns: {
|
2020-08-21 11:33:42 +02:00
|
|
|
'1': {
|
|
|
|
...sampleIndexPatterns['1'],
|
|
|
|
fields: [...sampleIndexPatterns['1'].fields],
|
|
|
|
},
|
2019-10-09 21:20:55 +02:00
|
|
|
},
|
|
|
|
});
|
2020-06-26 16:33:09 +02:00
|
|
|
expect(storage.set).toHaveBeenCalledWith('lens-settings', {
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternId: '1',
|
2020-06-26 16:33:09 +02:00
|
|
|
});
|
2019-10-09 21:20:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('handles errors', async () => {
|
|
|
|
const setState = jest.fn();
|
|
|
|
const onError = jest.fn();
|
|
|
|
const err = new Error('NOPE!');
|
|
|
|
const state: IndexPatternPrivateState = {
|
2020-08-21 11:33:42 +02:00
|
|
|
currentIndexPatternId: '2',
|
2019-10-09 21:20:55 +02:00
|
|
|
indexPatternRefs: [],
|
2019-10-14 18:46:33 +02:00
|
|
|
existingFields: {},
|
2019-10-09 21:20:55 +02:00
|
|
|
indexPatterns: {},
|
|
|
|
layers: {},
|
2020-07-01 10:08:37 +02:00
|
|
|
isFirstExistenceFetch: false,
|
2019-10-09 21:20:55 +02:00
|
|
|
};
|
|
|
|
|
2020-08-21 11:33:42 +02:00
|
|
|
const storage = createMockStorage({ indexPatternId: '2' });
|
2020-06-26 16:33:09 +02:00
|
|
|
|
2019-10-09 21:20:55 +02:00
|
|
|
await changeIndexPattern({
|
|
|
|
state,
|
|
|
|
setState,
|
2020-08-21 11:33:42 +02:00
|
|
|
id: '1',
|
|
|
|
indexPatternsService: {
|
|
|
|
get: jest.fn(async () => {
|
2019-10-09 21:20:55 +02:00
|
|
|
throw err;
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
onError,
|
2020-06-26 16:33:09 +02:00
|
|
|
storage,
|
2019-10-09 21:20:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(setState).not.toHaveBeenCalled();
|
2020-06-26 16:33:09 +02:00
|
|
|
expect(storage.set).not.toHaveBeenCalled();
|
2019-10-09 21:20:55 +02:00
|
|
|
expect(onError).toHaveBeenCalledWith(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('changeLayerIndexPattern', () => {
|
|
|
|
it('loads the index pattern and then changes the specified layer', async () => {
|
|
|
|
const setState = jest.fn();
|
|
|
|
const state: IndexPatternPrivateState = {
|
2020-08-21 11:33:42 +02:00
|
|
|
currentIndexPatternId: '2',
|
2019-10-09 21:20:55 +02:00
|
|
|
indexPatternRefs: [],
|
2019-10-14 18:46:33 +02:00
|
|
|
existingFields: {},
|
2019-10-09 21:20:55 +02:00
|
|
|
indexPatterns: {
|
2020-08-21 11:33:42 +02:00
|
|
|
'1': sampleIndexPatterns['1'],
|
2019-10-09 21:20:55 +02:00
|
|
|
},
|
|
|
|
layers: {
|
|
|
|
l0: {
|
|
|
|
columnOrder: ['col1'],
|
|
|
|
columns: {},
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternId: '1',
|
2019-10-09 21:20:55 +02:00
|
|
|
},
|
|
|
|
l1: {
|
|
|
|
columnOrder: ['col2'],
|
|
|
|
columns: {
|
|
|
|
col2: {
|
|
|
|
dataType: 'date',
|
|
|
|
isBucketed: true,
|
|
|
|
label: 'My hist',
|
|
|
|
operationType: 'date_histogram',
|
|
|
|
params: {
|
|
|
|
interval: 'm',
|
|
|
|
},
|
|
|
|
sourceField: 'timestamp',
|
|
|
|
},
|
|
|
|
},
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternId: '1',
|
2019-10-09 21:20:55 +02:00
|
|
|
},
|
|
|
|
},
|
2020-07-01 10:08:37 +02:00
|
|
|
isFirstExistenceFetch: false,
|
2019-10-09 21:20:55 +02:00
|
|
|
};
|
|
|
|
|
2020-08-21 11:33:42 +02:00
|
|
|
const storage = createMockStorage({ indexPatternId: '1' });
|
2020-06-26 16:33:09 +02:00
|
|
|
|
2019-10-09 21:20:55 +02:00
|
|
|
await changeLayerIndexPattern({
|
|
|
|
state,
|
|
|
|
setState,
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternId: '2',
|
2019-10-09 21:20:55 +02:00
|
|
|
layerId: 'l1',
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternsService: mockIndexPatternsService(),
|
2019-10-09 21:20:55 +02:00
|
|
|
onError: jest.fn(),
|
2020-06-26 16:33:09 +02:00
|
|
|
storage,
|
2019-10-09 21:20:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(setState).toHaveBeenCalledTimes(1);
|
|
|
|
expect(setState.mock.calls[0][0](state)).toMatchObject({
|
2020-08-21 11:33:42 +02:00
|
|
|
currentIndexPatternId: '2',
|
2019-10-09 21:20:55 +02:00
|
|
|
indexPatterns: {
|
2020-08-21 11:33:42 +02:00
|
|
|
1: sampleIndexPatterns['1'],
|
|
|
|
2: sampleIndexPatterns['2'],
|
2019-10-09 21:20:55 +02:00
|
|
|
},
|
|
|
|
layers: {
|
|
|
|
l0: {
|
|
|
|
columnOrder: ['col1'],
|
|
|
|
columns: {},
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternId: '1',
|
2019-10-09 21:20:55 +02:00
|
|
|
},
|
|
|
|
l1: {
|
|
|
|
columnOrder: ['col2'],
|
|
|
|
columns: {
|
|
|
|
col2: {
|
|
|
|
dataType: 'date',
|
|
|
|
isBucketed: true,
|
|
|
|
label: 'My hist',
|
|
|
|
operationType: 'date_histogram',
|
|
|
|
params: {
|
|
|
|
interval: '1d',
|
|
|
|
},
|
|
|
|
sourceField: 'timestamp',
|
|
|
|
},
|
|
|
|
},
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternId: '2',
|
2019-10-09 21:20:55 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2020-06-26 16:33:09 +02:00
|
|
|
expect(storage.set).toHaveBeenCalledWith('lens-settings', {
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternId: '2',
|
2020-06-26 16:33:09 +02:00
|
|
|
});
|
2019-10-09 21:20:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('handles errors', async () => {
|
|
|
|
const setState = jest.fn();
|
|
|
|
const onError = jest.fn();
|
|
|
|
const err = new Error('NOPE!');
|
|
|
|
const state: IndexPatternPrivateState = {
|
2020-08-21 11:33:42 +02:00
|
|
|
currentIndexPatternId: '2',
|
2019-10-09 21:20:55 +02:00
|
|
|
indexPatternRefs: [],
|
2019-10-14 18:46:33 +02:00
|
|
|
existingFields: {},
|
2019-10-09 21:20:55 +02:00
|
|
|
indexPatterns: {
|
2020-08-21 11:33:42 +02:00
|
|
|
'1': sampleIndexPatterns['1'],
|
2019-10-09 21:20:55 +02:00
|
|
|
},
|
|
|
|
layers: {
|
|
|
|
l0: {
|
|
|
|
columnOrder: ['col1'],
|
|
|
|
columns: {},
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternId: '1',
|
2019-10-09 21:20:55 +02:00
|
|
|
},
|
|
|
|
},
|
2020-07-01 10:08:37 +02:00
|
|
|
isFirstExistenceFetch: false,
|
2019-10-09 21:20:55 +02:00
|
|
|
};
|
|
|
|
|
2020-08-21 11:33:42 +02:00
|
|
|
const storage = createMockStorage({ indexPatternId: '2' });
|
2020-06-26 16:33:09 +02:00
|
|
|
|
2019-10-09 21:20:55 +02:00
|
|
|
await changeLayerIndexPattern({
|
|
|
|
state,
|
|
|
|
setState,
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternId: '2',
|
2019-10-09 21:20:55 +02:00
|
|
|
layerId: 'l0',
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternsService: {
|
|
|
|
get: jest.fn(async () => {
|
2019-10-09 21:20:55 +02:00
|
|
|
throw err;
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
onError,
|
2020-06-26 16:33:09 +02:00
|
|
|
storage,
|
2019-10-09 21:20:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(setState).not.toHaveBeenCalled();
|
2020-06-26 16:33:09 +02:00
|
|
|
expect(storage.set).not.toHaveBeenCalled();
|
2019-10-09 21:20:55 +02:00
|
|
|
expect(onError).toHaveBeenCalledWith(err);
|
|
|
|
});
|
|
|
|
});
|
2019-10-14 18:46:33 +02:00
|
|
|
|
|
|
|
describe('syncExistingFields', () => {
|
2020-03-12 09:03:40 +01:00
|
|
|
const dslQuery = {
|
|
|
|
bool: {
|
|
|
|
must: [],
|
|
|
|
filter: [{ match_all: {} }],
|
|
|
|
should: [],
|
|
|
|
must_not: [],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-10-14 18:46:33 +02:00
|
|
|
it('should call once for each index pattern', async () => {
|
|
|
|
const setState = jest.fn();
|
2020-07-01 10:08:37 +02:00
|
|
|
const fetchJson = (jest.fn((path: string) => {
|
2020-01-24 23:20:09 +01:00
|
|
|
const indexPatternTitle = _.last(path.split('/'));
|
2019-10-14 18:46:33 +02:00
|
|
|
return {
|
|
|
|
indexPatternTitle,
|
|
|
|
existingFieldNames: ['field_1', 'field_2'].map(
|
2020-08-21 11:33:42 +02:00
|
|
|
(fieldName) => `ip${indexPatternTitle}_${fieldName}`
|
2019-10-14 18:46:33 +02:00
|
|
|
),
|
|
|
|
};
|
2020-07-01 10:08:37 +02:00
|
|
|
}) as unknown) as HttpHandler;
|
2019-10-14 18:46:33 +02:00
|
|
|
|
|
|
|
await syncExistingFields({
|
|
|
|
dateRange: { fromDate: '1900-01-01', toDate: '2000-01-01' },
|
2020-07-01 10:08:37 +02:00
|
|
|
fetchJson,
|
2020-07-16 09:54:46 +02:00
|
|
|
indexPatterns: [
|
2020-08-26 09:09:40 +02:00
|
|
|
{ id: '1', title: '1', fields: [], hasRestrictions: false },
|
|
|
|
{ id: '2', title: '1', fields: [], hasRestrictions: false },
|
|
|
|
{ id: '3', title: '1', fields: [], hasRestrictions: false },
|
2020-07-16 09:54:46 +02:00
|
|
|
],
|
2019-10-14 18:46:33 +02:00
|
|
|
setState,
|
2020-03-12 09:03:40 +01:00
|
|
|
dslQuery,
|
2020-07-01 10:08:37 +02:00
|
|
|
showNoDataPopover: jest.fn(),
|
|
|
|
currentIndexPatternTitle: 'abc',
|
|
|
|
isFirstExistenceFetch: false,
|
2019-10-14 18:46:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(fetchJson).toHaveBeenCalledTimes(3);
|
|
|
|
expect(setState).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
const [fn] = setState.mock.calls[0];
|
|
|
|
const newState = fn({
|
|
|
|
foo: 'bar',
|
|
|
|
existingFields: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(newState).toEqual({
|
|
|
|
foo: 'bar',
|
2020-07-01 10:08:37 +02:00
|
|
|
isFirstExistenceFetch: false,
|
2020-07-16 09:54:46 +02:00
|
|
|
existenceFetchFailed: false,
|
2019-10-14 18:46:33 +02:00
|
|
|
existingFields: {
|
2020-08-21 11:33:42 +02:00
|
|
|
'1': { ip1_field_1: true, ip1_field_2: true },
|
|
|
|
'2': { ip2_field_1: true, ip2_field_2: true },
|
|
|
|
'3': { ip3_field_1: true, ip3_field_2: true },
|
2019-10-14 18:46:33 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2020-07-01 10:08:37 +02:00
|
|
|
|
|
|
|
it('should call showNoDataPopover callback if current index pattern returns no fields', async () => {
|
|
|
|
const setState = jest.fn();
|
|
|
|
const showNoDataPopover = jest.fn();
|
|
|
|
const fetchJson = (jest.fn((path: string) => {
|
|
|
|
const indexPatternTitle = _.last(path.split('/'));
|
|
|
|
return {
|
|
|
|
indexPatternTitle,
|
|
|
|
existingFieldNames:
|
2020-08-21 11:33:42 +02:00
|
|
|
indexPatternTitle === '1'
|
2020-07-01 10:08:37 +02:00
|
|
|
? ['field_1', 'field_2'].map((fieldName) => `${indexPatternTitle}_${fieldName}`)
|
|
|
|
: [],
|
|
|
|
};
|
|
|
|
}) as unknown) as HttpHandler;
|
|
|
|
|
|
|
|
const args = {
|
|
|
|
dateRange: { fromDate: '1900-01-01', toDate: '2000-01-01' },
|
|
|
|
fetchJson,
|
2020-07-16 09:54:46 +02:00
|
|
|
indexPatterns: [
|
2020-08-26 09:09:40 +02:00
|
|
|
{ id: '1', title: '1', fields: [], hasRestrictions: false },
|
|
|
|
{ id: '2', title: '1', fields: [], hasRestrictions: false },
|
|
|
|
{ id: 'c', title: '1', fields: [], hasRestrictions: false },
|
2020-07-16 09:54:46 +02:00
|
|
|
],
|
2020-07-01 10:08:37 +02:00
|
|
|
setState,
|
|
|
|
dslQuery,
|
|
|
|
showNoDataPopover: jest.fn(),
|
|
|
|
currentIndexPatternTitle: 'abc',
|
|
|
|
isFirstExistenceFetch: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
await syncExistingFields(args);
|
|
|
|
|
|
|
|
expect(showNoDataPopover).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
await syncExistingFields({ ...args, isFirstExistenceFetch: true });
|
|
|
|
expect(showNoDataPopover).not.toHaveBeenCalled();
|
|
|
|
});
|
2020-07-16 09:54:46 +02:00
|
|
|
|
|
|
|
it('should set all fields to available and existence error flag if the request fails', async () => {
|
|
|
|
const setState = jest.fn();
|
|
|
|
const fetchJson = (jest.fn((path: string) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
reject(new Error());
|
|
|
|
});
|
|
|
|
}) as unknown) as HttpHandler;
|
|
|
|
|
|
|
|
const args = {
|
|
|
|
dateRange: { fromDate: '1900-01-01', toDate: '2000-01-01' },
|
|
|
|
fetchJson,
|
|
|
|
indexPatterns: [
|
|
|
|
{
|
2020-08-21 11:33:42 +02:00
|
|
|
id: '1',
|
|
|
|
title: '1',
|
2020-08-26 09:09:40 +02:00
|
|
|
hasRestrictions: false,
|
2020-07-16 09:54:46 +02:00
|
|
|
fields: [{ name: 'field1' }, { name: 'field2' }] as IndexPatternField[],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
setState,
|
|
|
|
dslQuery,
|
|
|
|
showNoDataPopover: jest.fn(),
|
|
|
|
currentIndexPatternTitle: 'abc',
|
|
|
|
isFirstExistenceFetch: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
await syncExistingFields(args);
|
|
|
|
|
|
|
|
const [fn] = setState.mock.calls[0];
|
|
|
|
const newState = fn({
|
|
|
|
foo: 'bar',
|
|
|
|
existingFields: {},
|
|
|
|
}) as IndexPatternPrivateState;
|
|
|
|
|
|
|
|
expect(newState.existenceFetchFailed).toEqual(true);
|
2020-08-21 11:33:42 +02:00
|
|
|
expect(newState.existingFields['1']).toEqual({
|
2020-07-16 09:54:46 +02:00
|
|
|
field1: true,
|
|
|
|
field2: true,
|
|
|
|
});
|
|
|
|
});
|
2019-10-14 18:46:33 +02:00
|
|
|
});
|
2019-10-09 21:20:55 +02:00
|
|
|
});
|