Skip to content

@xstate/test, re-visting states #1579

Answered by davidkpiano
UberMouse asked this question in Q&A
Discussion options

You must be logged in to vote

There's a neat solution here: use nested states!

// instead of this:
states: {
  one: { on: { EVENT: 'final' } },
  two: { on: { EVENT: 'final' } },
  three: { on: { EVENT: 'final' } },
  final: {} // will only be reached once
}

// do this this:
states: {
  one: { on: { EVENT: 'final.one' } },
  two: { on: { EVENT: 'final.two' } },
  three: { on: { EVENT: 'final.three' } },
  final: {
    initial: 'default',
    states: {
      default: {},
      one: {},
      two: {},
      three: {}
    }
  }
}

This isn't a hack, and here's why: your intentions in the test ("test final from one", "test final from two", etc.) don't match the states in your machine... so you should translate your intent…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@rjdestigter
Comment options

@UberMouse
Comment options

Answer selected by UberMouse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants