Skip to content

Commit

Permalink
[added] includeSrcNode takes "asString" as option.
Browse files Browse the repository at this point in the history
"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 <[email protected]>
Signed-off-by: Geoff Pleiss <[email protected]>
  • Loading branch information
Kenny Wang authored and Dominick Reinhold and Kenny Wang committed Aug 17, 2015
1 parent 4402196 commit 0e6bb84
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
43 changes: 42 additions & 1 deletion lib/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ var k = () => {};
var captureWarnings = (fn) => {
var _warn = console.warn;
var msgs = {};
console.warn = (id, msg) => msgs[msg] = true;
console.warn = (id, msg, srcNode) => {
msgs[msg] = srcNode ? srcNode : true;
};
fn();
console.warn = _warn;
return msgs;
Expand Down Expand Up @@ -557,6 +559,45 @@ describe('labels', () => {
});
});

describe('includeSrcNode is "asString"', () => {
var createElement = React.createElement;
var fixture;

before(() => {
a11y(React, { includeSrcNode: "asString" });
fixture = document.createElement('div');
fixture.id = 'fixture-1';
document.body.appendChild(fixture);
});

after(() => {
React.createElement = createElement;
fixture = document.getElementById('fixture-1');
if (fixture)
document.body.removeChild(fixture);
});

it('returns the outerHTML as a string in the error message', () => {
var Bar = React.createClass({
_privateProp: 'bar',

componentDidMount: function() {
return this._privateProp;
},
render: () => {
return (
<div role="button"></div>
);
}
});

var msgs = captureWarnings(() => {React.render(<Bar/>, fixture);});
var regex = /^Source Node: <(\w+) .+>.*<\/\1>/;
var matches = msgs[assertions.render.NO_LABEL.msg].match(regex);
assert.equal(matches[1], "div");
});
});

describe('filterFn', () => {
var createElement = React.createElement;

Expand Down
7 changes: 4 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ var logWarning = (component, failureInfo, options) => {
// Guard against logging null element references should render()
// return null or false.
// https://facebook.github.io/react/docs/component-api.html#getdomnode
if (srcNode)
if (includeSrcNode === "asString")
warning.push("Source Node: " + srcNode.outerHTML);
else if (srcNode)
warning.push(srcNode);
}

console.warn.apply(console, warning);
};

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

var handleFailure = (options, reactEl, type, props, failureMsg) => {
var includeSrcNode = options && !!options.includeSrcNode;
var includeSrcNode = options && (options.includeSrcNode || false);
var warningPrefix = (options && options.warningPrefix) || '';
var reactComponent = reactEl._owner;

Expand Down

0 comments on commit 0e6bb84

Please sign in to comment.