Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/components/Code/FileTree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const { toggleDir, isExpanded, autoExpandAncestors } = useFileTreeState(props.ba
watch(
() => props.currentPath,
path => {
if (path) {
if (depth.value === 0 && path) {
autoExpandAncestors(path)
}
},
Expand Down
2 changes: 2 additions & 0 deletions app/composables/useFileTreeState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export function useFileTreeState(baseUrl: string) {
} else {
expanded.value.add(path)
}
expanded.value = new Set(expanded.value)
}

function isExpanded(path: string) {
Expand All @@ -21,6 +22,7 @@ export function useFileTreeState(baseUrl: string) {
prefix = prefix ? `${prefix}/${part}` : part
expanded.value.add(prefix)
}
expanded.value = new Set(expanded.value)
}

return {
Expand Down
89 changes: 89 additions & 0 deletions test/nuxt/components/CodeFileTree.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { mountSuspended } from '@nuxt/test-utils/runtime'
import { describe, expect, it, vi } from 'vitest'
import CodeFileTree from '~/components/Code/FileTree.vue'

const mockTree: PackageFileTree[] = [
{
name: 'distribution',
type: 'directory',
path: 'distribution',
children: [
{
name: 'core',
type: 'directory',
path: 'distribution/core',
children: [
{
name: 'constants.d.ts',
type: 'file',
path: 'distribution/core/constants.d.ts',
},
],
},
{
name: 'types',
type: 'directory',
path: 'distribution/types',
children: [
{
name: 'common.d.ts',
type: 'file',
path: 'distribution/types/common.d.ts',
},
],
},
],
},
]

function findDirButton(wrapper: Awaited<ReturnType<typeof mountCodeFileTree>>, name: string) {
return wrapper.findAll('button').find(button => button.text().trim() === name)
}

async function mountCodeFileTree() {
return mountSuspended(CodeFileTree, {
attachTo: document.body,
props: {
tree: mockTree,
currentPath: 'distribution/core/constants.d.ts',
baseUrl: '/package-code/ky/v/1.14.3/distribution/core/constants.d.ts?test=tree',
baseRoute: {
params: {
packageName: 'ky',
version: '1.14.3',
filePath: '',
},
},
},
})
}

describe('CodeFileTree', () => {
it('keeps a collapsed sibling directory closed when another sibling expands', async () => {
Copy link
Copy Markdown
Contributor

@jhroemer jhroemer Apr 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm personally a little wary of tests like this, because the space of "this thing should not happen" is incredibly large. There's nothing wrong with the expectation, but maybe it can just be part of a normal collapse/expand test, instead of being a test specifically for this bug. Wdyt?

const wrapper = await mountCodeFileTree()

await vi.waitFor(() => {
expect(wrapper.text()).toContain('constants.d.ts')
expect(wrapper.text()).not.toContain('common.d.ts')
})

const coreButton = findDirButton(wrapper, 'core')
expect(coreButton).toBeDefined()
await coreButton!.trigger('click')

await vi.waitFor(() => {
expect(wrapper.text()).not.toContain('constants.d.ts')
})

const typesButton = findDirButton(wrapper, 'types')
expect(typesButton).toBeDefined()
await typesButton!.trigger('click')

await vi.waitFor(() => {
expect(wrapper.text()).toContain('common.d.ts')
expect(wrapper.text()).not.toContain('constants.d.ts')
})

wrapper.unmount()
})
Comment on lines +63 to +88
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Ensure cleanup always runs when the test fails.

With attachTo: document.body, cleanup at Line 87 is skipped if an earlier assertion fails. Wrap the test body in try/finally so unmount() is guaranteed.

♻️ Suggested reliability fix
 it('keeps a collapsed sibling directory closed when another sibling expands', async () => {
   const wrapper = await mountCodeFileTree()
+  try {
 
-  await vi.waitFor(() => {
-    expect(wrapper.text()).toContain('constants.d.ts')
-    expect(wrapper.text()).not.toContain('common.d.ts')
-  })
+    await vi.waitFor(() => {
+      expect(wrapper.text()).toContain('constants.d.ts')
+      expect(wrapper.text()).not.toContain('common.d.ts')
+    })
 
-  const coreButton = findDirButton(wrapper, 'core')
-  expect(coreButton).toBeDefined()
-  await coreButton!.trigger('click')
+    const coreButton = findDirButton(wrapper, 'core')
+    expect(coreButton).toBeDefined()
+    await coreButton!.trigger('click')
 
-  await vi.waitFor(() => {
-    expect(wrapper.text()).not.toContain('constants.d.ts')
-  })
+    await vi.waitFor(() => {
+      expect(wrapper.text()).not.toContain('constants.d.ts')
+    })
 
-  const typesButton = findDirButton(wrapper, 'types')
-  expect(typesButton).toBeDefined()
-  await typesButton!.trigger('click')
+    const typesButton = findDirButton(wrapper, 'types')
+    expect(typesButton).toBeDefined()
+    await typesButton!.trigger('click')
 
-  await vi.waitFor(() => {
-    expect(wrapper.text()).toContain('common.d.ts')
-    expect(wrapper.text()).not.toContain('constants.d.ts')
-  })
+    await vi.waitFor(() => {
+      expect(wrapper.text()).toContain('common.d.ts')
+      expect(wrapper.text()).not.toContain('constants.d.ts')
+    })
 
-  wrapper.unmount()
+  } finally {
+    wrapper.unmount()
+  }
 })

})
Loading