Skip to content

Commit

Permalink
update snapshots and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed Mar 27, 2024
1 parent 1160185 commit fac2194
Show file tree
Hide file tree
Showing 18 changed files with 3,806 additions and 7,036 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./dist
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.admin-class { color: blue; }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.test { color: red; }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import '../css/admin-styles.css';

export const admin = () => {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-disable*/
import '../css/frontend.css';
import * as React from 'react';
import ReactDOM from 'react-dom';
import { useState } from 'react';

const App = () => {
const [state] = useState(1);

return <p>This is a react app {state}</p>;
};

ReactDOM.render(<App />, document.getElementById('root'));

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"title": "Example Block",
"description": "An Example Block",
"textdomain": "tenup-scaffold",
"name": "tenup/example",
"icon": "feedback",
"category": "tenup-scaffold-blocks",
"attributes": {
"title": {
"type": "string"
}
},
"example": {
"attributes": {
"title": "Example Block"
}
},
"supports": {
"align": false,
"alignWide": false,
"anchor": false,
"color": {
"gradients": false,
"background": false,
"text": false
},
"customClassName": false,
"defaultStylePicker": false,
"typography": {
"fontSize": false,
"lineHeight": true
},
"html": false,
"inserter": true,
"multiple": true,
"reusable": false,
"spacing": {
"padding": false
},
"interactivity": true
},
"editorScript": "file:./index.js",
"viewScriptModule": "file:./view-module.js",
"script": "file:./script.js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// import './editor-styles.css';

const ExampleBlockEdit = () => {};
export default ExampleBlockEdit;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Editor specific styles */
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable */
import { registerBlockType } from '@wordpress/blocks';

import edit from './edit';
import save from './save';
import block from './block.json';

registerBlockType(block, {
edit,
save,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* See https://wordpress.org/gutenberg/handbook/designers-developers/developers/block-api/block-edit-save/#save
*
* @returns {null} Dynamic blocks do not save the HTML.
*/
const ExampleBlockSave = () => null;

export default ExampleBlockSave;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// eslint-disable-next-line
import { store } from '@wordpress/interactivity';

store('example', {
actions: {
toggle: () => {
// eslint-disable-next-line no-console
console.log('Toggle Action');
},
},
});

// eslint-disable-next-line no-console
console.log('View Module Loaded');
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "test-build-project-modules",
"10up-toolkit": {
"entry": {
"admin": "./__fixtures__/assets/js/admin.js",
"frontend": "./__fixtures__/assets/js/frontend.js"
},
"paths": {
"blocksDir": "./__fixtures__/includes/blocks"
}
}
}
61 changes: 61 additions & 0 deletions packages/toolkit/__tests__/build-project-use-build-modules/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-disable import/no-extraneous-dependencies */
import spawn from 'cross-spawn';
import fs from 'fs';
import path from 'path';

describe('build a project (withb block-modules)', () => {
beforeAll(() => {
spawn.sync('node', ['../../scripts/build', '--block-modules'], {
cwd: __dirname,
});
});

it('builds and compiles js and css', async () => {
expect(fs.existsSync(path.join(__dirname, 'dist', 'js', 'admin.js'))).toBeTruthy();
expect(fs.existsSync(path.join(__dirname, 'dist', 'js', 'admin.asset.php'))).toBeTruthy();
expect(fs.existsSync(path.join(__dirname, 'dist', 'js', 'frontend.js'))).toBeTruthy();
expect(
fs.existsSync(path.join(__dirname, 'dist', 'js', 'frontend.asset.php')),
).toBeTruthy();
expect(fs.existsSync(path.join(__dirname, 'dist', 'css', 'frontend.css'))).toBeTruthy();
});

it('adds react dependencies to .asset.php files', () => {
const frontendAssetPHP = fs
.readFileSync(path.join(__dirname, 'dist', 'js', 'frontend.asset.php'))
.toString();

expect(frontendAssetPHP).toMatch('wp-element');
expect(frontendAssetPHP).toMatch('react-dom');
expect(frontendAssetPHP).toMatch('react');

const viewModuleAsset = fs
.readFileSync(
path.join(__dirname, 'dist', 'blocks', 'example', 'view-module.asset.php'),
)
.toString();

expect(viewModuleAsset).toMatch('@wordpress/interactivity');
expect(viewModuleAsset).toMatch("'type' => 'module'");
});

it('builds blocks with modules for vieewScriptModule', () => {
expect(
fs.existsSync(path.join(__dirname, 'dist', 'blocks', 'example', 'block.json')),
).toBeTruthy();

expect(
fs.existsSync(path.join(__dirname, 'dist', 'blocks', 'example', 'index.js')),
).toBeTruthy();

const viewModuleFile = path.join(__dirname, 'dist', 'blocks', 'example', 'view-module.js');

expect(
fs.existsSync(path.join(__dirname, 'dist', 'blocks', 'example', 'view-module.js')),
).toBeTruthy();

const viewModuleFileContents = fs.readFileSync(viewModuleFile).toString();

expect(viewModuleFileContents).toMatch(/import \* as .* from "@wordpress\/interactivity";/);
});
});
Loading

0 comments on commit fac2194

Please sign in to comment.