import { describe, it, expect, vi, beforeEach } from 'vitest'
import { mount } from '@vue/test-utils'
import { createPinia, setActivePinia } from 'pinia'
const mockPush = vi.fn()
let mockRoutePath = '/cloud/uuid-conn-1/some-folder'
vi.mock('vue-router', () => ({
useRouter: () => ({ push: mockPush }),
useRoute: () => ({ path: mockRoutePath }),
}))
vi.mock('../../../api/client.js', () => ({
getCloudFoldersByConnectionId: vi.fn().mockResolvedValue({ items: [] }),
getCloudFolders: vi.fn(), // deprecated — must NOT be called
}))
import * as api from '../../../api/client.js'
import CloudFolderTreeItem from '../CloudFolderTreeItem.vue'
const TreeItemStub = {
name: 'TreeItem',
template: `
`,
props: ['label', 'loadChildren', 'depth', 'isActive', 'expandable'],
emits: ['select'],
}
const globalStubs = {
TreeItem: TreeItemStub,
AppIcon: true,
}
const folder = { id: 'some-folder', name: 'Documents', is_dir: true }
beforeEach(() => {
setActivePinia(createPinia())
vi.clearAllMocks()
mockRoutePath = '/cloud/uuid-conn-1/some-folder'
})
describe('CloudFolderTreeItem', () => {
it('navigates to /cloud/{connectionId}/{folderId}, never /cloud/{provider}/{folderId}', async () => {
const wrapper = mount(CloudFolderTreeItem, {
props: { folder, connectionId: 'uuid-conn-1', depth: 2 },
global: { stubs: globalStubs },
})
await wrapper.find('[data-test="tree-item"]').trigger('click')
expect(mockPush).toHaveBeenCalledWith('/cloud/uuid-conn-1/some-folder')
expect(mockPush).not.toHaveBeenCalledWith(expect.stringContaining('nextcloud'))
})
it('browses children using getCloudFoldersByConnectionId with folder.id', async () => {
const wrapper = mount(CloudFolderTreeItem, {
props: { folder, connectionId: 'uuid-conn-1', depth: 2 },
global: { stubs: globalStubs },
})
const treeItemStub = wrapper.findComponent(TreeItemStub)
const loadChildren = treeItemStub.props('loadChildren')
if (loadChildren) {
await loadChildren()
expect(api.getCloudFoldersByConnectionId).toHaveBeenCalledWith('uuid-conn-1', 'some-folder')
}
expect(api.getCloudFolders).not.toHaveBeenCalled()
})
it('isActive=true when current route matches this folder', () => {
const wrapper = mount(CloudFolderTreeItem, {
props: { folder, connectionId: 'uuid-conn-1', depth: 2 },
global: { stubs: globalStubs },
})
expect(wrapper.findComponent(TreeItemStub).props('isActive')).toBe(true)
})
it('isActive=false when route does not match this folder', () => {
mockRoutePath = '/cloud/uuid-conn-1/other-folder'
const wrapper = mount(CloudFolderTreeItem, {
props: { folder, connectionId: 'uuid-conn-1', depth: 2 },
global: { stubs: globalStubs },
})
expect(wrapper.findComponent(TreeItemStub).props('isActive')).toBe(false)
})
})