Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ export const matches = (patterns, value) => {

return patterns.some(pattern => values.some(val => pattern.test(val)))
}

export const machineDidUpdate = (oldMachine, newMachine) => {
Copy link
Owner

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 with state or, better, with machineState, and rename it to something like hasMachineStateChanged.

Copy link
Owner

Choose a reason for hiding this comment

The 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
}
6 changes: 3 additions & 3 deletions src/withStateMachine.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getComponentName,
isStateless,
stringify,
machineDidUpdate
} from './utils'

const REDUX_DISPATCH = 'DISPATCH'
Expand Down Expand Up @@ -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? */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it definitely should.

if (prevState.machineState !== this.state.machineState) {
this.runActions()

Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions test/withStateMachine.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -16,6 +17,11 @@ const statechart = {
b: {
on: {
EVENT: 'a',
SAME: {
b: {
actions: ['sameStateMethod', sameStateFn]
}
}
},
onEntry: ['actionMethod', actionFunction],
activities: ['activityMethod'],
Expand Down Expand Up @@ -105,6 +111,7 @@ test('state', () => {
test('actions', () => {
Copy link
Owner

Choose a reason for hiding this comment

The 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) {
Expand All @@ -115,6 +122,10 @@ test('actions', () => {
activityMethod(...args)
}

sameStateMethod(...args) {
sameStateMethod(...args)
}

render() {
return <div />
}
Expand All @@ -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', () => {
Expand Down