Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ export function invoke(fn, ...transitions) {
});
}

export let nested = (to, states) => invoke(createMachine(states, identity),
transition('done', to)
);

let machine = {
get state() {
return {
Expand Down Expand Up @@ -172,6 +176,14 @@ function send(service, event) {
}

let service = {
matches(query) {
let states = query.split('.'), matched = false, service = this;
do {
matched = !!service && states.shift() === service.machine.current;
service = service.child;
} while(matched && states.length);
return matched;
},
send(event) {
this.machine = send(this, event);

Expand Down
28 changes: 27 additions & 1 deletion test/test-invoke.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createMachine, immediate, interpret, invoke, reduce, state, state as final, transition } from '../machine.js';
import { createMachine, immediate, interpret, invoke, nested, reduce, state, state as final, transition } from '../machine.js';

QUnit.module('Invoke', hooks => {
QUnit.module('Promise');
Expand Down Expand Up @@ -275,4 +275,30 @@ QUnit.module('Invoke', hooks => {

service.send('next');
});

QUnit.test('matches() matches the state', async assert => {
const machine = createMachine({
one: state(
transition('next', 'two')
),
two: nested('three', {
alpha: state(
transition('next', 'beta')
),
beta: state()
}),
three: state()
});

let service = interpret(machine, () => {});
assert.equal(service.matches('one'), true, 'is the first state');
assert.equal(service.matches('two.alpha'), false, 'not in the child state');

service.send('next');
assert.equal(service.matches('two'), true, 'is in the two state');
assert.equal(service.matches('two.alpha'), true, 'in the alpha state');

service.child.send('next');
assert.equal(service.matches('three'), true, 'In the last state');
});
});