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

Implement guard insertions #482

Merged
merged 1 commit into from
Mar 11, 2024
Merged
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
378 changes: 378 additions & 0 deletions new-packages/ts-project/__tests__/source-edits/add-guard.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,378 @@
import { expect, test } from 'vitest';
import { createTestProject, testdir, ts } from '../utils';

test('should be possible to add a guard to a transition', async () => {
const tmpPath = await testdir({
'tsconfig.json': JSON.stringify({}),
'index.ts': ts`
import { createMachine } from "xstate";

createMachine({
on: {
FOO: "a",
},
states: {
a: {},
},
});
`,
});

const project = await createTestProject(tmpPath);

const textEdits = project.editDigraph(
{
fileName: 'index.ts',
machineIndex: 0,
},
{
type: 'add_guard',
path: [],
transitionPath: ['on', 'FOO', 0],
name: 'isItTooLate',
},
);
expect(await project.applyTextEdits(textEdits)).toMatchInlineSnapshot(`
{
"index.ts": "import { createMachine } from "xstate";

createMachine({
on: {
FOO: {
target: "a",
guard: "isItTooLate"
},
},
states: {
a: {},
},
});",
}
`);
});

test('should be possible to add a guard to an object transition', async () => {
const tmpPath = await testdir({
'tsconfig.json': JSON.stringify({}),
'index.ts': ts`
import { createMachine } from "xstate";

createMachine({
on: {
FOO: {
target: "a",
},
},
states: {
a: {},
},
});
`,
});

const project = await createTestProject(tmpPath);

const textEdits = project.editDigraph(
{
fileName: 'index.ts',
machineIndex: 0,
},
{
type: 'add_guard',
path: [],
transitionPath: ['on', 'FOO', 0],
name: 'isItTooLate',
},
);
expect(await project.applyTextEdits(textEdits)).toMatchInlineSnapshot(`
{
"index.ts": "import { createMachine } from "xstate";

createMachine({
on: {
FOO: {
target: "a",
guard: "isItTooLate",
},
},
states: {
a: {},
},
});",
}
`);
});

test('should be possible to add a guard to the first transition for a given event', async () => {
const tmpPath = await testdir({
'tsconfig.json': JSON.stringify({}),
'index.ts': ts`
import { createMachine } from "xstate";

createMachine({
on: {
FOO: ["a", "b", "c"],
},
states: {
a: {},
b: {},
c: {},
},
});
`,
});

const project = await createTestProject(tmpPath);

const textEdits = project.editDigraph(
{
fileName: 'index.ts',
machineIndex: 0,
},
{
type: 'add_guard',
path: [],
transitionPath: ['on', 'FOO', 0],
name: 'isItTooLate',
},
);
expect(await project.applyTextEdits(textEdits)).toMatchInlineSnapshot(`
{
"index.ts": "import { createMachine } from "xstate";

createMachine({
on: {
FOO: [{
target: "a",
guard: "isItTooLate"
}, "b", "c"],
},
states: {
a: {},
b: {},
c: {},
},
});",
}
`);
});

test('should be possible to add a guard to the last transition for a given event', async () => {
const tmpPath = await testdir({
'tsconfig.json': JSON.stringify({}),
'index.ts': ts`
import { createMachine } from "xstate";

createMachine({
on: {
FOO: ["a", "b", "c"],
},
states: {
a: {},
b: {},
c: {},
},
});
`,
});

const project = await createTestProject(tmpPath);

const textEdits = project.editDigraph(
{
fileName: 'index.ts',
machineIndex: 0,
},
{
type: 'add_guard',
path: [],
transitionPath: ['on', 'FOO', 2],
name: 'isItTooLate',
},
);
expect(await project.applyTextEdits(textEdits)).toMatchInlineSnapshot(`
{
"index.ts": "import { createMachine } from "xstate";

createMachine({
on: {
FOO: ["a", "b", {
target: "c",
guard: "isItTooLate"
}],
},
states: {
a: {},
b: {},
c: {},
},
});",
}
`);
});

test('should be possible to add a guard to a middle transition for a given event', async () => {
const tmpPath = await testdir({
'tsconfig.json': JSON.stringify({}),
'index.ts': ts`
import { createMachine } from "xstate";

createMachine({
on: {
FOO: ["a", "b", "c"],
},
states: {
a: {},
b: {},
c: {},
},
});
`,
});

const project = await createTestProject(tmpPath);

const textEdits = project.editDigraph(
{
fileName: 'index.ts',
machineIndex: 0,
},
{
type: 'add_guard',
path: [],
transitionPath: ['on', 'FOO', 1],
name: 'isItTooLate',
},
);
expect(await project.applyTextEdits(textEdits)).toMatchInlineSnapshot(`
{
"index.ts": "import { createMachine } from "xstate";

createMachine({
on: {
FOO: ["a", {
target: "b",
guard: "isItTooLate"
}, "c"],
},
states: {
a: {},
b: {},
c: {},
},
});",
}
`);
});

test(`should be possible to add a guard to invoke's onDone`, async () => {
const tmpPath = await testdir({
'tsconfig.json': JSON.stringify({}),
'index.ts': ts`
import { createMachine } from "xstate";

createMachine({
states: {
a: {
invoke: {
src: "callDavid",
onDone: "b",
},
},
b: {},
},
});
`,
});

const project = await createTestProject(tmpPath);

const textEdits = project.editDigraph(
{
fileName: 'index.ts',
machineIndex: 0,
},
{
type: 'add_guard',
path: ['a'],
transitionPath: ['invoke', 0, 'onDone', 0],
name: 'isHeBusy',
},
);
expect(await project.applyTextEdits(textEdits)).toMatchInlineSnapshot(`
{
"index.ts": "import { createMachine } from "xstate";

createMachine({
states: {
a: {
invoke: {
src: "callDavid",
onDone: {
target: "b",
guard: "isHeBusy"
},
},
},
b: {},
},
});",
}
`);
});

test(`should be possible to add a guard to a transition defined for an empty event`, async () => {
const tmpPath = await testdir({
'tsconfig.json': JSON.stringify({}),
'index.ts': ts`
import { createMachine } from "xstate";

createMachine({
initial: "a",
states: {
a: {
on: {
"": "b",
},
},
b: {},
},
});
`,
});

const project = await createTestProject(tmpPath);

const textEdits = project.editDigraph(
{
fileName: 'index.ts',
machineIndex: 0,
},
{
type: 'add_guard',
path: ['a'],
transitionPath: ['on', '', 0],
name: 'isItHalfEmpty',
},
);
expect(await project.applyTextEdits(textEdits)).toMatchInlineSnapshot(`
{
"index.ts": "import { createMachine } from "xstate";

createMachine({
initial: "a",
states: {
a: {
on: {
"": {
target: "b",
guard: "isItHalfEmpty"
},
},
},
b: {},
},
});",
}
`);
});
Loading
Loading