Skip to content

Commit

Permalink
Fixed an issue with state.tags not having correct values when resol…
Browse files Browse the repository at this point in the history
…ving micro transitions (#3166)
  • Loading branch information
Andarist committed Mar 22, 2022
1 parent b36ef9d commit be4c5c7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-humans-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': patch
---

Fixed an issue with `state.tags` not having correct values when resolving micro transitions (taken in response to raised events). This was creating issues when checking tags in guards.
6 changes: 1 addition & 5 deletions packages/core/src/StateNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@ class StateNode<
transitions: stateTransition.transitions,
children,
done: isDone,
tags: currentState?.tags,
tags: getTagsFromConfiguration(resolvedConfiguration),
machine: this as any
});

Expand Down Expand Up @@ -1415,10 +1415,6 @@ class StateNode<
// Preserve original history after raised events
maybeNextState.history = history;

maybeNextState.tags = getTagsFromConfiguration(
maybeNextState.configuration
);

return maybeNextState;
}

Expand Down
32 changes: 31 additions & 1 deletion packages/core/test/guards.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Machine, interpret } from '../src';
import { Machine, interpret, createMachine, actions } from '../src';

describe('guard conditions', () => {
type LightMachineCtx = {
Expand Down Expand Up @@ -227,6 +227,36 @@ describe('guard conditions', () => {
B: 'B4'
});
});

it('should be able to check source state tags when checking', () => {
const machine = createMachine({
initial: 'a',
states: {
a: {
on: {
MACRO: 'b'
}
},
b: {
entry: actions.raise('MICRO'),
tags: 'theTag',
on: {
MICRO: {
cond: (_ctx: any, _event: any, { state }: any) =>
state.hasTag('theTag'),
target: 'c'
}
}
},
c: {}
}
});

const service = interpret(machine).start();
service.send('MACRO');

expect(service.state.value).toBe('c');
});
});

describe('custom guards', () => {
Expand Down

0 comments on commit be4c5c7

Please sign in to comment.