Skip to content

Commit

Permalink
Fixed a regression that has caused machine.getInitialState(value) t…
Browse files Browse the repository at this point in the history
…o not follow always transitions correctly (#3599)

* Fixed a regression that has caused `machine.getInitialState(value)` to not follow always transitions correctly

* Move the added test to a more appropriate place
  • Loading branch information
Andarist committed Sep 13, 2022
1 parent 9e35294 commit 333f803
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-radios-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': patch
---

Fixed a regression that has caused `machine.getInitialState(value)` to not follow always transitions correctly.
7 changes: 5 additions & 2 deletions packages/core/src/StateNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,10 @@ class StateNode<
);

for (const sn of resolvedConfig) {
if (!has(prevConfig, sn) || has(transition.entrySet, sn.parent)) {
if (
!has(prevConfig, sn) ||
(has(transition.entrySet, sn.parent) && !has(transition.entrySet, sn))
) {
transition.entrySet.push(sn);
}
}
Expand Down Expand Up @@ -1661,7 +1664,7 @@ class StateNode<
return this.resolveTransition(
{
configuration,
entrySet: [...configuration],
entrySet: configuration,
exitSet: [],
transitions: [],
source: undefined,
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,10 @@ export class Interpreter<
})
.start();

return actor as ActorRef<TChildEvent, State<TChildContext, TChildEvent>>;
return actor as ActorRef<
TChildEvent,
State<TChildContext, TChildEvent, any, any, any>
>;
}
private spawnBehavior<TActorEvent extends EventObject, TEmitted>(
behavior: Behavior<TActorEvent, TEmitted>,
Expand Down
16 changes: 16 additions & 0 deletions packages/core/test/machine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,22 @@ describe('machine', () => {
});
});

describe('machine.getInitialState', () => {
it('should follow always transition', () => {
const machine = createMachine({
initial: 'a',
states: {
a: {
always: [{ target: 'b' }]
},
b: {}
}
});

expect(machine.getInitialState('a').value).toBe('b');
});
});

describe('versioning', () => {
it('should allow a version to be specified', () => {
const versionMachine = Machine({
Expand Down

0 comments on commit 333f803

Please sign in to comment.