Skip to content

Commit

Permalink
Fixed an issue with states created using machine.getInitialState no…
Browse files Browse the repository at this point in the history
…t being "resolved" in full (#3023)
  • Loading branch information
Andarist committed Feb 7, 2022
1 parent 8d375ac commit 642e9f5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-crabs-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': patch
---

Fixed an issue with states created using `machine.getInitialState` not being "resolved" in full. This could cause some things, such as `after` transitions, not being executed correctly after starting an interpreter using such state.
2 changes: 1 addition & 1 deletion packages/core/src/StateNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,7 @@ class StateNode<
stateValue: StateValue,
context?: TContext
): State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta> {
this._init(); // TODO: this should be in the constructor (see note in constructor)
const configuration = this.getStateNodes(stateValue);

return this.resolveTransition(
Expand Down Expand Up @@ -1653,7 +1654,6 @@ class StateNode<
TTypestate,
TResolvedTypesMeta
> {
this._init(); // TODO: this should be in the constructor (see note in constructor)
const { initialStateValue } = this;

if (!initialStateValue) {
Expand Down
36 changes: 30 additions & 6 deletions packages/core/test/after.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Machine, interpret } from '../src';
import { createMachine, interpret } from '../src';
import { after, cancel, send, actionTypes } from '../src/actions';
import { toSCXMLEvent } from '../src/utils';

const lightMachine = Machine({
const lightMachine = createMachine({
id: 'light',
initial: 'green',
context: {
Expand Down Expand Up @@ -53,7 +53,7 @@ describe('delayed transitions', () => {
});

it('should be able to transition with delay from nested initial state', (done) => {
const machine = Machine({
const machine = createMachine({
initial: 'nested',
states: {
nested: {
Expand Down Expand Up @@ -82,7 +82,7 @@ describe('delayed transitions', () => {

it('parent state should enter child state without re-entering self (relative target)', (done) => {
const actual: string[] = [];
const machine = Machine({
const machine = createMachine({
initial: 'one',
states: {
one: {
Expand Down Expand Up @@ -119,7 +119,7 @@ describe('delayed transitions', () => {
it('should defer a single send event for a delayed transition with multiple conditions (#886)', () => {
type Events = { type: 'FOO' };

const machine = Machine<{}, Events>({
const machine = createMachine<{}, Events>({
initial: 'X',
states: {
X: {
Expand All @@ -146,11 +146,35 @@ describe('delayed transitions', () => {
expect(machine.initialState.actions.length).toBe(1);
});

it('should execute an after transition after starting from a state resolved using `machine.getInitialState`', (done) => {
const machine = createMachine({
id: 'machine',
initial: 'a',
states: {
a: {},

withAfter: {
after: {
1: { target: 'done' }
}
},

done: {
type: 'final'
}
}
});

interpret(machine)
.onDone(() => done())
.start(machine.getInitialState('withAfter'));
});

describe('delay expressions', () => {
type Events =
| { type: 'ACTIVATE'; delay: number }
| { type: 'NOEXPR'; delay: number };
const delayExprMachine = Machine<{ delay: number }, Events>(
const delayExprMachine = createMachine<{ delay: number }, Events>(
{
id: 'delayExpr',
initial: 'inactive',
Expand Down

0 comments on commit 642e9f5

Please sign in to comment.