-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[Feature] @xstate/react : snapshot testing of components by state #432
Comments
testStateMachine
option for @xstate/react
I am also interested in this feature, and exactly because I want to be able to save snapshots of my app state for testing. xstate features multiple times that it can jump to (i.e. reload) a snapshot of a state: https://xstate.js.org/docs/guides/states.html#persisting-state
https://xstate.js.org/docs/guides/interpretation.html#batched-events
and indeed I have some tests that look like
this works because I can manipulate the initial state with But this does not cover my react components, which wrap xstate StateMachines. Indeed, if you read the source for @xstate/react you see that it only ever calls
So that sets I propose the @xstate/react API should either grow a parameter
which gets fed into |
You can already do this with // ...
const myService = useMemo(() => interpret(myMachine).start(myInitialState), []);
const [current, send] = useService(myService);
// ... |
Thanks for the tip, David. I don't think it helps me though. The @xstate/react source code I linked already exposes `[current, send]` to callers but I'm wondering if it is possible to assign the state directly.
|
You mean after the machine is started? This would be contradictory to how state machines work. |
I would be okay with doing it at initialization. I just can't because this line does not expose the arguments to |
Doesn't https://xstate.js.org/docs/guides/interpretation.html#starting-and-stopping suggest doing just that?
|
It does not. The difference is that you are starting from a previous state, instead of arbitrarily putting an already-running machine into a specified state. You're looking at |
Oh! I didn't realize you were suggesting replacing |
It is pretty unintuitive that |
Ah, I spoke too soon. If I use your suggestion with this React component:
like so:
And pass in "RepairStep" for
the reason for this is that in passing an
which drops the default
I'll try reloading a persisted state next. |
I saved some state using
and it worked on the third try. I haven't found the need to call (the |
Hi @davidkpiano , Can I do something similiar with actors?, I'm doing something like this for actors but it looks that the This is my custom spawning implementation: export const spawnPersisted = <TContext, TEvent extends EventObject>(
machine: StateMachine<TContext, any, TEvent>,
name: string
): Actor<TContext> => {
if (process.browser) {
const state = window.sessionStorage.getItem(name);
if (!!state) {
const jsonState = JSON.parse(state);
machine.resolveState(jsonState);
}
}
return spawn(machine, name);
}; |
Sorry, can you explain your issue further? What are you trying to accomplish exactly? |
Hi @davidkpiano , I'm trying to set an initial state for the actor's machine coming from the session storage after being previously saved. Since an actor interpreter can't be serialize (it resolves to So, in the machine I have this:
Do you have any suggestion for me to accomplish this? |
This isn't an easy problem, because you can't guarantee that the spawned actor will be in the same state that it was at (in fact, it's easier to guarantee that it won't). However, in 4.7 I will be introducing a |
First off, great work! 👏
(Feature) Description
I would like to see a feature for testing snapshots of a component by state, like react-automata.
(Feature) Potential implementation:
Example usage:
The
testStateMachine
method should:initialData
andfixtures
as an optional second parameter.I would like to help with this feature, but I am not sure how to "get" the machine from within the component utilizing the
useMachine
hook. I would gladly contribute with some guidance!The text was updated successfully, but these errors were encountered: