phone/packages/robot3/test/test-reduce.js
Corey Johnson 27aa62f950 Upgrade robot3 to 1.3.0 for improved TypeScript support
- Bundle robot3 1.3.0 locally (not yet published to npm)
- Fix remaining type inference issues with `as any` casts for invoke() events

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-21 14:53:11 -08:00

53 lines
1.4 KiB
JavaScript

import { createMachine, interpret, reduce, state, transition } from '../machine.js';
QUnit.module('Reduce', () => {
QUnit.test('Basic state change', assert => {
let machine = createMachine({
one: state(
transition('ping', 'two',
reduce((ctx) => ({ ...ctx, one: 1 })),
reduce((ctx) => ({ ...ctx, two: 2 }))
)
),
two: state()
});
let service = interpret(machine, () => {});
service.send('ping');
let { one, two } = service.context;
assert.equal(one, 1, 'first reducer ran');
assert.equal(two, 2, 'second reducer ran');
});
QUnit.test('If no reducers, the context remains', assert => {
let machine = createMachine({
one: state(
transition('go', 'two')
),
two: state()
}, () => ({ one: 1, two: 2 }));
let service = interpret(machine, () => {});
service.send('go');
assert.deepEqual(service.context, { one: 1, two: 2 }, 'context remains');
});
QUnit.test('Event is the second argument', assert => {
assert.expect(2);
let machine = createMachine({
one: state(
transition('go', 'two',
reduce(function(ctx, ev) {
assert.equal(ev, 'go');
return { ...ctx, worked: true };
})
)
),
two: state()
});
let service = interpret(machine, () => {});
service.send('go');
assert.equal(service.context.worked, true, 'changed the context');
});
});