chore: basic machine vitest... need to figure out how to get window.electron implemented in vitest?

This commit is contained in:
Kevin Nadro
2025-04-15 07:41:22 -06:00
parent c9587fda07
commit 9978ad6e23
2 changed files with 69 additions and 2 deletions

View File

@ -0,0 +1,66 @@
import { DEFAULT_PROJECT_NAME } from '@src/lib/constants'
import { systemIOMachineDesktop } from '@src/machines/systemIO/systemIOMachineDesktop'
import {
NO_PROJECT_DIRECTORY,
SystemIOMachineEvents,
SystemIOMachineStates,
} from '@src/machines/systemIO/utils'
import path from 'node:path'
import { createActor, waitFor } from 'xstate'
describe('systemIOMachine - XState', () => {
describe('desktop', () => {
describe('when initializied', () => {
it('should contain the default context values', () => {
const actor = createActor(systemIOMachineDesktop).start()
const context = actor.getSnapshot().context
expect(context.folders).toStrictEqual([])
expect(context.defaultProjectFolderName).toStrictEqual(
DEFAULT_PROJECT_NAME
)
expect(context.projectDirectoryPath).toBe(NO_PROJECT_DIRECTORY)
expect(context.hasListedProjects).toBe(false)
expect(context.requestedProjectName).toStrictEqual({
name: NO_PROJECT_DIRECTORY,
})
expect(context.requestedFileName).toStrictEqual({
project: NO_PROJECT_DIRECTORY,
file: NO_PROJECT_DIRECTORY,
})
})
it('should be in idle state', () => {
const actor = createActor(systemIOMachineDesktop).start()
const state = actor.getSnapshot().value
expect(state).toBe(SystemIOMachineStates.idle)
})
})
describe('when reading projects', () => {
it('should exit early when project directory is empty string', async () => {
const actor = createActor(systemIOMachineDesktop).start()
actor.send({
type: SystemIOMachineEvents.readFoldersFromProjectDirectory,
})
await waitFor(actor, (state) =>
state.matches(SystemIOMachineStates.readingFolders)
)
await waitFor(actor, (state) =>
state.matches(SystemIOMachineStates.idle)
)
const context = actor.getSnapshot().context
expect(context.folders).toStrictEqual([])
})
})
describe('when setting project directory path', () => {
it('should set new project directory path', async () => {
const kclSamplesPath = path.join('public', 'kcl-samples')
const actor = createActor(systemIOMachineDesktop).start()
actor.send({
type: SystemIOMachineEvents.setProjectDirectoryPath,
data: { requestedProjectDirectoryPath: kclSamplesPath },
})
let context = actor.getSnapshot().context
expect(context.projectDirectoryPath).toBe(kclSamplesPath)
})
})
})
})

View File

@ -157,8 +157,9 @@ export const systemIOMachine = setup({
[SystemIOMachineStates.idle]: {
on: {
// on can be an action
[SystemIOMachineEvents.readFoldersFromProjectDirectory]:
SystemIOMachineStates.readingFolders,
[SystemIOMachineEvents.readFoldersFromProjectDirectory]: {
target: SystemIOMachineStates.readingFolders,
},
[SystemIOMachineEvents.setProjectDirectoryPath]: {
target: SystemIOMachineStates.readingFolders,
actions: [SystemIOMachineActions.setProjectDirectoryPath],