Skip to content

Commit

Permalink
Components: Adding Slot rendering unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Nov 10, 2017
1 parent c3ac841 commit 1fb6853
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
9 changes: 6 additions & 3 deletions components/slot-fill/slot.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { noop, map } from 'lodash';
import { noop, map, isString } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -56,8 +56,11 @@ class Slot extends Component {
<div ref={ this.bindNode }>
{ map( getFills( name ), ( fill ) => {
const fillKey = fill.props.instanceId;
return Children.toArray( fill.props.children ).map( ( child, childIndex ) => {
const childKey = `${ fillKey }---${ child.props.key || childIndex }`;
return Children.map( fill.props.children, ( child, childIndex ) => {
if ( ! child || isString( child ) ) {
return child;
}
const childKey = `${ fillKey }---${ child.key || childIndex }`;
return cloneElement( child, { key: childKey } );
} );
} ) }
Expand Down
63 changes: 63 additions & 0 deletions components/slot-fill/test/slot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* External dependencies
*/
import { mount } from 'enzyme';

/**
* Internal dependencies
*/
import Slot from '../slot';
import Fill from '../fill';
import Provider from '../provider';

describe( 'Slot', () => {
it( 'should render empty Fills', () => {
const element = mount(
<Provider>
<Slot name="chicken" />
<Fill name="chicken" />
</Provider>
);

expect( element.find( 'Slot > div' ).html() ).toBe( '<div></div>' );
} );

it( 'should render a string Fill', () => {
const element = mount(
<Provider>
<Slot name="chicken" />
<Fill name="chicken">
content
</Fill>
</Provider>
);

expect( element.find( 'Slot > div' ).html() ).toBe( '<div>content</div>' );
} );

it( 'should render a Fill containing an element', () => {
const element = mount(
<Provider>
<Slot name="chicken" />
<Fill name="chicken">
<span />
</Fill>
</Provider>
);

expect( element.find( 'Slot > div' ).html() ).toBe( '<div><span></span></div>' );
} );

it( 'should render a Fill containing an array', () => {
const element = mount(
<Provider>
<Slot name="chicken" />
<Fill name="chicken">
{ [ <span key="1" />, <div key="2" />, 'text' ] }
</Fill>
</Provider>
);

expect( element.find( 'Slot > div' ).html() ).toBe( '<div><span></span><div></div>text</div>' );
} );
} );

0 comments on commit 1fb6853

Please sign in to comment.