Skip to content

Commit

Permalink
Add Placeholder component tests
Browse files Browse the repository at this point in the history
Adds basic Placeholder component tests. Related to progress on #641.
Adds an extra conditional to make testing easier to prevent an element
that eventually renders to null from sticking in the tree when the icon
prop is not present.

Testing Instructions
Run npm i && npm run test-unit ensure tests pass. Change component logic
to ensure tests fail as they should.
  • Loading branch information
BE-Webdesign committed Jun 1, 2017
1 parent fc60e2f commit 4172a08
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion components/placeholder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function Placeholder( { icon, children, label, instructions, className, ...addit
return (
<div { ...additionalProps } aria-label={ label } className={ classes }>
<div className="components-placeholder__label">
<Dashicon icon={ icon } />
{ !! icon && <Dashicon icon={ icon } /> }
{ label }
</div>
{ !! instructions && <div className="components-placeholder__instructions">{ instructions }</div> }
Expand Down
77 changes: 77 additions & 0 deletions components/placeholder/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* External dependencies
*/
import { expect } from 'chai';
import { shallow } from 'enzyme';

/**
* Internal dependencies
*/
import Placeholder from '../';

describe( 'Placeholder', () => {
describe( 'basic rendering', () => {
it( 'should by default render label section and fieldset.', () => {
const placeholder = shallow( <Placeholder /> );
const placeholderLabel = placeholder.find( '.components-placeholder__label' );
const placeholderInstructions = placeholder.find( '.components-placeholder__instructions' );
const placeholderFieldset = placeholder.find( '.components-placeholder__fieldset' );

expect( placeholder.hasClass( 'components-placeholder' ) ).to.be.true();
// Test for empty label.
expect( placeholderLabel.exists() ).to.be.true();
expect( placeholderLabel.find( 'Dashicon' ).exists() ).to.be.false();
// Test for non existant instructions.
expect( placeholderInstructions.exists() ).to.be.false();
// Test for empty fieldset.
expect( placeholderFieldset.exists() ).to.be.true();
} );

it( 'should render an Dashicon in the label section', () => {
const placeholder = shallow( <Placeholder icon="wordpress" /> );
const placeholderLabel = placeholder.find( '.components-placeholder__label' ).shallow();

expect( placeholderLabel.find( 'Dashicon' ).exists() ).to.be.true();
} );

it( 'should render a label section and add aria label', () => {
const label = 'WordPress';
const placeholder = shallow( <Placeholder label={ label } /> );
const placeholderLabel = placeholder.find( '.components-placeholder__label' );
const child = placeholderLabel.childAt( 0 );

expect( placeholder.prop( 'aria-label' ) ).to.equal( label );
expect( child.text() ).to.equal( label );
} );

it( 'should display an instructions element', () => {
const element = <div>Instructions</div>;
const placeholder = shallow( <Placeholder instructions={ element } /> );
const placeholderInstructions = placeholder.find( '.components-placeholder__instructions' );
const child = placeholderInstructions.childAt( 0 );

expect( placeholderInstructions.exists() ).to.be.true();
expect( child.matchesElement( element ) ).to.be.true();
} );

it( 'should display a fieldset from the children property', () => {
const element = <div>Fieldset</div>;
const placeholder = shallow( <Placeholder children={ element } /> );
const placeholderFieldset = placeholder.find( '.components-placeholder__fieldset' );
const child = placeholderFieldset.childAt( 0 );

expect( placeholderFieldset.exists() ).to.be.true();
expect( child.matchesElement( element ) ).to.be.true();
} );

it( 'should add an additional className to the top container', () => {
const placeholder = shallow( <Placeholder className="wp-placeholder" /> );
expect( placeholder.hasClass( 'wp-placeholder' ) ).to.be.true();
} );

it( 'should add additional props to the top level container', () => {
const placeholder = shallow( <Placeholder test="test" /> );
expect( placeholder.props().test ).to.equal( 'test' );
} );
} );
} );

0 comments on commit 4172a08

Please sign in to comment.