From 5e69375c79656fd1045853105c5b4b41a2552789 Mon Sep 17 00:00:00 2001 From: Marta Bondyra Date: Thu, 25 Jun 2020 15:35:25 +0200 Subject: [PATCH] tests added --- .../fields_accordion.test.tsx | 97 +++++++++++++++++++ .../fields_accordion.tsx | 36 +++---- 2 files changed, 115 insertions(+), 18 deletions(-) create mode 100644 x-pack/plugins/lens/public/indexpattern_datasource/fields_accordion.test.tsx diff --git a/x-pack/plugins/lens/public/indexpattern_datasource/fields_accordion.test.tsx b/x-pack/plugins/lens/public/indexpattern_datasource/fields_accordion.test.tsx new file mode 100644 index 000000000000000..43eef79f4859f04 --- /dev/null +++ b/x-pack/plugins/lens/public/indexpattern_datasource/fields_accordion.test.tsx @@ -0,0 +1,97 @@ +/* + * 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'; +import { mountWithIntl, shallowWithIntl } from 'test_utils/enzyme_helpers'; +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'; + +describe('Fields Accordion', () => { + let defaultProps: FieldsAccordionProps; + let indexPattern: IndexPattern; + let core: ReturnType; + 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: [], + }; + + defaultProps = { + initialIsOpen: true, + onToggle: jest.fn(), + id: 'id', + label: 'label', + hasLoaded: true, + fieldsCount: 2, + isFiltered: false, + paginatedFields: indexPattern.fields, + fieldProps, + renderCallout:
Callout
, + exists: true, + }; + }); + + it('renders correctly', async () => { + const wrapper = mountWithIntl(); + expect(wrapper.find(FieldItem).length).toEqual(2); + }); + + it('renders callout if no fields', async () => { + const wrapper = shallowWithIntl( + + ); + expect(wrapper.find('#lens-test-callout').length).toEqual(1); + }); + + it('renders accented notificationBadge state if isFiltered', async () => { + const wrapper = mountWithIntl(); + expect(wrapper.find(EuiNotificationBadge).prop('color')).toEqual('accent'); + }); + + it('renders spinner if has not loaded', async () => { + const wrapper = mountWithIntl(); + expect(wrapper.find(EuiLoadingSpinner).length).toEqual(1); + }); +}); diff --git a/x-pack/plugins/lens/public/indexpattern_datasource/fields_accordion.tsx b/x-pack/plugins/lens/public/indexpattern_datasource/fields_accordion.tsx index 8395bdec688bf72..e86639343d938da 100644 --- a/x-pack/plugins/lens/public/indexpattern_datasource/fields_accordion.tsx +++ b/x-pack/plugins/lens/public/indexpattern_datasource/fields_accordion.tsx @@ -22,7 +22,7 @@ import { Query, Filter } from '../../../../../src/plugins/data/public'; import { DatasourceDataPanelProps } from '../types'; import { IndexPattern } from './types'; -export interface FieldProps { +export interface FieldItemSharedProps { core: DatasourceDataPanelProps['core']; data: DataPublicPluginStart; indexPattern: IndexPattern; @@ -32,6 +32,20 @@ export interface FieldProps { filters: Filter[]; } +export interface FieldsAccordionProps { + initialIsOpen: boolean; + onToggle: (open: boolean) => void; + id: string; + label: string; + hasLoaded: boolean; + fieldsCount: number; + isFiltered: boolean; + paginatedFields: IndexPatternField[]; + fieldProps: FieldItemSharedProps; + renderCallout: JSX.Element; + exists: boolean; +} + export const InnerFieldsAccordion = function InnerFieldsAccordion({ initialIsOpen, onToggle, @@ -44,26 +58,12 @@ export const InnerFieldsAccordion = function InnerFieldsAccordion({ fieldProps, renderCallout, exists, -}: { - initialIsOpen: boolean; - onToggle: (open: boolean) => void; - id: string; - label: string; - hasLoaded: boolean; - fieldsCount: number; - isFiltered: boolean; - paginatedFields: IndexPatternField[]; - fieldProps: FieldProps; - renderCallout: JSX.Element; - exists: boolean; -}) { +}: FieldsAccordionProps) { const renderField = useCallback( (field: IndexPatternField) => { - return ( - - ); + return ; }, - [fieldProps] + [fieldProps, exists] ); return (