Skip to content

Commit 6f38fe8

Browse files
author
Todd Kloots
committed
[Added] Ability to exclude tests (resolves #57)
1 parent 74232d1 commit 6f38fe8

6 files changed

Lines changed: 51 additions & 27 deletions

File tree

karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = function (config) {
2525
devtool: 'inline-source-map',
2626
module: {
2727
loaders: [
28-
{ test: /\.js$/, loader: 'babel-loader' }
28+
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader?optional=runtime' }
2929
]
3030
},
3131
plugins: [

lib/__tests__/index-test.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ describe('tags', () => {
125125
});
126126

127127
it('dissallows the word "image" in the alt attribute', () => {
128-
expectWarning(assertions.tags.img.REDUDANT_ALT.msg, () => {
128+
expectWarning(assertions.tags.img.REDUNDANT_ALT.msg, () => {
129129
<img src="cat.gif" alt="image of a cat"/>;
130130
});
131131
});
@@ -524,4 +524,24 @@ describe('device is set to mobile', () => {
524524
});
525525
});
526526
});
527-
});
527+
});
528+
529+
describe('exclusions', () => {
530+
var createElement = React.createElement;
531+
532+
before(() => {
533+
a11y(React, { exclude: ['REDUNDANT_ALT'] });
534+
});
535+
536+
after(() => {
537+
React.createElement = createElement;
538+
});
539+
540+
describe('when REDUNDANT_ALT is excluded', () => {
541+
it('does not warn when the word "image" in the alt attribute', () => {
542+
doNotExpectWarning(assertions.tags.img.REDUNDANT_ALT.msg, () => {
543+
<img src="cat.gif" alt="image of a cat"/>;
544+
});
545+
});
546+
});
547+
});

lib/assertions.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ var INTERACTIVE = {
2121
}
2222
};
2323

24-
const DEVICE = { 'DESKTOP': 'desktop', 'MOBILE': 'mobile' };
25-
2624
var hasAlt = (props) => {
2725
return typeof props.alt === 'string';
2826
};
@@ -117,7 +115,6 @@ exports.props = {
117115
},
118116

119117
NO_TABINDEX: {
120-
device: DEVICE.DESKTOP,
121118
msg: 'You have a click handler on a non-interactive element but no `tabIndex` DOM property. The element will not be navigable or interactive by keyboard users. http://www.w3.org/TR/wai-aria-practices/#focus_tabindex',
122119
test (tagName, props, children) {
123120
return !(
@@ -128,15 +125,13 @@ exports.props = {
128125
},
129126

130127
BUTTON_ROLE_SPACE: {
131-
device: DEVICE.DESKTOP,
132128
msg: 'You have `role="button"` but did not define an `onKeyDown` handler. Add it, and have the "Space" key do the same thing as an `onClick` handler.',
133129
test (tagName, props, children) {
134130
return !(props.role === 'button' && !props.onKeyDown);
135131
}
136132
},
137133

138134
BUTTON_ROLE_ENTER: {
139-
device: DEVICE.DESKTOP,
140135
msg: 'You have `role="button"` but did not define an `onKeyDown` handler. Add it, and have the "Enter" key do the same thing as an `onClick` handler.',
141136
test (tagName, props, children) {
142137
return !(props.role === 'button' && !props.onKeyDown);
@@ -170,7 +165,7 @@ exports.tags = {
170165
}
171166
},
172167

173-
REDUDANT_ALT: {
168+
REDUNDANT_ALT: {
174169
// TODO: have some way to set localization strings to match against
175170
msg: 'Screen-readers already announce `img` tags as an image, you don\'t need to use the word "image" in the description',
176171
test (tagName, props, children) {

lib/index.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
var assertions = require('./assertions');
22
var after = require('./after');
33

4-
var deviceMatches = (test, deviceFilter) => {
5-
if (!test.device)
6-
return true;
4+
const mobileExclusions = [
5+
'NO_TABINDEX',
6+
'BUTTON_ROLE_SPACE',
7+
'BUTTON_ROLE_ENTER'
8+
];
9+
10+
var shouldRunTest = (testName, options) => {
11+
var exclude = options.exclude || [];
12+
13+
if (options.device == 'mobile') {
14+
exclude = new Set(exclude.concat(mobileExclusions));
15+
exclude = [...exclude];
16+
}
717

8-
return (deviceFilter.indexOf(test.device) != -1);
18+
return (exclude.indexOf(testName) == -1);
919
};
1020

11-
var runTagTests = (tagName, props, children, deviceFilter, onFailure) => {
21+
var runTagTests = (tagName, props, children, options, onFailure) => {
1222
var key;
1323
var tagTests = assertions.tags[tagName] || [];
1424

1525
for (key in tagTests) {
16-
let shouldRunTest = deviceMatches(tagTests[key], deviceFilter);
17-
let testFailed = shouldRunTest &&
26+
let testFailed = shouldRunTest(key, options) &&
1827
!tagTests[key].test(tagName, props, children);
1928

2029
if (tagTests[key] && testFailed)
2130
onFailure(tagName, props, tagTests[key].msg);
2231
}
2332
};
2433

25-
var runPropTests = (tagName, props, children, deviceFilter, onFailure) => {
34+
var runPropTests = (tagName, props, children, options, onFailure) => {
2635
var key;
2736
var propTests;
2837

@@ -32,8 +41,7 @@ var runPropTests = (tagName, props, children, deviceFilter, onFailure) => {
3241
propTests = assertions.props[propName] || [];
3342

3443
for (key in propTests) {
35-
let shouldRunTest = deviceMatches(propTests[key], deviceFilter);
36-
let testTailed = shouldRunTest &&
44+
let testTailed = shouldRunTest(key, options) &&
3745
!propTests[key].test(tagName, props, children);
3846

3947
if (propTests[key] && testTailed)
@@ -42,12 +50,12 @@ var runPropTests = (tagName, props, children, deviceFilter, onFailure) => {
4250
}
4351
};
4452

45-
var runLabelTests = (tagName, props, children, deviceFilter, onFailure) => {
53+
var runLabelTests = (tagName, props, children, options, onFailure) => {
4654
var key;
4755
var renderTests = assertions.render;
4856

4957
for (key in renderTests) {
50-
if (renderTests[key]) {
58+
if (shouldRunTest(key, options) && renderTests[key]) {
5159
let failureCB = onFailure.bind(
5260
undefined, tagName, props, renderTests[key].msg);
5361

@@ -56,10 +64,10 @@ var runLabelTests = (tagName, props, children, deviceFilter, onFailure) => {
5664
}
5765
};
5866

59-
var runTests = (tagName, props, children, deviceFilter, onFailure) => {
67+
var runTests = (tagName, props, children, options, onFailure) => {
6068
var tests = [runTagTests, runPropTests, runLabelTests];
6169
tests.map((test) => {
62-
test(tagName, props, children, deviceFilter, onFailure);
70+
test(tagName, props, children, options, onFailure);
6371
});
6472
};
6573

@@ -168,17 +176,17 @@ var reactA11y = (React, options) => {
168176
assertions.setReact(React);
169177

170178
_createElement = React.createElement;
171-
var deviceFilter = options && options.device || ['desktop'];
172179

173180
React.createElement = (type, _props, ...children) => {
174181
var props = _props || {};
182+
options = options || {};
175183

176184
props.id = createId(props);
177185
var reactEl = _createElement.apply(this, [type, props].concat(children));
178186
var failureCB = handleFailure.bind(undefined, options, reactEl);
179187

180188
if (typeof type === 'string')
181-
runTests(type, props, children, deviceFilter, failureCB);
189+
runTests(type, props, children, options, failureCB);
182190

183191
return reactEl;
184192
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"scripts": {
1313
"test": "jsxhint . && karma start --single-run",
1414
"watch-tests": "npm test -- --watch",
15-
"prepublish": "babel -d dist lib",
15+
"prepublish": "babel --optional runtime lib --out-dir dist",
1616
"release": "release"
1717
},
1818
"authors": [
@@ -25,6 +25,7 @@
2525
"babel": "^5.2.17",
2626
"babel-core": "^5.2.17",
2727
"babel-loader": "^5.0.0",
28+
"babel-runtime": "^5.1.11",
2829
"jsx-loader": "^0.12.2",
2930
"jsxhint": "^0.8.1",
3031
"karma": "^0.12.28",

webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module.exports = {
3535

3636
module: {
3737
loaders: [
38-
{ test: /\.js$/, loader: 'jsx-loader?harmony' }
38+
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader?optional=runtime' }
3939
]
4040
}
4141

0 commit comments

Comments
 (0)