-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
attr.test.js
165 lines (137 loc) · 5.6 KB
/
attr.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
const magicToStringObject = {
toString () {
return 'magic'
}
}
class Fixture extends React.Component {
render () {
/* eslint-disable react/jsx-boolean-value */
return (
<div id='root'>
<span id='child'>test</span>
<video itemScope allowFullScreen={true} autoPlay={''} hidden={false} controls={null} loop={undefined}>test2</video>
<audio disabled name={''} contentEditable={false} spellCheck={null} rel={magicToStringObject}>test3</audio>
<table>
<tbody>
<tr rowSpan={0} rows={0} cols={'4'} size={'0'} />
</tbody>
</table>
</div>
)
/* eslint-enable react/jsx-boolean-value */
}
}
const it = createTest(<Fixture />)
describe('#attr', () => {
it('fails when the actual is not an enzyme wrapper', () => {
[undefined, { foo: 'bar' }, [], 'test', 12345].forEach((actual) => {
expect(() => {
expect(actual).to.have.attr('key', 'somekey')
}).to.throw()
})
})
describe('(attr)', () => {
it('passes when the actual matches the expected', (wrapper) => {
expect(wrapper.find('span')).to.have.attr('id')
expect(wrapper).to.have.attr('id')
})
it('passes negated when the actual does not match the expected', (wrapper) => {
expect(wrapper).to.not.have.attr('disabled')
expect(wrapper.find('span')).to.not.have.attr('disabled')
})
describe('HAS_NUMERIC_VALUE attrs', () => {
it('passes when attribute exists without a `0` vaue', (wrapper) => {
expect(wrapper.find('tr')).to.have.attr('rowspan')
})
})
describe('HAS_POSITIVE_NUMERIC_VALUE attrs', () => {
it('passes when attribute exists with a string value', (wrapper) => {
expect(wrapper.find('tr')).to.have.attr('cols')
})
it('passes negated when attribute exists but has value `0`', (wrapper) => {
expect(wrapper.find('tr')).to.not.have.attr('rows')
})
it('passes negated when attribute exists but has value `"0"`', (wrapper) => {
expect(wrapper.find('tr')).to.not.have.attr('size')
})
})
describe('HAS_BOOLEAN_VALUE attrs', () => {
it('passes when attribute exists without a value', (wrapper) => {
expect(wrapper.find('video')).to.have.attr('itemscope')
})
it('passes negated when attribute exists with a falsey (but not false/null/undefined) value', (wrapper) => {
expect(wrapper.find('video')).to.not.have.attr('autoplay')
expect(wrapper.find('video')).to.not.have.attr('autoPlay')
})
it('passes negated when attribute exists but has value `false`', (wrapper) => {
expect(wrapper.find('video')).to.not.have.attr('hidden')
})
it('passes negated when attribute exists but has value `null`', (wrapper) => {
expect(wrapper.find('video')).to.not.have.attr('controls')
})
it('passes negated when attribute exists but has value `undefined`', (wrapper) => {
expect(wrapper.find('video')).to.not.have.attr('loop')
})
})
describe('regular attrs', () => {
it('passes when attribute exists without a value', (wrapper) => {
expect(wrapper.find('audio')).to.have.attr('disabled')
})
it('passes when attribute exists with a falsey (but not false/null/undefined) value', (wrapper) => {
expect(wrapper.find('audio')).to.have.attr('name')
})
it('passes when attribute exists but has value `false`', (wrapper) => {
expect(wrapper.find('audio')).to.have.attr('contenteditable')
})
it('passes negated when attribute exists but has value `null`', (wrapper) => {
expect(wrapper.find('audio')).to.not.have.attr('spellcheck')
expect(wrapper.find('audio')).to.not.have.attr('spellCheck')
})
})
it('fails when the actual does not match the expected', (wrapper) => {
expect(() => {
expect(wrapper).to.have.attr('disabled')
}).to.throw("to have a 'disabled' attr")
expect(() => {
expect(wrapper.find('span')).to.have.attr('disabled')
}).to.throw("to have a 'disabled' attr")
})
})
describe('(attr, value)', () => {
it('passes when the actual matches the expected', (wrapper) => {
expect(wrapper).to.have.attr('id', 'root')
expect(wrapper.find('span')).to.have.attr('id', 'child')
})
it('passes negated when the actual does not match the expected', (wrapper) => {
expect(wrapper).to.not.have.attr('id', 'invalid')
expect(wrapper.find('span')).to.not.have.attr('id', 'invalid')
})
it('fails when the actual does not match the expected', (wrapper) => {
expect(() => {
expect(wrapper).to.have.attr('id', 'invalid')
}).to.throw("to have a 'id' attr")
expect(() => {
expect(wrapper.find('span')).to.have.attr('id', 'invalid')
}).to.throw("to have a 'id' attr")
expect(() => {
expect(wrapper).to.have.attr('id', undefined)
}).to.throw("to have a 'id' attr")
})
describe('HAS_BOOLEAN_VALUE attrs', () => {
it('converts values to empty strings', (wrapper) => {
expect(wrapper.find('video')).to.have.attr('itemscope', '')
expect(wrapper.find('video')).to.have.attr('allowfullscreen', '')
})
})
describe('regular attrs', () => {
it('converts values to strings', (wrapper) => {
expect(wrapper.find('audio')).to.have.attr('contenteditable', 'false')
expect(wrapper.find('audio')).to.have.attr('rel', 'magic')
})
})
})
it('chains', (wrapper) => {
expect(wrapper).to.have.attr('id').equal('root')
expect(wrapper.find('span')).to.have.attr('id').equal('child')
})
})