label-has-for-test.js
10.4 KB
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
/* eslint-env jest */
/**
* @fileoverview Enforce label tags have htmlFor attribute.
* @author Ethan Cohen
*/
// -----------------------------------------------------------------------------
// Requirements
// -----------------------------------------------------------------------------
import { RuleTester } from 'eslint';
import assign from 'object.assign';
import parserOptionsMapper from '../../__util__/parserOptionsMapper';
import rule from '../../../src/rules/label-has-for';
// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------
const ruleTester = new RuleTester();
const expectedNestingError = {
message: 'Form label must have the following type of associated control: nesting',
type: 'JSXOpeningElement',
};
const expectedSomeError = {
message: 'Form label must have ANY of the following types of associated control: nesting, id',
type: 'JSXOpeningElement',
};
const expectedEveryError = {
message: 'Form label must have ALL of the following types of associated control: nesting, id',
type: 'JSXOpeningElement',
};
const optionsComponents = [{
components: ['Label', 'Descriptor'],
}];
const optionsRequiredNesting = [{
required: 'nesting',
}];
const optionsRequiredSome = [{
required: { some: ['nesting', 'id'] },
}];
const optionsRequiredEvery = [{
required: { every: ['nesting', 'id'] },
}];
const optionsChildrenAllowed = [{
allowChildren: true,
}];
ruleTester.run('label-has-for', rule, {
valid: [
// DEFAULT ELEMENT 'label' TESTS
{ code: '<div />' },
{ code: '<label htmlFor="foo"><input /></label>' },
{ code: '<label htmlFor="foo"><textarea /></label>' },
{ code: '<Label />' }, // lower-case convention refers to real HTML elements.
{ code: '<Label htmlFor="foo" />' },
{ code: '<Descriptor />' },
{ code: '<Descriptor htmlFor="foo">Test!</Descriptor>' },
{ code: '<UX.Layout>test</UX.Layout>' },
// CUSTOM ELEMENT ARRAY OPTION TESTS
{ code: '<Label htmlFor="foo" />', options: [assign({}, optionsComponents[0], optionsRequiredSome[0])] },
{ code: '<Label htmlFor={"foo"} />', options: [assign({}, optionsComponents[0], optionsRequiredSome[0])] },
{ code: '<Label htmlFor={foo} />', options: [assign({}, optionsComponents[0], optionsRequiredSome[0])] },
{ code: '<Label htmlFor={`${id}`} />', options: [assign({}, optionsComponents[0], optionsRequiredSome[0])] },
{ code: '<div />', options: optionsComponents },
{ code: '<Label htmlFor="something"><input /></Label>', options: optionsComponents },
{ code: '<Label htmlFor="foo">Test!</Label>', options: [assign({}, optionsComponents[0], optionsRequiredSome[0])] },
{ code: '<Descriptor htmlFor="foo" />', options: [assign({}, optionsComponents[0], optionsRequiredSome[0])] },
{ code: '<Descriptor htmlFor={"foo"} />', options: [assign({}, optionsComponents[0], optionsRequiredSome[0])] },
{ code: '<Descriptor htmlFor={foo} />', options: [assign({}, optionsComponents[0], optionsRequiredSome[0])] },
{ code: '<Descriptor htmlFor={`${id}`} />', options: [assign({}, optionsComponents[0], optionsRequiredSome[0])] },
{ code: '<Descriptor htmlFor="foo">Test!</Descriptor>', options: [assign({}, optionsComponents[0], optionsRequiredSome[0])] },
{ code: '<label htmlFor="foo" />', options: optionsRequiredSome },
{ code: '<label htmlFor={"foo"} />', options: optionsRequiredSome },
{ code: '<label htmlFor={foo} />', options: optionsRequiredSome },
{ code: '<label htmlFor={`${id}`} />', options: optionsRequiredSome },
{ code: '<label htmlFor="foo">Test!</label>', options: optionsRequiredSome },
{ code: '<label><input /></label>', options: optionsRequiredSome },
{ code: '<label><input /></label>', options: optionsRequiredNesting },
{ code: '<label htmlFor="input"><input /></label>', options: optionsRequiredEvery },
{ code: '<label><input /></label>', options: optionsChildrenAllowed },
{ code: '<Descriptor htmlFor="foo">Test!</Descriptor>', options: [assign({}, optionsComponents, optionsChildrenAllowed)] },
{ code: '<label>Test!</label>', options: optionsChildrenAllowed },
{ code: '<label htmlFor="foo">Test!</label>', options: optionsChildrenAllowed },
{ code: '<label>{children}</label>', options: optionsChildrenAllowed },
{ code: '<label htmlFor="children">{children}</label>', options: optionsChildrenAllowed },
{ code: '<label htmlFor={id}>{ labelText }<div><input id={id} type="checkbox" name={id} value={value} /></div></label>', options: optionsRequiredEvery },
{ code: '<label htmlFor={id}>{ labelText }<div><div><div><div><input id={id} type="checkbox" name={id} value={value} /></div></div></div></div></label>', options: optionsRequiredEvery },
].map(parserOptionsMapper),
invalid: [
// DEFAULT ELEMENT 'label' TESTS
{ code: '<label id="foo" />', errors: [expectedEveryError], options: optionsRequiredEvery },
{ code: '<label htmlFor={undefined} />', errors: [expectedEveryError], options: optionsRequiredEvery },
{ code: '<label htmlFor={`${undefined}`} />', errors: [expectedEveryError], options: optionsRequiredEvery },
{ code: '<label>First Name</label>', errors: [expectedEveryError], options: optionsRequiredEvery },
{ code: '<label {...props}>Foo</label>', errors: [expectedEveryError], options: optionsRequiredEvery },
{ code: '<label><input /></label>', errors: [expectedEveryError], options: optionsRequiredEvery },
{ code: '<label><textarea /></label>', errors: [expectedEveryError], options: optionsRequiredEvery },
{ code: '<label>{children}</label>', errors: [expectedEveryError], options: optionsRequiredEvery },
{ code: '<label htmlFor="foo" />', errors: [expectedEveryError], options: optionsRequiredEvery },
{ code: '<label htmlFor={"foo"} />', errors: [expectedEveryError], options: optionsRequiredEvery },
{ code: '<label htmlFor={foo} />', errors: [expectedEveryError], options: optionsRequiredEvery },
{ code: '<label htmlFor={`${id}`} />', errors: [expectedEveryError], options: optionsRequiredEvery },
{ code: '<label htmlFor="foo">Test!</label>', errors: [expectedEveryError], options: optionsRequiredEvery },
{ code: '<label htmlFor={id}>{ labelText }<div><div><div><div><div id={id} type="checkbox" name={id} value={value} /></div></div></div></div></label>', errors: [expectedEveryError], options: optionsRequiredEvery },
//
// // CUSTOM ELEMENT ARRAY OPTION TESTS
{
code: '<Label></Label>',
errors: [expectedEveryError],
options: optionsComponents,
},
{
code: '<Label htmlFor="foo" />',
errors: [expectedEveryError],
options: [{ ...optionsComponents[0], ...optionsRequiredEvery[0] }],
},
{
code: '<Label htmlFor={"foo"} />',
errors: [expectedEveryError],
options: [{ ...optionsComponents[0], ...optionsRequiredEvery[0] }],
},
{
code: '<Label htmlFor={foo} />',
errors: [expectedEveryError],
options: [{ ...optionsComponents[0], ...optionsRequiredEvery[0] }],
},
{
code: '<Label htmlFor={`${id}`} />',
errors: [expectedEveryError],
options: [{ ...optionsComponents[0], ...optionsRequiredEvery[0] }],
},
{
code: '<Label htmlFor="foo">Test!</Label>',
errors: [expectedEveryError],
options: [{ ...optionsComponents[0], ...optionsRequiredEvery[0] }],
},
{
code: '<Descriptor htmlFor="foo" />',
errors: [expectedEveryError],
options: [{ ...optionsComponents[0], ...optionsRequiredEvery[0] }],
},
{
code: '<Descriptor htmlFor={"foo"} />',
errors: [expectedEveryError],
options: [{ ...optionsComponents[0], ...optionsRequiredEvery[0] }],
},
{
code: '<Descriptor htmlFor={foo} />',
errors: [expectedEveryError],
options: [{ ...optionsComponents[0], ...optionsRequiredEvery[0] }],
},
{
code: '<Descriptor htmlFor={`${id}`} />',
errors: [expectedEveryError],
options: [{ ...optionsComponents[0], ...optionsRequiredEvery[0] }],
},
{
code: '<Descriptor htmlFor="foo">Test!</Descriptor>',
errors: [expectedEveryError],
options: [{ ...optionsComponents[0], ...optionsRequiredEvery[0] }],
},
{ code: '<Label id="foo" />', errors: [expectedEveryError], options: optionsComponents },
{
code: '<Label htmlFor={undefined} />',
errors: [expectedEveryError],
options: optionsComponents,
},
{
code: '<Label htmlFor={`${undefined}`} />',
errors: [expectedEveryError],
options: optionsComponents,
},
{ code: '<Label>First Name</Label>', errors: [expectedEveryError], options: optionsComponents },
{
code: '<Label {...props}>Foo</Label>',
errors: [expectedEveryError],
options: optionsComponents,
},
{ code: '<Descriptor id="foo" />', errors: [expectedEveryError], options: optionsComponents },
{
code: '<Descriptor htmlFor={undefined} />',
errors: [expectedEveryError],
options: optionsComponents,
},
{
code: '<Descriptor htmlFor={`${undefined}`} />',
errors: [expectedEveryError],
options: optionsComponents,
},
{
code: '<Descriptor>First Name</Descriptor>',
errors: [expectedEveryError],
options: optionsComponents,
},
{
code: '<Descriptor {...props}>Foo</Descriptor>',
errors: [expectedEveryError],
options: optionsComponents,
},
{ code: '<label>{children}</label>', errors: [expectedEveryError], options: optionsComponents },
{ code: '<label htmlFor="foo" />', errors: [expectedNestingError], options: optionsRequiredNesting },
{ code: '<label>First Name</label>', errors: [expectedNestingError], options: optionsRequiredNesting },
{ code: '<label>First Name</label>', errors: [expectedSomeError], options: optionsRequiredSome },
{ code: '<label>{children}</label>', errors: [expectedSomeError], options: optionsRequiredSome },
{ code: '<label>{children}</label>', errors: [expectedNestingError], options: optionsRequiredNesting },
{
code: '<form><input type="text" id="howmuch" value="1" /><label htmlFor="howmuch">How much ?</label></form>',
errors: [expectedEveryError],
options: optionsRequiredEvery,
},
{
code: '<form><input type="text" id="howmuch" value="1" /><label htmlFor="howmuch">How much ?<span /></label></form>',
errors: [expectedEveryError],
options: optionsRequiredEvery,
},
].map(parserOptionsMapper),
});