Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard params, action params and actor input #449

Draft
wants to merge 3 commits into
base: scaffold-new-version
Choose a base branch
from
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
123 changes: 123 additions & 0 deletions new-packages/ts-project/__tests__/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -987,3 +987,126 @@ test('should not register multiple transition actions with duplicated actions pr
]
`);
});
test('should extract action params from parameterized action', async () => {
const tmpPath = await testdir({
'tsconfig.json': JSON.stringify({}),
'index.ts': ts`
import { createMachine } from "xstate";

createMachine({
initial: "foo",
states: {
foo: {
entry: {
type: "callDavid",
params: {
str: "some string",
num: 123,
bool: true,
arr: [1, 2, [3, 4]],
obj: {
x: {
y: 1,
},
},
},
},
},
},
});
`,
});

const project = await createTestProject(tmpPath);
expect(replaceUniqueIds(project.extractMachines('index.ts')))
.toMatchInlineSnapshot(`
[
[
{
"blocks": {
"block-0": {
"blockType": "action",
"parentId": "state-1",
"properties": {
"params": {
"arr": [
1,
2,
[
3,
4,
],
],
"bool": true,
"num": 123,
"obj": {
"x": {
"y": 1,
},
},
"str": "some string",
},
"type": "callDavid",
},
"sourceId": "callDavid",
"uniqueId": "block-0",
},
},
"data": {
"context": {},
},
"edges": {},
"implementations": {
"actions": {
"callDavid": {
"id": "callDavid",
"name": "callDavid",
"type": "action",
},
},
"actors": {},
"guards": {},
},
"nodes": {
"state-0": {
"data": {
"description": undefined,
"entry": [],
"exit": [],
"history": undefined,
"initial": "foo",
"invoke": [],
"metaEntries": [],
"tags": [],
"type": "normal",
},
"parentId": undefined,
"type": "node",
"uniqueId": "state-0",
},
"state-1": {
"data": {
"description": undefined,
"entry": [
"block-0",
],
"exit": [],
"history": undefined,
"initial": undefined,
"invoke": [],
"metaEntries": [],
"tags": [],
"type": "normal",
},
"parentId": "state-0",
"type": "node",
"uniqueId": "state-1",
},
},
"root": "state-0",
},
[],
],
]
`);
});
130 changes: 130 additions & 0 deletions new-packages/ts-project/__tests__/actors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ test('should extract an actor with string src (direct)', async () => {
"parentId": "state-0",
"properties": {
"id": "inline:actor-id-0",
"input": {},
"src": "foo",
},
"sourceId": "foo",
Expand Down Expand Up @@ -111,6 +112,7 @@ test('should extract multiple actors with different string sources (direct)', as
"parentId": "state-1",
"properties": {
"id": "inline:actor-id-0",
"input": {},
"src": "actor1",
},
"sourceId": "actor1",
Expand All @@ -121,6 +123,7 @@ test('should extract multiple actors with different string sources (direct)', as
"parentId": "state-2",
"properties": {
"id": "inline:actor-id-1",
"input": {},
"src": "actor2",
},
"sourceId": "actor2",
Expand Down Expand Up @@ -244,6 +247,7 @@ test('should extract multiple actors with the same string source (direct)', asyn
"parentId": "state-1",
"properties": {
"id": "inline:actor-id-0",
"input": {},
"src": "actor1",
},
"sourceId": "actor1",
Expand All @@ -254,6 +258,7 @@ test('should extract multiple actors with the same string source (direct)', asyn
"parentId": "state-2",
"properties": {
"id": "inline:actor-id-1",
"input": {},
"src": "actor1",
},
"sourceId": "actor1",
Expand Down Expand Up @@ -372,6 +377,7 @@ test('should extract multiple actors with string source (array)', async () => {
"parentId": "state-1",
"properties": {
"id": "inline:actor-id-0",
"input": {},
"src": "actor1",
},
"sourceId": "actor1",
Expand All @@ -382,6 +388,7 @@ test('should extract multiple actors with string source (array)', async () => {
"parentId": "state-1",
"properties": {
"id": "inline:actor-id-1",
"input": {},
"src": "actor2",
},
"sourceId": "actor2",
Expand Down Expand Up @@ -488,6 +495,7 @@ test('should extract actor with inline source (direct)', async () => {
"parentId": "state-1",
"properties": {
"id": "inline:actor-id-0",
"input": {},
"src": "inline:actor-0",
},
"sourceId": "inline:actor-0",
Expand All @@ -498,6 +506,7 @@ test('should extract actor with inline source (direct)', async () => {
"parentId": "state-2",
"properties": {
"id": "inline:actor-id-1",
"input": {},
"src": "inline:actor-1",
},
"sourceId": "inline:actor-1",
Expand Down Expand Up @@ -695,6 +704,127 @@ test('should extract actor id if it is present with a string value', async () =>
"parentId": "state-1",
"properties": {
"id": "user-provided-id",
"input": {},
"src": "actor1",
},
"sourceId": "actor1",
"uniqueId": "block-0",
},
},
"data": {
"context": {},
},
"edges": {},
"implementations": {
"actions": {},
"actors": {
"actor1": {
"id": "actor1",
"name": "actor1",
"type": "actor",
},
},
"guards": {},
},
"nodes": {
"state-0": {
"data": {
"description": undefined,
"entry": [],
"exit": [],
"history": undefined,
"initial": "foo",
"invoke": [],
"metaEntries": [],
"tags": [],
"type": "normal",
},
"parentId": undefined,
"type": "node",
"uniqueId": "state-0",
},
"state-1": {
"data": {
"description": undefined,
"entry": [],
"exit": [],
"history": undefined,
"initial": undefined,
"invoke": [
"block-0",
],
"metaEntries": [],
"tags": [],
"type": "normal",
},
"parentId": "state-0",
"type": "node",
"uniqueId": "state-1",
},
},
"root": "state-0",
},
[],
],
]
`);
});
test('should extract actor input', async () => {
const tmpPath = await testdir({
'tsconfig.json': JSON.stringify({}),
'index.ts': ts`
import { createMachine } from "xstate";

createMachine({
initial: "foo",
states: {
foo: {
invoke: {
src: "actor1",
input: {
str: "some string",
num: 123,
bool: true,
arr: [1, 2, [3, 4]],
obj: { x: { y: 1 } },
},
},
},
},
});
`,
});

const project = await createTestProject(tmpPath);
expect(replaceUniqueIds(project.extractMachines('index.ts')))
.toMatchInlineSnapshot(`
[
[
{
"blocks": {
"block-0": {
"blockType": "actor",
"parentId": "state-1",
"properties": {
"id": "inline:actor-id-0",
"input": {
"arr": [
1,
2,
[
3,
4,
],
],
"bool": true,
"num": 123,
"obj": {
"x": {
"y": 1,
},
},
"str": "some string",
},
"src": "actor1",
},
"sourceId": "actor1",
Expand Down
Loading