Skip to content

Commit

Permalink
toast component jest tests (elastic#215)
Browse files Browse the repository at this point in the history
* toast jest tests

* using test style guide this time

* test each color value
  • Loading branch information
nreese authored and snide committed Dec 21, 2017
1 parent 0493ee6 commit 89fa5cf
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ exports[`EuiGlobalToastList is rendered 1`] = `
aria-label="aria-label"
class="euiGlobalToastList testClass1 testClass2"
data-test-subj="test subject string"
/>
>
<div>
hi
</div>
</div>
`;
101 changes: 100 additions & 1 deletion src/components/toast/__snapshots__/toast.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,17 +1,116 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`EuiToast Props color danger is rendered 1`] = `
<div
className="euiToast euiToast--danger"
>
<div
className="euiToastHeader"
>
<span
className="euiToastHeader__title"
/>
</div>
</div>
`;

exports[`EuiToast Props color primary is rendered 1`] = `
<div
className="euiToast euiToast--primary"
>
<div
className="euiToastHeader"
>
<span
className="euiToastHeader__title"
/>
</div>
</div>
`;

exports[`EuiToast Props color success is rendered 1`] = `
<div
className="euiToast euiToast--success"
>
<div
className="euiToastHeader"
>
<span
className="euiToastHeader__title"
/>
</div>
</div>
`;

exports[`EuiToast Props color warning is rendered 1`] = `
<div
className="euiToast euiToast--warning"
>
<div
className="euiToastHeader"
>
<span
className="euiToastHeader__title"
/>
</div>
</div>
`;

exports[`EuiToast Props iconType is rendered 1`] = `
<div
className="euiToast"
>
<div
className="euiToastHeader"
>
<EuiIcon
aria-hidden="true"
className="euiToastHeader__icon"
size="m"
type="user"
/>
<span
className="euiToastHeader__title"
/>
</div>
</div>
`;

exports[`EuiToast Props title is rendered 1`] = `
<div
className="euiToast"
>
<div
className="euiToastHeader"
>
<span
className="euiToastHeader__title"
>
toast title
</span>
</div>
</div>
`;

exports[`EuiToast is rendered 1`] = `
<div
aria-label="aria-label"
class="euiToast testClass1 testClass2"
data-test-subj="test subject string"
>
<div
class="euiToastHeader"
class="euiToastHeader euiToastHeader--withBody"
>
<span
class="euiToastHeader__title"
/>
</div>
<div
class="euiText euiText--small"
>
<p>
Hi
</p>
</div>
</div>
`;
4 changes: 3 additions & 1 deletion src/components/toast/global_toast_list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { EuiGlobalToastList } from './global_toast_list';
describe('EuiGlobalToastList', () => {
test('is rendered', () => {
const component = render(
<EuiGlobalToastList {...requiredProps} />
<EuiGlobalToastList {...requiredProps}>
<div>hi</div>
</EuiGlobalToastList>
);

expect(component)
Expand Down
2 changes: 2 additions & 0 deletions src/components/toast/toast.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const EuiToast = ({ title, color, iconType, onClose, children, className,
className="euiToast__closeButton"
aria-label="Dismiss toast"
onClick={onClose}
data-test-subj="toastCloseButton"
>
<EuiIcon
type="cross"
Expand Down Expand Up @@ -92,4 +93,5 @@ EuiToast.propTypes = {
iconType: PropTypes.oneOf(ICON_TYPES),
color: PropTypes.oneOf(COLORS),
onClose: PropTypes.func,
children: PropTypes.node,
};
56 changes: 52 additions & 4 deletions src/components/toast/toast.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,64 @@
import React from 'react';
import { render } from 'enzyme';
import { requiredProps } from '../../test/required_props';
import { render, shallow } from 'enzyme';
import sinon from 'sinon';
import {
findTestSubject,
requiredProps
} from '../../test';

import { EuiToast } from './toast';
import {
COLORS,
EuiToast
} from './toast';

describe('EuiToast', () => {
test('is rendered', () => {
const component = render(
<EuiToast {...requiredProps} />
<EuiToast {...requiredProps}>
<p>Hi</p>
</EuiToast>
);

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

describe('Props', () => {
describe('title', () => {
test('is rendered', () => {
const component = <EuiToast title="toast title" />;
expect(shallow(component)).toMatchSnapshot();
});
});

describe('color', () => {
COLORS.forEach(color => {
test(`${color} is rendered`, () => {
const component = <EuiToast color={color} />;
expect(shallow(component)).toMatchSnapshot();
});
});
});

describe('iconType', () => {
test('is rendered', () => {
const component = <EuiToast iconType="user" />;
expect(shallow(component)).toMatchSnapshot();
});
});

describe('onClose', () => {
test('is called when the close button is clicked', () => {
const onCloseHandler = sinon.stub();

const component = shallow(
<EuiToast onClose={onCloseHandler} />
);
const closeButton = findTestSubject(component, 'toastCloseButton');
closeButton.simulate('click');

sinon.assert.calledOnce(onCloseHandler);
});
});
});
});

0 comments on commit 89fa5cf

Please sign in to comment.