mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-05-03 20:51:58 +00:00
## 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>
196 lines
5.2 KiB
TypeScript
196 lines
5.2 KiB
TypeScript
import { render, screen } from '@testing-library/vue'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import type { FilterChip } from '@/components/searchbox/v2/NodeSearchFilterBar.vue'
|
|
import NodeSearchInput from '@/components/searchbox/v2/NodeSearchInput.vue'
|
|
import {
|
|
setupTestPinia,
|
|
testI18n
|
|
} from '@/components/searchbox/v2/__test__/testUtils'
|
|
import type { ComfyNodeDefImpl } from '@/stores/nodeDefStore'
|
|
import type { FuseFilter, FuseFilterWithValue } from '@/utils/fuseUtil'
|
|
|
|
vi.mock('@/utils/litegraphUtil', () => ({
|
|
getLinkTypeColor: vi.fn((type: string) =>
|
|
type === 'IMAGE' ? '#64b5f6' : undefined
|
|
)
|
|
}))
|
|
|
|
vi.mock('@/platform/settings/settingStore', () => ({
|
|
useSettingStore: vi.fn(() => ({
|
|
get: vi.fn(),
|
|
set: vi.fn()
|
|
}))
|
|
}))
|
|
|
|
function createFilter(
|
|
id: string,
|
|
value: string
|
|
): FuseFilterWithValue<ComfyNodeDefImpl, string> {
|
|
return {
|
|
filterDef: {
|
|
id,
|
|
matches: vi.fn(() => true)
|
|
} as Partial<FuseFilter<ComfyNodeDefImpl, string>> as FuseFilter<
|
|
ComfyNodeDefImpl,
|
|
string
|
|
>,
|
|
value
|
|
}
|
|
}
|
|
|
|
function createActiveFilter(label: string): FilterChip {
|
|
return {
|
|
key: label.toLowerCase(),
|
|
label,
|
|
filter: {
|
|
id: label.toLowerCase(),
|
|
matches: vi.fn(() => true)
|
|
} as Partial<FuseFilter<ComfyNodeDefImpl, string>> as FuseFilter<
|
|
ComfyNodeDefImpl,
|
|
string
|
|
>
|
|
}
|
|
}
|
|
|
|
describe('NodeSearchInput', () => {
|
|
beforeEach(() => {
|
|
setupTestPinia()
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
function createRender(
|
|
props: Partial<{
|
|
filters: FuseFilterWithValue<ComfyNodeDefImpl, string>[]
|
|
activeFilter: FilterChip | null
|
|
searchQuery: string
|
|
filterQuery: string
|
|
}> = {}
|
|
) {
|
|
const user = userEvent.setup()
|
|
const onUpdateSearchQuery = vi.fn()
|
|
const onUpdateFilterQuery = vi.fn()
|
|
const onCancelFilter = vi.fn()
|
|
const onSelectCurrent = vi.fn()
|
|
const onNavigateDown = vi.fn()
|
|
const onNavigateUp = vi.fn()
|
|
render(NodeSearchInput, {
|
|
props: {
|
|
filters: [],
|
|
activeFilter: null,
|
|
searchQuery: '',
|
|
filterQuery: '',
|
|
'onUpdate:searchQuery': onUpdateSearchQuery,
|
|
'onUpdate:filterQuery': onUpdateFilterQuery,
|
|
onCancelFilter,
|
|
onSelectCurrent,
|
|
onNavigateDown,
|
|
onNavigateUp,
|
|
...props
|
|
},
|
|
global: { plugins: [testI18n] }
|
|
})
|
|
return {
|
|
user,
|
|
onUpdateSearchQuery,
|
|
onUpdateFilterQuery,
|
|
onCancelFilter,
|
|
onSelectCurrent,
|
|
onNavigateDown,
|
|
onNavigateUp
|
|
}
|
|
}
|
|
|
|
it('should route input to searchQuery when no active filter', async () => {
|
|
const { user, onUpdateSearchQuery } = createRender()
|
|
await user.type(screen.getByRole('combobox'), 'test search')
|
|
|
|
expect(onUpdateSearchQuery).toHaveBeenLastCalledWith('test search')
|
|
})
|
|
|
|
it('should route input to filterQuery when active filter is set', async () => {
|
|
const { user, onUpdateFilterQuery, onUpdateSearchQuery } = createRender({
|
|
activeFilter: createActiveFilter('Input')
|
|
})
|
|
await user.type(screen.getByRole('combobox'), 'IMAGE')
|
|
|
|
expect(onUpdateFilterQuery).toHaveBeenLastCalledWith('IMAGE')
|
|
expect(onUpdateSearchQuery).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('should show filter label placeholder when active filter is set', () => {
|
|
createRender({
|
|
activeFilter: createActiveFilter('Input')
|
|
})
|
|
|
|
expect(screen.getByRole('combobox')).toHaveAttribute(
|
|
'placeholder',
|
|
expect.stringContaining('input')
|
|
)
|
|
})
|
|
|
|
it('should show add node placeholder when no active filter', () => {
|
|
createRender()
|
|
|
|
expect(screen.getByRole('combobox')).toHaveAttribute(
|
|
'placeholder',
|
|
expect.stringContaining('Add a node')
|
|
)
|
|
})
|
|
|
|
it('should hide filter chips when active filter is set', () => {
|
|
createRender({
|
|
filters: [createFilter('input', 'IMAGE')],
|
|
activeFilter: createActiveFilter('Input')
|
|
})
|
|
|
|
expect(screen.queryAllByTestId('filter-chip')).toHaveLength(0)
|
|
})
|
|
|
|
it('should show filter chips when no active filter', () => {
|
|
createRender({
|
|
filters: [createFilter('input', 'IMAGE')]
|
|
})
|
|
|
|
expect(screen.getAllByTestId('filter-chip')).toHaveLength(1)
|
|
})
|
|
|
|
it('should emit cancelFilter when cancel button is clicked', async () => {
|
|
const { user, onCancelFilter } = createRender({
|
|
activeFilter: createActiveFilter('Input')
|
|
})
|
|
|
|
await user.click(screen.getByTestId('cancel-filter'))
|
|
|
|
expect(onCancelFilter).toHaveBeenCalledOnce()
|
|
})
|
|
|
|
it('should emit selectCurrent on Enter', async () => {
|
|
const { user, onSelectCurrent } = createRender()
|
|
|
|
await user.click(screen.getByRole('combobox'))
|
|
await user.keyboard('{Enter}')
|
|
|
|
expect(onSelectCurrent).toHaveBeenCalledOnce()
|
|
})
|
|
|
|
it('should emit navigateDown on ArrowDown', async () => {
|
|
const { user, onNavigateDown } = createRender()
|
|
|
|
await user.click(screen.getByRole('combobox'))
|
|
await user.keyboard('{ArrowDown}')
|
|
|
|
expect(onNavigateDown).toHaveBeenCalledOnce()
|
|
})
|
|
|
|
it('should emit navigateUp on ArrowUp', async () => {
|
|
const { user, onNavigateUp } = createRender()
|
|
|
|
await user.click(screen.getByRole('combobox'))
|
|
await user.keyboard('{ArrowUp}')
|
|
|
|
expect(onNavigateUp).toHaveBeenCalledOnce()
|
|
})
|
|
})
|