-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Button-test.js
301 lines (249 loc) · 10.1 KB
/
Button-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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import faker from 'faker'
import React from 'react'
import Button from 'src/elements/Button/Button'
import ButtonContent from 'src/elements/Button/ButtonContent'
import ButtonGroup from 'src/elements/Button/ButtonGroup'
import ButtonOr from 'src/elements/Button/ButtonOr'
import { SUI } from 'src/lib'
import * as common from 'test/specs/commonTests'
import { sandbox } from 'test/utils'
const syntheticEvent = { preventDefault: () => undefined }
describe('Button', () => {
common.isConformant(Button)
common.forwardsRef(Button, { tagName: 'button' })
common.forwardsRef(Button, { requiredProps: { label: faker.lorem.word() }, tagName: 'button' })
common.hasSubcomponents(Button, [ButtonContent, ButtonGroup, ButtonOr])
common.hasUIClassName(Button)
common.rendersChildren(Button)
common.implementsCreateMethod(Button)
common.implementsIconProp(Button, { autoGenerateKey: false })
common.implementsLabelProp(Button, {
autoGenerateKey: false,
shorthandDefaultProps: {
basic: true,
pointing: 'left',
},
})
common.propKeyAndValueToClassName(Button, 'floated', SUI.FLOATS)
common.propKeyOnlyToClassName(Button, 'active')
common.propKeyOnlyToClassName(Button, 'basic')
common.propKeyOnlyToClassName(Button, 'circular')
common.propKeyOnlyToClassName(Button, 'compact')
common.propKeyOnlyToClassName(Button, 'disabled')
common.propKeyOnlyToClassName(Button, 'fluid')
common.propKeyOnlyToClassName(Button, 'inverted')
common.propKeyOnlyToClassName(Button, 'loading')
common.propKeyOnlyToClassName(Button, 'primary')
common.propKeyOnlyToClassName(Button, 'negative')
common.propKeyOnlyToClassName(Button, 'positive')
common.propKeyOnlyToClassName(Button, 'secondary')
common.propKeyOrValueAndKeyToClassName(Button, 'animated', ['fade', 'vertical'])
common.propKeyOrValueAndKeyToClassName(Button, 'attached', ['left', 'right', 'top', 'bottom'])
common.propKeyOrValueAndKeyToClassName(Button, 'labelPosition', ['right', 'left'], {
className: 'labeled',
})
common.propValueOnlyToClassName(Button, 'color', [
...SUI.COLORS,
'facebook',
'twitter',
'google plus',
'vk',
'linkedin',
'instagram',
'youtube',
])
common.propValueOnlyToClassName(Button, 'size', SUI.SIZES)
it('renders a button by default', () => {
shallow(<Button />)
.first()
.should.have.tagName('button')
})
describe('attached', () => {
it('renders a div', () => {
shallow(<Button attached />).should.have.tagName('div')
})
})
describe('disabled', () => {
it('is not set by default', () => {
shallow(<Button />, { autoNesting: true }).should.not.have.prop('disabled')
})
it('applied when defined', () => {
shallow(<Button disabled />, { autoNesting: true }).should.have.prop('disabled', true)
})
it("don't apply when the element's type isn't button", () => {
shallow(<Button as='div' disabled />, { autoNesting: true }).should.not.have.prop('disabled')
})
it('is not set by default when has a label', () => {
shallow(<Button label='foo' />)
.find('button')
.should.not.have.prop('disabled')
})
it('applied when defined and has a label', () => {
shallow(<Button disabled label='foo' />)
.find('button')
.should.have.prop('disabled', true)
})
})
describe('toggle', () => {
it('is not set by default', () => {
shallow(<Button />, { autoNesting: true }).should.not.have.prop('toggle')
})
it('should have aria-pressed', () => {
shallow(<Button toggle />, { autoNesting: true }).should.have.prop('aria-pressed')
})
it('aria-pressed should be true when active', () => {
shallow(<Button toggle active />, { autoNesting: true }).should.have.prop(
'aria-pressed',
true,
)
})
it('aria-pressed should be false when inactive', () => {
shallow(<Button toggle />, { autoNesting: true }).should.have.prop('aria-pressed', false)
})
})
describe('icon', () => {
it('adds className icon', () => {
shallow(<Button icon='user' />, { autoNesting: true }).should.have.className('icon')
})
it('adds className icon when true', () => {
shallow(<Button icon />, { autoNesting: true }).should.have.className('icon')
})
it('does not add className icon when there is content', () => {
shallow(<Button icon='user' content={0} />, { autoNesting: true }).should.not.have.className(
'icon',
)
shallow(<Button icon='user' content='Yo' />, { autoNesting: true }).should.not.have.className(
'icon',
)
})
it('adds className icon given labelPosition and content', () => {
shallow(<Button labelPosition='left' icon='user' content='My Account' />, {
autoNesting: true,
}).should.have.className('icon')
shallow(<Button labelPosition='right' icon='user' content='My Account' />, {
autoNesting: true,
}).should.have.className('icon')
})
})
describe('label', () => {
it('renders as a div', () => {
shallow(<Button label='http' />).should.have.tagName('div')
})
it('renders a div with a button and Label child', () => {
const wrapper = shallow(<Button label='hi' />)
wrapper.should.have.tagName('div')
wrapper.should.have.exactly(1).descendants('button')
wrapper.should.have.exactly(1).descendants('Label')
})
it('adds the labeled className to the root element', () => {
shallow(<Button label='hi' />).should.have.className('labeled')
})
it('contains children without disabled class when disabled attribute is set', () => {
const wrapper = shallow(<Button label='hi' disabled />)
wrapper.should.have.className('disabled')
wrapper.find('Label').should.not.have.className('disabled')
wrapper.find('button').should.not.have.className('disabled')
})
it('contains children without floated class when floated attribute is set', () => {
const wrapper = shallow(<Button label='hi' floated='left' />)
wrapper.should.have.className('floated')
wrapper.find('Label').should.not.have.className('floated')
wrapper.find('button').should.not.have.className('floated')
})
it('creates a basic pointing label', () => {
shallow(<Button label='foo' />)
.should.have.exactly(1)
.descendants('Label[basic][pointing]')
})
it('is before the button and pointing="right" when labelPosition="left"', () => {
const wrapper = mount(<Button labelPosition='left' label='foo' />)
wrapper.should.have.exactly(1).descendants('Label[pointing="right"]')
wrapper.childAt(0).childAt(0).should.have.className('label')
wrapper.childAt(0).childAt(1).should.have.tagName('button')
})
it('is after the button and pointing="left" when labelPosition="right"', () => {
const wrapper = mount(<Button labelPosition='right' label='foo' />)
wrapper.should.have.exactly(1).descendants('Label[pointing="left"]')
wrapper.childAt(0).childAt(0).should.have.tagName('button')
wrapper.childAt(0).childAt(1).should.have.className('label')
})
it('is after the button and pointing="left" by default', () => {
const wrapper = mount(<Button label='foo' />)
wrapper.should.have.exactly(1).descendants('Label[pointing="left"]')
wrapper.childAt(0).childAt(0).should.have.tagName('button')
wrapper.childAt(0).childAt(1).should.have.className('label')
})
})
describe('labelPosition', () => {
it('renders as a button when given an icon', () => {
shallow(<Button labelPosition='left' icon='user' />).should.have.tagName('button')
shallow(<Button labelPosition='right' icon='user' />).should.have.tagName('button')
})
})
describe('onClick', () => {
it('is called with (e, data) when clicked', () => {
const onClick = sandbox.spy()
const wrapper = shallow(<Button onClick={onClick} />, { autoNesting: true })
wrapper.simulate('click', syntheticEvent)
onClick.should.have.been.calledOnce()
onClick.should.have.been.calledWithExactly(syntheticEvent, {
onClick,
...Button.defaultProps,
})
})
it('is not called when is disabled', () => {
const onClick = sandbox.spy()
shallow(<Button disabled onClick={onClick} />).simulate('click', syntheticEvent)
onClick.should.have.callCount(0)
})
})
describe('role', () => {
it('is not set by default', () => {
shallow(<Button />, { autoNesting: true }).should.not.have.prop('role')
})
it('defaults to "button" when rendered as not "button" element', () => {
shallow(<Button as='label' />, { autoNesting: true }).should.have.prop('role', 'button')
})
it('is configurable', () => {
shallow(<Button role='link' />, { autoNesting: true }).should.have.prop('role', 'link')
shallow(<Button role='button' />, { autoNesting: true }).should.have.prop('role', 'button')
})
})
describe('type', () => {
it('is not set by default', () => {
mount(<Button />)
.find('button')
.should.not.have.prop('type')
})
it('is passed to <button />', () => {
mount(<Button type='submit' />)
.find('button')
.should.have.prop('type', 'submit')
})
it('is passed to <button /> when "label" is defined', () => {
mount(<Button label='Foo' type='submit' />)
.find('button')
.should.have.prop('type', 'submit')
})
})
describe('tabIndex', () => {
it('is not set by default', () => {
shallow(<Button />, { autoNesting: true }).should.not.have.prop('tabIndex')
})
it('defaults to 0 as div', () => {
shallow(<Button as='div' />, { autoNesting: true }).should.have.prop('tabIndex', 0)
})
it('defaults to -1 when disabled', () => {
shallow(<Button disabled />, { autoNesting: true }).should.have.prop('tabIndex', -1)
})
it('can be set explicitly', () => {
shallow(<Button tabIndex={123} />, { autoNesting: true }).should.have.prop('tabIndex', 123)
})
it('can be set explicitly when disabled', () => {
shallow(<Button tabIndex={123} disabled />, { autoNesting: true }).should.have.prop(
'tabIndex',
123,
)
})
})
})