Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for the BlockMover Component. #980

Merged
merged 3 commits into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion editor/block-mover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { isFirstBlock, isLastBlock, getBlockIndex, getBlock } from '../selectors
import { getBlockMoverLabel } from './mover-label';
import { selectBlock } from '../actions';

function BlockMover( { onMoveUp, onMoveDown, isFirst, isLast, uids, blockType, firstIndex } ) {
export function BlockMover( { onMoveUp, onMoveDown, isFirst, isLast, uids, blockType, firstIndex } ) {
// We emulate a disabled state because forcefully applying the `disabled`
// attribute on the button while it has focus causes the screen to change
// to an unfocused state (body as active element) without firing blur on,
Expand Down
99 changes: 99 additions & 0 deletions editor/block-mover/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* External dependencies
*/
import { shallow } from 'enzyme';

/**
* Internal dependencies
*/
import { BlockMover } from '../';

describe( 'BlockMover', () => {
describe( 'basic rendering', () => {
const selectedUids = [ 'IisUID', 'IisOtherUID' ];

const blockType = {
title: 'yolo-block',
};

it( 'should render two IconButton components with the following props', () => {
const blockMover = shallow( <BlockMover uids={ selectedUids } blockType={ blockType } firstIndex={ 0 } /> );
expect( blockMover.hasClass( 'editor-block-mover' ) ).toBe( true );

const moveUp = blockMover.childAt( 0 );
const moveDown = blockMover.childAt( 1 );
expect( moveUp.type().name ).toBe( 'IconButton' );
expect( moveDown.type().name ).toBe( 'IconButton' );
expect( moveUp.props() ).toMatchObject( {
className: 'editor-block-mover__control',
onClick: undefined,
icon: 'arrow-up-alt2',
label: 'Move 2 blocks from position 1 up by one place',
'aria-disabled': undefined,
} );
expect( moveDown.props() ).toMatchObject( {
className: 'editor-block-mover__control',
onClick: undefined,
icon: 'arrow-down-alt2',
label: 'Move 2 blocks from position 1 down by one place',
'aria-disabled': undefined,
} );
} );

it( 'should render the up arrow with a onMoveUp callback', () => {
const onMoveUp = ( event ) => event;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably wouldn't hurt.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the Mocha to Jest migration it would require using jest.fn() instead to achieve similar result.

const blockMover = shallow(
<BlockMover uids={ selectedUids }
blockType={ blockType }
onMoveUp={ onMoveUp }
firstIndex={ 0 } />
);
const moveUp = blockMover.childAt( 0 );
expect( moveUp.prop( 'onClick' ) ).toBe( onMoveUp );
} );

it( 'should render the down arrow with a onMoveDown callback', () => {
const onMoveDown = ( event ) => event;
const blockMover = shallow(
<BlockMover uids={ selectedUids }
blockType={ blockType }
onMoveDown={ onMoveDown }
firstIndex={ 0 } />
);
const moveDown = blockMover.childAt( 1 );
expect( moveDown.prop( 'onClick' ) ).toBe( onMoveDown );
} );

it( 'should render with a disabled up arrown when the block isFirst', () => {
const onMoveUp = ( event ) => event;
const blockMover = shallow(
<BlockMover uids={ selectedUids }
blockType={ blockType }
onMoveUp={ onMoveUp }
isFirst
firstIndex={ 0 } />
);
const moveUp = blockMover.childAt( 0 );
expect( moveUp.props() ).toMatchObject( {
onClick: null,
'aria-disabled': true,
} );
} );

it( 'should render with a disabled down arrow when the block isLast', () => {
const onMoveDown = ( event ) => event;
const blockMover = shallow(
<BlockMover uids={ selectedUids }
blockType={ blockType }
onMoveDown={ onMoveDown }
isLast
firstIndex={ 0 } />
);
const moveDown = blockMover.childAt( 1 );
expect( moveDown.props() ).toMatchObject( {
onClick: null,
'aria-disabled': true,
} );
} );
} );
} );