forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccordion.test.js
145 lines (124 loc) · 4.26 KB
/
accordion.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import React from 'react';
import {shallow} from 'enzyme';
import Accordion from '../../lib/views/accordion';
class CustomChild extends React.Component {
render() {
return <div className="custom-child" />;
}
}
class WrongChild extends React.Component {
render() {
return <div className="wrong-child" />;
}
}
describe('Accordion', function() {
function buildApp(overrideProps = {}) {
return (
<Accordion
leftTitle=""
results={[]}
total={0}
isLoading={false}
children={() => null}
{...overrideProps}
/>
);
}
it('renders a left title', function() {
const wrapper = shallow(buildApp({leftTitle: 'left'}));
assert.strictEqual(
wrapper.find('summary.github-Accordion-header span.github-Accordion--leftTitle').text(),
'left',
);
});
it('renders a right title', function() {
const wrapper = shallow(buildApp({rightTitle: 'right'}));
assert.strictEqual(
wrapper.find('summary.github-Accordion-header span.github-Accordion--rightTitle').text(),
'right',
);
});
it('is initially expanded', function() {
const wrapper = shallow(buildApp());
assert.isTrue(wrapper.find('details.github-Accordion[open]').exists());
});
it('toggles expansion state on a header click', function() {
const wrapper = shallow(buildApp());
const e = {preventDefault: sinon.stub()};
wrapper.find('.github-Accordion-header').simulate('click', e);
assert.isFalse(wrapper.find('details.github-Accordion[open="false"]').exists());
assert.isTrue(e.preventDefault.called);
});
describe('while loading', function() {
it('defaults to rendering no children', function() {
const wrapper = shallow(buildApp({isLoading: true, children: WrongChild}));
assert.lengthOf(wrapper.find('CustomChild'), 0);
});
it('renders a custom child component', function() {
const wrapper = shallow(buildApp({
isLoading: true,
loadingComponent: CustomChild,
emptyComponent: WrongChild,
}));
assert.lengthOf(wrapper.find('CustomChild'), 1);
assert.lengthOf(wrapper.find('WrongChild'), 0);
});
});
describe('when empty', function() {
it('defaults to rendering no children', function() {
const wrapper = shallow(buildApp({results: [], children: WrongChild}));
assert.lengthOf(wrapper.find('WrongChild'), 0);
});
it('renders a custom child component', function() {
const wrapper = shallow(buildApp({
results: [],
loadingComponent: WrongChild,
emptyComponent: CustomChild,
}));
assert.lengthOf(wrapper.find('CustomChild'), 1);
assert.lengthOf(wrapper.find('WrongChild'), 0);
});
});
describe('with results', function() {
it('renders its child render prop with each', function() {
const results = [1, 2, 3];
const wrapper = shallow(buildApp({
results,
loadingComponent: WrongChild,
emptyComponent: WrongChild,
children: each => <CustomChild item={each} />,
}));
assert.lengthOf(wrapper.find('WrongChild'), 0);
assert.lengthOf(wrapper.find('CustomChild'), 3);
for (const i of results) {
assert.isTrue(wrapper.find('CustomChild').someWhere(c => c.prop('item') === i));
}
});
it('passes an onClick handler to each item', function() {
const results = [1, 2, 3];
const handler = sinon.stub();
const wrapper = shallow(buildApp({
results,
loadingComponent: WrongChild,
emptyComponent: WrongChild,
onClickItem: handler,
children: each => <CustomChild item={each} />,
}));
wrapper.find('.github-Accordion-listItem').at(1).simulate('click');
assert.isTrue(handler.calledWith(2));
wrapper.find('.github-Accordion-listItem').at(2).simulate('click');
assert.isTrue(handler.calledWith(3));
});
it('renders a more tile when the results have been truncated', function() {
const results = [1, 2, 3];
const wrapper = shallow(buildApp({
results,
total: 3,
moreComponent: CustomChild,
}));
assert.isFalse(wrapper.find('CustomChild').exists());
wrapper.setProps({total: 4});
assert.isTrue(wrapper.find('CustomChild').exists());
});
});
});