2020-06-26 18:43:35 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import { EuiLoadingSpinner, EuiNotificationBadge } from '@elastic/eui';
|
|
|
|
import { coreMock } from 'src/core/public/mocks';
|
2020-11-13 01:19:56 +01:00
|
|
|
import { mountWithIntl, shallowWithIntl } from '@kbn/test/jest';
|
2020-06-26 18:43:35 +02:00
|
|
|
import { DataPublicPluginStart } from '../../../../../src/plugins/data/public';
|
|
|
|
import { dataPluginMock } from '../../../../../src/plugins/data/public/mocks';
|
|
|
|
import { IndexPattern } from './types';
|
|
|
|
import { FieldItem } from './field_item';
|
|
|
|
import { FieldsAccordion, FieldsAccordionProps, FieldItemSharedProps } from './fields_accordion';
|
2020-07-02 16:02:30 +02:00
|
|
|
import { chartPluginMock } from '../../../../../src/plugins/charts/public/mocks';
|
2020-06-26 18:43:35 +02:00
|
|
|
|
|
|
|
describe('Fields Accordion', () => {
|
|
|
|
let defaultProps: FieldsAccordionProps;
|
|
|
|
let indexPattern: IndexPattern;
|
|
|
|
let core: ReturnType<typeof coreMock['createSetup']>;
|
|
|
|
let data: DataPublicPluginStart;
|
|
|
|
let fieldProps: FieldItemSharedProps;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
indexPattern = {
|
|
|
|
id: '1',
|
|
|
|
title: 'my-fake-index-pattern',
|
|
|
|
timeFieldName: 'timestamp',
|
|
|
|
fields: [
|
|
|
|
{
|
|
|
|
name: 'timestamp',
|
|
|
|
type: 'date',
|
|
|
|
aggregatable: true,
|
|
|
|
searchable: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'bytes',
|
|
|
|
type: 'number',
|
|
|
|
aggregatable: true,
|
|
|
|
searchable: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
} as IndexPattern;
|
|
|
|
core = coreMock.createSetup();
|
|
|
|
data = dataPluginMock.createStartContract();
|
|
|
|
core.http.post.mockClear();
|
|
|
|
|
|
|
|
fieldProps = {
|
|
|
|
indexPattern,
|
|
|
|
data,
|
|
|
|
core,
|
|
|
|
highlight: '',
|
|
|
|
dateRange: {
|
|
|
|
fromDate: 'now-7d',
|
|
|
|
toDate: 'now',
|
|
|
|
},
|
|
|
|
query: { query: '', language: 'lucene' },
|
|
|
|
filters: [],
|
2020-07-02 16:02:30 +02:00
|
|
|
chartsThemeService: chartPluginMock.createSetupContract().theme,
|
2020-06-26 18:43:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
defaultProps = {
|
|
|
|
initialIsOpen: true,
|
|
|
|
onToggle: jest.fn(),
|
|
|
|
id: 'id',
|
|
|
|
label: 'label',
|
|
|
|
hasLoaded: true,
|
|
|
|
fieldsCount: 2,
|
|
|
|
isFiltered: false,
|
|
|
|
paginatedFields: indexPattern.fields,
|
|
|
|
fieldProps,
|
|
|
|
renderCallout: <div id="lens-test-callout">Callout</div>,
|
2020-09-24 09:55:32 +02:00
|
|
|
exists: () => true,
|
2020-06-26 18:43:35 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders correct number of Field Items', () => {
|
2020-09-24 09:55:32 +02:00
|
|
|
const wrapper = mountWithIntl(
|
|
|
|
<FieldsAccordion {...defaultProps} exists={(field) => field.name === 'timestamp'} />
|
|
|
|
);
|
|
|
|
expect(wrapper.find(FieldItem).at(0).prop('exists')).toEqual(true);
|
|
|
|
expect(wrapper.find(FieldItem).at(1).prop('exists')).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passed correct exists flag to each field', () => {
|
2020-06-26 18:43:35 +02:00
|
|
|
const wrapper = mountWithIntl(<FieldsAccordion {...defaultProps} />);
|
|
|
|
expect(wrapper.find(FieldItem).length).toEqual(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders callout if no fields', () => {
|
|
|
|
const wrapper = shallowWithIntl(
|
|
|
|
<FieldsAccordion {...defaultProps} fieldsCount={0} paginatedFields={[]} />
|
|
|
|
);
|
|
|
|
expect(wrapper.find('#lens-test-callout').length).toEqual(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders accented notificationBadge state if isFiltered', () => {
|
|
|
|
const wrapper = mountWithIntl(<FieldsAccordion {...defaultProps} isFiltered={true} />);
|
|
|
|
expect(wrapper.find(EuiNotificationBadge).prop('color')).toEqual('accent');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders spinner if has not loaded', () => {
|
|
|
|
const wrapper = mountWithIntl(<FieldsAccordion {...defaultProps} hasLoaded={false} />);
|
|
|
|
expect(wrapper.find(EuiLoadingSpinner).length).toEqual(1);
|
|
|
|
});
|
|
|
|
});
|