-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test cases and other improvements.
- Loading branch information
1 parent
0334269
commit a943aa6
Showing
3 changed files
with
175 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getChildBlockNames } from '../selectors'; | ||
|
||
describe( 'selectors', () => { | ||
describe( 'getChildBlockNames', () => { | ||
it( 'should return an empty array if state is empty', () => { | ||
const state = {}; | ||
|
||
expect( getChildBlockNames( state, 'parent1' ) ).toHaveLength( 0 ); | ||
} ); | ||
|
||
it( 'should return an empty array if no childs exist', () => { | ||
const state = { | ||
blockTypes: [ | ||
{ | ||
name: 'child1', | ||
parent: [ 'parent1' ], | ||
}, | ||
{ | ||
name: 'child2', | ||
parent: [ 'parent2' ], | ||
}, | ||
{ | ||
name: 'parent3', | ||
}, | ||
], | ||
}; | ||
|
||
expect( getChildBlockNames( state, 'parent3' ) ).toHaveLength( 0 ); | ||
} ); | ||
|
||
it( 'should return an empty array if the parent block is not found', () => { | ||
const state = { | ||
blockTypes: [ | ||
{ | ||
name: 'child1', | ||
parent: [ 'parent1' ], | ||
}, | ||
{ | ||
name: 'parent1', | ||
}, | ||
], | ||
}; | ||
|
||
expect( getChildBlockNames( state, 'parent3' ) ).toHaveLength( 0 ); | ||
} ); | ||
|
||
it( 'should return an array with the child block names', () => { | ||
const state = { | ||
blockTypes: [ | ||
{ | ||
name: 'child1', | ||
parent: [ 'parent1' ], | ||
}, | ||
{ | ||
name: 'child2', | ||
parent: [ 'parent2' ], | ||
}, | ||
{ | ||
name: 'child3', | ||
parent: [ 'parent1' ], | ||
}, | ||
{ | ||
name: 'child4', | ||
}, | ||
{ | ||
name: 'parent1', | ||
}, | ||
{ | ||
name: 'parent2', | ||
}, | ||
], | ||
}; | ||
|
||
expect( getChildBlockNames( state, 'parent1' ) ).toEqual( [ 'child1', 'child3' ] ); | ||
} ); | ||
|
||
it( 'should return an array with the child block names even if only one child exists', () => { | ||
const state = { | ||
blockTypes: [ | ||
{ | ||
name: 'child1', | ||
parent: [ 'parent1' ], | ||
}, | ||
{ | ||
name: 'child2', | ||
parent: [ 'parent2' ], | ||
}, | ||
{ | ||
name: 'child4', | ||
}, | ||
{ | ||
name: 'parent1', | ||
}, | ||
{ | ||
name: 'parent2', | ||
}, | ||
], | ||
}; | ||
|
||
expect( getChildBlockNames( state, 'parent1' ) ).toEqual( [ 'child1' ] ); | ||
} ); | ||
|
||
it( 'should return an array with the child block names even if childs have multiple parents', () => { | ||
const state = { | ||
blockTypes: [ | ||
{ | ||
name: 'child1', | ||
parent: [ 'parent1' ], | ||
}, | ||
{ | ||
name: 'child2', | ||
parent: [ 'parent1', 'parent2' ], | ||
}, | ||
{ | ||
name: 'child3', | ||
parent: [ 'parent1' ], | ||
}, | ||
{ | ||
name: 'child4', | ||
}, | ||
{ | ||
name: 'parent1', | ||
}, | ||
{ | ||
name: 'parent2', | ||
}, | ||
], | ||
}; | ||
|
||
expect( getChildBlockNames( state, 'parent1' ) ).toEqual( [ 'child1', 'child2', 'child3' ] ); | ||
expect( getChildBlockNames( state, 'parent2' ) ).toEqual( [ 'child2' ] ); | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.scss'; | ||
import ItemList from './item-list'; | ||
import BlockIcon from '../block-icon'; | ||
|
||
function ChildBlocks( { rootBlockIcon, rootBlockTitle, items, ...props } ) { | ||
if ( ! items || ! items.length ) { | ||
return null; | ||
} | ||
return ( | ||
<div className="editor-inserter__child-blocks"> | ||
{ ( rootBlockIcon || rootBlockTitle ) && ( | ||
<div className="editor-inserter__parent-block-header"> | ||
{ rootBlockIcon && ( | ||
<div className="editor-inserter__parent-block-icon"> | ||
<BlockIcon icon={ rootBlockIcon } /> | ||
</div> | ||
) } | ||
{ rootBlockTitle && <h2>{ rootBlockTitle }</h2> } | ||
</div> | ||
) } | ||
<ItemList items={ items } { ...props } /> | ||
</div> | ||
); | ||
} | ||
|
||
export default ChildBlocks; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters