main
js 75 lines 1.88 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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 describe('onlyChild', () => {
13 let React;
14 let WrapComponent;
15
16 beforeEach(() => {
17 React = require('react');
18 WrapComponent = class extends React.Component {
19 render() {
20 return (
21 <div>
22 {React.Children.only(this.props.children, this.props.mapFn, this)}
23 </div>
24 );
25 }
26 };
27 });
28
29 it('should fail when passed two children', () => {
30 expect(function () {
31 const instance = (
32 <WrapComponent>
33 <div />
34 <span />
35 </WrapComponent>
36 );
37 React.Children.only(instance.props.children);
38 }).toThrow();
39 });
40
41 it('should fail when passed nully values', () => {
42 expect(function () {
43 const instance = <WrapComponent>{null}</WrapComponent>;
44 React.Children.only(instance.props.children);
45 }).toThrow();
46
47 expect(function () {
48 const instance = <WrapComponent>{undefined}</WrapComponent>;
49 React.Children.only(instance.props.children);
50 }).toThrow();
51 });
52
53 it('should fail when key/value objects', () => {
54 expect(function () {
55 const instance = <WrapComponent>{[<span key="abc" />]}</WrapComponent>;
56 React.Children.only(instance.props.children);
57 }).toThrow();
58 });
59
60 it('should not fail when passed interpolated single child', () => {
61 expect(function () {
62 const instance = <WrapComponent>{<span />}</WrapComponent>;
63 React.Children.only(instance.props.children);
64 }).not.toThrow();
65 });
66
67 it('should return the only child', () => {
68 const instance = (
69 <WrapComponent>
70 <span />
71 </WrapComponent>
72 );
73 expect(React.Children.only(instance.props.children)).toEqual(<span />);
74 });
75 });