diff --git a/packages/core/machine.js b/packages/core/machine.js index 5cd89e7c..7cceee1e 100644 --- a/packages/core/machine.js +++ b/packages/core/machine.js @@ -169,8 +169,9 @@ function transitionTo(service, machine, fromEvent, candidates) { if (d._onEnter) d._onEnter(machine, to, service.context, context, fromEvent); let state = newMachine.state.value; service.machine = newMachine; + let ret = state.enter(newMachine, service, fromEvent); service.onChange(service); - return state.enter(newMachine, service, fromEvent); + return ret; } } } diff --git a/packages/robot-hooks/machine.js b/packages/robot-hooks/machine.js index fda3f689..6f8bbd21 100644 --- a/packages/robot-hooks/machine.js +++ b/packages/robot-hooks/machine.js @@ -24,7 +24,11 @@ export function createUseMachine(useEffect, useState) { if (!mounted) { return; } - setCurrent(createCurrent(service.child || service)); + let currentService = service; + while(currentService.child) { + currentService = currentService.child; + } + setCurrent(createCurrent(currentService)); }, data || initialContext); } diff --git a/packages/robot-hooks/test/test.js b/packages/robot-hooks/test/test.js index bf94df1c..c831cc05 100644 --- a/packages/robot-hooks/test/test.js +++ b/packages/robot-hooks/test/test.js @@ -133,12 +133,19 @@ QUnit.module('useMachine', hooks => { el.remove(); }); - QUnit.skip('Invoking machines the "current" is the child machine', async assert => { + QUnit.test('Invoking machines the "current" is the child machine', async assert => { + const deep = createMachine({ + deepOne: state( + transition('next', 'deepTwo') + ), + deepTwo: state(), + }) const nested = createMachine({ nestedOne: state( transition('next', 'nestedTwo') ), - nestedTwo: state() + nestedTwo: invoke(deep, transition('done', 'nestedThree')), + nestedThree: state() }); const machine = createMachine({ one: state( @@ -150,11 +157,9 @@ QUnit.module('useMachine', hooks => { three: state() }); - let current, send, service; + let service; function App() { - const [currentState, sendEvent, thisService] = useMachine(machine); - current = currentState; - send = sendEvent; + const [current, _send, thisService] = useMachine(machine); service = thisService; return html` @@ -167,10 +172,13 @@ QUnit.module('useMachine', hooks => { let text = () => el.textContent.trim(); assert.equal(text(), 'State: one'); - yield send('next'); + yield service.send('next'); assert.equal(text(), 'State: nestedOne'); yield service.child.send('next'); + + assert.equal(text(), 'State: deepOne'); + yield service.child.child.send('next'); assert.equal(text(), 'State: three'); });