Skip to content

Commit

Permalink
Use EuiThemeProvider in lists plugin tests and stories
Browse files Browse the repository at this point in the history
Remove `getMockTheme` and use `EuiThemeProvider` from the kibana_react plugin.

Use the CSF-style decorators with `EuiThemeProvider` in the stories.

No functional changes, but should be less code to maintain.
  • Loading branch information
smith committed Apr 2, 2021
1 parent f042ec8 commit b8bb57e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 81 deletions.
13 changes: 0 additions & 13 deletions x-pack/plugins/lists/public/common/test_utils/kibana_react.mock.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,17 @@
* 2.0.
*/

import { Story, addDecorator } from '@storybook/react';
import { Story } from '@storybook/react';
import React from 'react';
import { ThemeProvider } from 'styled-components';
import euiLightVars from '@elastic/eui/dist/eui_theme_light.json';
import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui';

import { getMockTheme } from '../../../common/test_utils/kibana_react.mock';
import { EuiThemeProvider } from '../../../../../../../src/plugins/kibana_react/common';

import { AndOrBadge, AndOrBadgeProps } from '.';

const sampleText =
'Doggo ipsum i am bekom fat snoot wow such tempt waggy wags floofs, ruff heckin good boys and girls mlem. Ruff heckin good boys and girls mlem stop it fren borkf borking doggo very hand that feed shibe, you are doing me the shock big ol heck smol borking doggo with a long snoot for pats heckin good boys. You are doing me the shock smol borking doggo with a long snoot for pats wow very biscit, length boy. Doggo ipsum i am bekom fat snoot wow such tempt waggy wags floofs, ruff heckin good boys and girls mlem. Ruff heckin good boys and girls mlem stop it fren borkf borking doggo very hand that feed shibe, you are doing me the shock big ol heck smol borking doggo with a long snoot for pats heckin good boys.';

const mockTheme = getMockTheme({
darkMode: false,
eui: euiLightVars,
});

addDecorator((storyFn) => <ThemeProvider theme={mockTheme}>{storyFn()}</ThemeProvider>);

export default {
argTypes: {
includeAntennas: {
Expand Down Expand Up @@ -58,6 +49,13 @@ export default {
},
},
component: AndOrBadge,
decorators: [
(DecoratorStory: React.ComponentClass): React.ReactNode => (
<EuiThemeProvider>
<DecoratorStory />
</EuiThemeProvider>
),
],
title: 'AndOrBadge',
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@
*/

import React from 'react';
import { ThemeProvider } from 'styled-components';
import { mount } from 'enzyme';

import { getMockTheme } from '../../../common/test_utils/kibana_react.mock';
import { EuiThemeProvider } from '../../../../../../../src/plugins/kibana_react/common';

import { AndOrBadge } from './';

const mockTheme = getMockTheme({ eui: { euiColorLightShade: '#ece' } });

describe('AndOrBadge', () => {
test('it renders top and bottom antenna bars when "includeAntennas" is true', () => {
const wrapper = mount(
<ThemeProvider theme={mockTheme}>
<EuiThemeProvider>
<AndOrBadge includeAntennas type="and" />
</ThemeProvider>
</EuiThemeProvider>
);

expect(wrapper.find('[data-test-subj="and-or-badge"]').at(0).text()).toEqual('AND');
Expand All @@ -30,9 +27,9 @@ describe('AndOrBadge', () => {

test('it does not render top and bottom antenna bars when "includeAntennas" is false', () => {
const wrapper = mount(
<ThemeProvider theme={mockTheme}>
<EuiThemeProvider>
<AndOrBadge type="or" />
</ThemeProvider>
</EuiThemeProvider>
);

expect(wrapper.find('[data-test-subj="and-or-badge"]').at(0).text()).toEqual('OR');
Expand All @@ -42,19 +39,19 @@ describe('AndOrBadge', () => {

test('it renders "and" when "type" is "and"', () => {
const wrapper = mount(
<ThemeProvider theme={mockTheme}>
<EuiThemeProvider>
<AndOrBadge type="and" />
</ThemeProvider>
</EuiThemeProvider>
);

expect(wrapper.find('[data-test-subj="and-or-badge"]').at(0).text()).toEqual('AND');
});

test('it renders "or" when "type" is "or"', () => {
const wrapper = mount(
<ThemeProvider theme={mockTheme}>
<EuiThemeProvider>
<AndOrBadge type="or" />
</ThemeProvider>
</EuiThemeProvider>
);

expect(wrapper.find('[data-test-subj="and-or-badge"]').at(0).text()).toEqual('OR');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@
*/

import React from 'react';
import { ThemeProvider } from 'styled-components';
import { mount } from 'enzyme';

import { getMockTheme } from '../../../common/test_utils/kibana_react.mock';
import { EuiThemeProvider } from '../../../../../../../src/plugins/kibana_react/common';

import { RoundedBadgeAntenna } from './rounded_badge_antenna';

const mockTheme = getMockTheme({ eui: { euiColorLightShade: '#ece' } });

describe('RoundedBadgeAntenna', () => {
test('it renders top and bottom antenna bars', () => {
const wrapper = mount(
<ThemeProvider theme={mockTheme}>
<EuiThemeProvider>
<RoundedBadgeAntenna type="and" />
</ThemeProvider>
</EuiThemeProvider>
);

expect(wrapper.find('[data-test-subj="and-or-badge"]').at(0).text()).toEqual('AND');
Expand All @@ -30,19 +27,19 @@ describe('RoundedBadgeAntenna', () => {

test('it renders "and" when "type" is "and"', () => {
const wrapper = mount(
<ThemeProvider theme={mockTheme}>
<EuiThemeProvider>
<RoundedBadgeAntenna type="and" />
</ThemeProvider>
</EuiThemeProvider>
);

expect(wrapper.find('[data-test-subj="and-or-badge"]').at(0).text()).toEqual('AND');
});

test('it renders "or" when "type" is "or"', () => {
const wrapper = mount(
<ThemeProvider theme={mockTheme}>
<EuiThemeProvider>
<RoundedBadgeAntenna type="or" />
</ThemeProvider>
</EuiThemeProvider>
);

expect(wrapper.find('[data-test-subj="and-or-badge"]').at(0).text()).toEqual('OR');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@
*/

import React from 'react';
import { ThemeProvider } from 'styled-components';
import { mount } from 'enzyme';

import { getMockTheme } from '../../../common/test_utils/kibana_react.mock';
import { EuiThemeProvider } from '../../../../../../../src/plugins/kibana_react/common';

import { BuilderAndBadgeComponent } from './and_badge';

const mockTheme = getMockTheme({ eui: { euiColorLightShade: '#ece' } });

describe('BuilderAndBadgeComponent', () => {
test('it renders exceptionItemEntryFirstRowAndBadge for very first exception item in builder', () => {
const wrapper = mount(
<ThemeProvider theme={mockTheme}>
<EuiThemeProvider>
<BuilderAndBadgeComponent entriesLength={2} exceptionItemIndex={0} />
</ThemeProvider>
</EuiThemeProvider>
);

expect(
Expand All @@ -30,9 +27,9 @@ describe('BuilderAndBadgeComponent', () => {

test('it renders exceptionItemEntryInvisibleAndBadge if "entriesLength" is 1 or less', () => {
const wrapper = mount(
<ThemeProvider theme={mockTheme}>
<EuiThemeProvider>
<BuilderAndBadgeComponent entriesLength={1} exceptionItemIndex={0} />
</ThemeProvider>
</EuiThemeProvider>
);

expect(
Expand All @@ -42,9 +39,9 @@ describe('BuilderAndBadgeComponent', () => {

test('it renders regular "and" badge if exception item is not the first one and includes more than one entry', () => {
const wrapper = mount(
<ThemeProvider theme={mockTheme}>
<EuiThemeProvider>
<BuilderAndBadgeComponent entriesLength={2} exceptionItemIndex={1} />
</ThemeProvider>
</EuiThemeProvider>
);

expect(wrapper.find('[data-test-subj="exceptionItemEntryAndBadge"]').exists()).toBeTruthy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,18 @@
* 2.0.
*/

import { Story, addDecorator } from '@storybook/react';
import { Story } from '@storybook/react';
import { action } from '@storybook/addon-actions';
import React from 'react';
import { ThemeProvider } from 'styled-components';
import euiLightVars from '@elastic/eui/dist/eui_theme_light.json';
import { HttpStart } from 'kibana/public';

import { OperatorEnum, OperatorTypeEnum } from '../../../../common';
import { AutocompleteStart } from '../../../../../../../src/plugins/data/public';
import { fields } from '../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks';
import { getMockTheme } from '../../../common/test_utils/kibana_react.mock';
import { EuiThemeProvider } from '../../../../../../../src/plugins/kibana_react/common';

import { BuilderEntryItem, EntryItemProps } from './entry_renderer';

const mockTheme = getMockTheme({
darkMode: false,
eui: euiLightVars,
});
const mockAutocompleteService = ({
getValueSuggestions: () =>
new Promise((resolve) => {
Expand Down Expand Up @@ -59,8 +53,6 @@ const mockAutocompleteService = ({
}),
} as unknown) as AutocompleteStart;

addDecorator((storyFn) => <ThemeProvider theme={mockTheme}>{storyFn()}</ThemeProvider>);

export default {
argTypes: {
allowLargeValueLists: {
Expand Down Expand Up @@ -163,6 +155,13 @@ export default {
},
},
component: BuilderEntryItem,
decorators: [
(DecoratorStory: React.ComponentClass): React.ReactNode => (
<EuiThemeProvider>
<DecoratorStory />
</EuiThemeProvider>
),
],
title: 'BuilderEntryItem',
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,18 @@
*/

import React from 'react';
import { ThemeProvider } from 'styled-components';
import { mount } from 'enzyme';
import { dataPluginMock } from 'src/plugins/data/public/mocks';

import { fields } from '../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks';
import { EuiThemeProvider } from '../../../../../../../src/plugins/kibana_react/common';
import { getExceptionListItemSchemaMock } from '../../../../common/schemas/response/exception_list_item_schema.mock';
import { getEntryMatchMock } from '../../../../common/schemas/types/entry_match.mock';
import { getEntryMatchAnyMock } from '../../../../common/schemas/types/entry_match_any.mock';
import { coreMock } from '../../../../../../../src/core/public/mocks';
import { getMockTheme } from '../../../common/test_utils/kibana_react.mock';

import { BuilderExceptionListItemComponent } from './exception_item_renderer';

const mockTheme = getMockTheme({
eui: {
euiColorLightShade: '#ece',
},
});
const mockKibanaHttpService = coreMock.createStart().http;
const { autocomplete: autocompleteStartMock } = dataPluginMock.createStartContract();

Expand All @@ -41,7 +35,7 @@ describe('BuilderExceptionListItemComponent', () => {
entries: [getEntryMatchMock(), getEntryMatchMock()],
};
const wrapper = mount(
<ThemeProvider theme={mockTheme}>
<EuiThemeProvider>
<BuilderExceptionListItemComponent
allowLargeValueLists={true}
andLogicIncluded={true}
Expand All @@ -60,7 +54,7 @@ describe('BuilderExceptionListItemComponent', () => {
onDeleteExceptionItem={jest.fn()}
setErrorsExist={jest.fn()}
/>
</ThemeProvider>
</EuiThemeProvider>
);

expect(
Expand All @@ -72,7 +66,7 @@ describe('BuilderExceptionListItemComponent', () => {
const exceptionItem = getExceptionListItemSchemaMock();
exceptionItem.entries = [getEntryMatchMock(), getEntryMatchMock()];
const wrapper = mount(
<ThemeProvider theme={mockTheme}>
<EuiThemeProvider>
<BuilderExceptionListItemComponent
allowLargeValueLists={true}
andLogicIncluded={true}
Expand All @@ -91,7 +85,7 @@ describe('BuilderExceptionListItemComponent', () => {
onDeleteExceptionItem={jest.fn()}
setErrorsExist={jest.fn()}
/>
</ThemeProvider>
</EuiThemeProvider>
);

expect(wrapper.find('[data-test-subj="exceptionItemEntryAndBadge"]').exists()).toBeTruthy();
Expand All @@ -101,7 +95,7 @@ describe('BuilderExceptionListItemComponent', () => {
const exceptionItem = getExceptionListItemSchemaMock();
exceptionItem.entries = [getEntryMatchMock()];
const wrapper = mount(
<ThemeProvider theme={mockTheme}>
<EuiThemeProvider>
<BuilderExceptionListItemComponent
allowLargeValueLists={true}
andLogicIncluded={true}
Expand All @@ -120,7 +114,7 @@ describe('BuilderExceptionListItemComponent', () => {
onDeleteExceptionItem={jest.fn()}
setErrorsExist={jest.fn()}
/>
</ThemeProvider>
</EuiThemeProvider>
);

expect(
Expand All @@ -132,7 +126,7 @@ describe('BuilderExceptionListItemComponent', () => {
const exceptionItem = getExceptionListItemSchemaMock();
exceptionItem.entries = [getEntryMatchMock()];
const wrapper = mount(
<ThemeProvider theme={mockTheme}>
<EuiThemeProvider>
<BuilderExceptionListItemComponent
allowLargeValueLists={true}
andLogicIncluded={false}
Expand All @@ -151,7 +145,7 @@ describe('BuilderExceptionListItemComponent', () => {
onDeleteExceptionItem={jest.fn()}
setErrorsExist={jest.fn()}
/>
</ThemeProvider>
</EuiThemeProvider>
);

expect(
Expand Down

0 comments on commit b8bb57e

Please sign in to comment.