Skip to content

Instantly share code, notes, and snippets.

@stevector
Forked from Shelob9/1.js
Created July 25, 2019 21:40
Show Gist options
  • Save stevector/d9c6ab76290ceedc23887e290df59cb0 to your computer and use it in GitHub Desktop.
Save stevector/d9c6ab76290ceedc23887e290df59cb0 to your computer and use it in GitHub Desktop.
//do not test like this please!
test("Form component value", () => {
//Props with sample data
const props = {
defaultValue: "Food",
className: "wp-blocks-tooths",
id: "something"
};
//Render component
const component = TestRenderer.create(<Form {...props} />);
//Get root node
const testInstance = component.root;
//Find input
const input = testInstance.findByProps({ className: "isoblock-field" }).
expect(
input.props.value
).toEqual("Food");
});
//use snapshots!
test("Form component", () => {
//Props with sample data
const props = {
defaultValue: "Tooths",
className: "wp-blocks-tooths",
id: "something"
};
const component = TestRenderer.create(<Form {...props} />);
expect(component.toJSON()).toMatchSnapshot();
});
//Functions to test:
import { save, edit } from "./index";
//Import a renderer to test with
import TestRenderer from "react-test-renderer";
//We will use this to make edit/save rendereable
import { createElement } from "@wordpress/element";
test("Block edit callback", () => {
});
test("Block save callback", () => {
});
test("Block edit callback", () => {
//Props with sample data
const props = {
attributes: {
defaultValue: "Faces",
blockId: ""
},
className: "wp-blocks-tooths",
clientId: "something",
setAttributes: jest.fn()
};
const component = TestRenderer.create(createElement(edit, props));
expect(component.toJSON()).toMatchSnapshot();
});
test("Block save callback", () => {
//Props with sample data
const props = {
attributes: {
defaultValue: "Faces",
blockId: ""
},
className: "wp-blocks-tooths",
clientId: "something"
};
const component = TestRenderer.create(createElement(save, props));
expect(component.toJSON()).toMatchSnapshot();
});
class BlockEditorWithOutWordPress extends React.Component {
constructor(props) {
super(props);
//Setup state to match attributes of block
this.state = {
defaultValue: "Food"
};
//Create setAttributes to update state
const setAttributes = attributes => this.setState(attributes);
}
//Render editor of block
render() {
return (
<div>
{React.createElement(edit, {
setAttributes: this.setAttributes,
attributes: this.state
})}
</div>
);
}
}
import FormPreview form "..."
const Editor = () => <div>Editor Coming Soon</div>.
export const FormEditor = ({form,onChange}) => {
return (<div><FormPreview form={form}><Editor {...{form,onChange}} /></div>
}
registerBlockType(name, {
title: "Hydrate Demo",
attributes,
category: "common",
edit(props) => {
//...
},
save (props){
//...
};
});
/**
* Edit callback for block
*/
export const edit = ({ attributes, setAttributes, className, clientId }) => {
const { defaultValue, blockId } = attributes;
const setDefaultValue = defaultValue => setAttributes({ defaultValue });
const setBlockId = blockId => setAttributes({ blockId });
if (clientId !== blockId) {
setBlockId(clientId);
}
return (
<div>
<InspectorControls>
<TextControl
onChange={setDefaultValue}
label={"Default Value For Form Field"}
value={defaultValue}
/>
</InspectorControls>
<Form
defaultValue={defaultValue}
className={className}
id={clientId}
onChange={event => {
console.log(event);
setDefaultValue(event.target.value);
}}
/>
</div>
);
};
/**
* Save callback for block
*/
export const save = props => {
const { attributes, className, clientId } = props;
const { defaultValue, blockId } = attributes;
return (
<Form defaultValue={defaultValue} className={className} id={blockId} />
);
};
const name = "josh/isoblock";
registerBlockType(name, {
title: "Hydrate Demo",
attributes,
category: "common",
edit,
save
});
//Import component to test
import { Form } from "./Form";
//Import a renderer to test with
import TestRenderer from "react-test-renderer";
test("Form component", () => {
//Do some tests here
});
export const A = () => <div>A</div>;
import {A} from './A'
export const B = () => <div><p>B</p><A/></div>;
function increaseByFive(number) {
return number + 5;
}
test('It works with negative numbers', () => {
expect(increaseByFive(-10)).toBe(5);
});
# Install with Yarn:
yarn add react-test-renderer --dev
# Or with NPM:
npm i react-test-renderer --dev --save
{
"scripts": {
"build": "wp-scripts build",
"start": "wp-scripts start",
"test:unit": "wp-scripts test-unit-js"
}
}
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Form component 1`] = `
<form
className="wp-blocks-tooths"
>
<div>
<label
htmlFor="something"
>
Form Field
</label>
<input
className="isoblock-field"
id="something"
value="Tooths"
/>
</div>
</form>
`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment