Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Apr 3, 2020
1 parent f1a1036 commit 7c32a8c
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 605 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@
*/

import React from 'react';
import { shallow } from 'enzyme';
import { mount, ReactWrapper } from 'enzyme';

import { ClosureOptions } from './closure_options';
import { useMountAppended } from '../../../../utils/use_mount_appended';
import { ClosureOptions, ClosureOptionsProps } from './closure_options';
import { TestProviders } from '../../../../mock';
import { ClosureOptionsRadio } from './closure_options_radio';

describe('ClosureOptions', () => {
const mount = useMountAppended();

test('it shows the left side', () => {
const wrapper = shallow(
<ClosureOptions
disabled={false}
closureTypeSelected="close-by-user"
onChangeClosureType={jest.fn()}
/>
);
let wrapper: ReactWrapper;
const onChangeClosureType = jest.fn();
const props: ClosureOptionsProps = {
disabled: false,
closureTypeSelected: 'close-by-user',
onChangeClosureType,
};

beforeAll(() => {
wrapper = mount(<ClosureOptions {...props} />, { wrappingComponent: TestProviders });
});

test('it shows the closure options form group', () => {
expect(
wrapper
.find('[data-test-subj="case-closure-options-form-group"]')
Expand All @@ -32,16 +33,7 @@ describe('ClosureOptions', () => {
).toBe(true);
});

test('it shows the right side', () => {
const wrapper = mount(
<ClosureOptions
disabled={false}
closureTypeSelected="close-by-user"
onChangeClosureType={jest.fn()}
/>,
{ wrappingComponent: TestProviders }
);

test('it shows the closure options form row', () => {
expect(
wrapper
.find('[data-test-subj="case-closure-options-form-row"]')
Expand All @@ -51,15 +43,6 @@ describe('ClosureOptions', () => {
});

test('it shows closure options', () => {
const wrapper = mount(
<ClosureOptions
disabled={false}
closureTypeSelected="close-by-user"
onChangeClosureType={jest.fn()}
/>,
{ wrappingComponent: TestProviders }
);

expect(
wrapper
.find('[data-test-subj="case-closure-options-radio"]')
Expand All @@ -69,17 +52,6 @@ describe('ClosureOptions', () => {
});

test('it pass the correct props to child', () => {
const onChangeClosureType = jest.fn();

const wrapper = mount(
<ClosureOptions
disabled={false}
closureTypeSelected="close-by-user"
onChangeClosureType={onChangeClosureType}
/>,
{ wrappingComponent: TestProviders }
);

const closureOptionsRadioComponent = wrapper.find(ClosureOptionsRadio);
expect(closureOptionsRadioComponent.props().disabled).toEqual(false);
expect(closureOptionsRadioComponent.props().closureTypeSelected).toEqual('close-by-user');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ClosureType } from '../../../../containers/case/configure/types';
import { ClosureOptionsRadio } from './closure_options_radio';
import * as i18n from './translations';

interface ClosureOptionsProps {
export interface ClosureOptionsProps {
closureTypeSelected: ClosureType;
disabled: boolean;
onChangeClosureType: (newClosureType: ClosureType) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@
*/

import React from 'react';
import { shallow } from 'enzyme';
import { ReactWrapper, mount } from 'enzyme';

import { ClosureOptionsRadio } from './closure_options_radio';
import { useMountAppended } from '../../../../utils/use_mount_appended';
import { ClosureOptionsRadio, ClosureOptionsRadioComponentProps } from './closure_options_radio';
import { TestProviders } from '../../../../mock';

describe('ClosureOptionsRadio', () => {
const mount = useMountAppended();
let wrapper: ReactWrapper;
const onChangeClosureType = jest.fn();
const props: ClosureOptionsRadioComponentProps = {
disabled: false,
closureTypeSelected: 'close-by-user',
onChangeClosureType,
};

beforeAll(() => {
wrapper = mount(<ClosureOptionsRadio {...props} />, { wrappingComponent: TestProviders });
});

test('it renders', () => {
const wrapper = shallow(
<ClosureOptionsRadio
disabled={false}
closureTypeSelected="close-by-user"
onChangeClosureType={jest.fn()}
/>
);

expect(
wrapper
.find('[data-test-subj="closure-options-radio-group"]')
Expand All @@ -32,71 +33,44 @@ describe('ClosureOptionsRadio', () => {
});

test('it shows the correct number of radio buttons', () => {
const wrapper = mount(
<ClosureOptionsRadio
disabled={false}
closureTypeSelected="close-by-user"
onChangeClosureType={jest.fn()}
/>,
{ wrappingComponent: TestProviders }
);

expect(wrapper.find('input[name="closure_options"]')).toHaveLength(2);
});

test('it renders the correct radio buttons', () => {
const wrapper = mount(
<ClosureOptionsRadio
disabled={false}
closureTypeSelected="close-by-user"
onChangeClosureType={jest.fn()}
/>,
{ wrappingComponent: TestProviders }
);
test('it renders close by user radio button', () => {
expect(wrapper.find('input[id="close-by-user"]').exists()).toBeTruthy();
});

expect(wrapper.find('input[id="close-by-user"]')).toBeDefined();
expect(wrapper.find('input[id="close-by-pushing"]')).toBeDefined();
test('it renders close by pushing radio button', () => {
expect(wrapper.find('input[id="close-by-pushing"]').exists()).toBeTruthy();
});

test('it disables correctly', () => {
const wrapper = mount(
<ClosureOptionsRadio
disabled={true}
closureTypeSelected="close-by-user"
onChangeClosureType={jest.fn()}
/>,
{ wrappingComponent: TestProviders }
);
test('it disables the close by user radio button', () => {
const newWrapper = mount(<ClosureOptionsRadio {...props} disabled={true} />, {
wrappingComponent: TestProviders,
});

expect(wrapper.find('input[id="close-by-user"]').prop('disabled')).toEqual(true);
expect(wrapper.find('input[id="close-by-pushing"]').prop('disabled')).toEqual(true);
expect(newWrapper.find('input[id="close-by-user"]').prop('disabled')).toEqual(true);
});

test('it selects the correct radio button', () => {
const wrapper = mount(
<ClosureOptionsRadio
disabled={false}
closureTypeSelected="close-by-pushing"
onChangeClosureType={jest.fn()}
/>,
{ wrappingComponent: TestProviders }
);
test('it disables correctly the close by pushing radio button', () => {
const newWrapper = mount(<ClosureOptionsRadio {...props} disabled={true} />, {
wrappingComponent: TestProviders,
});

expect(wrapper.find('input[id="close-by-pushing"]').prop('checked')).toEqual(true);
expect(newWrapper.find('input[id="close-by-pushing"]').prop('disabled')).toEqual(true);
});

test('it calls the onChangeClosureType function', () => {
const onChangeClosureType = jest.fn();

const wrapper = mount(
<ClosureOptionsRadio
disabled={false}
closureTypeSelected="close-by-user"
onChangeClosureType={onChangeClosureType}
/>,
{ wrappingComponent: TestProviders }
test('it selects the correct radio button', () => {
const newWrapper = mount(
<ClosureOptionsRadio {...props} closureTypeSelected={'close-by-pushing'} />,
{
wrappingComponent: TestProviders,
}
);
expect(newWrapper.find('input[id="close-by-pushing"]').prop('checked')).toEqual(true);
});

test('it calls the onChangeClosureType function', () => {
wrapper.find('input[id="close-by-pushing"]').simulate('change');
wrapper.update();
expect(onChangeClosureType).toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const radios: ClosureRadios[] = [
},
];

interface ClosureOptionsRadioComponentProps {
export interface ClosureOptionsRadioComponentProps {
closureTypeSelected: ClosureType;
disabled: boolean;
onChangeClosureType: (newClosureType: ClosureType) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,31 @@
*/

import React from 'react';
import { shallow } from 'enzyme';
import { mount, ReactWrapper } from 'enzyme';

import { Connectors } from './connectors';
import { useMountAppended } from '../../../../utils/use_mount_appended';
import { Connectors, Props } from './connectors';
import { TestProviders } from '../../../../mock';
import { ConnectorsDropdown } from './connectors_dropdown';
import { connectors } from './__mock__';

describe('Connectors', () => {
const mount = useMountAppended();
let wrapper: ReactWrapper;
const onChangeConnector = jest.fn();
const handleShowAddFlyout = jest.fn();
const props: Props = {
disabled: false,
connectors,
selectedConnector: 'none',
isLoading: false,
onChangeConnector,
handleShowAddFlyout,
};

test('it shows the left side', () => {
const wrapper = shallow(
<Connectors
connectors={[]}
disabled={false}
selectedConnector={'none'}
isLoading={false}
onChangeConnector={jest.fn()}
handleShowAddFlyout={jest.fn()}
/>
);
beforeAll(() => {
wrapper = mount(<Connectors {...props} />, { wrappingComponent: TestProviders });
});

test('it shows the connectors from group', () => {
expect(
wrapper
.find('[data-test-subj="case-connectors-form-group"]')
Expand All @@ -36,19 +38,7 @@ describe('Connectors', () => {
).toBe(true);
});

test('it shows the right side', () => {
const wrapper = mount(
<Connectors
connectors={[]}
disabled={false}
selectedConnector={'none'}
isLoading={false}
onChangeConnector={jest.fn()}
handleShowAddFlyout={jest.fn()}
/>,
{ wrappingComponent: TestProviders }
);

test('it shows the connectors form row', () => {
expect(
wrapper
.find('[data-test-subj="case-connectors-form-row"]')
Expand All @@ -58,18 +48,6 @@ describe('Connectors', () => {
});

test('it shows the connectors dropdown', () => {
const wrapper = mount(
<Connectors
connectors={[]}
disabled={false}
selectedConnector={'none'}
isLoading={false}
onChangeConnector={jest.fn()}
handleShowAddFlyout={jest.fn()}
/>,
{ wrappingComponent: TestProviders }
);

expect(
wrapper
.find('[data-test-subj="case-connectors-dropdown"]')
Expand All @@ -79,24 +57,13 @@ describe('Connectors', () => {
});

test('it pass the correct props to child', () => {
const onChange = jest.fn();

const wrapper = shallow(
<Connectors
connectors={connectors}
disabled={false}
selectedConnector={'none'}
isLoading={false}
onChangeConnector={onChange}
handleShowAddFlyout={jest.fn()}
/>
);

const connectorsDropdownComponent = wrapper.find(ConnectorsDropdown);
expect(connectorsDropdownComponent.props().disabled).toEqual(false);
expect(connectorsDropdownComponent.props().isLoading).toEqual(false);
expect(connectorsDropdownComponent.props().connectors).toEqual(connectors);
expect(connectorsDropdownComponent.props().selectedConnector).toEqual('none');
expect(connectorsDropdownComponent.props().onChange).toEqual(onChange);
const connectorsDropdownProps = wrapper.find(ConnectorsDropdown).props();
expect(connectorsDropdownProps).toMatchObject({
disabled: false,
isLoading: false,
connectors,
selectedConnector: 'none',
onChange: props.onChangeConnector,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const EuiFormRowExtended = styled(EuiFormRow)`
}
`;

interface Props {
export interface Props {
connectors: Connector[];
disabled: boolean;
isLoading: boolean;
Expand Down
Loading

0 comments on commit 7c32a8c

Please sign in to comment.