From f52f2f9d94d60d4a7006fc691587b55e9df8ee65 Mon Sep 17 00:00:00 2001 From: Matthew Reishus Date: Wed, 9 Oct 2024 19:04:25 +0000 Subject: [PATCH] add test --- .../build-blocks-manifest.js.snap | 122 ++++++++++++++++++ .../scripts/test/build-blocks-manifest.js | 69 ++++++++++ .../input/custom-header/block.json | 33 +++++ .../input/image-gallery/block.json | 50 +++++++ .../input/simple-button/block.json | 16 +++ 5 files changed, 290 insertions(+) create mode 100644 packages/scripts/scripts/test/__snapshots__/build-blocks-manifest.js.snap create mode 100644 packages/scripts/scripts/test/build-blocks-manifest.js create mode 100644 packages/scripts/scripts/test/fixtures/build-blocks-manifest/input/custom-header/block.json create mode 100644 packages/scripts/scripts/test/fixtures/build-blocks-manifest/input/image-gallery/block.json create mode 100644 packages/scripts/scripts/test/fixtures/build-blocks-manifest/input/simple-button/block.json diff --git a/packages/scripts/scripts/test/__snapshots__/build-blocks-manifest.js.snap b/packages/scripts/scripts/test/__snapshots__/build-blocks-manifest.js.snap new file mode 100644 index 00000000000000..7e2ccaf6b5ffec --- /dev/null +++ b/packages/scripts/scripts/test/__snapshots__/build-blocks-manifest.js.snap @@ -0,0 +1,122 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`build-blocks-manifest script should generate expected blocks manifest 1`] = ` +" array( + '$schema' => 'https://schemas.wp.org/trunk/block.json', + 'apiVersion' => 2, + 'name' => 'my-plugin/custom-header', + 'title' => 'Custom Header', + 'category' => 'text', + 'icon' => 'heading', + 'description' => 'A custom header block with color options.', + 'attributes' => array( + 'content' => array( + 'type' => 'string', + 'source' => 'html', + 'selector' => 'h2' + ), + 'textColor' => array( + 'type' => 'string' + ), + 'backgroundColor' => array( + 'type' => 'string' + ) + ), + 'supports' => array( + 'html' => false, + 'color' => array( + 'background' => true, + 'text' => true + ) + ), + 'textdomain' => 'my-plugin', + 'editorScript' => 'file:./index.js', + 'editorStyle' => 'file:./index.css', + 'style' => 'file:./style-index.css' + ), + 'image-gallery' => array( + '$schema' => 'https://schemas.wp.org/trunk/block.json', + 'apiVersion' => 2, + 'name' => 'my-plugin/image-gallery', + 'title' => 'Image Gallery', + 'category' => 'media', + 'icon' => 'format-gallery', + 'description' => 'An image gallery block with customizable layout.', + 'attributes' => array( + 'images' => array( + 'type' => 'array', + 'default' => array( + + ), + 'source' => 'query', + 'selector' => 'img', + 'query' => array( + 'url' => array( + 'type' => 'string', + 'source' => 'attribute', + 'attribute' => 'src' + ), + 'alt' => array( + 'type' => 'string', + 'source' => 'attribute', + 'attribute' => 'alt', + 'default' => '' + ), + 'id' => array( + 'type' => 'number', + 'source' => 'attribute', + 'attribute' => 'data-id' + ) + ) + ), + 'columns' => array( + 'type' => 'number', + 'default' => 3 + ), + 'imageCrop' => array( + 'type' => 'boolean', + 'default' => true + ) + ), + 'supports' => array( + 'align' => array( + 'wide', + 'full' + ) + ), + 'textdomain' => 'my-plugin', + 'editorScript' => 'file:./index.js', + 'editorStyle' => 'file:./index.css', + 'style' => 'file:./style-index.css' + ), + 'simple-button' => array( + '$schema' => 'https://schemas.wp.org/trunk/block.json', + 'apiVersion' => 2, + 'name' => 'my-plugin/simple-button', + 'title' => 'Simple Button', + 'category' => 'design', + 'icon' => 'button', + 'description' => 'A simple button block.', + 'supports' => array( + 'html' => false + ), + 'textdomain' => 'my-plugin', + 'editorScript' => 'file:./index.js', + 'editorStyle' => 'file:./index.css', + 'style' => 'file:./style-index.css' + ) +); +" +`; + +exports[`build-blocks-manifest script should handle empty input directory 1`] = ` +" { + beforeAll( () => { + if ( ! fs.existsSync( outputPath ) ) { + fs.mkdirSync( outputPath, { recursive: true } ); + } + rimraf.sync( outputPath ); + } ); + + afterAll( () => { + rimraf.sync( outputPath ); + } ); + + it( 'should generate expected blocks manifest', () => { + const inputDir = path.join( fixturesPath, 'input' ); + const outputFile = path.join( outputPath, 'blocks-manifest.php' ); + + // Run the build-blocks-manifest script + const scriptPath = path.resolve( + __dirname, + '..', + 'build-blocks-manifest.js' + ); + execSync( + `node ${ scriptPath } --input=${ inputDir } --output=${ outputFile }` + ); + + const generatedContent = fs.readFileSync( outputFile, 'utf8' ); + expect( generatedContent ).toMatchSnapshot(); + } ); + + it( 'should handle empty input directory', () => { + const emptyInputDir = path.join( fixturesPath, 'empty-input' ); + const outputFile = path.join( outputPath, 'empty-blocks-manifest.php' ); + + // Run the build-blocks-manifest script with empty input + const scriptPath = path.resolve( + __dirname, + '..', + 'build-blocks-manifest.js' + ); + const result = execSync( + `node ${ scriptPath } --input=${ emptyInputDir } --output=${ outputFile }`, + { encoding: 'utf8' } + ); + + // Check if warning message is displayed + expect( result ).toContain( 'WARNING' ); + expect( result ).toContain( 'No block.json files were found' ); + + // Read the generated file, compare with expected output + const generatedContent = fs.readFileSync( outputFile, 'utf8' ); + expect( generatedContent ).toMatchSnapshot(); + } ); +} ); diff --git a/packages/scripts/scripts/test/fixtures/build-blocks-manifest/input/custom-header/block.json b/packages/scripts/scripts/test/fixtures/build-blocks-manifest/input/custom-header/block.json new file mode 100644 index 00000000000000..056bffbe28b184 --- /dev/null +++ b/packages/scripts/scripts/test/fixtures/build-blocks-manifest/input/custom-header/block.json @@ -0,0 +1,33 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 2, + "name": "my-plugin/custom-header", + "title": "Custom Header", + "category": "text", + "icon": "heading", + "description": "A custom header block with color options.", + "attributes": { + "content": { + "type": "string", + "source": "html", + "selector": "h2" + }, + "textColor": { + "type": "string" + }, + "backgroundColor": { + "type": "string" + } + }, + "supports": { + "html": false, + "color": { + "background": true, + "text": true + } + }, + "textdomain": "my-plugin", + "editorScript": "file:./index.js", + "editorStyle": "file:./index.css", + "style": "file:./style-index.css" +} diff --git a/packages/scripts/scripts/test/fixtures/build-blocks-manifest/input/image-gallery/block.json b/packages/scripts/scripts/test/fixtures/build-blocks-manifest/input/image-gallery/block.json new file mode 100644 index 00000000000000..9c84fff4338704 --- /dev/null +++ b/packages/scripts/scripts/test/fixtures/build-blocks-manifest/input/image-gallery/block.json @@ -0,0 +1,50 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 2, + "name": "my-plugin/image-gallery", + "title": "Image Gallery", + "category": "media", + "icon": "format-gallery", + "description": "An image gallery block with customizable layout.", + "attributes": { + "images": { + "type": "array", + "default": [], + "source": "query", + "selector": "img", + "query": { + "url": { + "type": "string", + "source": "attribute", + "attribute": "src" + }, + "alt": { + "type": "string", + "source": "attribute", + "attribute": "alt", + "default": "" + }, + "id": { + "type": "number", + "source": "attribute", + "attribute": "data-id" + } + } + }, + "columns": { + "type": "number", + "default": 3 + }, + "imageCrop": { + "type": "boolean", + "default": true + } + }, + "supports": { + "align": [ "wide", "full" ] + }, + "textdomain": "my-plugin", + "editorScript": "file:./index.js", + "editorStyle": "file:./index.css", + "style": "file:./style-index.css" +} diff --git a/packages/scripts/scripts/test/fixtures/build-blocks-manifest/input/simple-button/block.json b/packages/scripts/scripts/test/fixtures/build-blocks-manifest/input/simple-button/block.json new file mode 100644 index 00000000000000..23e607c411fbdc --- /dev/null +++ b/packages/scripts/scripts/test/fixtures/build-blocks-manifest/input/simple-button/block.json @@ -0,0 +1,16 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 2, + "name": "my-plugin/simple-button", + "title": "Simple Button", + "category": "design", + "icon": "button", + "description": "A simple button block.", + "supports": { + "html": false + }, + "textdomain": "my-plugin", + "editorScript": "file:./index.js", + "editorStyle": "file:./index.css", + "style": "file:./style-index.css" +}