From 0e6bb84d4628426d4dafcfa991dd2c49fb4df82a Mon Sep 17 00:00:00 2001 From: Kenny Wang Date: Tue, 28 Jul 2015 17:52:38 -0700 Subject: [PATCH] [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 Signed-off-by: Geoff Pleiss --- lib/__tests__/index-test.js | 43 ++++++++++++++++++++++++++++++++++++- lib/index.js | 7 +++--- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/lib/__tests__/index-test.js b/lib/__tests__/index-test.js index b0b7ed8..97194f5 100644 --- a/lib/__tests__/index-test.js +++ b/lib/__tests__/index-test.js @@ -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; @@ -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 ( +
+ ); + } + }); + + var msgs = captureWarnings(() => {React.render(, 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; diff --git a/lib/index.js b/lib/index.js index 30af43f..e4f682c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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); }; @@ -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;