Skip to content

Commit

Permalink
Add tests for emit and log
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkpiano committed Nov 4, 2024
1 parent a2d8fb0 commit b47f77f
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions packages/core/test/transition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import {
cancel,
createActor,
createMachine,
emit,
enqueueActions,
EventFrom,
ExecutableActionsFrom,
ExecutableSpawnAction,
fromPromise,
fromTransition,
log,
raise,
sendTo,
setup,
Expand Down Expand Up @@ -328,6 +330,71 @@ describe('transition function', () => {
);
});

it('emit actions should be returned', async () => {
const machine = createMachine({
initial: 'a',
context: { count: 10 },
states: {
a: {
on: {
NEXT: {
actions: emit(({ context }) => ({
type: 'counted',
count: context.count
}))
}
}
}
}
});

const [state] = initialTransition(machine);

expect(state.value).toEqual('a');

const [, nextActions] = transition(machine, state, { type: 'NEXT' });

expect(nextActions).toContainEqual(
expect.objectContaining({
type: 'xstate.emit',
params: expect.objectContaining({
event: { type: 'counted', count: 10 }
})
})
);
});

it('log actions should be returned', async () => {
const machine = createMachine({
initial: 'a',
context: { count: 10 },
states: {
a: {
on: {
NEXT: {
actions: log(({ context }) => `count: ${context.count}`)
}
}
}
}
});

const [state] = initialTransition(machine);

expect(state.value).toEqual('a');

const [, nextActions] = transition(machine, state, { type: 'NEXT' });

expect(nextActions).toContainEqual(
expect.objectContaining({
type: 'xstate.log',
params: expect.objectContaining({
value: 'count: 10'
})
})
);
});

// Copied from getSnapshot.test.ts

it('should calculate the next snapshot for transition logic', () => {
Expand Down

0 comments on commit b47f77f

Please sign in to comment.