Skip to content

Commit fe551de

Browse files
authored
Enable bundle tests for React.Fragment (#11673)
1 parent f6894dc commit fe551de

File tree

4 files changed

+79
-101
lines changed

4 files changed

+79
-101
lines changed

packages/react/src/__tests__/ReactJSXElementValidator-test.internal.js

Lines changed: 0 additions & 101 deletions
This file was deleted.

packages/react/src/__tests__/ReactJSXElementValidator-test.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,4 +424,83 @@ describe('ReactJSXElementValidator', () => {
424424
);
425425
}
426426
});
427+
428+
it('warns for fragments with illegal attributes', () => {
429+
spyOnDev(console, 'error');
430+
431+
class Foo extends React.Component {
432+
render() {
433+
return (
434+
<React.Fragment a={1} b={2}>
435+
hello
436+
</React.Fragment>
437+
);
438+
}
439+
}
440+
441+
ReactTestUtils.renderIntoDocument(<Foo />);
442+
443+
if (__DEV__) {
444+
expect(console.error.calls.count()).toBe(1);
445+
expect(console.error.calls.argsFor(0)[0]).toContain('Invalid prop `');
446+
expect(console.error.calls.argsFor(0)[0]).toContain(
447+
'` supplied to `React.Fragment`. React.Fragment ' +
448+
'can only have `key` and `children` props.',
449+
);
450+
}
451+
});
452+
453+
it('warns for fragments with refs', () => {
454+
spyOnDev(console, 'error');
455+
456+
class Foo extends React.Component {
457+
render() {
458+
return (
459+
<React.Fragment
460+
ref={bar => {
461+
this.foo = bar;
462+
}}>
463+
hello
464+
</React.Fragment>
465+
);
466+
}
467+
}
468+
469+
ReactTestUtils.renderIntoDocument(<Foo />);
470+
471+
if (__DEV__) {
472+
expect(console.error.calls.count()).toBe(1);
473+
expect(console.error.calls.argsFor(0)[0]).toContain(
474+
'Invalid attribute `ref` supplied to `React.Fragment`.',
475+
);
476+
}
477+
});
478+
479+
it('does not warn for fragments of multiple elements without keys', () => {
480+
ReactTestUtils.renderIntoDocument(
481+
<React.Fragment>
482+
<span>1</span>
483+
<span>2</span>
484+
</React.Fragment>,
485+
);
486+
});
487+
488+
it('warns for fragments of multiple elements with same key', () => {
489+
spyOnDev(console, 'error');
490+
491+
ReactTestUtils.renderIntoDocument(
492+
<React.Fragment>
493+
<span key="a">1</span>
494+
<span key="a">2</span>
495+
<span key="b">3</span>
496+
</React.Fragment>,
497+
);
498+
499+
if (__DEV__) {
500+
expect(console.error.calls.count()).toBe(1);
501+
expect(console.error.calls.argsFor(0)[0]).toContain(
502+
'Encountered two children with the same key, `a`.',
503+
);
504+
}
505+
});
427506
});

0 commit comments

Comments
 (0)