Files
ComfyUI_frontend/src/composables/node/useNodePreviewAndDrag.test.ts
Alexander Brown 661e3d7949 test: migrate as unknown as to @total-typescript/shoehorn (#10761)
*PR Created by the Glary-Bot Agent*

---

## Summary

- Replace all `as unknown as Type` assertions in 59 unit test files with
type-safe `@total-typescript/shoehorn` functions
- Use `fromPartial<Type>()` for partial mock objects where deep-partial
type-checks (21 files)
- Use `fromAny<Type>()` for fundamentally incompatible types: null,
undefined, primitives, variables, class expressions, and mocks with
test-specific extra properties that `PartialDeepObject` rejects
(remaining files)
- All explicit type parameters preserved so TypeScript return types are
correct
- Browser test `.spec.ts` files excluded (shoehorn unavailable in
`page.evaluate` browser context)

## Verification

- `pnpm typecheck` 
- `pnpm lint` 
- `pnpm format` 
- Pre-commit hooks passed (format + oxlint + eslint + typecheck)
- Migrated test files verified passing (ran representative subset)
- No test behavior changes — only type assertion syntax changed
- No UI changes — screenshots not applicable

┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-10761-test-migrate-as-unknown-as-to-total-typescript-shoehorn-3336d73d365081f6b8adc44db5dcc380)
by [Unito](https://www.unito.io)

---------

Co-authored-by: Glary-Bot <glary-bot@users.noreply.github.com>
Co-authored-by: Amp <amp@ampcode.com>
2026-03-30 19:20:18 -07:00

184 lines
5.4 KiB
TypeScript

import { fromAny, fromPartial } from '@total-typescript/shoehorn'
import { ref } from 'vue'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { ComfyNodeDefImpl } from '@/stores/nodeDefStore'
import { useNodePreviewAndDrag } from './useNodePreviewAndDrag'
const mockStartDrag = vi.fn()
const mockHandleNativeDrop = vi.fn()
vi.mock('@/composables/node/useNodeDragToCanvas', () => ({
useNodeDragToCanvas: () => ({
startDrag: mockStartDrag,
handleNativeDrop: mockHandleNativeDrop
})
}))
vi.mock('@/platform/settings/settingStore', () => ({
useSettingStore: () => ({
get: vi.fn().mockReturnValue('left')
})
}))
describe('useNodePreviewAndDrag', () => {
const mockNodeDef = {
name: 'TestNode',
display_name: 'Test Node'
} as ComfyNodeDefImpl
beforeEach(() => {
vi.clearAllMocks()
})
describe('initial state', () => {
it('should initialize with correct default values', () => {
const nodeDef = ref<ComfyNodeDefImpl | undefined>(mockNodeDef)
const result = useNodePreviewAndDrag(nodeDef)
expect(result.isHovered.value).toBe(false)
expect(result.isDragging.value).toBe(false)
expect(result.showPreview.value).toBe(false)
expect(result.previewRef.value).toBeNull()
})
it('should compute showPreview based on hover and drag state', () => {
const nodeDef = ref<ComfyNodeDefImpl | undefined>(mockNodeDef)
const result = useNodePreviewAndDrag(nodeDef)
result.isHovered.value = true
expect(result.showPreview.value).toBe(true)
result.isDragging.value = true
expect(result.showPreview.value).toBe(false)
})
})
describe('handleMouseEnter', () => {
it('should set isHovered to true when nodeDef exists', () => {
const nodeDef = ref<ComfyNodeDefImpl | undefined>(mockNodeDef)
const result = useNodePreviewAndDrag(nodeDef)
const mockElement = document.createElement('div')
vi.spyOn(mockElement, 'getBoundingClientRect').mockReturnValue({
top: 100,
left: 50,
right: 150,
bottom: 200,
width: 100,
height: 100,
x: 50,
y: 100,
toJSON: () => ({})
})
const mockEvent = fromPartial<MouseEvent>({
currentTarget: mockElement
})
result.handleMouseEnter(mockEvent)
expect(result.isHovered.value).toBe(true)
})
it('should not set isHovered when nodeDef is undefined', () => {
const nodeDef = ref<ComfyNodeDefImpl | undefined>(undefined)
const result = useNodePreviewAndDrag(nodeDef)
const mockElement = document.createElement('div')
const mockEvent = fromPartial<MouseEvent>({
currentTarget: mockElement
})
result.handleMouseEnter(mockEvent)
expect(result.isHovered.value).toBe(false)
})
})
describe('handleMouseLeave', () => {
it('should set isHovered to false', () => {
const nodeDef = ref<ComfyNodeDefImpl | undefined>(mockNodeDef)
const result = useNodePreviewAndDrag(nodeDef)
result.isHovered.value = true
result.handleMouseLeave()
expect(result.isHovered.value).toBe(false)
})
})
describe('handleDragStart', () => {
it('should call startDrag with native mode when nodeDef exists', () => {
const nodeDef = ref<ComfyNodeDefImpl | undefined>(mockNodeDef)
const result = useNodePreviewAndDrag(nodeDef)
const mockDataTransfer = {
effectAllowed: '',
setData: vi.fn(),
setDragImage: vi.fn()
}
const mockEvent = fromAny<DragEvent, unknown>({
dataTransfer: mockDataTransfer
})
result.handleDragStart(mockEvent)
expect(result.isDragging.value).toBe(true)
expect(result.isHovered.value).toBe(false)
expect(mockStartDrag).toHaveBeenCalledWith(mockNodeDef, 'native')
expect(mockDataTransfer.effectAllowed).toBe('copy')
expect(mockDataTransfer.setData).toHaveBeenCalledWith(
'application/x-comfy-node',
'TestNode'
)
})
it('should not start drag when nodeDef is undefined', () => {
const nodeDef = ref<ComfyNodeDefImpl | undefined>(undefined)
const result = useNodePreviewAndDrag(nodeDef)
const mockEvent = { dataTransfer: null } as DragEvent
result.handleDragStart(mockEvent)
expect(result.isDragging.value).toBe(false)
expect(mockStartDrag).not.toHaveBeenCalled()
})
})
describe('handleDragEnd', () => {
it('should call handleNativeDrop with drop coordinates', () => {
const nodeDef = ref<ComfyNodeDefImpl | undefined>(mockNodeDef)
const result = useNodePreviewAndDrag(nodeDef)
result.isDragging.value = true
const mockEvent = fromPartial<DragEvent>({
clientX: 100,
clientY: 200
})
result.handleDragEnd(mockEvent)
expect(result.isDragging.value).toBe(false)
expect(mockHandleNativeDrop).toHaveBeenCalledWith(100, 200)
})
it('should always call handleNativeDrop regardless of dropEffect', () => {
const nodeDef = ref<ComfyNodeDefImpl | undefined>(mockNodeDef)
const result = useNodePreviewAndDrag(nodeDef)
result.isDragging.value = true
const mockEvent = fromPartial<DragEvent>({
dataTransfer: { dropEffect: 'none' },
clientX: 300,
clientY: 400
})
result.handleDragEnd(mockEvent)
expect(result.isDragging.value).toBe(false)
expect(mockHandleNativeDrop).toHaveBeenCalledWith(300, 400)
})
})
})