Skip to content

Commit

Permalink
add failing raise test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Oct 12, 2024
1 parent 0c344da commit ece423d
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/actions/raise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function resolveRaise(
}

function executeRaise(
_clock: AnyActorScope,
_actorScope: AnyActorScope,
_params: {
event: EventObject;
id: string | undefined;
Expand Down
93 changes: 87 additions & 6 deletions packages/core/test/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3178,6 +3178,49 @@ describe('sendTo', () => {
]
`);
});

it('a self-event "handler" sent using sendTo should be able to read updated snapshot of self', () => {
const spy = jest.fn();
const machine = createMachine({
context: {
counter: 0
},
initial: 'a',
states: {
a: {
on: { NEXT: 'b' }
},
b: {
entry: [
assign({ counter: 1 }),
sendTo(({ self }) => self, { type: 'EVENT' })
],
on: {
EVENT: {
actions: ({ self }) => spy(self.getSnapshot().context),
target: 'c'
}
}
},
c: {}
}
});

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

actorRef.send({ type: 'NEXT' });
actorRef.send({ type: 'EVENT' });

expect(spy).toMatchMockCallsInlineSnapshot(`
[
[
{
"counter": 1,
},
],
]
`);
});
});

describe('raise', () => {
Expand Down Expand Up @@ -3215,7 +3258,7 @@ describe('raise', () => {
service.send({ type: 'TO_B' });
});

it('should be able to send a delayed event to itself with delay = 0', (done) => {
it('should be able to send a delayed event to itself with delay = 0', async () => {
const machine = createMachine({
initial: 'a',
states: {
Expand All @@ -3239,11 +3282,9 @@ describe('raise', () => {
// The state should not be changed yet; `delay: 0` is equivalent to `setTimeout(..., 0)`
expect(service.getSnapshot().value).toEqual('a');

setTimeout(() => {
// The state should be changed now
expect(service.getSnapshot().value).toEqual('b');
done();
});
await sleep(0);
// The state should be changed now
expect(service.getSnapshot().value).toEqual('b');
});

it('should be able to raise an event and respond to it in the same state', () => {
Expand Down Expand Up @@ -3384,6 +3425,46 @@ describe('raise', () => {
]
`);
});

it('a raised event "handler" should be able to read updated snapshot of self', () => {
const spy = jest.fn();
const machine = createMachine({
context: {
counter: 0
},
initial: 'a',
states: {
a: {
on: { NEXT: 'b' }
},
b: {
entry: [assign({ counter: 1 }), raise({ type: 'EVENT' })],
on: {
EVENT: {
actions: ({ self }) => spy(self.getSnapshot().context),
target: 'c'
}
}
},
c: {}
}
});

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

actorRef.send({ type: 'NEXT' });
actorRef.send({ type: 'EVENT' });

expect(spy).toMatchMockCallsInlineSnapshot(`
[
[
{
"counter": 1,
},
],
]
`);
});
});

describe('cancel', () => {
Expand Down

0 comments on commit ece423d

Please sign in to comment.