Skip to content

Commit 8c6a7ce

Browse files
author
Todd Kloots
committed
[fixed] bug where the label assertion can return a false failure #48
1 parent bd7cc9a commit 8c6a7ce

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

lib/__tests__/index-test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ describe('labels', () => {
333333
});
334334
});
335335

336-
it('does not warn when a child is a component with text and and an image without alt', (done) => {
336+
it('does not warn when there is an image without alt text with a sibling text node', (done) => {
337337
var Foo = React.createClass({
338338
render: function() {
339339
return (
@@ -349,7 +349,7 @@ describe('labels', () => {
349349
});
350350
});
351351

352-
it('warns when a child is a component without text content', () => {
352+
it('warns when a child is a component without text content', (done) => {
353353
var Bar = React.createClass({
354354
render: () => {
355355
return (
@@ -359,7 +359,7 @@ describe('labels', () => {
359359
});
360360

361361
expectWarning(assertions.render.NO_LABEL.msg, () => {
362-
React.render(<div role="button"><Bar/></div>, fixture);
362+
React.render(<div role="button"><Bar/></div>, fixture, done);
363363
});
364364
});
365365

@@ -387,7 +387,7 @@ describe('labels', () => {
387387
});
388388
});
389389

390-
it('warns if no child components have label text', () => {
390+
it('warns if no child components have label text', (done) => {
391391
var Bar = React.createClass({
392392
render: () => {
393393
return (
@@ -405,12 +405,12 @@ describe('labels', () => {
405405
});
406406

407407
expectWarning(assertions.render.NO_LABEL.msg, () => {
408-
React.render(<div role="button"><Bar/><Foo/></div>, fixture);
408+
React.render(<div role="button"><Bar/><Foo/></div>, fixture, done);
409409
});
410410
});
411411

412412

413-
it('does not error when the component has a componentDidMount callback', () => {
413+
it('does not error when the component has a componentDidMount callback', (done) => {
414414
var Bar = React.createClass({
415415
_privateProp: 'bar',
416416

@@ -425,7 +425,7 @@ describe('labels', () => {
425425
});
426426

427427
expectWarning(assertions.render.NO_LABEL.msg, () => {
428-
React.render(<div role="button"><Bar/></div>, fixture);
428+
React.render(<div role="button"><Bar/></div>, fixture, done);
429429
});
430430
});
431431

lib/assertions.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ var hasChildTextNode = (props, children, failureCB) => {
9393
after(child.type.prototype, 'componentDidMount', function() {
9494
assertLabel(React.findDOMNode(this), context, failureCB);
9595
});
96+
97+
// Return true because the label check is now going to be async
98+
// (due to the componentDidMount listener) and we want to avoid
99+
// pre-maturely calling the failure callback.
100+
hasText = true;
96101
}
97102
});
98103
return hasText;
@@ -180,10 +185,12 @@ exports.render = {
180185

181186
var failed = !(
182187
(isInteractive(tagName, props) || props.role) &&
183-
(props['aria-label'] ||
184-
props['aria-labelled-by'] ||
185-
(tagName === 'img' && props.alt) ||
186-
hasChildTextNode(props, children, failureCB))
188+
(
189+
props['aria-label'] ||
190+
props['aria-labelled-by'] ||
191+
(tagName === 'img' && props.alt) ||
192+
hasChildTextNode(props, children, failureCB)
193+
)
187194
);
188195

189196
if (failed)

0 commit comments

Comments
 (0)