Enable bundle tests for React.Fragment (#11673)
Dan Abramov committed
Nov 27, 2017 at 21:44 UTC
fe551de2731375f673799d845fb329e5c2846648
4 files changed
+79
-101
packages/react-dom/src/__tests__/ReactDOMServerIntegrationFragment-test.js
renamed
packages/react-reconciler/src/__tests__/ReactFragment-test.js
renamed
packages/react/src/__tests__/ReactJSXElementValidator-test.internal.js
deleted
-101
@@ -1,101 +0,0 @@
1
-/**
2
- * Copyright (c) 2013-present, Facebook, Inc.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- * @emails react-core
8
- */
9
-
10
-'use strict';
11
-
12
-var React;
13
-var ReactTestUtils;
14
-
15
-describe('ReactJSXElementValidator', () => {
16
- beforeEach(() => {
17
- jest.resetModules();
18
-
19
- React = require('react');
20
- ReactTestUtils = require('react-dom/test-utils');
21
- });
22
-
23
- it('warns for fragments with illegal attributes', () => {
24
- spyOnDev(console, 'error');
25
-
26
- class Foo extends React.Component {
27
- render() {
28
- return (
29
- <React.Fragment a={1} b={2}>
30
- hello
31
- </React.Fragment>
32
- );
33
- }
34
- }
35
-
36
- ReactTestUtils.renderIntoDocument(<Foo />);
37
-
38
- if (__DEV__) {
39
- expect(console.error.calls.count()).toBe(1);
40
- expect(console.error.calls.argsFor(0)[0]).toContain('Invalid prop `');
41
- expect(console.error.calls.argsFor(0)[0]).toContain(
42
- '` supplied to `React.Fragment`. React.Fragment ' +
43
- 'can only have `key` and `children` props.',
44
- );
45
- }
46
- });
47
-
48
- it('warns for fragments with refs', () => {
49
- spyOnDev(console, 'error');
50
-
51
- class Foo extends React.Component {
52
- render() {
53
- return (
54
- <React.Fragment
55
- ref={bar => {
56
- this.foo = bar;
57
- }}>
58
- hello
59
- </React.Fragment>
60
- );
61
- }
62
- }
63
-
64
- ReactTestUtils.renderIntoDocument(<Foo />);
65
-
66
- if (__DEV__) {
67
- expect(console.error.calls.count()).toBe(1);
68
- expect(console.error.calls.argsFor(0)[0]).toContain(
69
- 'Invalid attribute `ref` supplied to `React.Fragment`.',
70
- );
71
- }
72
- });
73
-
74
- it('does not warn for fragments of multiple elements without keys', () => {
75
- ReactTestUtils.renderIntoDocument(
76
- <React.Fragment>
77
- <span>1</span>
78
- <span>2</span>
79
- </React.Fragment>,
80
- );
81
- });
82
-
83
- it('warns for fragments of multiple elements with same key', () => {
84
- spyOnDev(console, 'error');
85
-
86
- ReactTestUtils.renderIntoDocument(
87
- <React.Fragment>
88
- <span key="a">1</span>
89
- <span key="a">2</span>
90
- <span key="b">3</span>
91
- </React.Fragment>,
92
- );
93
-
94
- if (__DEV__) {
95
- expect(console.error.calls.count()).toBe(1);
96
- expect(console.error.calls.argsFor(0)[0]).toContain(
97
- 'Encountered two children with the same key, `a`.',
98
- );
99
- }
100
- });
101
-});
packages/react/src/__tests__/ReactJSXElementValidator-test.js
+79
@@ -424,4 +424,83 @@ describe('ReactJSXElementValidator', () => {
424
);
425
}
426
});
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
+ });
506
});