Skip to content

Commit

Permalink
Add extra tests for adding a state
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Mar 11, 2024
1 parent a4c5608 commit 569ccbc
Showing 1 changed file with 133 additions and 0 deletions.
133 changes: 133 additions & 0 deletions new-packages/ts-project/__tests__/source-edits/add-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,56 @@ import { outdent } from 'outdent';
import { expect, test } from 'vitest';
import { createTestProject, testdir, ts } from '../utils';

test(`should append a state after existing states of a nested state`, async () => {
const tmpPath = await testdir({
'tsconfig.json': JSON.stringify({}),
'index.ts': ts`
import { createMachine } from "xstate";
createMachine({
states: {
foo: {
states: {
bar: {},
},
},
},
});
`,
});

const project = await createTestProject(tmpPath);

const textEdits = project.editDigraph(
{
fileName: 'index.ts',
machineIndex: 0,
},

{
type: 'add_state',
path: ['foo'],
name: 'just_added',
},
);
expect(await project.applyTextEdits(textEdits)).toMatchInlineSnapshot(`
{
"index.ts": "import { createMachine } from "xstate";
createMachine({
states: {
foo: {
states: {
bar: {},
just_added: {},
},
},
},
});",
}
`);
});

test('should append a state after existing states (with trailing comma)', async () => {
const tmpPath = await testdir({
'tsconfig.json': JSON.stringify({}),
Expand Down Expand Up @@ -617,6 +667,43 @@ test('should successfully add a state to the root and use it as initial state of
);
});

// this shouldn't be possible in the Studio but we handle this here regardless
test('should be possible to add a state to the empty root (without making it its initial state)', async () => {
const tmpPath = await testdir({
'tsconfig.json': JSON.stringify({}),
'index.ts': ts`
import { createMachine } from "xstate";
createMachine({});
`,
});

const project = await createTestProject(tmpPath);

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

test('should successfully add a state to a nested state and use it as initial state of its parent', async () => {
const tmpPath = await testdir({
'tsconfig.json': JSON.stringify({}),
Expand Down Expand Up @@ -712,3 +799,49 @@ test(`should be possible to add a state with name that isn't a valid identifier`
`,
);
});

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

const project = await createTestProject(tmpPath);

const textEdits = project.editDigraph(
{
fileName: 'index.ts',
machineIndex: 0,
},
[
{
type: 'add_state',
path: ['foo'],
name: 'just_added',
},
],
);
expect(await project.applyTextEdits(textEdits)).toMatchInlineSnapshot(`
{
"index.ts": "import { createMachine } from "xstate";
createMachine({
states: {
foo: {
states: {
just_added: {}
}
},
},
});",
}
`);
});

0 comments on commit 569ccbc

Please sign in to comment.