Files
ComfyUI_frontend/src/components/sidebar/tabs/AssetsSidebarListView.test.ts
Alexander Brown f90d6cf607 test: migrate 132 test files from @vue/test-utils to @testing-library/vue (#10965)
## Summary

Migrate 132 test files from `@vue/test-utils` (VTU) to
`@testing-library/vue` (VTL) with `@testing-library/user-event`,
adopting user-centric behavioral testing patterns across the codebase.

## Changes

- **What**: Systematic migration of component/unit tests from VTU's
`mount`/`wrapper` API to VTL's `render`/`screen`/`userEvent` API across
132 files in `src/`
- **Breaking**: None — test-only changes, no production code affected

### Migration breakdown

| Batch | Files | Description |
|-------|-------|-------------|
| 1 | 19 | Simple render/assert tests |
| 2A | 16 | Interactive tests with user events |
| 2B-1 | 14 | Interactive tests (continued) |
| 2B-2 | 32 | Interactive tests (continued) |
| 3A–3E | 51 | Complex tests (stores, composables, heavy mocking) |
| Lint fix | 7 | `await` on `fireEvent` calls for `no-floating-promises`
|
| Review fixes | 15 | Address CodeRabbit feedback (3 rounds) |

### Review feedback addressed

- Removed class-based assertions (`text-ellipsis`, `pr-3`, `.pi-save`,
`.skeleton`, `.bg-black\/15`, Tailwind utilities) in favor of
behavioral/accessible queries
- Added null guards before `querySelector` casts
- Added `expect(roots).toHaveLength(N)` guards before indexed NodeList
access
- Wrapped fake timer tests in `try/finally` for guaranteed cleanup
- Split double-render tests into focused single-render tests
- Replaced CSS class selectors with
`screen.getByText`/`screen.getByRole` queries
- Updated stubs to use semantic `role`/`aria-label` instead of CSS
classes
- Consolidated redundant edge-case tests
- Removed manual `document.body.appendChild` in favor of VTL container
management
- Used distinct mock return values to verify command wiring

### VTU holdouts (2 files)

These files intentionally retain `@vue/test-utils` because their
components use `<script setup>` without `defineExpose`, making internal
computed properties and methods inaccessible via VTL:

1. **`NodeWidgets.test.ts`** — partial VTU for `vm.processedWidgets`
2. **`WidgetSelectDropdown.test.ts`** — full VTU for heavy
`wrapper.vm.*` access

## Follow-up

Deferred items (`ComponentProps` typing, camelCase listener props)
tracked in #10966.

## Review Focus

- Test correctness: all migrated tests preserve original behavioral
coverage
- VTL idioms: proper use of `screen` queries, `userEvent`, and
accessibility-based selectors
- The 2 VTU holdout files are intentional, not oversights

┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-10965-test-migrate-132-test-files-from-vue-test-utils-to-testing-library-vue-33c6d73d36508199a6a7e513cf5d8296)
by [Unito](https://www.unito.io)

---------

Co-authored-by: Amp <amp@ampcode.com>
Co-authored-by: Christian Byrne <cbyrne@comfy.org>
2026-04-08 19:21:42 -07:00

176 lines
5.5 KiB
TypeScript

/* eslint-disable vue/one-component-per-file */
import { render, fireEvent } from '@testing-library/vue'
import { defineComponent } from 'vue'
import { describe, expect, it, vi } from 'vitest'
import type { OutputStackListItem } from '@/platform/assets/composables/useOutputStacks'
import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
import AssetsSidebarListView from './AssetsSidebarListView.vue'
vi.mock('vue-i18n', () => ({
useI18n: () => ({
t: (key: string) => key
})
}))
vi.mock('@/stores/assetsStore', () => ({
useAssetsStore: () => ({
isAssetDeleting: () => false
})
}))
const VirtualGridStub = defineComponent({
name: 'VirtualGrid',
props: {
items: {
type: Array,
default: () => []
}
},
template:
'<div><slot v-for="item in items" :key="item.key" name="item" :item="item" /></div>'
})
const AssetsListItemStub = defineComponent({
name: 'AssetsListItem',
props: {
previewUrl: { type: String, default: '' },
isVideoPreview: { type: Boolean, default: false },
previewAlt: { type: String, default: '' },
iconName: { type: String, default: '' },
iconAriaLabel: { type: String, default: '' },
iconClass: { type: String, default: '' },
iconWrapperClass: { type: String, default: '' },
primaryText: { type: String, default: '' },
secondaryText: { type: String, default: '' },
stackCount: { type: Number, default: 0 },
stackIndicatorLabel: { type: String, default: '' },
stackExpanded: { type: Boolean, default: false },
progressTotalPercent: { type: Number, default: undefined },
progressCurrentPercent: { type: Number, default: undefined }
},
template: `<div
class="assets-list-item-stub"
:data-preview-url="previewUrl"
:data-is-video-preview="isVideoPreview"
data-testid="assets-list-item"
><button data-testid="preview-click-trigger" @click="$emit('preview-click')" /><slot /></div>`
})
const buildAsset = (id: string, name: string): AssetItem =>
({
id,
name,
tags: []
}) satisfies AssetItem
const buildOutputItem = (asset: AssetItem): OutputStackListItem => ({
key: `asset-${asset.id}`,
asset
})
function renderListView(
assetItems: OutputStackListItem[] = [],
props: Record<string, unknown> = {}
) {
return render(AssetsSidebarListView, {
props: {
assetItems,
selectableAssets: [],
isSelected: () => false,
isStackExpanded: () => false,
toggleStack: async () => {},
...props
},
global: {
stubs: {
VirtualGrid: VirtualGridStub,
AssetsListItem: AssetsListItemStub
}
}
})
}
describe('AssetsSidebarListView', () => {
it('marks mp4 assets as video previews', () => {
const videoAsset = {
...buildAsset('video-asset', 'clip.mp4'),
preview_url: '/api/view/clip.mp4',
user_metadata: {}
} satisfies AssetItem
const { container } = renderListView([buildOutputItem(videoAsset)])
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
const stubs = container.querySelectorAll('[data-testid="assets-list-item"]')
const assetListItem = stubs[stubs.length - 1]
expect(assetListItem).toBeDefined()
expect(assetListItem?.getAttribute('data-preview-url')).toBe(
'/api/view/clip.mp4'
)
expect(assetListItem?.getAttribute('data-is-video-preview')).toBe('true')
})
it('uses icon fallback for text assets even when preview_url exists', () => {
const textAsset = {
...buildAsset('text-asset', 'notes.txt'),
preview_url: '/api/view/notes.txt',
user_metadata: {}
} satisfies AssetItem
const { container } = renderListView([buildOutputItem(textAsset)])
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
const stubs = container.querySelectorAll('[data-testid="assets-list-item"]')
const assetListItem = stubs[stubs.length - 1]
expect(assetListItem).toBeDefined()
expect(assetListItem?.getAttribute('data-preview-url')).toBe('')
expect(assetListItem?.getAttribute('data-is-video-preview')).toBe('false')
})
it('emits preview-asset when item preview is clicked', async () => {
const imageAsset = {
...buildAsset('image-asset', 'image.png'),
preview_url: '/api/view/image.png',
user_metadata: {}
} satisfies AssetItem
const onPreviewAsset = vi.fn()
const { container } = renderListView([buildOutputItem(imageAsset)], {
'onPreview-asset': onPreviewAsset
})
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
const trigger = container.querySelector(
'[data-testid="preview-click-trigger"]'
)!
// eslint-disable-next-line testing-library/prefer-user-event
await fireEvent.click(trigger)
expect(onPreviewAsset).toHaveBeenCalledWith(imageAsset)
})
it('emits preview-asset when item is double-clicked', async () => {
const imageAsset = {
...buildAsset('image-asset-dbl', 'image.png'),
preview_url: '/api/view/image.png',
user_metadata: {}
} satisfies AssetItem
const onPreviewAsset = vi.fn()
const { container } = renderListView([buildOutputItem(imageAsset)], {
'onPreview-asset': onPreviewAsset
})
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
const stub = container.querySelector('[data-testid="assets-list-item"]')!
// eslint-disable-next-line testing-library/prefer-user-event
await fireEvent.dblClick(stub)
expect(onPreviewAsset).toHaveBeenCalledWith(imageAsset)
})
})