Skip to content

Commit

Permalink
Fix state.can() from restored state (#3097)
Browse files Browse the repository at this point in the history
* Fix `state.can()` form restored state

* Add changeset

* Update packages/core/test/state.test.ts

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>

* Update .changeset/twenty-timers-cheat.md

* Move a test to the dedicated test suite

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
  • Loading branch information
davidkpiano and Andarist committed Mar 11, 2022
1 parent e18b241 commit c881c8c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-timers-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': patch
---

State that is persisted and restored from `machine.resolveState(state)` will now have the correct `state.machine` value, so that `state.can(...)` and other methods will work as expected. See [#3096](https://github.com/statelyai/xstate/issues/3096) for more details.
6 changes: 4 additions & 2 deletions packages/core/src/StateNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ import {
StateMachine,
InternalMachineOptions,
ServiceMap,
StateConfig
StateConfig,
AnyStateMachine
} from './types';
import { matchesState } from './utils';
import { State, stateValuesEqual } from './State';
Expand Down Expand Up @@ -761,7 +762,8 @@ class StateNode<
value: this.resolve(stateFromConfig.value),
configuration,
done: isInFinalState(configuration, this),
tags: getTagsFromConfiguration(configuration)
tags: getTagsFromConfiguration(configuration),
machine: (this.machine as unknown) as AnyStateMachine
});
}

Expand Down
16 changes: 16 additions & 0 deletions packages/core/test/rehydration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ describe('rehydration', () => {

expect(actual).toEqual(['a', 'root']);
});

it('should get correct result back from `can` immediately', () => {
const machine = createMachine({
on: {
FOO: {
actions: () => {}
}
}
});

const persistedState = JSON.stringify(interpret(machine).start().state);
const restoredState = JSON.parse(persistedState);
const service = interpret(machine).start(restoredState);

expect(service.state.can('FOO')).toBe(true);
});
});

describe('using state value', () => {
Expand Down

0 comments on commit c881c8c

Please sign in to comment.