Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit75539fd

Browse files
compare machine state value
fix#66
1 parenta7ee256 commit75539fd

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

‎README.md‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ A state machine abstraction for React that provides declarative state management
1111

1212
##Installation
1313

14+
>`react` and`react-test-renderer` are peer dependencies
15+
1416
```sh
1517
yarn add react-automata
1618
```
@@ -175,7 +177,7 @@ class App extends React.Component {
175177

176178
####componentWillTransition(event)
177179

178-
The lifecycle method invoked whenatransitionis about to happen.
180+
The lifecycle method invoked whenthe[transitionfunction](#transitionevent-updater) has been called.
179181
It provides the event, and can be used to run side-effects.
180182

181183
```js
@@ -254,10 +256,6 @@ The component to define which parts of the tree should be rendered for a given s
254256
The method to automagically generate tests given a component wrapped into`withStateMachine`.
255257
It accepts an additional`fixtures` option to describe the data to be injected into the component for a given transition, and an`extendedState` option to control the statechart's conditions - both are optional.
256258

257-
```
258-
yarn add --dev react-test-renderer
259-
```
260-
261259
```js
262260
constfixtures= {
263261
initialData: {

‎src/withStateMachine.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ const withStateMachine = (statechart, options = {}) => Component => {
176176
)
177177

178178
if(
179-
machineState===prevState.machineState&&
179+
machineState.value===prevState.machineState.value&&
180180
(!stateChange||stateChange===prevState.componentState)
181181
){
182182
this.isTransitioning=false
@@ -185,7 +185,9 @@ const withStateMachine = (statechart, options = {}) => Component => {
185185
}
186186

187187
return{
188-
componentState:{ ...prevState.componentState, ...stateChange},
188+
componentState:stateChange
189+
?{ ...prevState.componentState, ...stateChange}
190+
:prevState.componentState,
189191
machineState,
190192
}
191193
})

‎test/withStateMachine.spec.js‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,24 @@ test('actions', () => {
114114
instance.handleTransition('EVENT')
115115

116116
expect(actionMethod).toHaveBeenCalledTimes(1)
117-
expect(actionMethod).toHaveBeenCalledWith({},'EVENT')
117+
expect(actionMethod).toHaveBeenCalledWith(undefined,'EVENT')
118118
expect(actionFunction).toHaveBeenCalledTimes(1)
119-
expect(actionFunction).toHaveBeenCalledWith({},'EVENT')
119+
expect(actionFunction).toHaveBeenCalledWith(undefined,'EVENT')
120120
expect(activityMethod).toHaveBeenCalledTimes(1)
121121
expect(activityMethod).toHaveBeenCalledWith(true)
122122
})
123123

124124
test('lifecycle hooks',()=>{
125-
constspy=jest.fn()
125+
constwillTransition=jest.fn()
126+
constdidTransition=jest.fn()
126127

127128
classComponentextendsReact.Component{
128129
componentWillTransition(...args){
129-
spy(...args)
130+
willTransition(...args)
130131
}
131132

132133
componentDidTransition(...args){
133-
spy(...args)
134+
didTransition(...args)
134135
}
135136

136137
render(){
@@ -143,10 +144,16 @@ test('lifecycle hooks', () => {
143144

144145
instance.handleTransition('EVENT')
145146

146-
expect(spy).toHaveBeenCalledTimes(2)
147-
expect(spy).toHaveBeenCalledWith('EVENT')
148-
expect(spy).toHaveBeenLastCalledWith(
147+
expect(willTransition).toHaveBeenCalledWith('EVENT')
148+
expect(didTransition).toHaveBeenCalledWith(
149149
expect.objectContaining({value:'a'}),
150150
'EVENT'
151151
)
152+
153+
willTransition.mockClear()
154+
didTransition.mockClear()
155+
instance.handleTransition('FOO')
156+
157+
expect(willTransition).toHaveBeenCalled()
158+
expect(didTransition).not.toHaveBeenCalled()
152159
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp