Skip to content

Commit

Permalink
[@xstate/graph] Mock actorContext (#4308)
Browse files Browse the repository at this point in the history
* Add mock actorContext

* Changeset

* Refine test
  • Loading branch information
davidkpiano authored Sep 24, 2023
1 parent 5fd8f26 commit af032db
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 25 deletions.
21 changes: 21 additions & 0 deletions .changeset/angry-seals-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
'@xstate/graph': patch
---

Traversing state machines that have delayed transitions will now work as expected:

```ts
const machine = createMachine({
initial: 'a',
states: {
a: {
after: {
1000: 'b'
}
},
b: {}
}
});

const paths = getShortestPaths(machine); // works
```
14 changes: 14 additions & 0 deletions packages/xstate-graph/src/actorContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { AnyActorContext, createEmptyActor } from 'xstate';

export function createMockActorContext(): AnyActorContext {
const emptyActor = createEmptyActor();
return {
self: emptyActor,
logger: console.log,
id: '',
sessionId: Math.random().toString(32).slice(2),
defer: () => {},
system: emptyActor,
stopChild: () => {}
};
}
3 changes: 2 additions & 1 deletion packages/xstate-graph/src/adjacency.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ActorLogic, ActorSystem, EventObject, Snapshot } from 'xstate';
import { SerializedEvent, SerializedState, TraversalOptions } from './types';
import { AdjacencyMap, resolveTraversalOptions } from './graph';
import { createMockActorContext } from './actorContext';

export function getAdjacencyMap<
TSnapshot extends Snapshot<unknown>,
Expand All @@ -21,7 +22,7 @@ export function getAdjacencyMap<
fromState: customFromState,
stopCondition
} = resolveTraversalOptions(logic, options);
const actorContext = { self: {} } as any; // TODO: figure out the simulation API
const actorContext = createMockActorContext();
const fromState =
customFromState ??
logic.getInitialState(
Expand Down
7 changes: 4 additions & 3 deletions packages/xstate-graph/src/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
AnyStateNode,
TraversalConfig
} from './types.ts';
import { createMockActorContext } from './actorContext.ts';

function flatten<T>(array: Array<T | T[]>): T[] {
return ([] as T[]).concat(...array);
Expand Down Expand Up @@ -101,9 +102,9 @@ export function createDefaultMachineOptions<TMachine extends AnyStateMachine>(
})
) as any[];
},
fromState: machine.getInitialState(
{} as any // TODO: figure out the simulation API
) as ReturnType<TMachine['transition']>,
fromState: machine.getInitialState(createMockActorContext()) as ReturnType<
TMachine['transition']
>,
...otherOptions
};

Expand Down
3 changes: 2 additions & 1 deletion packages/xstate-graph/src/pathFromEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
createDefaultLogicOptions
} from './graph';
import { alterPath } from './alterPath';
import { createMockActorContext } from './actorContext';

function isMachine(value: any): value is AnyStateMachine {
return !!value && '__xstatenode' in value;
Expand All @@ -45,7 +46,7 @@ export function getPathsFromEvents<
? createDefaultMachineOptions(logic)
: createDefaultLogicOptions()) as TraversalOptions<TSnapshot, TEvent>
);
const actorContext = { self: {} } as any; // TODO: figure out the simulation API
const actorContext = createMockActorContext();
const fromState =
resolvedOptions.fromState ??
logic.getInitialState(
Expand Down
6 changes: 2 additions & 4 deletions packages/xstate-graph/src/shortestPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
StatePlanMap,
TraversalOptions
} from './types';
import { createMockActorContext } from './actorContext';

export function getShortestPaths<TLogic extends AnyActorLogic>(
logic: TLogic,
Expand All @@ -26,10 +27,7 @@ export function getShortestPaths<TLogic extends AnyActorLogic>(
) => SerializedState;
const fromState =
resolvedOptions.fromState ??
logic.getInitialState(
{} as any, // TODO: figure out the simulation API
undefined
);
logic.getInitialState(createMockActorContext(), undefined);
const adjacency = getAdjacencyMap(logic, resolvedOptions);

// weight, state, event
Expand Down
3 changes: 2 additions & 1 deletion packages/xstate-graph/src/simplePaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { resolveTraversalOptions, createDefaultMachineOptions } from './graph';
import { getAdjacencyMap } from './adjacency';
import { alterPath } from './alterPath';
import { createMockActorContext } from './actorContext';

export function getSimplePaths<TLogic extends AnyActorLogic>(
logic: TLogic,
Expand All @@ -31,7 +32,7 @@ export function getSimplePaths<TLogic extends AnyActorLogic>(
type TEvent = EventFromLogic<TLogic>;

const resolvedOptions = resolveTraversalOptions(logic, options);
const actorContext = { self: {} } as any; // TODO: figure out the simulation API
const actorContext = createMockActorContext();
const fromState =
resolvedOptions.fromState ?? logic.getInitialState(actorContext, undefined);
const serializeState = resolvedOptions.serializeState as (
Expand Down
21 changes: 6 additions & 15 deletions packages/xstate-graph/test/graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
getSimplePaths
} from '../src';
import { joinPaths } from '../src/graph';
import { createMockActorContext } from '../src/actorContext';

function getPathsSnapshot(
paths: Array<StatePath<Snapshot<unknown>, EventObject>>
Expand Down Expand Up @@ -216,9 +217,7 @@ describe('@xstate/graph', () => {
expect(
shortestPaths.find((path) =>
path.state.matches(
lightMachine.getInitialState(
{} as any // TODO: figure out the simulation API
).value
lightMachine.getInitialState(createMockActorContext()).value
)
)!.steps
).toHaveLength(1);
Expand Down Expand Up @@ -377,36 +376,28 @@ describe('@xstate/graph', () => {
expect(
getSimplePaths(lightMachine).find((p) =>
p.state.matches(
lightMachine.getInitialState(
{} as any // TODO: figure out the simulation API
).value
lightMachine.getInitialState(createMockActorContext()).value
)
)
).toBeDefined();
expect(
getSimplePaths(lightMachine).find((p) =>
p.state.matches(
lightMachine.getInitialState(
{} as any // TODO: figure out the simulation API
).value
lightMachine.getInitialState(createMockActorContext()).value
)
)!.steps
).toHaveLength(1);
expect(
getSimplePaths(equivMachine).find((p) =>
p.state.matches(
equivMachine.getInitialState(
{} as any // TODO: figure out the simulation API
).value
equivMachine.getInitialState(createMockActorContext()).value
)
)!
).toBeDefined();
expect(
getSimplePaths(equivMachine).find((p) =>
p.state.matches(
equivMachine.getInitialState(
{} as any // TODO: figure out the simulation API
).value
equivMachine.getInitialState(createMockActorContext()).value
)
)!.steps
).toHaveLength(1);
Expand Down
21 changes: 21 additions & 0 deletions packages/xstate-graph/test/shortestPaths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,25 @@ describe('getShortestPaths', () => {

expect(pathWithTwoTodos).toBeDefined();
});

it('should work for machines with delays', () => {
const machine = createMachine({
initial: 'a',
states: {
a: {
after: {
1000: 'b'
}
},
b: {}
}
});

const shortestPaths = getShortestPaths(machine);

expect(shortestPaths.map((p) => p.steps.map((s) => s.event.type))).toEqual([
['xstate.init'],
['xstate.init', 'xstate.after(1000)#(machine).a']
]);
});
});

0 comments on commit af032db

Please sign in to comment.