Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form tests - Phase 1 #196

Merged
merged 11 commits into from
Dec 14, 2017
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions src/components/form/checkbox/__snapshots__/checkbox.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,108 @@ exports[`EuiCheckbox is rendered 1`] = `
</div>
</div>
`;

exports[`EuiCheckbox props check is rendered 1`] = `
<div
class="euiCheckbox"
>
<input
checked=""
class="euiCheckbox__input"
id="id"
type="checkbox"
/>
<div
class="euiCheckbox__square"
>
<div
class="euiCheckbox__check"
/>
</div>
</div>
`;

exports[`EuiCheckbox props disabled false is rendered 1`] = `
<div
class="euiCheckbox"
>
<input
class="euiCheckbox__input"
id="id"
type="checkbox"
/>
<div
class="euiCheckbox__square"
>
<div
class="euiCheckbox__check"
/>
</div>
</div>
`;

exports[`EuiCheckbox props disabled true is rendered 1`] = `
<div
class="euiCheckbox"
>
<input
class="euiCheckbox__input"
disabled=""
id="id"
type="checkbox"
/>
<div
class="euiCheckbox__square"
>
<div
class="euiCheckbox__check"
/>
</div>
</div>
`;

exports[`EuiCheckbox props label is rendered 1`] = `
<div
class="euiCheckbox"
>
<input
class="euiCheckbox__input"
id="id"
type="checkbox"
/>
<div
class="euiCheckbox__square"
>
<div
class="euiCheckbox__check"
/>
</div>
<label
class="euiCheckbox__label"
for="id"
>
<span>
Label
</span>
</label>
</div>
`;

exports[`EuiCheckbox props type inList is rendered 1`] = `
<div
class="euiCheckbox euiCheckbox--inList"
>
<input
class="euiCheckbox__input"
id="id"
type="checkbox"
/>
<div
class="euiCheckbox__square"
>
<div
class="euiCheckbox__check"
/>
</div>
</div>
`;
Original file line number Diff line number Diff line change
@@ -1,9 +1,59 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`EuiCheckboxGroup is rendered 1`] = `
exports[`EuiCheckboxGroupMockedCheckbox disabled is rendered 1`] = `
<div>
<eui_checkbox
checked=""
class="euiCheckboxGroup__item"
disabled=""
id="1"
label="kibana"
/>
<eui_checkbox
class="euiCheckboxGroup__item"
disabled=""
id="2"
label="elastic"
/>
</div>
`;

exports[`EuiCheckboxGroupMockedCheckbox idToSelectedMap is rendered 1`] = `
<div>
<eui_checkbox
checked=""
class="euiCheckboxGroup__item"
id="1"
label="kibana"
/>
<eui_checkbox
class="euiCheckboxGroup__item"
id="2"
label="elastic"
/>
</div>
`;

exports[`EuiCheckboxGroupMockedCheckbox is rendered 1`] = `
<div
aria-label="aria-label"
class="testClass1 testClass2"
data-test-subj="test subject string"
id="foobar"
/>
`;

exports[`EuiCheckboxGroupMockedCheckbox options are rendered 1`] = `
<div>
<eui_checkbox
class="euiCheckboxGroup__item"
id="1"
label="kibana"
/>
<eui_checkbox
class="euiCheckboxGroup__item"
id="2"
label="elastic"
/>
</div>
`;
95 changes: 92 additions & 3 deletions src/components/form/checkbox/checkbox.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,105 @@
import React from 'react';
import { render } from 'enzyme';
import { requiredProps } from '../../../test/required_props';
import {
requiredProps,
startThrowingReactWarnings,
stopThrowingReactWarnings,
} from '../../../test';

import { EuiCheckbox } from './checkbox';
import {
EuiCheckbox,
TYPES,
} from './checkbox';

beforeAll(startThrowingReactWarnings);
afterAll(stopThrowingReactWarnings);

const checkboxRequiredProps = {
id: 'id',
onChange: () => {},
};

describe('EuiCheckbox', () => {
test('is rendered', () => {
const component = render(
<EuiCheckbox id="id" onChange={() => {}} {...requiredProps} />
<EuiCheckbox
id="id"
onChange={() => {}}
{...requiredProps}
/>
);

expect(component)
.toMatchSnapshot();
});

describe('props', () => {
test('id is required', () => {
expect(
() => <EuiCheckbox checked={true} onChange={() => {}}/>
).toThrow();
});

test('onChange is required', () => {
expect(
() => <EuiCheckbox id="id" checked={true}/>
).toThrow();
});

test('check is rendered', () => {
const component = render(
<EuiCheckbox
{...checkboxRequiredProps}
checked
/>
);

expect(component)
.toMatchSnapshot();
});

test('label is rendered', () => {
const component = render(
<EuiCheckbox
{...checkboxRequiredProps}
label={<span>Label</span>}
/>
);

expect(component)
.toMatchSnapshot();
});

describe('type', () => {
TYPES.forEach(value => {
test(`${value} is rendered`, () => {
const component = render(
<EuiCheckbox
{...checkboxRequiredProps}
type={value}
/>
);

expect(component)
.toMatchSnapshot();
});
});
});

describe('disabled', () => {
[true, false].forEach(value => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get away with just testing true?

test(`${value} is rendered`, () => {
const component = render(
<EuiCheckbox
{...checkboxRequiredProps}
disabled={value}
/>
);

expect(component)
.toMatchSnapshot();
});
});
});
});
});
28 changes: 28 additions & 0 deletions src/components/form/checkbox/checkbox_group.behavior.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';
import { mount } from 'enzyme';
import sinon from 'sinon';

import { EuiCheckboxGroup } from './checkbox_group';

// This exists because we need to run the following tests
// without mocking the Checkbox component, such as testing
// an interaction that is handled by the Checkbox component.
describe('EuiCheckboxGroupUnmockedCheckbox', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rephrase this as "EuiCheckboxGroup behavior" to make it a bit more readable and reflect the file name?

test('id is bound to onChange', () => {
const onChangeHandler = sinon.stub();
const component = mount(
<EuiCheckboxGroup
options={[
{ id: '1', label: 'kibana', disabled: false },
]}
idToSelectedMap={{
'1': true,
}}
onChange={onChangeHandler}
/>
);

component.find('input[type="checkbox"]').simulate('change');
sinon.assert.calledWith(onChangeHandler, '1');
});
});
60 changes: 58 additions & 2 deletions src/components/form/checkbox/checkbox_group.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,66 @@ import { requiredProps } from '../../../test/required_props';

import { EuiCheckboxGroup } from './checkbox_group';

describe('EuiCheckboxGroup', () => {
jest.mock('./checkbox', () => ({ EuiCheckbox: 'eui_checkbox' }));

describe('EuiCheckboxGroupMockedCheckbox', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rephrase this as "EuiCheckboxGroup (mocked checkbox)" to make it a bit more readable?

test('is rendered', () => {
const component = render(
<EuiCheckboxGroup onChange={() => {}} {...requiredProps} />
<EuiCheckboxGroup onChange={() => {}} {...requiredProps} id="foobar" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What were your thoughts on id here? I feel like requiredProps is sufficient for verifying that DOM attributes get passed-through to the element that gets rendered.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

);

expect(component)
.toMatchSnapshot();
});

test('options are rendered', () => {
const component = render(
<EuiCheckboxGroup
options={[
{ id: '1', label: 'kibana' },
{ id: '2', label: 'elastic' },
]}
onChange={() => {}}
/>
);

expect(component)
.toMatchSnapshot();
});

test('idToSelectedMap is rendered', () => {
const component = render(
<EuiCheckboxGroup
options={[
{ id: '1', label: 'kibana' },
{ id: '2', label: 'elastic' },
]}
idToSelectedMap={{
'1': true,
'2': false,
}}
onChange={() => {}}
/>
);

expect(component)
.toMatchSnapshot();
});

test('disabled is rendered', () => {
const component = render(
<EuiCheckboxGroup
options={[
{ id: '1', label: 'kibana' },
{ id: '2', label: 'elastic' },
]}
idToSelectedMap={{
'1': true,
'2': false,
}}
onChange={() => {}}
disabled
/>
);

expect(component)
Expand Down
Loading