Skip to content

Finite State Machine for Mobx State Tree and React.

Notifications You must be signed in to change notification settings

paul-veevers/mst-fsm

Repository files navigation

Note this is experimental

MST-FSM

Finite State Machine for Mobx State Tree and React.

Why?

Finite State Machines (FSM's) are popular in many types of UI development - but not Javascript/browsers. This is an exploration to see where and when FSM's might be useful in browser UI development.

FSM theoretically could be useful in browsers because they:

  • have declarative states, e.g. jquery vs. react
  • ensure "correctness", e.g. it is not possible for a traffic light to transition from green to red, it must go through orange
  • See all the reading below for more information.

This is relevant in modern browser UIs because:

  • apps are increasingly complex:
    • lots of business logic is implemented client-side
    • apps have many different states (loading, error, success, toggles, pagination, etc)
    • testing (unit, integration, etc) is not great. Each component has it's own logic and states, and are often async. How can you possibly test all the permutations? Current best practice seems to be a UI integration test: generate fake data, render the whole tree for each permutation, snapshot it, then eyeball any difference to spot errors/regressions. Pretty sub-optimal.
  • some recent-ish ecosystem developments potentially make FSM's much more useful:
    • declarative UI (React) where UI is a function of state (view = f(state))
    • type systems (Typescript, Flow, Mobx State Tree)

The idea is to combine the following:

  • declarative state using types
  • declarative transitions between states
  • declarative UI

If you combine those you statically have all the information you need to:

  • render all possible permutations of the UI
  • ensure the UI cannot put the FSM into an invalid state.

That sounds pretty useful for automated testing.

Implementation

The best way to see what I'm talking about is to see the example folders - e.g. /examples/traffic_light. Start in the index.view.js file then the store then the state files. Once you think you've groked what is happening, have a look at the spec/test file.

And if you're brave have a look inside the lib folder.

Conclusions

Using a FSM with MST types and React we're able to achieve:

  • one-line auto-generation of a state chart diagram that can embed the corresponding rendered UI for each state.
  • one-line test that snapshots all possible permutations of a React tree given a machine, with deterministic fake value generation so that snapshots are consistent on subsequent runs.
  • one-line test that monkey tests your react tree. It starts from initial state, randomly clicks a click-able element to transition to next state, re-renders react tree, and repeats n times. It ensures your UI and machine can never be in an invalid state. It would also be possible to simulate other UI events like input typing, etc.

However:

  • Current implementation is not purely static. In practice, you would need to mock some things like API, etc. It should be possible to parse the code (e.g. using babel/babylon) to get all the required informaton, and then only React render function needs to be called with props to generate tree.
  • There is likely a much more flexible implementation somehow using above point (static analysis), with a proper type system like Typescript, Flow, (Graphql?).
  • With that in mind a future implementation would need to:
    • statically extract all possible states and their transitions (a state chart)
    • statically extract the shape of each state (types)
    • pure render React tree given props
    • pass a 'transition' function in props, which requires a runtime of some kind

Reading

TODO

About

Finite State Machine for Mobx State Tree and React.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published