Skip to content

Commit 0b5b261

Browse files
committed
Added JSXElement and JSXAttribute mocks; isNon* rules
1 parent 0794f64 commit 0b5b261

12 files changed

+535
-92
lines changed

__mocks__/JSXElementMock.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default function JSXElementMock (tagName, attributes) {
2+
return {
3+
type: 'JSXElement',
4+
openingElement: {
5+
type: 'JSXOpeningElement',
6+
name: {
7+
type: 'JSXIdentifier',
8+
name: tagName
9+
},
10+
attributes,
11+
},
12+
};
13+
}

__mocks__/genInteractives.js

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
import JSXAttributeMock from 'JSXAttributeMock';
2+
import JSXElementMock from 'JSXElementMock';
3+
4+
const interactiveElementsMap = {
5+
a: [
6+
{prop: 'href', value: '#'}
7+
],
8+
area: [
9+
{prop: 'href', value: '#'}
10+
],
11+
button: [],
12+
input: [
13+
{prop: 'type', value: 'text'}
14+
],
15+
option: [],
16+
select: [],
17+
textarea: [],
18+
};
19+
20+
const nonInteractiveElementsMap = {
21+
html: [],
22+
base: [],
23+
head: [],
24+
link: [],
25+
meta: [],
26+
style: [],
27+
title: [],
28+
address: [],
29+
article: [],
30+
aside: [],
31+
footer: [],
32+
header: [],
33+
h1: [],
34+
h2: [],
35+
h3: [],
36+
h4: [],
37+
h5: [],
38+
h6: [],
39+
hgroup: [],
40+
nav: [],
41+
section: [],
42+
dd: [],
43+
dl: [],
44+
dt: [],
45+
figcaption: [],
46+
figure: [],
47+
hr: [],
48+
li: [],
49+
main: [],
50+
ol: [],
51+
p: [],
52+
pre: [],
53+
ul: [],
54+
a: [],
55+
abbr: [],
56+
b: [],
57+
bdi: [],
58+
bdo: [],
59+
br: [],
60+
cite: [],
61+
code: [],
62+
data: [],
63+
dfn: [],
64+
em: [],
65+
i: [],
66+
kbd: [],
67+
mark: [],
68+
q: [],
69+
rp: [],
70+
rt: [],
71+
rtc: [],
72+
ruby: [],
73+
s: [],
74+
samp: [],
75+
small: [],
76+
strong: [],
77+
sub: [],
78+
sup: [],
79+
time: [],
80+
u: [],
81+
var: [],
82+
wbr: [],
83+
area: [],
84+
audio: [],
85+
img: [],
86+
map: [],
87+
track: [],
88+
video: [],
89+
embed: [],
90+
object: [],
91+
param: [],
92+
source: [],
93+
canvas: [],
94+
noscript: [],
95+
script: [],
96+
del: [],
97+
ins: [],
98+
caption: [],
99+
col: [],
100+
colgroup: [],
101+
table: [],
102+
tbody: [],
103+
td: [],
104+
tfoot: [],
105+
th: [],
106+
thead: [],
107+
tr: [],
108+
datalist: [],
109+
fieldset: [],
110+
form: [],
111+
input: [
112+
{prop: 'type', value: 'hidden'}
113+
],
114+
label: [],
115+
legend: [],
116+
meter: [],
117+
optgroup: [],
118+
output: [],
119+
progress: [],
120+
details: [],
121+
dialog: [],
122+
menu: [],
123+
menuitem: [],
124+
summary: [],
125+
content: [],
126+
acronym: [],
127+
applet: [],
128+
big: [],
129+
blink: [],
130+
center: [],
131+
dir: [],
132+
font: [],
133+
frame: [],
134+
frameset: [],
135+
keygen: [],
136+
marquee: [],
137+
noembed: [],
138+
spacer: [],
139+
strike: [],
140+
tt: [],
141+
xmp: [],
142+
};
143+
144+
const interactiveRoles = [
145+
'button',
146+
'checkbox',
147+
'link',
148+
'menuitem',
149+
'menuitemcheckbox',
150+
'menuitemradio',
151+
'option',
152+
'radio',
153+
'spinbutton',
154+
'tab',
155+
'textbox',
156+
];
157+
158+
const nonInteractiveRoles = [
159+
'alert',
160+
'alertdialog',
161+
'dialog',
162+
'gridcell',
163+
'log',
164+
'marquee',
165+
'progressbar',
166+
'scrollbar',
167+
'slider',
168+
'status',
169+
'tabpanel',
170+
'timer',
171+
'tooltip',
172+
'treeitem',
173+
'combobox',
174+
'grid',
175+
'listbox',
176+
'menu',
177+
'menubar',
178+
'radiogroup',
179+
'tablist',
180+
'tree',
181+
'treegrid',
182+
'article',
183+
'columnheader',
184+
'definition',
185+
'directory',
186+
'document',
187+
'group',
188+
'heading',
189+
'img',
190+
'list',
191+
'listitem',
192+
'math',
193+
'note',
194+
'presentation',
195+
'region',
196+
'row',
197+
'rowgroup',
198+
'rowheader',
199+
'separator',
200+
'toolbar',
201+
'application',
202+
'banner',
203+
'complementary',
204+
'contentinfo',
205+
'form',
206+
'main',
207+
'navigation',
208+
'search',
209+
];
210+
211+
export function genInteractiveElements () {
212+
const elements = [];
213+
for (const name in interactiveElementsMap) {
214+
const attributes = interactiveElementsMap[name].map(
215+
({prop, value}) => JSXAttributeMock(prop, value)
216+
);
217+
elements.push(JSXElementMock(name, attributes));
218+
}
219+
return elements;
220+
}
221+
222+
export function genInteractiveRoleElements () {
223+
return interactiveRoles.map(
224+
value => JSXElementMock('div', [
225+
JSXAttributeMock('role', value)
226+
])
227+
);
228+
}
229+
230+
export function genNonInteractiveElements () {
231+
const elements = [];
232+
for (const name in nonInteractiveElementsMap) {
233+
const attributes = nonInteractiveElementsMap[name].map(
234+
({prop, value}) => JSXAttributeMock(prop, value)
235+
);
236+
elements.push(JSXElementMock(name, attributes));
237+
}
238+
return elements;
239+
}
240+
241+
export function genNonInteractiveRoleElements () {
242+
return nonInteractiveRoles.map(
243+
value => JSXElementMock('div', [
244+
JSXAttributeMock('role', value)
245+
])
246+
);
247+
}

__tests__/__mocks__/attrMock.js

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* eslint-env mocha */
2+
import assert from 'assert';
3+
import { elementType } from 'jsx-ast-utils';
4+
import isInteractiveElement from '../../../src/util/isInteractiveElement';
5+
import {
6+
genInteractiveElements,
7+
genNonInteractiveElements,
8+
} from '../../../__mocks__/genInteractives';
9+
10+
describe('isInteractiveElement', () => {
11+
describe('JSX Components (no tagName)', () => {
12+
it('should identify them as interactive elements', () => {
13+
expect(isInteractiveElement(undefined, []))
14+
.toBe(true);
15+
});
16+
});
17+
describe('non-interactive elements', () => {
18+
it('should not identify them as interactive elements', () => {
19+
genNonInteractiveElements().forEach(
20+
({
21+
type,
22+
openingElement,
23+
}) => expect(isInteractiveElement(
24+
elementType(openingElement),
25+
openingElement.attributes,
26+
)).toBe(false)
27+
);
28+
});
29+
});
30+
describe('interactive elements', () => {
31+
it('should identify them as interactive elements', () => {
32+
genInteractiveElements().forEach(
33+
({
34+
type,
35+
openingElement,
36+
}) => expect(isInteractiveElement(
37+
elementType(openingElement),
38+
openingElement.attributes,
39+
)).toBe(true)
40+
);
41+
});
42+
});
43+
});
Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,48 @@
11
/* eslint-env mocha */
22
import assert from 'assert';
3-
import isInteractiveRole, {
4-
interactiveRoles,
5-
nonInteractiveRoles,
6-
} from '../../../src/util/isInteractiveRole';
7-
import attrMock from '../../__mocks__/attrMock';
3+
import { elementType } from 'jsx-ast-utils';
4+
import isInteractiveRole from '../../../src/util/isInteractiveRole';
5+
import {
6+
genInteractiveRoleElements,
7+
genNonInteractiveRoleElements,
8+
} from '../../../__mocks__/genInteractives';
89

910
describe('isInteractiveRole', () => {
1011
describe('JSX Components (no tagName)', () => {
1112
it('should identify them as interactive elements', () => {
12-
expect(isInteractiveRole(undefined, [])).toBe(true);
13+
expect(isInteractiveRole(undefined, []))
14+
.toBe(true);
1315
});
1416
});
15-
describe('elements with an interactive role', () => {
16-
it('should identify them as interactive elements', () => {
17-
interactiveRoles.forEach(role => expect(isInteractiveRole('div', [
18-
attrMock('role', role)
19-
])).toBe(true));
17+
describe('elements with a non-interactive role', () => {
18+
it('should not identify them as interactive elements', () => {
19+
genNonInteractiveRoleElements().forEach(
20+
({
21+
type,
22+
openingElement,
23+
}) => expect(isInteractiveRole(
24+
elementType(openingElement),
25+
openingElement.attributes,
26+
)).toBe(false)
27+
);
2028
});
2129
});
22-
describe('elements with a non-interactive role', () => {
30+
describe('elements without a role', () => {
2331
it('should not identify them as interactive elements', () => {
24-
nonInteractiveRoles.forEach(role => expect(isInteractiveRole('div', [
25-
attrMock('role', role)
26-
])).toBe(false));
32+
expect(isInteractiveRole('div', [])).toBe(false);
33+
});
34+
});
35+
describe('elements with an interactive role', () => {
36+
it('should identify them as interactive elements', () => {
37+
genInteractiveRoleElements().forEach(
38+
({
39+
type,
40+
openingElement,
41+
}) => expect(isInteractiveRole(
42+
elementType(openingElement),
43+
openingElement.attributes,
44+
)).toBe(true)
45+
);
2746
});
2847
});
2948
});

0 commit comments

Comments
 (0)