Skip to content

Commit

Permalink
add extra cancel tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Oct 12, 2024
1 parent 9576616 commit 85b1f67
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions packages/core/test/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 85b1f67

Please sign in to comment.