diff --git a/packages/core/test/actions.test.ts b/packages/core/test/actions.test.ts index ffcb6f4f0d..02abe7e32f 100644 --- a/packages/core/test/actions.test.ts +++ b/packages/core/test/actions.test.ts @@ -3623,6 +3623,97 @@ describe('cancel', () => { expect(spy.mock.calls.length).toBe(0); }); + + it('should be able to cancel a just scheduled delayed event to a just invoked child', async () => { + const spy = jest.fn(); + + const child = createMachine({ + on: { + PING: { + actions: spy + } + } + }); + + const machine = setup({ + actors: { + child + } + }).createMachine({ + initial: 'a', + states: { + a: { + on: { + START: 'b' + } + }, + b: { + entry: [ + sendTo('myChild', { type: 'PING' }, { id: 'myEvent', delay: 0 }), + cancel('myEvent') + ], + invoke: { + src: 'child', + id: 'myChild' + } + } + } + }); + + const actorRef = createActor(machine).start(); + + actorRef.send({ + type: 'START' + }); + + await sleep(10); + expect(spy.mock.calls.length).toBe(0); + }); + + it('should not be able to cancel a just scheduled non-delayed event to a just invoked child', async () => { + const spy = jest.fn(); + + const child = createMachine({ + on: { + PING: { + actions: spy + } + } + }); + + const machine = setup({ + actors: { + child + } + }).createMachine({ + initial: 'a', + states: { + a: { + on: { + START: 'b' + } + }, + b: { + entry: [ + sendTo('myChild', { type: 'PING' }, { id: 'myEvent' }), + cancel('myEvent') + ], + invoke: { + src: 'child', + id: 'myChild' + } + } + } + }); + + const actorRef = createActor(machine).start(); + + actorRef.send({ + type: 'START' + }); + + expect(spy.mock.calls.length).toBe(1); + }); }); describe('assign action order', () => {