From 8ba20dad9338b2b5c6e03c8f829f69944bc278a4 Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Mon, 14 May 2018 15:25:55 +0200 Subject: [PATCH] Blocks: Update all tests which depend on editor --- blocks/api/test/factory.js | 39 +----- blocks/api/test/parser.js | 12 +- blocks/api/test/registration.js | 75 +---------- blocks/api/test/serializer.js | 117 +----------------- .../test/__snapshots__/index.js.snap | 17 +++ editor/components/inner-blocks/test/index.js | 111 +++++++++++++++++ test/integration/is-valid-block.spec.js | 59 +++++++++ 7 files changed, 200 insertions(+), 230 deletions(-) create mode 100644 editor/components/inner-blocks/test/__snapshots__/index.js.snap create mode 100644 editor/components/inner-blocks/test/index.js create mode 100644 test/integration/is-valid-block.spec.js diff --git a/blocks/api/test/factory.js b/blocks/api/test/factory.js index 553d337245fd0..1504c6356d8ee 100644 --- a/blocks/api/test/factory.js +++ b/blocks/api/test/factory.js @@ -30,8 +30,8 @@ describe( 'block factory', () => { }; beforeAll( () => { - // Load all hooks that modify blocks - require( 'editor/hooks' ); + // Load blocks store + require( 'blocks/store' ); } ); afterEach( () => { @@ -78,41 +78,6 @@ describe( 'block factory', () => { expect( block.innerBlocks[ 0 ].name ).toBe( 'core/test-block' ); expect( typeof block.uid ).toBe( 'string' ); } ); - - it( 'should keep the className if the block supports it', () => { - registerBlockType( 'core/test-block', { - attributes: {}, - save: noop, - category: 'common', - title: 'test block', - } ); - const block = createBlock( 'core/test-block', { - className: 'chicken', - } ); - - expect( block.attributes ).toEqual( { - className: 'chicken', - } ); - expect( block.isValid ).toBe( true ); - } ); - - it( 'should not keep the className if the block supports it', () => { - registerBlockType( 'core/test-block', { - attributes: {}, - save: noop, - category: 'common', - title: 'test block', - supports: { - customClassName: false, - }, - } ); - const block = createBlock( 'core/test-block', { - className: 'chicken', - } ); - - expect( block.attributes ).toEqual( {} ); - expect( block.isValid ).toBe( true ); - } ); } ); describe( 'cloneBlock()', () => { diff --git a/blocks/api/test/parser.js b/blocks/api/test/parser.js index 1eacdc317e37f..b883efbd17a87 100644 --- a/blocks/api/test/parser.js +++ b/blocks/api/test/parser.js @@ -42,8 +42,8 @@ describe( 'block parser', () => { }; beforeAll( () => { - // Load all hooks that modify blocks - require( 'editor/hooks' ); + // Initialize the block store. + require( '../../store' ); } ); afterEach( () => { @@ -235,7 +235,7 @@ describe( 'block parser', () => { }, ], }, - 'Bananas', + 'Bananas', {}, ); expect( attributesAndInnerBlocks.attributes ).toEqual( { fruit: 'Bananas' } ); @@ -260,7 +260,7 @@ describe( 'block parser', () => { }, ], }, - 'Bananas', + 'Bananas', {}, [ { name: 'core/test-block', @@ -301,7 +301,7 @@ describe( 'block parser', () => { }, ], }, - 'Bananas', + 'Bananas', {}, ); @@ -396,7 +396,7 @@ describe( 'block parser', () => { const block = createBlockWithFallback( { blockName: 'core/test-block', - innerHTML: 'Bananas', + innerHTML: 'Bananas', attrs: { fruit: 'Bananas' }, } ); expect( block.name ).toEqual( 'core/test-block' ); diff --git a/blocks/api/test/registration.js b/blocks/api/test/registration.js index f0399088a78ae..29f437579a943 100644 --- a/blocks/api/test/registration.js +++ b/blocks/api/test/registration.js @@ -29,10 +29,7 @@ describe( 'blocks', () => { const defaultBlockSettings = { save: noop, category: 'common', title: 'block title' }; beforeAll( () => { - // Load all hooks that modify blocks - require( 'editor/hooks' ); - - // Initialize the block store + // Initialize the block store. require( '../../store' ); } ); @@ -91,14 +88,6 @@ describe( 'blocks', () => { save: noop, category: 'common', title: 'block title', - attributes: { - className: { - type: 'string', - }, - layout: { - type: 'string', - }, - }, } ); } ); @@ -183,12 +172,6 @@ describe( 'blocks', () => { ok: { type: 'boolean', }, - className: { - type: 'string', - }, - layout: { - type: 'string', - }, }, } ); } ); @@ -204,14 +187,6 @@ describe( 'blocks', () => { category: 'common', title: 'block title', icon: 'block-default', - attributes: { - className: { - type: 'string', - }, - layout: { - type: 'string', - }, - }, } ); } ); @@ -250,14 +225,6 @@ describe( 'blocks', () => { category: 'common', title: 'block title', icon: 'block-default', - attributes: { - className: { - type: 'string', - }, - layout: { - type: 'string', - }, - }, }, ] ); const oldBlock = unregisterBlockType( 'core/test-block' ); @@ -268,14 +235,6 @@ describe( 'blocks', () => { category: 'common', title: 'block title', icon: 'block-default', - attributes: { - className: { - type: 'string', - }, - layout: { - type: 'string', - }, - }, } ); expect( getBlockTypes() ).toEqual( [] ); } ); @@ -318,14 +277,6 @@ describe( 'blocks', () => { category: 'common', title: 'block title', icon: 'block-default', - attributes: { - className: { - type: 'string', - }, - layout: { - type: 'string', - }, - }, } ); } ); @@ -339,14 +290,6 @@ describe( 'blocks', () => { category: 'common', title: 'block title', icon: 'block-default', - attributes: { - className: { - type: 'string', - }, - layout: { - type: 'string', - }, - }, } ); } ); } ); @@ -367,14 +310,6 @@ describe( 'blocks', () => { category: 'common', title: 'block title', icon: 'block-default', - attributes: { - className: { - type: 'string', - }, - layout: { - type: 'string', - }, - }, }, { name: 'core/test-block-with-settings', @@ -383,14 +318,6 @@ describe( 'blocks', () => { category: 'common', title: 'block title', icon: 'block-default', - attributes: { - className: { - type: 'string', - }, - layout: { - type: 'string', - }, - }, }, ] ); } ); diff --git a/blocks/api/test/serializer.js b/blocks/api/test/serializer.js index 345c3ef3cc2f7..f7c7c293b0889 100644 --- a/blocks/api/test/serializer.js +++ b/blocks/api/test/serializer.js @@ -16,7 +16,6 @@ import serialize, { getBlockContent, } from '../serializer'; import { - getBlockType, getBlockTypes, registerBlockType, unregisterBlockType, @@ -24,13 +23,10 @@ import { } from '../registration'; import { createBlock } from '../'; -// Todo: move the test to the inner-blocks folder -import InnerBlocks from '../../../editor/components/inner-blocks'; - describe( 'block serializer', () => { beforeAll( () => { - // Load all hooks that modify blocks - require( 'editor/hooks' ); + // Initialize the block store. + require( '../../store' ); } ); afterEach( () => { @@ -59,50 +55,6 @@ describe( 'block serializer', () => { { fruit: 'Bananas' } ); - expect( saved ).toBe( '
Bananas
' ); - } ); - - it( 'should use the namespace in the classname for non-core blocks', () => { - const saved = getSaveContent( - { - save: ( { attributes } ) => createElement( 'div', null, attributes.fruit ), - name: 'myplugin/fruit', - }, - { fruit: 'Bananas' } - ); - - expect( saved ).toBe( '
Bananas
' ); - } ); - - it( 'should include additional classes in block attributes', () => { - const saved = getSaveContent( - { - save: ( { attributes } ) => createElement( 'div', { - className: 'fruit', - }, attributes.fruit ), - name: 'myplugin/fruit', - }, - { - fruit: 'Bananas', - className: 'fresh', - } - ); - - expect( saved ).toBe( '
Bananas
' ); - } ); - - it( 'should not add a className if falsy', () => { - const saved = getSaveContent( - { - save: ( { attributes } ) => createElement( 'div', null, attributes.fruit ), - name: 'myplugin/fruit', - supports: { - className: false, - }, - }, - { fruit: 'Bananas' } - ); - expect( saved ).toBe( '
Bananas
' ); } ); } ); @@ -121,46 +73,7 @@ describe( 'block serializer', () => { { fruit: 'Bananas' } ); - expect( saved ).toBe( '
Bananas
' ); - } ); - - it( 'should return element as string, with inner blocks', () => { - registerBlockType( 'core/fruit', { - category: 'common', - - title: 'fruit', - - attributes: { - fruit: { - type: 'string', - }, - }, - - supports: { - className: false, - }, - - save( { attributes } ) { - return ( -
- { attributes.fruit } - -
- ); - }, - } ); - - const saved = getSaveContent( - getBlockType( 'core/fruit' ), - { fruit: 'Bananas' }, - [ createBlock( 'core/fruit', { fruit: 'Apples' } ) ], - ); - - expect( saved ).toBe( - '
Bananas\n' + - '
Apples
\n' + - '
' - ); + expect( saved ).toBe( '
Bananas
' ); } ); } ); } ); @@ -332,7 +245,6 @@ describe( 'block serializer', () => { return (

{ attributes.content } -

); }, @@ -347,7 +259,7 @@ describe( 'block serializer', () => { content: 'Ribs & Chicken', stuff: 'left & right -- but ', } ); - const expectedPostContent = '\n

Ribs & Chicken

\n'; + const expectedPostContent = '\n

Ribs & Chicken

\n'; expect( serialize( [ block ] ) ).toEqual( expectedPostContent ); expect( serialize( block ) ).toEqual( expectedPostContent ); @@ -366,27 +278,6 @@ describe( 'block serializer', () => { ); } ); - it( 'should force serialize for invalid block with inner blocks', () => { - const block = createBlock( - 'core/test-block', - { content: 'Invalid' }, - [ createBlock( 'core/test-block' ) ] - ); - - block.isValid = false; - block.originalContent = 'Original'; - - expect( serialize( block ) ).toEqual( - '\n' + - '

Invalid\n' + - '\t\n' + - '\t

\n' + - '\t\n' + - '

\n' + - '' - ); - } ); - it( 'should preserve content for crashing block', () => { const block = createBlock( 'core/test-block', { content: 'Incorrect', diff --git a/editor/components/inner-blocks/test/__snapshots__/index.js.snap b/editor/components/inner-blocks/test/__snapshots__/index.js.snap new file mode 100644 index 0000000000000..abbf1a84bfdce --- /dev/null +++ b/editor/components/inner-blocks/test/__snapshots__/index.js.snap @@ -0,0 +1,17 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`InnerBlocks should force serialize for invalid block with inner blocks 1`] = ` +" +

Invalid + +

+ +

+" +`; + +exports[`InnerBlocks should return element as string, with inner blocks 1`] = ` +"
Bananas +
Apples
+
" +`; diff --git a/editor/components/inner-blocks/test/index.js b/editor/components/inner-blocks/test/index.js new file mode 100644 index 0000000000000..1472b8817b9cd --- /dev/null +++ b/editor/components/inner-blocks/test/index.js @@ -0,0 +1,111 @@ +/** + * WordPress dependencies + */ +import { + createBlock, + getBlockType, + getBlockTypes, + setUnknownTypeHandlerName, + getSaveElement, + registerBlockType, + serialize, + unregisterBlockType, +} from '@wordpress/blocks'; +import { renderToString } from '@wordpress/element'; + +/** + * Internal dependencies + */ +import InnerBlocks from '../'; + +describe( 'InnerBlocks', () => { + afterEach( () => { + setUnknownTypeHandlerName( undefined ); + getBlockTypes().forEach( ( block ) => { + unregisterBlockType( block.name ); + } ); + } ); + + it( 'should return element as string, with inner blocks', () => { + registerBlockType( 'core/fruit', { + category: 'common', + + title: 'fruit', + + attributes: { + fruit: { + type: 'string', + }, + }, + + supports: { + className: false, + }, + + save( { attributes } ) { + return ( +
+ { attributes.fruit } + +
+ ); + }, + } ); + + const saved = renderToString( + getSaveElement( + getBlockType( 'core/fruit' ), + { fruit: 'Bananas' }, + [ createBlock( 'core/fruit', { fruit: 'Apples' } ) ], + ) + ); + + expect( saved ).toMatchSnapshot(); + } ); + + it( 'should force serialize for invalid block with inner blocks', () => { + const blockType = { + attributes: { + throw: { + type: 'boolean', + }, + defaulted: { + type: 'boolean', + default: false, + }, + content: { + type: 'string', + source: 'text', + }, + stuff: { + type: 'string', + }, + }, + save( { attributes } ) { + if ( attributes.throw ) { + throw new Error(); + } + + return ( +

+ { attributes.content } + +

+ ); + }, + category: 'common', + title: 'block title', + }; + registerBlockType( 'core/test-block', blockType ); + const block = createBlock( + 'core/test-block', + { content: 'Invalid' }, + [ createBlock( 'core/test-block' ) ] + ); + + block.isValid = false; + block.originalContent = 'Original'; + + expect( serialize( block ) ).toMatchSnapshot(); + } ); +} ); diff --git a/test/integration/is-valid-block.spec.js b/test/integration/is-valid-block.spec.js new file mode 100644 index 0000000000000..db647e0bbe387 --- /dev/null +++ b/test/integration/is-valid-block.spec.js @@ -0,0 +1,59 @@ +/** + * External dependencies + */ +import { isValidBlock } from '@wordpress/blocks'; +import { createElement } from '@wordpress/element'; + +describe( 'isValidBlock', () => { + beforeAll( () => { + // Load all hooks that modify blocks + require( 'editor/hooks' ); + } ); + + it( 'should use the namespace in the classname for non-core blocks', () => { + const valid = isValidBlock( + '
Bananas
', + { + save: ( { attributes } ) => createElement( 'div', null, attributes.fruit ), + name: 'myplugin/fruit', + }, + { fruit: 'Bananas' } + ); + + expect( valid ).toBe( true ); + } ); + + it( 'should include additional classes in block attributes', () => { + const valid = isValidBlock( + '
Bananas
', + { + save: ( { attributes } ) => createElement( 'div', { + className: 'fruit', + }, attributes.fruit ), + name: 'myplugin/fruit', + }, + { + fruit: 'Bananas', + className: 'fresh', + } + ); + + expect( valid ).toBe( true ); + } ); + + it( 'should not add a className if falsy', () => { + const valid = isValidBlock( + '
Bananas
', + { + save: ( { attributes } ) => createElement( 'div', null, attributes.fruit ), + name: 'myplugin/fruit', + supports: { + className: false, + }, + }, + { fruit: 'Bananas' } + ); + + expect( valid ).toBe( true ); + } ); +} );