Files
ComfyUI_frontend/src/components/graph/modals/ZoomControlsModal.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

177 lines
4.7 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 { createI18n } from 'vue-i18n'
import ZoomControlsModal from '@/components/graph/modals/ZoomControlsModal.vue'
const mockExecute = vi.fn()
const mockGetCommand = vi.fn().mockImplementation((commandId: string) => ({
keybinding: {
combo: {
getKeySequences: () => [
'Ctrl',
commandId === 'Comfy.Canvas.ZoomIn'
? '+'
: commandId === 'Comfy.Canvas.ZoomOut'
? '-'
: '0'
]
}
}
}))
const mockFormatKeySequence = vi
.fn()
.mockImplementation(
(command: {
keybinding: { combo: { getKeySequences: () => string[] } }
}) => {
const seq = command.keybinding.combo.getKeySequences()
if (seq.includes('+')) return 'Ctrl+'
if (seq.includes('-')) return 'Ctrl-'
return 'Ctrl+0'
}
)
const mockSetAppZoom = vi.fn()
const mockSettingGet = vi.fn().mockReturnValue(true)
const i18n = createI18n({
legacy: false,
locale: 'en',
messages: { en: {} }
})
vi.mock('@/renderer/extensions/minimap/composables/useMinimap', () => ({
useMinimap: () => ({
containerStyles: {
value: { backgroundColor: '#fff', borderRadius: '8px' }
}
})
}))
vi.mock('@/stores/commandStore', () => ({
useCommandStore: () => ({
execute: mockExecute,
getCommand: mockGetCommand,
formatKeySequence: mockFormatKeySequence
})
}))
vi.mock('@/renderer/core/canvas/canvasStore', () => ({
useCanvasStore: () => ({
appScalePercentage: 100,
setAppZoomFromPercentage: mockSetAppZoom
})
}))
vi.mock('@/platform/settings/settingStore', () => ({
useSettingStore: () => ({
get: mockSettingGet
})
}))
function renderComponent(props = {}) {
return render(ZoomControlsModal, {
props: {
visible: true,
...props
},
global: {
plugins: [i18n],
stubs: {
Button: false,
InputNumber: false
}
}
})
}
describe('ZoomControlsModal', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('should execute zoom in command when zoom in button is clicked', async () => {
const user = userEvent.setup()
renderComponent()
const zoomInButton = screen.getByTestId('zoom-in-action')
await user.click(zoomInButton)
expect(mockExecute).toHaveBeenCalledWith('Comfy.Canvas.ZoomIn')
})
it('should execute zoom out command when zoom out button is clicked', async () => {
const user = userEvent.setup()
renderComponent()
const zoomOutButton = screen.getByTestId('zoom-out-action')
await user.click(zoomOutButton)
expect(mockExecute).toHaveBeenCalledWith('Comfy.Canvas.ZoomOut')
})
it('should execute fit view command when fit view button is clicked', async () => {
const user = userEvent.setup()
renderComponent()
const fitViewButton = screen.getByTestId('zoom-to-fit-action')
await user.click(fitViewButton)
expect(mockExecute).toHaveBeenCalledWith('Comfy.Canvas.FitView')
})
it('should call setAppZoomFromPercentage with valid zoom input values', async () => {
const user = userEvent.setup()
renderComponent()
const input = screen.getByRole('spinbutton')
await user.tripleClick(input)
await user.keyboard('150')
expect(mockSetAppZoom).toHaveBeenCalledWith(150)
})
it('should not call setAppZoomFromPercentage when value is below minimum', async () => {
const user = userEvent.setup()
renderComponent()
const input = screen.getByRole('spinbutton')
await user.tripleClick(input)
await user.keyboard('0')
expect(mockSetAppZoom).not.toHaveBeenCalled()
})
it('should not apply zoom values exceeding the maximum', async () => {
const user = userEvent.setup()
renderComponent()
const input = screen.getByRole('spinbutton')
await user.tripleClick(input)
await user.keyboard('100')
mockSetAppZoom.mockClear()
await user.keyboard('1')
expect(mockSetAppZoom).not.toHaveBeenCalled()
})
it('should display keyboard shortcuts for commands', () => {
renderComponent()
expect(screen.getByText('Ctrl+')).toBeInTheDocument()
expect(screen.getByText('Ctrl-')).toBeInTheDocument()
expect(screen.getByText('Ctrl+0')).toBeInTheDocument()
expect(mockGetCommand).toHaveBeenCalledWith('Comfy.Canvas.ZoomIn')
expect(mockGetCommand).toHaveBeenCalledWith('Comfy.Canvas.ZoomOut')
expect(mockGetCommand).toHaveBeenCalledWith('Comfy.Canvas.FitView')
})
it('should not be visible when visible prop is false', () => {
renderComponent({ visible: false })
expect(screen.queryByTestId('zoom-in-action')).toBeNull()
})
})