Skip to content

Commit

Permalink
Remove ReactFragment dependency from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Mar 17, 2017
1 parent 334409d commit ef45923
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 144 deletions.
160 changes: 59 additions & 101 deletions src/isomorphic/children/__tests__/ReactChildren-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@

describe('ReactChildren', () => {
var React;
var ReactFragment;

beforeEach(() => {
jest.resetModules();
React = require('react');
ReactFragment = require('ReactFragment');
});

it('should support identity for simple', () => {
Expand Down Expand Up @@ -150,8 +148,8 @@ describe('ReactChildren', () => {
var instance = (
<div>
{div}
{[ReactFragment.create({span})]}
{ReactFragment.create({a: a})}
{[[span]]}
{[a]}
{'string'}
{1234}
{true}
Expand All @@ -164,8 +162,8 @@ describe('ReactChildren', () => {
function assertCalls() {
expect(callback.calls.count()).toBe(9);
expect(callback).toHaveBeenCalledWith(div, 0);
expect(callback).toHaveBeenCalledWith(<span key="span/.$spanNode" />, 1);
expect(callback).toHaveBeenCalledWith(<a key="a/.$aNode" />, 2);
expect(callback).toHaveBeenCalledWith(span, 1);
expect(callback).toHaveBeenCalledWith(a, 2);
expect(callback).toHaveBeenCalledWith('string', 3);
expect(callback).toHaveBeenCalledWith(1234, 4);
expect(callback).toHaveBeenCalledWith(null, 5);
Expand All @@ -186,8 +184,8 @@ describe('ReactChildren', () => {
assertCalls();
expect(mappedChildren).toEqual([
<div key=".$divNode" />,
<span key=".1:0:$span/.$spanNode" />,
<a key=".2:$a/.$aNode" />,
<span key=".1:0:$spanNode" />,
<a key=".2:$aNode" />,
'string',
1234,
]);
Expand All @@ -199,12 +197,7 @@ describe('ReactChildren', () => {
var two = <div key="keyTwo" />;
var three = null;
var four = <div key="keyFour" />;
var five = <div key="keyFiveInner" />;
// five is placed into a JS object with a key that is joined to the
// component key attribute.
// Precedence is as follows:
// 1. If grouped in an Object, the object key combined with `key` prop
// 2. If grouped in an Array, the `key` prop, falling back to array index
var five = <div key="keyFive" />;

var context = {};
var callback = jasmine.createSpy().and.callFake(function(kid) {
Expand All @@ -213,34 +206,18 @@ describe('ReactChildren', () => {

var instance = (
<div>
{[
ReactFragment.create({
firstHalfKey: [zero, one, two],
secondHalfKey: [three, four],
keyFive: five,
}),
]}
{[[zero, one, two], [three, four], five]}
</div>
);

function assertCalls() {
expect(callback.calls.count()).toBe(4);
expect(callback).toHaveBeenCalledWith(
<div key="firstHalfKey/.$keyZero" />,
0,
);
expect(callback).toHaveBeenCalledWith(
<div key="firstHalfKey/.$keyTwo" />,
1,
);
expect(callback).toHaveBeenCalledWith(
<div key="secondHalfKey/.$keyFour" />,
2,
);
expect(callback).toHaveBeenCalledWith(
<div key="keyFive/.$keyFiveInner" />,
3,
);
expect(callback.calls.count()).toBe(6);
expect(callback).toHaveBeenCalledWith(zero, 0);
expect(callback).toHaveBeenCalledWith(one, 1);
expect(callback).toHaveBeenCalledWith(two, 2);
expect(callback).toHaveBeenCalledWith(three, 3);
expect(callback).toHaveBeenCalledWith(four, 4);
expect(callback).toHaveBeenCalledWith(five, 5);
callback.calls.reset();
}

Expand All @@ -254,10 +231,10 @@ describe('ReactChildren', () => {
);
assertCalls();
expect(mappedChildren).toEqual([
<div key=".0:$firstHalfKey/.$keyZero" />,
<div key=".0:$firstHalfKey/.$keyTwo" />,
<div key=".0:$secondHalfKey/.$keyFour" />,
<div key=".0:$keyFive/.$keyFiveInner" />,
<div key=".0:$keyZero" />,
<div key=".0:$keyTwo" />,
<div key=".1:$keyFour" />,
<div key=".$keyFive" />,
]);
});

Expand Down Expand Up @@ -601,52 +578,49 @@ describe('ReactChildren', () => {
var two = <div key="keyTwo" />;
var three = null;
var four = <div key="keyFour" />;
var five = <div key="keyFiveInner" />;
// five is placed into a JS object with a key that is joined to the
// component key attribute.
// Precedence is as follows:
// 1. If grouped in an Object, the object key combined with `key` prop
// 2. If grouped in an Array, the `key` prop, falling back to array index
var five = <div key="keyFive" />;

var zeroMapped = <div key="giraffe" />; // Key should be overridden
var twoMapped = <div />; // Key should be added even if not supplied!
var fourMapped = <div key="keyFour" />;
var fiveMapped = <div />;

var callback = jasmine.createSpy().and.callFake(function(kid, index) {
return index === 0
? zeroMapped
: index === 1 ? twoMapped : index === 2 ? fourMapped : fiveMapped;
var callback = jasmine.createSpy().and.callFake(function(kid) {
switch (kid) {
case zero:
return zeroMapped;
case two:
return twoMapped;
case four:
return fourMapped;
case five:
return fiveMapped;
default:
return kid;
}
});

var frag = ReactFragment.create({
firstHalfKey: [zero, one, two],
secondHalfKey: [three, four],
keyFive: five,
});
var frag = [[zero, one, two], [three, four], five];
var instance = <div>{[frag]}</div>;

expect([frag[0].key, frag[1].key, frag[2].key, frag[3].key]).toEqual([
'firstHalfKey/.$keyZero',
'firstHalfKey/.$keyTwo',
'secondHalfKey/.$keyFour',
'keyFive/.$keyFiveInner',
]);

React.Children.forEach(instance.props.children, callback);
expect(callback.calls.count()).toBe(4);
expect(callback).toHaveBeenCalledWith(frag[0], 0);
expect(callback).toHaveBeenCalledWith(frag[1], 1);
expect(callback).toHaveBeenCalledWith(frag[2], 2);
expect(callback).toHaveBeenCalledWith(frag[3], 3);
expect(callback.calls.count()).toBe(6);
expect(callback).toHaveBeenCalledWith(zero, 0);
expect(callback).toHaveBeenCalledWith(one, 1);
expect(callback).toHaveBeenCalledWith(two, 2);
expect(callback).toHaveBeenCalledWith(three, 3);
expect(callback).toHaveBeenCalledWith(four, 4);
expect(callback).toHaveBeenCalledWith(five, 5);
callback.calls.reset();

var mappedChildren = React.Children.map(instance.props.children, callback);
expect(callback.calls.count()).toBe(4);
expect(callback).toHaveBeenCalledWith(frag[0], 0);
expect(callback).toHaveBeenCalledWith(frag[1], 1);
expect(callback).toHaveBeenCalledWith(frag[2], 2);
expect(callback).toHaveBeenCalledWith(frag[3], 3);
expect(callback.calls.count()).toBe(6);
expect(callback).toHaveBeenCalledWith(zero, 0);
expect(callback).toHaveBeenCalledWith(one, 1);
expect(callback).toHaveBeenCalledWith(two, 2);
expect(callback).toHaveBeenCalledWith(three, 3);
expect(callback).toHaveBeenCalledWith(four, 4);
expect(callback).toHaveBeenCalledWith(five, 5);

expect(React.Children.count(mappedChildren)).toBe(4);
// Keys default to indices.
Expand All @@ -656,20 +630,16 @@ describe('ReactChildren', () => {
mappedChildren[2].key,
mappedChildren[3].key,
]).toEqual([
'giraffe/.0:$firstHalfKey/.$keyZero',
'.0:$firstHalfKey/.$keyTwo',
'keyFour/.0:$secondHalfKey/.$keyFour',
'.0:$keyFive/.$keyFiveInner',
'giraffe/.0:0:$keyZero',
'.0:0:$keyTwo',
'.0:1:$keyFour',
'.0:$keyFive',
]);

expect(mappedChildren[0]).toEqual(
<div key="giraffe/.0:$firstHalfKey/.$keyZero" />,
);
expect(mappedChildren[1]).toEqual(<div key=".0:$firstHalfKey/.$keyTwo" />);
expect(mappedChildren[2]).toEqual(
<div key="keyFour/.0:$secondHalfKey/.$keyFour" />,
);
expect(mappedChildren[3]).toEqual(<div key=".0:$keyFive/.$keyFiveInner" />);
expect(mappedChildren[0]).toEqual(<div key="giraffe/.0:0:$keyZero" />);
expect(mappedChildren[1]).toEqual(<div key=".0:0:$keyTwo" />);
expect(mappedChildren[2]).toEqual(<div key=".0:1:$keyFour" />);
expect(mappedChildren[3]).toEqual(<div key=".0:$keyFive" />);
});

it('should retain key across two mappings', () => {
Expand Down Expand Up @@ -812,27 +782,15 @@ describe('ReactChildren', () => {
var two = <div key="keyTwo" />;
var three = null;
var four = <div key="keyFour" />;
var five = <div key="keyFiveInner" />;
// five is placed into a JS object with a key that is joined to the
// component key attribute.
// Precedence is as follows:
// 1. If grouped in an Object, the object key combined with `key` prop
// 2. If grouped in an Array, the `key` prop, falling back to array index
var five = <div key="keyFive" />;

var instance = (
<div>
{[
ReactFragment.create({
firstHalfKey: [zero, one, two],
secondHalfKey: [three, four],
keyFive: five,
}),
null,
]}
{[[[zero, one, two], [three, four], five], null]}
</div>
);
var numberOfChildren = React.Children.count(instance.props.children);
expect(numberOfChildren).toBe(5);
expect(numberOfChildren).toBe(7);
});

it('should flatten children to an array', () => {
Expand Down
4 changes: 1 addition & 3 deletions src/isomorphic/children/__tests__/onlyChild-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@

describe('onlyChild', () => {
var React;
var ReactFragment;
var onlyChild;
var WrapComponent;

beforeEach(() => {
React = require('react');
ReactFragment = require('ReactFragment');
onlyChild = require('onlyChild');
WrapComponent = class extends React.Component {
render() {
Expand Down Expand Up @@ -68,7 +66,7 @@ describe('onlyChild', () => {
expect(function() {
var instance = (
<WrapComponent>
{ReactFragment.create({oneThing: <span />})}
{{oneThing: <span />}}
</WrapComponent>
);
onlyChild(instance.props.children);
Expand Down
25 changes: 2 additions & 23 deletions src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var checkPropTypes;
var checkReactTypeSpec;
var React;
var ReactDOM;
var ReactFragment;

var Component;
var MyComponent;
Expand Down Expand Up @@ -115,7 +114,6 @@ describe('ReactPropTypes', () => {
PropTypes = require('ReactPropTypes');
React = require('react');
ReactDOM = require('react-dom');
ReactFragment = require('ReactFragment');
resetWarningCache();
});

Expand Down Expand Up @@ -615,39 +613,20 @@ describe('ReactPropTypes', () => {
});

it('should not warn for valid values', () => {
spyOn(console, 'error');
typeCheckPass(PropTypes.node, <div />);
typeCheckPass(PropTypes.node, false);
typeCheckPass(PropTypes.node, <MyComponent />);
typeCheckPass(PropTypes.node, 'Some string');
typeCheckPass(PropTypes.node, []);

typeCheckPass(PropTypes.node, [
123,
'Some string',
<div />,
['Another string', [456], <span />, <MyComponent />],
<MyComponent />,
null,
undefined,
]);

// Object of renderable things
var frag = ReactFragment.create;
typeCheckPass(
PropTypes.node,
frag({
k0: 123,
k1: 'Some string',
k2: <div />,
k3: frag({
k30: <MyComponent />,
k31: frag({k310: <a />}),
k32: 'Another string',
}),
k4: null,
k5: undefined,
}),
);
expectDev(console.error.calls.count()).toBe(0);
});

it('should not warn for iterables', () => {
Expand Down
20 changes: 3 additions & 17 deletions src/renderers/__tests__/ReactIdentity-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,16 @@

var React;
var ReactDOM;
var ReactFragment;
var ReactTestUtils;

describe('ReactIdentity', () => {
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactFragment = require('ReactFragment');
ReactTestUtils = require('ReactTestUtils');
});

function frag(obj) {
return ReactFragment.create(obj);
}

it('should allow key property to express identity', () => {
var node;
var Component = props => (
Expand Down Expand Up @@ -72,21 +66,13 @@ describe('ReactIdentity', () => {
function renderAComponentWithKeyIntoContainer(key, container) {
class Wrapper extends React.Component {
render() {
var s1 = <span ref="span1" key={key} />;
var s2 = <span ref="span2" />;

var map = {};
map[key] = s2;
return <div>{[s1, frag(map)]}</div>;
return <div><span ref="span" key={key} /></div>;
}
}

var instance = ReactDOM.render(<Wrapper />, container);
var span1 = instance.refs.span1;
var span2 = instance.refs.span2;

expect(ReactDOM.findDOMNode(span1)).not.toBe(null);
expect(ReactDOM.findDOMNode(span2)).not.toBe(null);
var span = instance.refs.span;
expect(ReactDOM.findDOMNode(span)).not.toBe(null);
}

it('should allow any character as a key, in a detached parent', () => {
Expand Down

0 comments on commit ef45923

Please sign in to comment.