Skip to content

Commit

Permalink
Fix history state entering after transient transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Dec 6, 2020
1 parent 7949247 commit 75a91b0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-bugs-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': patch
---

Fixed an issue with history state entering a wrong state if the most recent visit in its parent has been caused by a transient transition.
1 change: 0 additions & 1 deletion packages/core/src/StateNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1312,7 +1312,6 @@ class StateNode<
maybeNextState.changed = changed;

// Preserve original history after raised events
maybeNextState.historyValue = nextState.historyValue;
maybeNextState.history = history;

return maybeNextState;
Expand Down
51 changes: 50 additions & 1 deletion packages/core/test/history.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Machine, createMachine } from '../src/index';
import { Machine, createMachine, interpret } from '../src/index';

describe('history states', () => {
const historyMachine = createMachine({
Expand Down Expand Up @@ -71,6 +71,55 @@ describe('history states', () => {
const nextState = historyMachine.transition(onState, 'H_POWER');
expect(nextState.history!.history).not.toBeDefined();
});

it('should go to the most recently visited state by a transient transition', () => {
const machine = createMachine({
initial: 'idle',
states: {
idle: {
id: 'idle',
initial: 'absent',
states: {
absent: {
on: {
DEPLOY: '#deploy'
}
},
present: {
on: {
DEPLOY: '#deploy',
DESTROY: '#destroy'
}
},
hist: {
type: 'history'
}
}
},
deploy: {
id: 'deploy',
on: {
SUCCESS: 'idle.present',
FAILURE: 'idle.hist'
}
},
destroy: {
id: 'destroy',
always: [{ target: 'idle.absent' }]
}
}
});

const service = interpret(machine).start();

service.send('DEPLOY');
service.send('SUCCESS');
service.send('DESTROY');
service.send('DEPLOY');
service.send('FAILURE');

expect(service.state.value).toEqual({ idle: 'absent' });
});
});

describe('deep history states', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/xstate-graph/test/__snapshots__/graph.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6410,7 +6410,7 @@ Object {
"value": "start",
},
"historyValue": Object {
"current": "start",
"current": "finish",
"states": Object {
"finish": undefined,
"start": undefined,
Expand Down Expand Up @@ -6485,7 +6485,7 @@ Object {
"value": "start",
},
"historyValue": Object {
"current": "start",
"current": "finish",
"states": Object {
"finish": undefined,
"start": undefined,
Expand Down

0 comments on commit 75a91b0

Please sign in to comment.