mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-05-03 04:31: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>
242 lines
7.7 KiB
TypeScript
242 lines
7.7 KiB
TypeScript
import { render, waitFor } from '@testing-library/vue'
|
|
import { ref } from 'vue'
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
|
|
import type { ComfyNodeDefImpl } from '@/stores/nodeDefStore'
|
|
import type { RenderedTreeExplorerNode } from '@/types/treeExplorerTypes'
|
|
|
|
import EssentialNodesPanel from './EssentialNodesPanel.vue'
|
|
|
|
vi.mock('@/platform/settings/settingStore', () => ({
|
|
useSettingStore: () => ({
|
|
get: vi.fn().mockReturnValue('left')
|
|
})
|
|
}))
|
|
|
|
vi.mock('@/composables/node/useNodeDragToCanvas', () => ({
|
|
useNodeDragToCanvas: () => ({
|
|
startDrag: vi.fn(),
|
|
handleNativeDrop: vi.fn(),
|
|
cancelDrag: vi.fn()
|
|
})
|
|
}))
|
|
|
|
vi.mock('@/components/node/NodePreviewCard.vue', () => ({
|
|
default: { template: '<div />' }
|
|
}))
|
|
|
|
describe('EssentialNodesPanel', () => {
|
|
function createMockNode(
|
|
name: string
|
|
): RenderedTreeExplorerNode<ComfyNodeDefImpl> {
|
|
return {
|
|
key: `node-${name}`,
|
|
label: name,
|
|
icon: 'icon-[comfy--node]',
|
|
type: 'node',
|
|
totalLeaves: 1,
|
|
data: {
|
|
name,
|
|
display_name: name
|
|
} as ComfyNodeDefImpl
|
|
}
|
|
}
|
|
|
|
function createMockFolder(
|
|
name: string,
|
|
children: RenderedTreeExplorerNode<ComfyNodeDefImpl>[]
|
|
): RenderedTreeExplorerNode<ComfyNodeDefImpl> {
|
|
return {
|
|
key: `folder-${name}`,
|
|
label: name,
|
|
icon: 'icon-[lucide--folder]',
|
|
type: 'folder',
|
|
totalLeaves: children.length,
|
|
children
|
|
}
|
|
}
|
|
|
|
function createMockRoot(): RenderedTreeExplorerNode<ComfyNodeDefImpl> {
|
|
return {
|
|
key: 'root',
|
|
label: 'Root',
|
|
icon: '',
|
|
type: 'folder',
|
|
totalLeaves: 6,
|
|
children: [
|
|
createMockFolder('images', [
|
|
createMockNode('LoadImage'),
|
|
createMockNode('SaveImage')
|
|
]),
|
|
createMockFolder('video', [
|
|
createMockNode('LoadVideo'),
|
|
createMockNode('SaveVideo')
|
|
]),
|
|
createMockFolder('audio', [
|
|
createMockNode('LoadAudio'),
|
|
createMockNode('SaveAudio')
|
|
])
|
|
]
|
|
}
|
|
}
|
|
|
|
function renderComponent(
|
|
root = createMockRoot(),
|
|
expandedKeys: string[] = [],
|
|
flatNodes: RenderedTreeExplorerNode<ComfyNodeDefImpl>[] = []
|
|
) {
|
|
const WrapperComponent = {
|
|
template: `<EssentialNodesPanel :root="root" :flat-nodes="flatNodes" v-model:expandedKeys="keys" />`,
|
|
components: { EssentialNodesPanel },
|
|
setup() {
|
|
const keys = ref(expandedKeys)
|
|
return { root, flatNodes, keys }
|
|
}
|
|
}
|
|
return render(WrapperComponent, {
|
|
global: {
|
|
stubs: {
|
|
Teleport: true,
|
|
TabsContent: {
|
|
template: '<div class="tabs-content"><slot /></div>'
|
|
},
|
|
CollapsibleRoot: {
|
|
template:
|
|
'<div class="collapsible-root" :data-state="open ? \'open\' : \'closed\'"><slot /></div>',
|
|
props: ['open'],
|
|
emits: ['update:open']
|
|
},
|
|
CollapsibleTrigger: {
|
|
template:
|
|
'<button class="collapsible-trigger" @click="$emit(\'click\')"><slot /></button>'
|
|
},
|
|
CollapsibleContent: {
|
|
template: '<div class="collapsible-content"><slot /></div>'
|
|
},
|
|
EssentialNodeCard: {
|
|
template: '<div data-testid="essential-node-card" />'
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
describe('folder rendering', () => {
|
|
it('should render all top-level folders', () => {
|
|
const { container } = renderComponent()
|
|
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
|
|
expect(container.querySelectorAll('.collapsible-trigger')).toHaveLength(3)
|
|
})
|
|
|
|
it('should display folder labels', () => {
|
|
const { container } = renderComponent()
|
|
expect(container.textContent).toContain('images')
|
|
expect(container.textContent).toContain('video')
|
|
expect(container.textContent).toContain('audio')
|
|
})
|
|
})
|
|
|
|
describe('default expansion', () => {
|
|
it('should expand all folders by default when expandedKeys is empty', async () => {
|
|
const { container } = renderComponent(createMockRoot(), [])
|
|
|
|
await waitFor(() => {
|
|
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
|
|
const roots = container.querySelectorAll('.collapsible-root')
|
|
expect(roots).toHaveLength(3)
|
|
expect(roots[0].getAttribute('data-state')).toBe('open')
|
|
expect(roots[1].getAttribute('data-state')).toBe('open')
|
|
expect(roots[2].getAttribute('data-state')).toBe('open')
|
|
})
|
|
})
|
|
|
|
it('should respect provided expandedKeys', async () => {
|
|
const { container } = renderComponent(createMockRoot(), ['folder-audio'])
|
|
|
|
await waitFor(() => {
|
|
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
|
|
const roots = container.querySelectorAll('.collapsible-root')
|
|
expect(roots).toHaveLength(3)
|
|
expect(roots[0].getAttribute('data-state')).toBe('closed')
|
|
expect(roots[1].getAttribute('data-state')).toBe('closed')
|
|
expect(roots[2].getAttribute('data-state')).toBe('open')
|
|
})
|
|
})
|
|
|
|
it('should expand all provided keys', async () => {
|
|
const { container } = renderComponent(createMockRoot(), [
|
|
'folder-images',
|
|
'folder-video',
|
|
'folder-audio'
|
|
])
|
|
|
|
await waitFor(() => {
|
|
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
|
|
const roots = container.querySelectorAll('.collapsible-root')
|
|
expect(roots).toHaveLength(3)
|
|
expect(roots[0].getAttribute('data-state')).toBe('open')
|
|
expect(roots[1].getAttribute('data-state')).toBe('open')
|
|
expect(roots[2].getAttribute('data-state')).toBe('open')
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('with single folder', () => {
|
|
it('should expand only one folder when there is only one', async () => {
|
|
const root: RenderedTreeExplorerNode<ComfyNodeDefImpl> = {
|
|
key: 'root',
|
|
label: 'Root',
|
|
icon: '',
|
|
type: 'folder',
|
|
totalLeaves: 2,
|
|
children: [
|
|
createMockFolder('images', [
|
|
createMockNode('LoadImage'),
|
|
createMockNode('SaveImage')
|
|
])
|
|
]
|
|
}
|
|
|
|
const { container } = renderComponent(root, [])
|
|
|
|
await waitFor(() => {
|
|
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
|
|
const roots = container.querySelectorAll('.collapsible-root')
|
|
expect(roots).toHaveLength(1)
|
|
expect(roots[0].getAttribute('data-state')).toBe('open')
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('node cards', () => {
|
|
it('should render node cards for each node in expanded folders', () => {
|
|
const { container } = renderComponent(createMockRoot(), ['folder-images'])
|
|
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
|
|
const cards = container.querySelectorAll(
|
|
'[data-testid="essential-node-card"]'
|
|
)
|
|
expect(cards.length).toBeGreaterThanOrEqual(2)
|
|
})
|
|
})
|
|
|
|
describe('flat nodes mode', () => {
|
|
it('should render flat grid without collapsible folders when flatNodes is provided', () => {
|
|
const flatNodes = [
|
|
createMockNode('LoadAudio'),
|
|
createMockNode('LoadImage'),
|
|
createMockNode('SaveImage')
|
|
]
|
|
const { container } = renderComponent(createMockRoot(), [], flatNodes)
|
|
|
|
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
|
|
expect(container.querySelectorAll('.collapsible-root')).toHaveLength(0)
|
|
|
|
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
|
|
const cards = container.querySelectorAll(
|
|
'[data-testid="essential-node-card"]'
|
|
)
|
|
expect(cards).toHaveLength(3)
|
|
})
|
|
})
|
|
})
|