Skip to content

Commit

Permalink
Add extra test cases for final outgoing events (#4389)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored Oct 24, 2023
1 parent 7a916da commit e4a4838
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions packages/core/test/final.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1155,4 +1155,90 @@ describe('final states', () => {

expect(spy).toHaveBeenCalledTimes(1);
});

it('should deliver final outgoing events (from final entry action) to the parent before delivering the `xstate.done.actor.*` event', () => {
const child = createMachine({
initial: 'start',
states: {
start: {
on: {
CANCEL: 'canceled'
}
},
canceled: {
type: 'final',
entry: sendParent({ type: 'CHILD_CANCELED' })
}
}
});
const parent = createMachine({
initial: 'start',
states: {
start: {
invoke: {
id: 'child',
src: child,
onDone: 'completed'
},
on: {
CHILD_CANCELED: 'canceled'
}
},
canceled: {},
completed: {}
}
});

const actorRef = createActor(parent).start();

actorRef.getSnapshot().children.child.send({
type: 'CANCEL'
});

// if `xstate.done.actor.*` would be delivered first the value would be `completed`
expect(actorRef.getSnapshot().value).toBe('canceled');
});

it('should deliver final outgoing events (from root exit action) to the parent before delivering the `xstate.done.actor.*` event', () => {
const child = createMachine({
initial: 'start',
states: {
start: {
on: {
CANCEL: 'canceled'
}
},
canceled: {
type: 'final'
}
},
exit: sendParent({ type: 'CHILD_CANCELED' })
});
const parent = createMachine({
initial: 'start',
states: {
start: {
invoke: {
id: 'child',
src: child,
onDone: 'completed'
},
on: {
CHILD_CANCELED: 'canceled'
}
},
canceled: {},
completed: {}
}
});

const actorRef = createActor(parent).start();

actorRef.getSnapshot().children.child.send({
type: 'CANCEL'
});

// if `xstate.done.actor.*` would be delivered first the value would be `completed`
expect(actorRef.getSnapshot().value).toBe('canceled');
});
});

0 comments on commit e4a4838

Please sign in to comment.