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
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
comfyExpect as expect,
comfyPageFixture as test
} from '../../../fixtures/ComfyPage'

test.describe('Image Dropdown Virtualization', { tag: '@widget' }, () => {
test.beforeEach(async ({ comfyPage }) => {
await comfyPage.setSetting('Comfy.VueNodes.Enabled', true)
await comfyPage.vueNodes.waitForNodes()
})

test('should virtualize items when dropdown has many entries', async ({
comfyPage
}) => {
await comfyPage.loadWorkflow('widgets/load_image_widget')
await comfyPage.vueNodes.waitForNodes()

const totalItems = await comfyPage.page.evaluate(() => {
const node = window['graph']._nodes_by_id['10']
const widget = node.widgets.find(
(w: { name: string }) => w.name === 'image'
)
const count = 60
const values = Array.from(
{ length: count },
(_, i) => `test_image_${i}.png`
)
widget.options.values = values
widget.value = values[0]
return count
})

const loadImageNode = comfyPage.vueNodes.getNodeByTitle('Load Image')
const dropdownButton = loadImageNode.locator(
'button:has(span:has-text("test_image_0.png"))'
)
await dropdownButton.waitFor({ state: 'visible' })
await dropdownButton.click()

const virtualGridItems = comfyPage.page.locator('[data-virtual-grid-item]')
await expect(virtualGridItems.first()).toBeVisible()

const renderedCount = await virtualGridItems.count()
expect(renderedCount).toBeLessThan(totalItems)
expect(renderedCount).toBeGreaterThan(0)
})
})
180 changes: 180 additions & 0 deletions src/components/common/VirtualGrid.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import { mount } from '@vue/test-utils'
import { describe, expect, it, vi } from 'vitest'
import { nextTick, ref } from 'vue'

import VirtualGrid from './VirtualGrid.vue'

type TestItem = { key: string; name: string }

const mockedWidth = ref(400)
const mockedHeight = ref(200)
const mockedScrollY = ref(0)

vi.mock('@vueuse/core', async () => {
const actual = await vi.importActual<Record<string, unknown>>('@vueuse/core')
return {
...actual,
useElementSize: () => ({ width: mockedWidth, height: mockedHeight }),
useScroll: () => ({ y: mockedScrollY })
}
})

function createItems(count: number): TestItem[] {
return Array.from({ length: count }, (_, i) => ({
key: `item-${i}`,
name: `Item ${i}`
}))
}

describe('VirtualGrid', () => {
const defaultGridStyle = {
display: 'grid',
gridTemplateColumns: 'repeat(4, 1fr)',
gap: '1rem'
}

it('renders items within the visible range', async () => {
const items = createItems(100)
mockedWidth.value = 400
mockedHeight.value = 200
mockedScrollY.value = 0

const wrapper = mount(VirtualGrid<TestItem>, {
props: {
items,
gridStyle: defaultGridStyle,
defaultItemHeight: 100,
defaultItemWidth: 100,
maxColumns: 4,
bufferRows: 1
},
slots: {
item: `<template #item="{ item }">
<div class="test-item">{{ item.name }}</div>
</template>`
},
attachTo: document.body
})

await nextTick()

const renderedItems = wrapper.findAll('.test-item')
expect(renderedItems.length).toBeGreaterThan(0)
expect(renderedItems.length).toBeLessThan(items.length)

wrapper.unmount()
})

it('provides correct index in slot props', async () => {
const items = createItems(20)
const receivedIndices: number[] = []
mockedWidth.value = 400
mockedHeight.value = 200
mockedScrollY.value = 0

const wrapper = mount(VirtualGrid<TestItem>, {
props: {
items,
gridStyle: defaultGridStyle,
defaultItemHeight: 50,
defaultItemWidth: 100,
maxColumns: 1,
bufferRows: 0
},
slots: {
item: ({ index }: { index: number }) => {
receivedIndices.push(index)
return null
}
},
attachTo: document.body
})

await nextTick()

expect(receivedIndices.length).toBeGreaterThan(0)
expect(receivedIndices[0]).toBe(0)
for (let i = 1; i < receivedIndices.length; i++) {
expect(receivedIndices[i]).toBe(receivedIndices[i - 1] + 1)
}

wrapper.unmount()
})

it('respects maxColumns prop', async () => {
const items = createItems(10)
mockedWidth.value = 400
mockedHeight.value = 200
mockedScrollY.value = 0

const wrapper = mount(VirtualGrid<TestItem>, {
props: {
items,
gridStyle: defaultGridStyle,
maxColumns: 2
},
attachTo: document.body
})

await nextTick()

const gridElement = wrapper.find('[style*="display: grid"]')
expect(gridElement.exists()).toBe(true)

const gridEl = gridElement.element as HTMLElement
expect(gridEl.style.gridTemplateColumns).toBe('repeat(2, minmax(0, 1fr))')

wrapper.unmount()
})

it('renders empty when no items provided', async () => {
const wrapper = mount(VirtualGrid<TestItem>, {
props: {
items: [],
gridStyle: defaultGridStyle
},
slots: {
item: `<template #item="{ item }">
<div class="test-item">{{ item.name }}</div>
</template>`
}
})

await nextTick()

const renderedItems = wrapper.findAll('.test-item')
expect(renderedItems.length).toBe(0)
})

it('forces cols to maxColumns when maxColumns is finite', async () => {
mockedWidth.value = 100
mockedHeight.value = 200
mockedScrollY.value = 0

const items = createItems(20)
const wrapper = mount(VirtualGrid<TestItem>, {
props: {
items,
gridStyle: defaultGridStyle,
defaultItemHeight: 50,
defaultItemWidth: 200,
maxColumns: 4,
bufferRows: 0
},
slots: {
item: `<template #item="{ item }">
<div class="test-item">{{ item.name }}</div>
</template>`
},
attachTo: document.body
})

await nextTick()

const renderedItems = wrapper.findAll('.test-item')
expect(renderedItems.length).toBeGreaterThan(0)
expect(renderedItems.length % 4).toBe(0)

wrapper.unmount()
})
})
17 changes: 8 additions & 9 deletions src/components/common/VirtualGrid.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<template>
<div
ref="container"
class="h-full overflow-y-auto scrollbar-thin scrollbar-track-transparent scrollbar-thumb-(--dialog-surface)"
class="h-full overflow-y-auto [overflow-anchor:none] [scrollbar-gutter:stable] scrollbar-thin scrollbar-track-transparent scrollbar-thumb-(--dialog-surface)"
>
<div :style="topSpacerStyle" />
<div :style="mergedGridStyle">
<div
v-for="item in renderedItems"
v-for="(item, i) in renderedItems"
:key="item.key"
class="transition-[width] duration-150 ease-out"
data-virtual-grid-item
>
<slot name="item" :item="item" />
<slot name="item" :item :index="state.start + i" />
</div>
</div>
<div :style="bottomSpacerStyle" />
Expand Down Expand Up @@ -66,9 +65,10 @@ const { y: scrollY } = useScroll(container, {
eventListenerOptions: { passive: true }
})

const cols = computed(() =>
Math.min(Math.floor(width.value / itemWidth.value) || 1, maxColumns)
)
const cols = computed(() => {
if (maxColumns !== Infinity) return maxColumns
return Math.floor(width.value / itemWidth.value) || 1
})

const mergedGridStyle = computed<CSSProperties>(() => {
if (maxColumns === Infinity) return gridStyle
Expand Down Expand Up @@ -118,11 +118,10 @@ whenever(
}
)

const updateItemSize = () => {
function updateItemSize(): void {
if (container.value) {
const firstItem = container.value.querySelector('[data-virtual-grid-item]')

// Don't update item size if the first item is not rendered yet
if (!firstItem?.clientHeight || !firstItem?.clientWidth) return

if (itemHeight.value !== firstItem.clientHeight) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { mount } from '@vue/test-utils'
import { describe, expect, it } from 'vitest'
import { nextTick } from 'vue'

import FormDropdownMenu from './FormDropdownMenu.vue'
import type { FormDropdownItem, LayoutMode } from './types'

function createItem(id: string, name: string): FormDropdownItem {
return {
id,
preview_url: '',
name,
label: name
}
}

describe('FormDropdownMenu', () => {
const defaultProps = {
items: [createItem('1', 'Item 1'), createItem('2', 'Item 2')],
isSelected: () => false,
filterOptions: [],
sortOptions: []
}

it('renders empty state when no items', async () => {
const wrapper = mount(FormDropdownMenu, {
props: {
...defaultProps,
items: []
},
global: {
stubs: {
FormDropdownMenuFilter: true,
FormDropdownMenuActions: true,
VirtualGrid: true
},
mocks: {
$t: (key: string) => key
}
}
})

await nextTick()

const emptyIcon = wrapper.find('.icon-\\[lucide--circle-off\\]')
expect(emptyIcon.exists()).toBe(true)
})

it('renders VirtualGrid when items exist', async () => {
const wrapper = mount(FormDropdownMenu, {
props: defaultProps,
global: {
stubs: {
FormDropdownMenuFilter: true,
FormDropdownMenuActions: true,
VirtualGrid: true
}
}
})

await nextTick()

const virtualGrid = wrapper.findComponent({ name: 'VirtualGrid' })
expect(virtualGrid.exists()).toBe(true)
})

it('transforms items to include key property for VirtualGrid', async () => {
const items = [createItem('1', 'Item 1'), createItem('2', 'Item 2')]
const wrapper = mount(FormDropdownMenu, {
props: {
...defaultProps,
items
},
global: {
stubs: {
FormDropdownMenuFilter: true,
FormDropdownMenuActions: true,
VirtualGrid: true
}
}
})

await nextTick()

const virtualGrid = wrapper.findComponent({ name: 'VirtualGrid' })
const virtualItems = virtualGrid.props('items')

expect(virtualItems).toHaveLength(2)
expect(virtualItems[0]).toHaveProperty('key', '1')
expect(virtualItems[1]).toHaveProperty('key', '2')
})

it('uses single column layout for list modes', async () => {
const wrapper = mount(FormDropdownMenu, {
props: {
...defaultProps,
layoutMode: 'list' as LayoutMode
},
global: {
stubs: {
FormDropdownMenuFilter: true,
FormDropdownMenuActions: true,
VirtualGrid: true
}
}
})

await nextTick()

const virtualGrid = wrapper.findComponent({ name: 'VirtualGrid' })
expect(virtualGrid.props('maxColumns')).toBe(1)
})
})
Loading
Loading