cmvr-visual/frontend/src/components/FolderManager.interactions.test.ts

90 lines
3.0 KiB
TypeScript
Raw Normal View History

import { afterEach, beforeEach, expect, it, vi } from 'vitest'
import { createRenderer, h, ssrContextKey } from 'vue'
import { createPinia, setActivePinia } from 'pinia'
import { ElMessageBox } from 'element-plus'
import { api } from '../api'
import { useFoldersStore } from '../stores/folders'
import { useAuthStore } from '../stores/auth'
import FolderManager from './FolderManager.vue'
const apps: any[] = []
beforeEach(() => {
setActivePinia(createPinia())
vi.spyOn(api, 'folders').mockResolvedValue({ items: [] })
})
afterEach(() => {
apps.splice(0).forEach((a) => a.unmount())
vi.restoreAllMocks()
})
function mount(folderId: string | null = null) {
let v: any
const renderer = createRenderer<any, any>({
createElement: () => ({}),
createText: () => ({}),
createComment: () => ({}),
setText() {},
setElementText() {},
parentNode: () => null,
nextSibling: () => null,
patchProp() {},
insert() {},
remove() {},
})
const app = renderer.createApp(
{
...FolderManager,
setup(p: any, c: any) {
v = (FolderManager as any).setup(p, c)
return () => h('section')
},
},
{ folderId },
)
app.use((useFoldersStore() as any)._p)
app.provide(ssrContextKey, { modules: new Set() })
app.mount({})
apps.push(app)
return v
}
it('opens a child creation dialog with current folder as parent and sends exact payload', async () => {
const store = useFoldersStore()
store.items = [{ id: 'current', name: 'parent', parent_id: null }] as any
const request = vi.spyOn(api, 'createFolder').mockResolvedValue({ id: 'child' } as any)
const v = mount('current')
v.open('create')
expect(v.dialogOpen.value).toBe(true)
expect(v.parent.value).toBe('current')
v.name.value = 'child'
await v.save()
expect(request).toHaveBeenCalledWith('child', 'current')
})
it('edits the current folder without asking for a second folder selection', async () => {
useFoldersStore().items = [{ id: 'current', name: 'old', parent_id: 'ancestor' }] as any
const request = vi.spyOn(api, 'updateFolder').mockResolvedValue({ id: 'current' } as any)
const v = mount('current')
v.open('edit')
expect(v.name.value).toBe('old')
expect(v.parent.value).toBe('ancestor')
v.name.value = 'new'
v.parent.value = null
await v.save()
expect(request).toHaveBeenCalledWith('current', { name: 'new', parent_id: null })
})
it('does not delete under a different session after confirmation resolves', async () => {
useAuthStore().user = { id: 'alice' } as any
useFoldersStore().items = [{ id: 'current', name: 'old', parent_id: null }] as any
let resolve!: () => void
vi.spyOn(ElMessageBox, 'confirm').mockImplementation(
() =>
new Promise((r) => {
resolve = () => r('confirm')
}) as any,
)
const request = vi.spyOn(api, 'deleteFolder')
const v = mount('current')
const pending = v.remove()
useAuthStore().user = { id: 'bob' } as any
resolve()
await pending
expect(request).not.toHaveBeenCalled()
})