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

Validation: Log debugging messages for invalid blocks #3522

Merged
merged 1 commit into from
Nov 20, 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
73 changes: 28 additions & 45 deletions blocks/api/test/parser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { noop } from 'lodash';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -32,6 +27,18 @@ describe( 'block parser', () => {
title: 'block title',
};

const unknownBlockSettings = {
category: 'common',
title: 'unknown block',
attributes: {
content: {
type: 'string',
source: 'html',
},
},
save: ( { attributes } ) => attributes.content,
};

afterEach( () => {
setUnknownTypeHandlerName( undefined );
getBlockTypes().forEach( ( block ) => {
Expand Down Expand Up @@ -163,7 +170,7 @@ describe( 'block parser', () => {

const block = createBlockWithFallback(
'core/test-block',
'content',
'Bananas',
{ fruit: 'Bananas' }
);
expect( block.name ).toEqual( 'core/test-block' );
Expand All @@ -173,49 +180,35 @@ describe( 'block parser', () => {
it( 'should create the requested block with no attributes if it exists', () => {
registerBlockType( 'core/test-block', defaultBlockSettings );

const block = createBlockWithFallback( 'core/test-block', 'content' );
const block = createBlockWithFallback( 'core/test-block', '' );
expect( block.name ).toEqual( 'core/test-block' );
expect( block.attributes ).toEqual( {} );
} );

it( 'should fall back to the unknown type handler for unknown blocks if present', () => {
registerBlockType( 'core/unknown-block', {
category: 'common',
title: 'unknown block',
attributes: {
content: {
type: 'string',
source: 'html',
},
fruit: {
type: 'string',
},
},
save: ( { attributes } ) => attributes.content,
} );
registerBlockType( 'core/unknown-block', unknownBlockSettings );
setUnknownTypeHandlerName( 'core/unknown-block' );

const block = createBlockWithFallback(
'core/test-block',
'content',
'Bananas',
{ fruit: 'Bananas' }
);
expect( block.name ).toBe( 'core/unknown-block' );
expect( block.attributes.fruit ).toBe( 'Bananas' );
expect( block.attributes.content ).toContain( 'wp:test-block' );
} );

it( 'should fall back to the unknown type handler if block type not specified', () => {
registerBlockType( 'core/unknown-block', defaultBlockSettings );
registerBlockType( 'core/unknown-block', unknownBlockSettings );
setUnknownTypeHandlerName( 'core/unknown-block' );

const block = createBlockWithFallback( null, 'content' );
expect( block.name ).toEqual( 'core/unknown-block' );
expect( block.attributes ).toEqual( {} );
expect( block.attributes ).toEqual( { content: 'content' } );
} );

it( 'should not create a block if no unknown type handler', () => {
const block = createBlockWithFallback( 'core/test-block', 'content' );
const block = createBlockWithFallback( 'core/test-block', '' );
expect( block ).toBeUndefined();
} );
} );
Expand All @@ -232,7 +225,7 @@ describe( 'block parser', () => {
url: { type: 'string' },
chicken: { type: 'string' },
},
save: noop,
save: ( { attributes } ) => attributes.content,
category: 'common',
title: 'test block',
} );
Expand Down Expand Up @@ -268,7 +261,7 @@ describe( 'block parser', () => {
source: 'text',
},
},
save: noop,
save: ( { attributes } ) => attributes.content,
category: 'common',
title: 'test block',
} );
Expand All @@ -291,7 +284,7 @@ describe( 'block parser', () => {
registerBlockType( 'core/test-block', defaultBlockSettings );

const parsed = parse(
'<!-- wp:test-block -->\nRibs\n<!-- /wp:test-block -->'
'<!-- wp:test-block {"fruit":"Bananas"} -->\nBananas\n<!-- /wp:test-block -->'
);

expect( parsed ).toHaveLength( 1 );
Expand All @@ -304,7 +297,7 @@ describe( 'block parser', () => {
setUnknownTypeHandlerName( 'core/unknown-block' );

const parsed = parse(
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<!-- wp:test-block {"fruit":"Bananas"} -->\nBananas\n<!-- /wp:test-block -->' +
'<p>Broccoli</p>' +
'<!-- wp:core/unknown/block -->Ribs<!-- /wp:core/unknown/block -->'
);
Expand All @@ -314,12 +307,12 @@ describe( 'block parser', () => {

it( 'should parse the post content, using unknown block handler', () => {
registerBlockType( 'core/test-block', defaultBlockSettings );
registerBlockType( 'core/unknown-block', defaultBlockSettings );
registerBlockType( 'core/unknown-block', unknownBlockSettings );

setUnknownTypeHandlerName( 'core/unknown-block' );

const parsed = parse(
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<!-- wp:test-block {"fruit":"Bananas"} -->\nBananas\n<!-- /wp:test-block -->' +
'<p>Broccoli</p>' +
'<!-- wp:core/unknown-block -->Ribs<!-- /wp:core/unknown-block -->'
);
Expand All @@ -334,25 +327,15 @@ describe( 'block parser', () => {

it( 'should parse the post content, including raw HTML at each end', () => {
registerBlockType( 'core/test-block', defaultBlockSettings );
registerBlockType( 'core/unknown-block', {
attributes: {
content: {
type: 'string',
source: 'html',
},
},
save: noop,
category: 'common',
title: 'unknown block',
} );
registerBlockType( 'core/unknown-block', unknownBlockSettings );

setUnknownTypeHandlerName( 'core/unknown-block' );

const parsed = parse(
'<p>Cauliflower</p>' +
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<!-- wp:test-block {"fruit":"Bananas"} -->\nBananas\n<!-- /wp:test-block -->' +
'\n<p>Broccoli</p>\n' +
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<!-- wp:test-block {"fruit":"Bananas"} -->\nBananas\n<!-- /wp:test-block -->' +
'<p>Romanesco</p>'
);

Expand Down
49 changes: 40 additions & 9 deletions blocks/api/test/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ describe( 'validation', () => {
title: 'block title',
};

/* eslint-disable no-console */
function expectError() {
expect( console.error ).toHaveBeenCalled();
console.error.mockClear();
}

function expectWarning() {
expect( console.warn ).toHaveBeenCalled();
console.warn.mockClear();
}
/* eslint-enable no-console */

afterEach( () => {
setUnknownTypeHandlerName( undefined );
getBlockTypes().forEach( ( block ) => {
Expand Down Expand Up @@ -102,6 +114,7 @@ describe( 'validation', () => {
{ chars: 'a \n c \t b ' },
);

expectWarning();
expect( isEqual ).toBe( false );
} );

Expand Down Expand Up @@ -178,7 +191,7 @@ describe( 'validation', () => {
expect( isEqual ).toBe( true );
} );

it( 'returns true if not same style', () => {
it( 'returns false if not same style', () => {
const isEqual = isEqualAttributesOfName.style(
'background-image: url( "https://wordpress.org/img.png" ); color: red;',
'color: red; font-size: 13px; background-image: url(\'https://wordpress.org/img.png\');'
Expand All @@ -201,6 +214,7 @@ describe( 'validation', () => {
]
);

expectWarning();
expect( isEqual ).toBe( false );
} );

Expand Down Expand Up @@ -228,6 +242,7 @@ describe( 'validation', () => {
{ tagName: 'section' }
);

expectWarning();
expect( isEqual ).toBe( false );
} );

Expand All @@ -248,6 +263,7 @@ describe( 'validation', () => {
}
);

expectWarning();
expect( isEqual ).toBe( false );
} );

Expand Down Expand Up @@ -306,6 +322,7 @@ describe( 'validation', () => {
'<div>Hello <span class="a">World!</span></div>'
);

expectWarning();
expect( isEquivalent ).toBe( false );
} );

Expand All @@ -324,6 +341,7 @@ describe( 'validation', () => {
'<div>Hello'
);

expectWarning();
expect( isEquivalent ).toBe( false );
} );

Expand All @@ -333,6 +351,7 @@ describe( 'validation', () => {
'<div>Hello</div>'
);

expectWarning();
expect( isEquivalent ).toBe( false );
} );

Expand Down Expand Up @@ -369,6 +388,7 @@ describe( 'validation', () => {
'<input>'
);

expectWarning();
expect( isEquivalent ).toBe( false );
} );

Expand All @@ -378,6 +398,7 @@ describe( 'validation', () => {
'<div>'
);

expectWarning();
expect( isEquivalent ).toBe( false );
} );

Expand All @@ -387,44 +408,54 @@ describe( 'validation', () => {
'<div>'
);

expectWarning();
expect( isEquivalent ).toBe( false );
} );
} );

describe( 'isValidBlock()', () => {
it( 'returns false is block is not valid', () => {
it( 'returns false if block is not valid', () => {
registerBlockType( 'core/test-block', defaultBlockSettings );

expect( isValidBlock(
const isValid = isValidBlock(
'Apples',
getBlockType( 'core/test-block' ),
{ fruit: 'Bananas' }
) ).toBe( false );
);

expectWarning();
expectError();
expect( isValid ).toBe( false );
} );

it( 'returns false is error occurs while generating block save', () => {
it( 'returns false if error occurs while generating block save', () => {
registerBlockType( 'core/test-block', {
...defaultBlockSettings,
save() {
throw new Error();
},
} );

expect( isValidBlock(
const isValid = isValidBlock(
'Bananas',
getBlockType( 'core/test-block' ),
{ fruit: 'Bananas' }
) ).toBe( false );
);

expectError();
expect( isValid ).toBe( false );
} );

it( 'returns true is block is valid', () => {
registerBlockType( 'core/test-block', defaultBlockSettings );

expect( isValidBlock(
const isValid = isValidBlock(
'Bananas',
getBlockType( 'core/test-block' ),
{ fruit: 'Bananas' }
) ).toBe( true );
);

expect( isValid ).toBe( true );
} );
} );
} );
Loading