Skip to content

Commit

Permalink
Tests: Add integration tests for block schema validation (#36351)
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Nov 10, 2021
1 parent 4ccab03 commit 24ff8ad
Show file tree
Hide file tree
Showing 11 changed files with 398 additions and 1,364 deletions.
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
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
@@ -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
@@ -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
@@ -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
@@ -1,4 +1,5 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "core/comment-template",
"title": "Comment Template",
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/comments-query-loop/block.json
@@ -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
@@ -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
@@ -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
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
@@ -0,0 +1,42 @@
/**
* External dependencies
*/
import Ajv from 'ajv-draft-04';
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 );
}
);
} );

0 comments on commit 24ff8ad

Please sign in to comment.