mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-02-08 17:10:07 +00:00
Simplifies test mocking patterns across multiple test files. - Removes redundant `vi.hoisted()` calls - Cleans up mock implementations - Removes unused imports and variables ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-8320-test-simplify-test-file-mocking-patterns-2f46d73d36508150981bd8ecb99a6a11) by [Unito](https://www.unito.io) --------- Co-authored-by: Amp <amp@ampcode.com> Co-authored-by: GitHub Action <action@github.com>
119 lines
3.1 KiB
TypeScript
119 lines
3.1 KiB
TypeScript
import { mount } from '@vue/test-utils'
|
|
import { createTestingPinia } from '@pinia/testing'
|
|
import { setActivePinia } from 'pinia'
|
|
import PrimeVue from 'primevue/config'
|
|
import Tooltip from 'primevue/tooltip'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { createI18n } from 'vue-i18n'
|
|
|
|
import ExecuteButton from '@/components/graph/selectionToolbox/ExecuteButton.vue'
|
|
import { useSelectionState } from '@/composables/graph/useSelectionState'
|
|
import type { LGraphCanvas, LGraphNode } from '@/lib/litegraph/src/litegraph'
|
|
import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
|
|
import { useCommandStore } from '@/stores/commandStore'
|
|
|
|
// Mock the utils
|
|
vi.mock('@/utils/litegraphUtil', () => ({
|
|
isLGraphNode: vi.fn((node) => !!node?.type)
|
|
}))
|
|
|
|
vi.mock('@/utils/nodeFilterUtil', () => ({
|
|
isOutputNode: vi.fn((node) => !!node?.constructor?.nodeData?.output_node)
|
|
}))
|
|
|
|
// Mock the composables
|
|
vi.mock('@/composables/graph/useSelectionState', () => ({
|
|
useSelectionState: vi.fn(() => ({
|
|
selectedNodes: {
|
|
value: []
|
|
}
|
|
}))
|
|
}))
|
|
|
|
describe('ExecuteButton', () => {
|
|
let mockCanvas: LGraphCanvas
|
|
let mockSelectedNodes: LGraphNode[]
|
|
|
|
const i18n = createI18n({
|
|
legacy: false,
|
|
locale: 'en',
|
|
messages: {
|
|
en: {
|
|
selectionToolbox: {
|
|
executeButton: {
|
|
tooltip: 'Execute selected nodes'
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
beforeEach(() => {
|
|
// Set up Pinia with testing utilities
|
|
setActivePinia(
|
|
createTestingPinia({
|
|
createSpy: vi.fn
|
|
})
|
|
)
|
|
|
|
// Reset mocks
|
|
const partialCanvas: Partial<LGraphCanvas> = {
|
|
setDirty: vi.fn()
|
|
}
|
|
mockCanvas = partialCanvas as Partial<LGraphCanvas> as LGraphCanvas
|
|
|
|
mockSelectedNodes = []
|
|
|
|
// Get store instances and mock methods
|
|
const canvasStore = useCanvasStore()
|
|
const commandStore = useCommandStore()
|
|
|
|
vi.spyOn(canvasStore, 'getCanvas').mockReturnValue(mockCanvas)
|
|
vi.spyOn(commandStore, 'execute').mockResolvedValue()
|
|
|
|
// Update the useSelectionState mock
|
|
vi.mocked(useSelectionState).mockReturnValue({
|
|
selectedNodes: {
|
|
value: mockSelectedNodes
|
|
}
|
|
} as ReturnType<typeof useSelectionState>)
|
|
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
const mountComponent = () => {
|
|
return mount(ExecuteButton, {
|
|
global: {
|
|
plugins: [i18n, PrimeVue],
|
|
directives: { tooltip: Tooltip },
|
|
stubs: {
|
|
'i-lucide:play': { template: '<div class="play-icon" />' }
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
describe('Rendering', () => {
|
|
it('should be able to render', () => {
|
|
const wrapper = mountComponent()
|
|
const button = wrapper.find('button')
|
|
expect(button.exists()).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('Click Handler', () => {
|
|
it('should execute Comfy.QueueSelectedOutputNodes command on click', async () => {
|
|
const commandStore = useCommandStore()
|
|
const wrapper = mountComponent()
|
|
const button = wrapper.find('button')
|
|
|
|
await button.trigger('click')
|
|
|
|
expect(commandStore.execute).toHaveBeenCalledWith(
|
|
'Comfy.QueueSelectedOutputNodes'
|
|
)
|
|
expect(commandStore.execute).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|
|
})
|