Skip to content

Commit 0e6bb84

Browse files
Kenny WangDominick Reinhold and Kenny Wang
authored andcommitted
[added] includeSrcNode takes "asString" as option.
"asString" pushes the violating srcNode.outerHTML to the warning message so it gives a better description when using react-a11y in ci. Keeps existing functionality with `includeSrcNode: true` Signed-off-by: Matt Royal <mroyal@pivotal.io> Signed-off-by: Geoff Pleiss <gpleiss@pivotal.io>
1 parent 4402196 commit 0e6bb84

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

lib/__tests__/index-test.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ var k = () => {};
88
var captureWarnings = (fn) => {
99
var _warn = console.warn;
1010
var msgs = {};
11-
console.warn = (id, msg) => msgs[msg] = true;
11+
console.warn = (id, msg, srcNode) => {
12+
msgs[msg] = srcNode ? srcNode : true;
13+
};
1214
fn();
1315
console.warn = _warn;
1416
return msgs;
@@ -557,6 +559,45 @@ describe('labels', () => {
557559
});
558560
});
559561

562+
describe('includeSrcNode is "asString"', () => {
563+
var createElement = React.createElement;
564+
var fixture;
565+
566+
before(() => {
567+
a11y(React, { includeSrcNode: "asString" });
568+
fixture = document.createElement('div');
569+
fixture.id = 'fixture-1';
570+
document.body.appendChild(fixture);
571+
});
572+
573+
after(() => {
574+
React.createElement = createElement;
575+
fixture = document.getElementById('fixture-1');
576+
if (fixture)
577+
document.body.removeChild(fixture);
578+
});
579+
580+
it('returns the outerHTML as a string in the error message', () => {
581+
var Bar = React.createClass({
582+
_privateProp: 'bar',
583+
584+
componentDidMount: function() {
585+
return this._privateProp;
586+
},
587+
render: () => {
588+
return (
589+
<div role="button"></div>
590+
);
591+
}
592+
});
593+
594+
var msgs = captureWarnings(() => {React.render(<Bar/>, fixture);});
595+
var regex = /^Source Node: <(\w+) .+>.*<\/\1>/;
596+
var matches = msgs[assertions.render.NO_LABEL.msg].match(regex);
597+
assert.equal(matches[1], "div");
598+
});
599+
});
600+
560601
describe('filterFn', () => {
561602
var createElement = React.createElement;
562603

lib/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,11 @@ var logWarning = (component, failureInfo, options) => {
110110
// Guard against logging null element references should render()
111111
// return null or false.
112112
// https://facebook.github.io/react/docs/component-api.html#getdomnode
113-
if (srcNode)
113+
if (includeSrcNode === "asString")
114+
warning.push("Source Node: " + srcNode.outerHTML);
115+
else if (srcNode)
114116
warning.push(srcNode);
115117
}
116-
117118
console.warn.apply(console, warning);
118119
};
119120

@@ -127,7 +128,7 @@ var logWarning = (component, failureInfo, options) => {
127128
};
128129

129130
var handleFailure = (options, reactEl, type, props, failureMsg) => {
130-
var includeSrcNode = options && !!options.includeSrcNode;
131+
var includeSrcNode = options && (options.includeSrcNode || false);
131132
var warningPrefix = (options && options.warningPrefix) || '';
132133
var reactComponent = reactEl._owner;
133134

0 commit comments

Comments
 (0)