Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mreishus committed Oct 9, 2024
1 parent e24747e commit f52f2f9
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`build-blocks-manifest script should generate expected blocks manifest 1`] = `
"<?php
// This file is generated. Do not modify it manually.
return array(
'custom-header' => 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`] = `
"<?php
// This file is generated. Do not modify it manually.
return array(
);
"
`;
69 changes: 69 additions & 0 deletions packages/scripts/scripts/test/build-blocks-manifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* External dependencies
*/
const fs = require( 'fs' );
const path = require( 'path' );
const { execSync } = require( 'child_process' );
const rimraf = require( 'rimraf' );

const fixturesPath = path.join(
__dirname,
'fixtures',
'build-blocks-manifest'
);
const outputPath = path.join( __dirname, 'build', 'test-blocks-manifest' );

describe( 'build-blocks-manifest script', () => {
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();
} );
} );
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit f52f2f9

Please sign in to comment.