-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Fixes actions on same-state transitions #75
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,12 @@ export const matches = (patterns, value) => { | |
|
||
return patterns.some(pattern => values.some(val => pattern.test(val))) | ||
} | ||
|
||
export const machineDidUpdate = (oldMachine, newMachine) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although this is pretty simple, I would add a couple of tests. |
||
// Compare the value | ||
if (oldMachine.value !== newMachine.value) return true | ||
// Compare the actions | ||
// /* WIP - | ||
if (oldMachine.actions !== newMachine.actions) return true | ||
return false | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import { | |
getComponentName, | ||
isStateless, | ||
stringify, | ||
machineDidUpdate | ||
} from './utils' | ||
|
||
const REDUX_DISPATCH = 'DISPATCH' | ||
|
@@ -118,7 +119,7 @@ const withStateMachine = (statechart, options = {}) => Component => { | |
|
||
handleComponentDidUpdate(prevProps, prevState) { | ||
this.isTransitioning = false | ||
|
||
/* WIP - Should this logic be the same as what is used in handleTransition? */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, it definitely should. |
||
if (prevState.machineState !== this.state.machineState) { | ||
this.runActions() | ||
|
||
|
@@ -176,9 +177,8 @@ const withStateMachine = (statechart, options = {}) => Component => { | |
event, | ||
stateChange | ||
) | ||
|
||
if ( | ||
machineState.value === prevState.machineState.value && | ||
!machineDidUpdate(machineState, prevState.machineState) && | ||
(!stateChange || stateChange === prevState.componentState) | ||
) { | ||
this.isTransitioning = false | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import { Machine, State } from 'xstate' | |
import { withStateMachine } from '../src' | ||
|
||
const actionFunction = jest.fn() | ||
const sameStateFn = jest.fn() | ||
|
||
const statechart = { | ||
initial: 'a', | ||
|
@@ -16,6 +17,11 @@ const statechart = { | |
b: { | ||
on: { | ||
EVENT: 'a', | ||
SAME: { | ||
b: { | ||
actions: ['sameStateMethod', sameStateFn] | ||
} | ||
} | ||
}, | ||
onEntry: ['actionMethod', actionFunction], | ||
activities: ['activityMethod'], | ||
|
@@ -105,6 +111,7 @@ test('state', () => { | |
test('actions', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't repurpose this test, but I suggest we create a separate one (like "invalid transition with extended state") or, add a new case to "render". |
||
const actionMethod = jest.fn() | ||
const activityMethod = jest.fn() | ||
const sameStateMethod = jest.fn() | ||
|
||
class Component extends React.Component { | ||
actionMethod(...args) { | ||
|
@@ -115,6 +122,10 @@ test('actions', () => { | |
activityMethod(...args) | ||
} | ||
|
||
sameStateMethod(...args) { | ||
sameStateMethod(...args) | ||
} | ||
|
||
render() { | ||
return <div /> | ||
} | ||
|
@@ -131,6 +142,20 @@ test('actions', () => { | |
expect(actionFunction).toHaveBeenCalledWith(undefined, 'EVENT') | ||
expect(activityMethod).toHaveBeenCalledTimes(1) | ||
expect(activityMethod).toHaveBeenCalledWith(true) | ||
expect(sameStateFn).not.toHaveBeenCalled() | ||
expect(sameStateMethod).not.toHaveBeenCalled() | ||
|
||
instance.handleTransition('SAME') | ||
expect(actionMethod).toHaveBeenCalledTimes(2) | ||
expect(actionMethod).toHaveBeenCalledWith(undefined, 'EVENT') | ||
expect(actionFunction).toHaveBeenCalledTimes(2) | ||
expect(actionFunction).toHaveBeenCalledWith(undefined, 'EVENT') | ||
/* WIP - should the activity method have been called two or three times? */ | ||
expect(activityMethod).toHaveBeenCalledTimes(3) | ||
expect(activityMethod).toHaveBeenCalledWith(true) | ||
|
||
expect(sameStateFn).toHaveBeenCalledTimes(1) | ||
expect(sameStateMethod).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test('lifecycle hooks', () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be:
(prevMachineState.value !== machineState.value) || machineState.actions.length
Otherwise, this would be true when the previous machine has actions, and the current one doesn't (i.e. it's invalid).
I would also replace
machine
withstate
or, better, withmachineState
, and rename it to something likehasMachineStateChanged
.