From 52742ab8f3772f58b51301415b8f81f8d31f6766 Mon Sep 17 00:00:00 2001 From: Erik Huelsmann Date: Tue, 28 Nov 2023 03:08:42 +0100 Subject: [PATCH] Issue 179: `onChange` not called for `immediate` states (#180) * Fix #179: Make sure to call onChange with 'immediate' states * Adapt test expectations * Add changeset as requested --- .changeset/good-foxes-build.md | 5 +++++ packages/core/machine.js | 7 +++---- packages/core/test/test-invoke.js | 7 ++++--- 3 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 .changeset/good-foxes-build.md diff --git a/.changeset/good-foxes-build.md b/.changeset/good-foxes-build.md new file mode 100644 index 00000000..304beffd --- /dev/null +++ b/.changeset/good-foxes-build.md @@ -0,0 +1,5 @@ +--- +"robot3": major +--- + +Call onChange callbacks for immediate states too diff --git a/packages/core/machine.js b/packages/core/machine.js index 8987efa7..9805d54d 100644 --- a/packages/core/machine.js +++ b/packages/core/machine.js @@ -161,6 +161,8 @@ 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; + service.onChange(service); return state.enter(newMachine, service, fromEvent); } } @@ -181,10 +183,7 @@ function send(service, event) { let service = { send(event) { - this.machine = send(this, event); - - // TODO detect change - this.onChange(this); + send(this, event); } }; diff --git a/packages/core/test/test-invoke.js b/packages/core/test/test-invoke.js index 88d230aa..a75e05b0 100644 --- a/packages/core/test/test-invoke.js +++ b/packages/core/test/test-invoke.js @@ -314,7 +314,8 @@ QUnit.module('Invoke', hooks => { }); QUnit.test('Invoking a machine that immediately finishes', async assert => { - assert.expect(1); + assert.expect(3); + const expectations = [ 'two', 'nestedTwo', 'three' ]; const child = createMachine({ nestedOne: state( @@ -336,9 +337,9 @@ QUnit.module('Invoke', hooks => { let service = interpret(parent, s => { // TODO not totally sure if this is correct, but I think it should // hit this only once and be equal to three - assert.equal(s.machine.current, 'three'); + assert.equal(s.machine.current, expectations.shift()); }); service.send('next'); }); -}); \ No newline at end of file +});