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

Tests: Add integration tests with core blocks schema validation #36351

Merged
merged 1 commit into from
Nov 10, 2021
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
1,683 changes: 334 additions & 1,349 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@
"@wordpress/readable-js-assets-webpack-plugin": "file:packages/readable-js-assets-webpack-plugin",
"@wordpress/scripts": "file:packages/scripts",
"@wordpress/stylelint-config": "file:packages/stylelint-config",
"ajv": "8.7.1",
"ajv-draft-04": "1.0.0",
"appium": "1.22.0",
"babel-jest": "26.6.3",
"babel-loader": "8.2.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "core/comment-author-avatar",
"title": "Comment Author Avatar",
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/comment-edit-link/block.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "core/comment-edit-link",
"title": "Comment Edit Link",
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/comment-reply-link/block.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "core/comment-reply-link",
"title": "Comment Reply Link",
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/comment-template/block.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "core/comment-template",
"title": "Comment Template",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "core/comments-query-loop",
"title": "Comments Query Loop",
Expand Down
8 changes: 2 additions & 6 deletions packages/block-library/src/navigation-area/block.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "core/navigation-area",
"title": "Navigation Area",
"category": "theme",
"description": "Define a navigation area for your theme. The navigation block associated with this area will be automatically displayed.",
"keywords": [
"menu",
"navigation",
"links",
"location"
],
"keywords": [ "menu", "navigation", "links", "location" ],
"textdomain": "default",
"attributes": {
"area": {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/pattern/block.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "core/pattern",
"title": "Pattern",
Expand Down
21 changes: 12 additions & 9 deletions schemas/json/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,17 @@
"type": "array",
"description": "An attribute can be defined as one of a fixed set of values. This is specified by an enum, which contains an array of allowed values:",
"items": {
"type": "string"
"oneOf": [
{
"type": "boolean"
},
{
"type": "number"
},
{
"type": "string"
}
]
}
},
"source": {
Expand Down Expand Up @@ -160,14 +170,7 @@
"description": "TA block attribute can contain a default value, which will be used if the type and source do not match anything within the block content.\n\nThe value is provided by the default field, and the value should match the expected format of the attribute."
}
},
"anyOf": [
{
"required": [ "type" ]
},
{
"required": [ "enum" ]
}
]
"required": [ "type" ]
}
},
"additionalProperties": false
Expand Down
42 changes: 42 additions & 0 deletions test/integration/blocks-schema.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* External dependencies
*/
import Ajv from 'ajv-draft-04';
Copy link
Member Author

Choose a reason for hiding this comment

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

I had to install ajv-draft-04 to make everything work with the defined schema:
http://json-schema.org/draft-04/schema#

Out of the box, Ajv supports JSON Schema (drafts 04, 06, 07, 2019-09 and 2020-12) and JSON Type Definition (RFC8927 (opens new window)).

However, in practice, only JSON Schema draft-06/07/2019-09/2020-12 standards work with the core ajv package. Is there any reason we picked draft-04?

Copy link
Member

Choose a reason for hiding this comment

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

@gziolo This is all the context I have. We briefly talked about it in the initial review of the block.json introduction PR.

SchemaStore/schemastore#1879 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess it’s fine to leave it as is. Thank you for sharing the context 💯

Copy link
Contributor

Choose a reason for hiding this comment

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

SchemaStore has some best practices listed in their contributing docs that I was also basing my decisions off of. https://github.com/SchemaStore/schemastore/blob/master/CONTRIBUTING.md#best-practices

import glob from 'fast-glob';
import path from 'path';

/**
* Internal dependencies
*/
import blockSchema from '../../schemas/json/block.json';

describe( 'block.json schema', () => {
const blockFolders = glob.sync( 'packages/block-library/src/*', {
onlyDirectories: true,
ignore: [ 'packages/block-library/src/utils' ],
} );
const testData = blockFolders.map( ( blockFolder ) => [
'core/' + path.basename( blockFolder ),
path.join( blockFolder, 'block.json' ),
] );
const ajv = new Ajv();

test( 'found block folders', () => {
expect( blockFolders.length ).toBeGreaterThan( 0 );
} );

test.each( testData )(
'validates schema for `%s`',
( blockName, filepath ) => {
// We want to validate the block.json file using the local schema.
const { $schema, ...blockMetadata } = require( filepath );

expect( $schema ).toBe( 'https://schemas.wp.org/trunk/block.json' );

const result =
ajv.validate( blockSchema, blockMetadata ) || ajv.errors;

expect( result ).toBe( true );
}
);
} );