import * as React from 'react';
import { ReactShallowRenderer } from '../../src';
import { elementSymbol } from '../../src/constants';
import { compare } from '../helpers/compare';
describe('ReactShallowRenderer', () => {
class ComponentClass extends React.Component {
public render() {
return
Component class child
;
}
}
// tslint:disable-next-line:max-classes-per-file
class ComponentClassWithDisplayName extends React.Component {
public static displayName = 'DisplayName';
public render() {
return Component class (with display name) child
;
}
}
// tslint:disable-next-line:max-classes-per-file
class ComponentClassWithSuppliedChildren extends React.Component {
public render() {
return {this.props.children}
;
}
}
// tslint:disable-next-line:max-classes-per-file
class ComponentClassReturnsArray extends React.Component {
public render() {
return [First
, 'Second'];
}
}
const ComponentWithClassChildren: React.FunctionComponent = () => (
);
const ComponentWithComponentClassSuppliedChildren: React.FunctionComponent = () => (
I am a child!
);
describe('toJSON', () => {
it('renders a component class that returns an array', () => {
const element = ;
const renderer = new ReactShallowRenderer(element);
compare(renderer.toJSON(), [
{
$$typeof: elementSymbol,
type: 'p',
key: '1',
ref: null,
props: {
children: ['First'],
},
_owner: null,
_store: {},
},
'Second',
]);
});
it('renders a component class', () => {
const element = ;
const renderer = new ReactShallowRenderer(element);
compare(renderer.toJSON(), {
$$typeof: elementSymbol,
type: 'p',
key: null,
ref: null,
props: {
children: ['Component class child'],
},
_owner: null,
_store: {},
});
});
it('renders a component class with a display name', () => {
const element = ;
const renderer = new ReactShallowRenderer(element);
compare(renderer.toJSON(), {
$$typeof: elementSymbol,
type: 'p',
key: null,
ref: null,
props: {
children: ['Component class (with display name) child'],
},
_owner: null,
_store: {},
});
});
it('renders a component with component class children', () => {
const element = ;
const renderer = new ReactShallowRenderer(element);
compare(renderer.toJSON(), {
$$typeof: elementSymbol,
type: 'div',
key: null,
ref: null,
props: {
children: [
{
$$typeof: elementSymbol,
type: 'ComponentClass',
key: null,
ref: null,
props: {
children: [],
},
_owner: null,
_store: {},
},
{
$$typeof: elementSymbol,
type: 'DisplayName',
key: null,
ref: null,
props: {
children: [],
},
_owner: null,
_store: {},
},
],
},
_owner: null,
_store: {},
});
});
it('renders a component class with supplied children', () => {
const element = (
I am a child!
);
const renderer = new ReactShallowRenderer(element);
compare(renderer.toJSON(), {
$$typeof: elementSymbol,
type: 'p',
key: null,
ref: null,
props: {
children: ['I am a child!'],
},
_owner: null,
_store: {},
});
});
it('renders a component with a component class child with supplied children', () => {
const element = ;
const renderer = new ReactShallowRenderer(element);
compare(renderer.toJSON(), {
$$typeof: elementSymbol,
type: 'div',
key: null,
ref: null,
props: {
children: [
{
$$typeof: elementSymbol,
type: 'ComponentClassWithSuppliedChildren',
key: null,
ref: null,
props: {
children: ['I am a child!'],
},
_owner: null,
_store: {},
},
],
},
_owner: null,
_store: {},
});
});
it("renders a component that is class-like, but doesn't inherit from component", () => {
function ClassLike() {
return null;
}
ClassLike.prototype.render = () => Here's the bit we care about!
;
const renderer = new ReactShallowRenderer();
expect(renderer.toJSON()).toEqual({
$$typeof: elementSymbol,
type: 'p',
key: null,
ref: null,
props: {
children: ["Here's the bit we care about!"],
},
_owner: null,
_store: {},
});
});
});
});