@@ -9,53 +9,84 @@ import { expect } from 'chai';
99import * as blocks from '../' ;
1010
1111describe ( 'blocks API' , ( ) => {
12- // TODO: We probably want a way to undo this, and split this logic out into
13- // separate tests (clearing require.cache for example, but this probably
14- // won't work with webpack)
15- before ( ( ) => {
16- blocks . registerBlock ( 'core/test-block' ) ;
17- const blockSettings = { settingName : 'settingValue' } ;
18- blocks . registerBlock ( 'core/test-block-with-settings' , blockSettings ) ;
19- // This is tested later: `registerBlock` should store a copy of its input
20- blockSettings . mutated = true ;
12+ // Reset block state before each test.
13+ beforeEach ( ( ) => {
14+ blocks . getBlocks ( ) . forEach ( block => {
15+ blocks . unregisterBlock ( block . slug ) ;
16+ } ) ;
2117 } ) ;
2218
23- describe ( 'validateBlockSlug ' , ( ) => {
19+ describe ( 'registerBlock ' , ( ) => {
2420 it ( 'should reject numbers' , ( ) => {
2521 expect (
26- ( ) => blocks . validateBlockSlug ( 999 )
27- ) . to . throw ( / ^ B l o c k s l u g s m u s t c o n t a i n a n a m e s p a c e p r e f i x / ) ;
22+ ( ) => blocks . registerBlock ( 999 )
23+ ) . to . throw ( ' Block slugs must be strings.' ) ;
2824 } ) ;
2925
3026 it ( 'should reject blocks without a namespace' , ( ) => {
3127 expect (
32- ( ) => blocks . validateBlockSlug ( 'doing-it-wrong' )
28+ ( ) => blocks . registerBlock ( 'doing-it-wrong' )
3329 ) . to . throw ( / ^ B l o c k s l u g s m u s t c o n t a i n a n a m e s p a c e p r e f i x / ) ;
3430 } ) ;
3531
3632 it ( 'should reject blocks with invalid characters' , ( ) => {
3733 expect (
38- ( ) => blocks . validateBlockSlug ( 'still/_doing_it_wrong' )
34+ ( ) => blocks . registerBlock ( 'still/_doing_it_wrong' )
3935 ) . to . throw ( / ^ B l o c k s l u g s m u s t c o n t a i n a n a m e s p a c e p r e f i x / ) ;
4036 } ) ;
4137
4238 it ( 'should accept valid block names' , ( ) => {
4339 expect (
44- ( ) => blocks . validateBlockSlug ( 'my-plugin/fancy-block-4' )
40+ ( ) => blocks . registerBlock ( 'my-plugin/fancy-block-4' )
4541 ) . not . to . throw ( ) ;
4642 } ) ;
43+
44+ it ( 'should prohibit registering the same block twice' , ( ) => {
45+ blocks . registerBlock ( 'core/test-block' ) ;
46+ expect (
47+ ( ) => blocks . registerBlock ( 'core/test-block' )
48+ ) . to . throw ( 'Block "core/test-block" is already registered.' ) ;
49+ } ) ;
50+
51+ it ( 'should store a copy of block settings' , ( ) => {
52+ const blockSettings = { settingName : 'settingValue' } ;
53+ blocks . registerBlock ( 'core/test-block-with-settings' , blockSettings ) ;
54+ blockSettings . mutated = true ;
55+ expect ( blocks . getBlockSettings ( 'core/test-block-with-settings' ) ) . to . eql ( {
56+ slug : 'core/test-block-with-settings' ,
57+ settingName : 'settingValue' ,
58+ } ) ;
59+ } ) ;
4760 } ) ;
4861
49- // TODO: registerBlock tests
62+ describe ( 'unregisterBlock' , ( ) => {
63+ it ( 'should fail if a block is not registered' , ( ) => {
64+ expect (
65+ ( ) => blocks . unregisterBlock ( 'core/test-block' )
66+ ) . to . throw ( 'Block "core/test-block" is not registered.' ) ;
67+ } ) ;
68+
69+ it ( 'should unregister existing blocks' , ( ) => {
70+ blocks . registerBlock ( 'core/test-block' ) ;
71+ expect ( blocks . getBlocks ( ) ) . to . eql ( [
72+ { slug : 'core/test-block' } ,
73+ ] ) ;
74+ blocks . unregisterBlock ( 'core/test-block' ) ;
75+ expect ( blocks . getBlocks ( ) ) . to . eql ( [ ] ) ;
76+ } ) ;
77+ } ) ;
5078
5179 describe ( 'getBlockSettings' , ( ) => {
5280 it ( 'should return { slug } for blocks with no settings' , ( ) => {
81+ blocks . registerBlock ( 'core/test-block' ) ;
5382 expect ( blocks . getBlockSettings ( 'core/test-block' ) ) . to . eql ( {
5483 slug : 'core/test-block' ,
5584 } ) ;
5685 } ) ;
5786
58- it ( 'should return { slug } for blocks with no settings' , ( ) => {
87+ it ( 'should return all block settings' , ( ) => {
88+ const blockSettings = { settingName : 'settingValue' } ;
89+ blocks . registerBlock ( 'core/test-block-with-settings' , blockSettings ) ;
5990 expect ( blocks . getBlockSettings ( 'core/test-block-with-settings' ) ) . to . eql ( {
6091 slug : 'core/test-block-with-settings' ,
6192 settingName : 'settingValue' ,
@@ -64,10 +95,17 @@ describe( 'blocks API', () => {
6495 } ) ;
6596
6697 describe ( 'getBlocks' , ( ) => {
98+ it ( 'should return an empty array at first' , ( ) => {
99+ expect ( blocks . getBlocks ( ) ) . to . eql ( [ ] ) ;
100+ } ) ;
101+
67102 it ( 'should return all registered blocks' , ( ) => {
103+ blocks . registerBlock ( 'core/test-block' ) ;
104+ const blockSettings = { settingName : 'settingValue' } ;
105+ blocks . registerBlock ( 'core/test-block-with-settings' , blockSettings ) ;
68106 expect ( blocks . getBlocks ( ) ) . to . eql ( [
69- blocks . getBlockSettings ( 'core/test-block' ) ,
70- blocks . getBlockSettings ( 'core/test-block-with-settings' ) ,
107+ { slug : 'core/test-block' } ,
108+ { slug : 'core/test-block-with-settings' , settingName : 'settingValue' } ,
71109 ] ) ;
72110 } ) ;
73111 } ) ;
0 commit comments