├── .babelrc ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── diagram.png ├── dist └── index.js ├── lib └── index.js ├── package.json └── test ├── __snapshots__ └── react-states-machine.test.js.snap └── react-states-machine.test.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["env", "es2015", "react"], 3 | "plugins": ["transform-object-rest-spread"] 4 | } 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "6.0" 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Olivier Wietrich 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React State Machine 2 | 3 | [![NPM](https://img.shields.io/npm/v/react-states-machine.svg?style=flat-square)](https://www.npmjs.com/package/react-states-machine) 4 | [![Downloads](https://img.shields.io/npm/dm/react-states-machine.svg?style=flat-square)](http://npm-stat.com/charts.html?package=react-states-machine) 5 | [![pledge](https://bredele.github.io/contributing-guide/community-pledge.svg)](https://github.com/bredele/contributing-guide/blob/master/community.md) 6 | 7 | Inspired by [mood](https://github.com/bredele/mood) this module is using the well known [finite state machine](https://en.wikipedia.org/wiki/Finite-state_machine) pattern to alleviate some of the issues that crop up in complex React applications by strictly separating the management of states from your components. This module stay true to the original intent behind React: 8 | - Describe states as static components only (dynamic relationships within a component are expressed outside of the component itself) 9 | - Describe the logic for handling changes/updates as simple functions (called transitions). 10 | - Describe changes as plain objects to pass well defined and thought props. 11 | 12 | [Try online!](https://codesandbox.io/s/7jn717on4x) 13 | 14 | 15 | In addition, this module makes easy to: 16 | - develop stateless components (easier to understand and maintain) 17 | - develop components in isolation (easier to reuse and scale) 18 | - test components (dynamic relationships between components are tested separately) 19 | - manage asynchronous changes (props can be resolved by promises) 20 | 21 | ![react-states-machine](./diagram.png) 22 | 23 | ## Usage 24 | 25 | A state is made of a component as well as a set of actions to be executed (called transitions). Those actions are called through transitions events and either update the current state or display a new state. A transition manages changes by passing props to the wanted state. Here's a simple example of navigation flow using react-states-machine: 26 | 27 | 28 | ```js 29 | import machine from 'react-states-machine' 30 | 31 | function NavigationFlow (attrs) { 32 | return ( 33 |
34 | { 35 | machine({ 36 | // welcome state 37 | 'welcome': [ 38 | props => , 39 | { 40 | // click event transition from welcome state to next state with a new message prop 41 | 'click': [() => ({message: 'Hello you!'}), 'next'] 42 | } 43 | ], 44 | // next state 45 | 'next': [ 46 | props => { 47 | return ( 48 |
49 | 50 | {props.message} 51 | 52 |
53 | ) 54 | }, 55 | { 56 | // update event update next state with new message prop after 1 second 57 | 'update': [() => new Promise(resolve => setTimeout(() => resolve({ message: 'This is awesome!' }), 1000))] 58 | } 59 | ] 60 | }, attrs) 61 | } 62 |
63 | ) 64 | } 65 | ``` 66 | 67 | ## Getting started 68 | 69 | A state machine is an object describing your application/component states. 70 | 71 | ```js 72 | machine({ 73 | state: [ 74 | component, 75 | transitions 76 | ] 77 | }) 78 | ``` 79 | 80 | A state is composed of a component as well as an optional object containing transitions to mutate this component. Here's an example that shows how to style an input when empty using a transition called `validity`: 81 | 82 | ```js 83 | machine({ 84 | 'inputState': [ 85 | props => props.transition('validity', e.target.value)}/>, 86 | { 87 | 'validity': [(prev, value) => { 88 | return { 89 | invalid: !value 90 | } 91 | }] 92 | } 93 | ] 94 | }) 95 | ``` 96 | 97 | A transition is a function used to pass props to your component and update it. This function can return any types as well as promises (transition is resolved with the promise). 98 | 99 | A transition is also useful to describe the passage to an other state. Here's an example: 100 | 101 | ```js 102 | machine({ 103 | 'formEmail': [ 104 | props => { 105 | return ( 106 |
107 | 108 | 109 |
110 | ) 111 | }, 112 | { 113 | next: [() => ({name: 'John Doe'}), 'formPassword'] 114 | } 115 | ], 116 | 'formPassword': [ 117 | props => { 118 | return ( 119 |
120 |

Hello {props.name}

121 | 122 | 123 |
124 | ) 125 | } 126 | ] 127 | }) 128 | ``` 129 | 130 | But you also can go to an other state without transition: 131 | 132 | ```js 133 | machine({ 134 | 'formEmail': [ 135 | props => { 136 | return ( 137 |
138 | 139 | 140 |
141 | ) 142 | } 143 | ], 144 | 'formPassword': [ 145 | props => { 146 | return ( 147 |
148 |

Hello {props.name}

149 | 150 | 151 |
152 | ) 153 | } 154 | ] 155 | }) 156 | ``` 157 | 158 | Check out [our test suite](./test/react-states-machine.test.js) for more information. 159 | 160 | ## Installation 161 | 162 | ```shell 163 | npm install react-states-machine --save 164 | ``` 165 | 166 | [![NPM](https://nodei.co/npm/react-states-machine.png)](https://nodei.co/npm/react-states-machine/) 167 | 168 | 169 | ## Question 170 | 171 | For questions and feedback please use our [twitter account](https://twitter.com/bredeleca). For support, bug reports and or feature requests please make sure to read our 172 | community guideline and use the issue list of this repo and make sure it's not present yet in our reporting checklist. 173 | 174 | ## Contribution 175 | 176 | This is an open source project and would not exist without its community. If you want to participate please make sure to read our guideline before making a pull request. If you have any react-states-machine related project, component or other let everyone know in our wiki. 177 | 178 | 179 | ## Licence 180 | 181 | The MIT License (MIT) 182 | 183 | Copyright (c) 2016 Olivier Wietrich 184 | 185 | Permission is hereby granted, free of charge, to any person obtaining a copy 186 | of this software and associated documentation files (the "Software"), to deal 187 | in the Software without restriction, including without limitation the rights 188 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 189 | copies of the Software, and to permit persons to whom the Software is 190 | furnished to do so, subject to the following conditions: 191 | 192 | The above copyright notice and this permission notice shall be included in all 193 | copies or substantial portions of the Software. 194 | 195 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 196 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 197 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 198 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 199 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 200 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 201 | SOFTWARE. 202 | -------------------------------------------------------------------------------- /diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bredele/react-states-machine/240d22851e590866e9893b27ae20b2c4d53ef4aa/diagram.png -------------------------------------------------------------------------------- /dist/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); 8 | 9 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); 10 | 11 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; /** 12 | * Dependencie(s) 13 | */ 14 | 15 | var _react = require('react'); 16 | 17 | var _react2 = _interopRequireDefault(_react); 18 | 19 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 20 | 21 | function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } 22 | 23 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 24 | 25 | function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } 26 | 27 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } 28 | 29 | /** 30 | * State machine instance. 31 | * 32 | * @param {Object} flow 33 | * @param {Object} props 34 | * @api public 35 | */ 36 | 37 | exports.default = function (flow) { 38 | var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; 39 | var state = arguments[2]; 40 | 41 | return _react2.default.createElement(Machine, _extends({ flow: flow, state: state }, props)); 42 | }; 43 | 44 | /** 45 | * State machine component. 46 | * 47 | * All props are passed to the state machine. 48 | * Props: 49 | * 50 | * - flow: object describing state machine flow 51 | * 52 | * 53 | * @param {Object} props 54 | * @api public 55 | */ 56 | 57 | var Machine = function (_React$Component) { 58 | _inherits(Machine, _React$Component); 59 | 60 | function Machine(props) { 61 | _classCallCheck(this, Machine); 62 | 63 | var _this = _possibleConstructorReturn(this, (Machine.__proto__ || Object.getPrototypeOf(Machine)).call(this, props)); 64 | 65 | var flow = props.flow, 66 | state = props.state, 67 | attrs = _objectWithoutProperties(props, ['flow', 'state']); 68 | 69 | _this.state = { 70 | path: state || first(flow), 71 | data: attrs 72 | }; 73 | return _this; 74 | } 75 | 76 | _createClass(Machine, [{ 77 | key: 'render', 78 | value: function render() { 79 | var _this2 = this; 80 | 81 | var _props$flow$state$pat = _slicedToArray(this.props.flow[this.state.path], 2), 82 | state = _props$flow$state$pat[0], 83 | transitions = _props$flow$state$pat[1]; 84 | 85 | return state(_extends({}, this.state.data, { 86 | transition: transition(this, transitions), 87 | goto: function goto(next, data) { 88 | return _goto(_this2, data, next); 89 | }, 90 | reset: function reset(data) { 91 | return _goto(_this2, data); 92 | } 93 | })); 94 | } 95 | }]); 96 | 97 | return Machine; 98 | }(_react2.default.Component); 99 | 100 | /** 101 | * Transition factory (function that triggers 102 | * a state machine transition). 103 | * 104 | * @param {Object} source 105 | * @param {Object} transitions 106 | * @return {Function} 107 | * @api private 108 | */ 109 | 110 | function transition(source) { 111 | var transitions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; 112 | 113 | return function (path) { 114 | for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { 115 | args[_key - 1] = arguments[_key]; 116 | } 117 | 118 | var dest = transitions[path]; 119 | if (dest) { 120 | var _transition = [].concat(dest); 121 | var next = _transition[1]; 122 | Promise.resolve(_transition[0].apply(_transition, [source.state.data].concat(args))).then(function (value) { 123 | return _goto(source, value, typeof next === 'function' ? next(value) : next); 124 | }, function (reason) { 125 | return _goto(source, reason, typeof next === 'function' ? next(reason) : next); 126 | }); 127 | } 128 | }; 129 | } 130 | 131 | /** 132 | * Change source state with given path and data. 133 | * 134 | * Avoid invalid attempt to destructure non-iterable instance. 135 | * 136 | * @param {Object} source 137 | * @param {Object} data 138 | * @param {String} path 139 | * @api private 140 | */ 141 | 142 | function _goto(source, data, path) { 143 | source.setState(function (prev) { 144 | return { 145 | path: path || prev.path, 146 | data: data || prev.data 147 | }; 148 | }); 149 | } 150 | 151 | /** 152 | * Get first property of an object. 153 | * 154 | * @param {Object} obj 155 | * @return {String} 156 | * @api private 157 | */ 158 | 159 | function first(obj) { 160 | for (var key in obj) { 161 | return key; 162 | } 163 | } -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Dependencie(s) 3 | */ 4 | 5 | import React from 'react' 6 | 7 | 8 | /** 9 | * State machine instance. 10 | * 11 | * @param {Object} flow 12 | * @param {Object} props 13 | * @api public 14 | */ 15 | 16 | export default (flow, props = {}, state) => { 17 | return 18 | } 19 | 20 | 21 | /** 22 | * State machine component. 23 | * 24 | * All props are passed to the state machine. 25 | * Props: 26 | * 27 | * - flow: object describing state machine flow 28 | * 29 | * 30 | * @param {Object} props 31 | * @api public 32 | */ 33 | 34 | class Machine extends React.Component { 35 | 36 | constructor(props) { 37 | super(props) 38 | const { 39 | flow, 40 | state, 41 | ...attrs 42 | } = props 43 | this.state = { 44 | path: state || first(flow), 45 | data: attrs 46 | } 47 | } 48 | 49 | render () { 50 | const [state, transitions] = this.props.flow[this.state.path] 51 | return state({ 52 | ...this.state.data, 53 | transition: transition(this, transitions), 54 | goto: (next, data) => goto(this, data, next), 55 | reset: (data) => goto(this, data) 56 | }) 57 | } 58 | } 59 | 60 | 61 | /** 62 | * Transition factory (function that triggers 63 | * a state machine transition). 64 | * 65 | * @param {Object} source 66 | * @param {Object} transitions 67 | * @return {Function} 68 | * @api private 69 | */ 70 | 71 | function transition (source, transitions = {}) { 72 | return (path, ...args) => { 73 | const dest = transitions[path] 74 | if (dest) { 75 | const transition = [].concat(dest) 76 | const next = transition[1] 77 | Promise.resolve(transition[0](source.state.data, ...args)) 78 | .then( 79 | value => { 80 | return goto(source, value, typeof next === 'function' ? next(value) : next) 81 | }, 82 | reason => { 83 | return goto(source, reason, typeof next === 'function' ? next(reason) : next) 84 | } 85 | ) 86 | } 87 | } 88 | } 89 | 90 | 91 | /** 92 | * Change source state with given path and data. 93 | * 94 | * Avoid invalid attempt to destructure non-iterable instance. 95 | * 96 | * @param {Object} source 97 | * @param {Object} data 98 | * @param {String} path 99 | * @api private 100 | */ 101 | 102 | function goto (source, data, path) { 103 | source.setState(prev => { 104 | return { 105 | path: path || prev.path, 106 | data : data || prev.data 107 | } 108 | }) 109 | } 110 | 111 | 112 | 113 | /** 114 | * Get first property of an object. 115 | * 116 | * @param {Object} obj 117 | * @return {String} 118 | * @api private 119 | */ 120 | 121 | function first (obj) { 122 | for (var key in obj) return key 123 | } 124 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-states-machine", 3 | "version": "1.2.0", 4 | "description": "React finite state machine done right", 5 | "main": "dist/index.js", 6 | "scripts": { 7 | "test": "jest", 8 | "build": "babel lib -d dist", 9 | "prepublish": "npm run build" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/bredele/react-states-machine.git" 14 | }, 15 | "keywords": [ 16 | "react", 17 | "state", 18 | "machine", 19 | "redux", 20 | "flux" 21 | ], 22 | "author": "Olivier Wietrich", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/bredele/react-states-machine/issues" 26 | }, 27 | "dependencies": { 28 | "react": "^16.2.0" 29 | }, 30 | "homepage": "https://github.com/bredele/react-states-machine#readme", 31 | "devDependencies": { 32 | "babel-cli": "^6.26.0", 33 | "babel-jest": "^22.2.2", 34 | "babel-plugin-transform-object-rest-spread": "^6.26.0", 35 | "babel-preset-env": "^1.6.1", 36 | "babel-preset-es2015": "^6.24.1", 37 | "babel-preset-react": "^6.24.1", 38 | "jest": "^22.2.2", 39 | "react-dom": "^16.2.0", 40 | "react-test-renderer": "^16.2.0" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /test/__snapshots__/react-states-machine.test.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`should go to given state 1`] = ` 4 | 9 | `; 10 | 11 | exports[`should go to given state 2`] = ` 12 | 15 | `; 16 | 17 | exports[`should go to given state and pass props 1`] = ` 18 | 23 | `; 24 | 25 | exports[`should go to given state and pass props 2`] = ` 26 | 29 | `; 30 | 31 | exports[`should render first state 1`] = ` 32 | 35 | `; 36 | 37 | exports[`should render given state 1`] = ` 38 | 41 | `; 42 | 43 | exports[`should transition current state 1`] = ` 44 | 50 | `; 51 | 52 | exports[`should transition current state 2`] = ` 53 | 59 | `; 60 | 61 | exports[`should transition current state and update props 1`] = ` 62 | 68 | `; 69 | 70 | exports[`should transition current state and update props 2`] = ` 71 | 77 | `; 78 | 79 | exports[`should transition to other state 1`] = ` 80 | 85 | `; 86 | 87 | exports[`should transition to other state 2`] = ` 88 | 89 | other 90 | 91 | `; 92 | 93 | exports[`should transition to other state and pass props 1`] = ` 94 | 99 | `; 100 | 101 | exports[`should transition to other state and pass props 2`] = ` 102 | 107 | `; 108 | -------------------------------------------------------------------------------- /test/react-states-machine.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Dependencie(s) 3 | */ 4 | 5 | import React from 'react' 6 | import machine from '../lib' 7 | import renderer from 'react-test-renderer' 8 | 9 | 10 | test('should render first state', () => { 11 | const flow = renderer.create(machine({ 12 | 'init': [ 13 | props => 14 | ] 15 | })) 16 | let tree = flow.toJSON() 17 | expect(tree).toMatchSnapshot() 18 | }) 19 | 20 | 21 | test('should render given state', () => { 22 | const flow = renderer.create(machine({ 23 | 'init': [ 24 | props => 25 | ], 26 | 'other': [ 27 | props => 28 | ] 29 | }, {}, 'other')) 30 | let tree = flow.toJSON() 31 | expect(tree).toMatchSnapshot() 32 | }) 33 | 34 | 35 | test('should go to given state', () => { 36 | const flow = renderer.create(machine({ 37 | 'init': [ 38 | props => 39 | ], 40 | 'other': [ 41 | props => 42 | ] 43 | })) 44 | 45 | let tree = flow.toJSON() 46 | expect(tree).toMatchSnapshot() 47 | 48 | tree.props.onClick() 49 | 50 | 51 | tree = flow.toJSON() 52 | expect(tree).toMatchSnapshot() 53 | }) 54 | 55 | test('should go to given state and pass props', () => { 56 | const flow = renderer.create(machine({ 57 | 'init': [ 58 | props => 61 | ], 62 | 'other': [ 63 | props => 64 | ] 65 | })) 66 | 67 | let tree = flow.toJSON() 68 | expect(tree).toMatchSnapshot() 69 | 70 | tree.props.onClick() 71 | 72 | 73 | tree = flow.toJSON() 74 | expect(tree).toMatchSnapshot() 75 | }) 76 | 77 | 78 | test('should transition current state and update props', () => { 79 | const flow = renderer.create(machine({ 80 | 'init': [ 81 | props => , 82 | { 83 | 'john': [() => { 84 | return { 85 | message: 'john' 86 | } 87 | }] 88 | } 89 | ] 90 | })) 91 | 92 | let tree = flow.toJSON() 93 | expect(tree).toMatchSnapshot() 94 | 95 | tree.props.onClick() 96 | 97 | tree = flow.toJSON() 98 | expect(tree).toMatchSnapshot() 99 | }) 100 | 101 | 102 | test('should transition to other state and pass props', () => { 103 | const flow = renderer.create(machine({ 104 | 'init': [ 105 | props => , 106 | { 107 | 'next': [() => ({message: 'other'}), 'other'] 108 | } 109 | ], 110 | 'other': [ 111 | props => {props.message} 112 | ] 113 | })) 114 | 115 | let tree = flow.toJSON() 116 | expect(tree).toMatchSnapshot() 117 | 118 | tree.props.onClick() 119 | 120 | 121 | tree = flow.toJSON() 122 | expect(tree).toMatchSnapshot() 123 | }) 124 | --------------------------------------------------------------------------------