This repository has been archived by the owner on Oct 19, 2021. It is now read-only.
/
AccordionItem-test.js
123 lines (106 loc) · 4.03 KB
/
AccordionItem-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import React from 'react';
import AccordionItem from '../AccordionItem';
import Icon from '../Icon';
import { shallow, mount } from 'enzyme';
describe('AccordionItem', () => {
describe('Renders as expected', () => {
const wrapper = shallow(
<AccordionItem title="A heading" className="extra-class">
Lorem ipsum.
</AccordionItem>
);
it('renders children as expected', () => {
expect(wrapper.find('.bx--accordion__content').text()).toBe('Lorem ipsum.');
});
it('renders heading as expected', () => {
const heading = wrapper.find('.bx--accordion__heading');
expect(heading.length).toBe(1);
expect(heading.find(Icon).length).toBe(1);
expect(heading.find('.bx--accordion__title').text()).toBe('A heading');
});
it('should use correct icon', () => {
const heading = wrapper.find('.bx--accordion__heading');
expect(heading.find(Icon).props().name).toEqual('chevron--right');
});
it('has the expected classes', () => {
expect(wrapper.hasClass('bx--accordion__item')).toEqual(true);
expect(wrapper.hasClass('bx--accordion__item--active')).toEqual(false);
});
it('renders extra classes passed in via className', () => {
expect(wrapper.hasClass('extra-class')).toEqual(true);
});
it('can be open by default', () => {
const openItem = shallow(
<AccordionItem title="A heading" open>
Lorem ipsum.
</AccordionItem>
);
expect(openItem.hasClass('bx--accordion__item--active')).toEqual(true);
expect(openItem.state().open).toEqual(true);
});
it('should apply the active class when the state is open', () => {
const toggler = mount(<AccordionItem />);
const item = toggler.find('li');
expect(item.hasClass('bx--accordion__item--active')).toEqual(false);
toggler.setState({ open: true });
expect(toggler.find('li').hasClass('bx--accordion__item--active')).toEqual(true);
});
});
describe('Check that functions passed in as props are called', () => {
const onClick = jest.fn();
const onHeadingClick = jest.fn();
const wrapper = mount(
<AccordionItem onClick={onClick} onHeadingClick={onHeadingClick} />
);
const heading = wrapper.find('.bx--accordion__heading');
it('should call onClick', () => {
wrapper.simulate('click');
expect(onClick).toBeCalled();
});
it('should call onHeadingClick', () => {
heading.simulate('click');
expect(onHeadingClick).toBeCalled();
});
});
describe('Check that clicking the item toggles its open state', () => {
const toggler = mount(
<AccordionItem title="A heading">
Lorem ipsum.
</AccordionItem>
);
const heading = toggler.find('.bx--accordion__heading');
it('should set state to open when clicked', () => {
expect(toggler.state().open).toEqual(false);
heading.simulate('click');
expect(toggler.state().open).toEqual(true);
});
});
describe('Check that the keyboard toggles its open state', () => {
let toggler;
beforeEach(() => {
toggler = mount(
<AccordionItem title="A heading">
Lorem ipsum.
<input className="testInput" />
</AccordionItem>
);
});
it('should toggle state when using enter or space', () => {
expect(toggler.state().open).toEqual(false);
toggler.simulate('keypress', { which: 32 });
expect(toggler.state().open).toEqual(true);
toggler.simulate('keypress', { which: 13 });
expect(toggler.state().open).toEqual(false);
toggler.simulate('keypress', { which: 97 });
expect(toggler.state().open).toEqual(false);
});
it('should not toggle if a keypress is made in a child element', () => {
const input = toggler.find('.testInput');
expect(toggler.state().open).toEqual(false);
toggler.simulate('keypress', { which: 32 });
expect(toggler.state().open).toEqual(true);
input.simulate('keypress', { which: 32 });
expect(toggler.state().open).toEqual(true);
});
});
});