Skip to content

Commit

Permalink
tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
mbondyra committed Jun 25, 2020
1 parent 0ad4772 commit 5e69375
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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<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: [],
};

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>,
exists: true,
};
});

it('renders correctly', async () => {
const wrapper = mountWithIntl(<FieldsAccordion {...defaultProps} />);
expect(wrapper.find(FieldItem).length).toEqual(2);
});

it('renders callout if no fields', async () => {
const wrapper = shallowWithIntl(
<FieldsAccordion {...defaultProps} fieldsCount={0} paginatedFields={[]} />
);
expect(wrapper.find('#lens-test-callout').length).toEqual(1);
});

it('renders accented notificationBadge state if isFiltered', async () => {
const wrapper = mountWithIntl(<FieldsAccordion {...defaultProps} isFiltered={true} />);
expect(wrapper.find(EuiNotificationBadge).prop('color')).toEqual('accent');
});

it('renders spinner if has not loaded', async () => {
const wrapper = mountWithIntl(<FieldsAccordion {...defaultProps} hasLoaded={false} />);
expect(wrapper.find(EuiLoadingSpinner).length).toEqual(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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 (
<FieldItem {...fieldProps} key={field.name} field={field} exists={exists ? true : false} />
);
return <FieldItem {...fieldProps} key={field.name} field={field} exists={!!exists} />;
},
[fieldProps]
[fieldProps, exists]
);

return (
Expand Down

0 comments on commit 5e69375

Please sign in to comment.