-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
448 additions
and
16 deletions.
There are no files selected for viewing
336 changes: 336 additions & 0 deletions
336
new-packages/ts-project/__tests__/source-edits/add-invoke.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,336 @@ | ||
import { expect, test } from 'vitest'; | ||
import { createTestProject, testdir, ts } from '../utils'; | ||
|
||
test('should be possible to add an invoke to the root', 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_invoke', | ||
path: [], | ||
invokeIndex: 0, | ||
source: 'callDavid', | ||
}, | ||
); | ||
expect(await project.applyTextEdits(textEdits)).toMatchInlineSnapshot(` | ||
{ | ||
"index.ts": "import { createMachine } from "xstate"; | ||
createMachine({ | ||
invoke: { | ||
src: "callDavid" | ||
} | ||
});", | ||
} | ||
`); | ||
}); | ||
|
||
test('should be possible to add an invoke to a nested state', 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_invoke', | ||
path: ['a'], | ||
invokeIndex: 0, | ||
source: 'callDavid', | ||
}, | ||
); | ||
expect(await project.applyTextEdits(textEdits)).toMatchInlineSnapshot(` | ||
{ | ||
"index.ts": "import { createMachine } from "xstate"; | ||
createMachine({ | ||
on: { | ||
FOO: "a", | ||
}, | ||
states: { | ||
a: { | ||
invoke: { | ||
src: "callDavid" | ||
} | ||
}, | ||
}, | ||
});", | ||
} | ||
`); | ||
}); | ||
|
||
test('should be possible to add an invoke with an ID', 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_invoke', | ||
path: [], | ||
invokeIndex: 0, | ||
source: 'callDavid', | ||
id: 'importantCall', | ||
}, | ||
); | ||
expect(await project.applyTextEdits(textEdits)).toMatchInlineSnapshot(` | ||
{ | ||
"index.ts": "import { createMachine } from "xstate"; | ||
createMachine({ | ||
invoke: { | ||
src: "callDavid", | ||
id: "importantCall" | ||
} | ||
});", | ||
} | ||
`); | ||
}); | ||
|
||
test('should be possible to add a new invoke to an existing invoke property', async () => { | ||
const tmpPath = await testdir({ | ||
'tsconfig.json': JSON.stringify({}), | ||
'index.ts': ts` | ||
import { createMachine } from "xstate"; | ||
createMachine({ | ||
invoke: { | ||
src: "callDavid", | ||
}, | ||
}); | ||
`, | ||
}); | ||
|
||
const project = await createTestProject(tmpPath); | ||
|
||
const textEdits = project.editDigraph( | ||
{ | ||
fileName: 'index.ts', | ||
machineIndex: 0, | ||
}, | ||
{ | ||
type: 'add_invoke', | ||
path: [], | ||
invokeIndex: 1, | ||
source: 'getRaise', | ||
}, | ||
); | ||
expect(await project.applyTextEdits(textEdits)).toMatchInlineSnapshot(` | ||
{ | ||
"index.ts": "import { createMachine } from "xstate"; | ||
createMachine({ | ||
invoke: [ { | ||
src: "callDavid", | ||
}, | ||
{ | ||
src: "getRaise" | ||
} | ||
], | ||
});", | ||
} | ||
`); | ||
}); | ||
|
||
test('should be possible to add a new invoke at the start of the existing invoke list', async () => { | ||
const tmpPath = await testdir({ | ||
'tsconfig.json': JSON.stringify({}), | ||
'index.ts': ts` | ||
import { createMachine } from "xstate"; | ||
createMachine({ | ||
invoke: [ | ||
{ | ||
src: "miny", | ||
}, | ||
{ | ||
src: "moe", | ||
}, | ||
], | ||
}); | ||
`, | ||
}); | ||
|
||
const project = await createTestProject(tmpPath); | ||
|
||
const textEdits = project.editDigraph( | ||
{ | ||
fileName: 'index.ts', | ||
machineIndex: 0, | ||
}, | ||
{ | ||
type: 'add_invoke', | ||
path: [], | ||
invokeIndex: 0, | ||
source: 'eeny', | ||
}, | ||
); | ||
expect(await project.applyTextEdits(textEdits)).toMatchInlineSnapshot(` | ||
{ | ||
"index.ts": "import { createMachine } from "xstate"; | ||
createMachine({ | ||
invoke: [ | ||
{ | ||
src: "eeny" | ||
}, | ||
{ | ||
src: "miny", | ||
}, | ||
{ | ||
src: "moe", | ||
}, | ||
], | ||
});", | ||
} | ||
`); | ||
}); | ||
|
||
test('should be possible to add a new invoke in the middle of the existing invoke list', async () => { | ||
const tmpPath = await testdir({ | ||
'tsconfig.json': JSON.stringify({}), | ||
'index.ts': ts` | ||
import { createMachine } from "xstate"; | ||
createMachine({ | ||
invoke: [ | ||
{ | ||
src: "eeny", | ||
}, | ||
{ | ||
src: "moe", | ||
}, | ||
], | ||
}); | ||
`, | ||
}); | ||
|
||
const project = await createTestProject(tmpPath); | ||
|
||
const textEdits = project.editDigraph( | ||
{ | ||
fileName: 'index.ts', | ||
machineIndex: 0, | ||
}, | ||
{ | ||
type: 'add_invoke', | ||
path: [], | ||
invokeIndex: 1, | ||
source: 'miny', | ||
}, | ||
); | ||
expect(await project.applyTextEdits(textEdits)).toMatchInlineSnapshot(` | ||
{ | ||
"index.ts": "import { createMachine } from "xstate"; | ||
createMachine({ | ||
invoke: [ | ||
{ | ||
src: "eeny", | ||
}, | ||
{ | ||
src: "miny" | ||
}, | ||
{ | ||
src: "moe", | ||
}, | ||
], | ||
});", | ||
} | ||
`); | ||
}); | ||
|
||
test('should be possible to add a new invoke at the end of the existing invoke list', async () => { | ||
const tmpPath = await testdir({ | ||
'tsconfig.json': JSON.stringify({}), | ||
'index.ts': ts` | ||
import { createMachine } from "xstate"; | ||
createMachine({ | ||
invoke: [ | ||
{ | ||
src: "eeny", | ||
}, | ||
{ | ||
src: "miny", | ||
}, | ||
], | ||
}); | ||
`, | ||
}); | ||
|
||
const project = await createTestProject(tmpPath); | ||
|
||
const textEdits = project.editDigraph( | ||
{ | ||
fileName: 'index.ts', | ||
machineIndex: 0, | ||
}, | ||
{ | ||
type: 'add_invoke', | ||
path: [], | ||
invokeIndex: 2, | ||
source: 'moe', | ||
}, | ||
); | ||
expect(await project.applyTextEdits(textEdits)).toMatchInlineSnapshot(` | ||
{ | ||
"index.ts": "import { createMachine } from "xstate"; | ||
createMachine({ | ||
invoke: [ | ||
{ | ||
src: "eeny", | ||
}, | ||
{ | ||
src: "miny", | ||
}, | ||
{ | ||
src: "moe" | ||
}, | ||
], | ||
});", | ||
} | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.