mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-04-19 22:09:37 +00:00
fix: improve widgetStore tests per review feedback
- Remove unsafe `as unknown` casts; use `as const` for V1 tuples, direct objects for V2 - Assert ComfyWidgets.INT identity instead of fragile not.toBe(override) - Remove change-detector core widgets test that only tests the mock - Remove redundant registerCustomWidgets test (duplicate of L40-44) Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/10647#discussion_r3011921020 https://github.com/Comfy-Org/ComfyUI_frontend/pull/10647#discussion_r3005617426 https://github.com/Comfy-Org/ComfyUI_frontend/pull/10647#discussion_r3011921056 https://github.com/Comfy-Org/ComfyUI_frontend/pull/10647#discussion_r3011921018 https://github.com/Comfy-Org/ComfyUI_frontend/pull/10647#discussion_r3011921060
This commit is contained in:
@@ -2,14 +2,9 @@ import { createTestingPinia } from '@pinia/testing'
|
||||
import { setActivePinia } from 'pinia'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import type { InputSpec as InputSpecV2 } from '@/schemas/nodeDef/nodeDefSchemaV2'
|
||||
import type { InputSpec as InputSpecV1 } from '@/schemas/nodeDefSchema'
|
||||
import { ComfyWidgets } from '@/scripts/widgets'
|
||||
import { useWidgetStore } from '@/stores/widgetStore'
|
||||
|
||||
/** Cast shorthand — the mock bypasses Zod validation, so we only need the shape `inputIsWidget` reads. */
|
||||
const v1 = (spec: unknown) => spec as InputSpecV1
|
||||
const v2 = (spec: unknown) => spec as InputSpecV2
|
||||
|
||||
vi.mock('@/scripts/widgets', () => ({
|
||||
ComfyWidgets: {
|
||||
INT: vi.fn(),
|
||||
@@ -30,63 +25,43 @@ describe('widgetStore', () => {
|
||||
})
|
||||
|
||||
describe('widgets getter', () => {
|
||||
it('includes core widgets', () => {
|
||||
const store = useWidgetStore()
|
||||
expect(store.widgets.has('INT')).toBe(true)
|
||||
expect(store.widgets.has('FLOAT')).toBe(true)
|
||||
expect(store.widgets.has('STRING')).toBe(true)
|
||||
})
|
||||
|
||||
it('includes custom widgets after registration', () => {
|
||||
const store = useWidgetStore()
|
||||
const customFn = vi.fn()
|
||||
store.registerCustomWidgets({ CUSTOM_TYPE: customFn })
|
||||
expect(store.widgets.has('CUSTOM_TYPE')).toBe(true)
|
||||
expect(store.widgets.get('CUSTOM_TYPE')).toBe(customFn)
|
||||
})
|
||||
|
||||
it('core widgets take precedence over custom widgets with same key', () => {
|
||||
const store = useWidgetStore()
|
||||
const override = vi.fn()
|
||||
store.registerCustomWidgets({ INT: override })
|
||||
// Core widgets are spread last, so they win
|
||||
expect(store.widgets.get('INT')).not.toBe(override)
|
||||
})
|
||||
})
|
||||
|
||||
describe('registerCustomWidgets', () => {
|
||||
it('registers multiple custom widgets', () => {
|
||||
const store = useWidgetStore()
|
||||
store.registerCustomWidgets({
|
||||
TYPE_A: vi.fn(),
|
||||
TYPE_B: vi.fn()
|
||||
})
|
||||
expect(store.widgets.has('TYPE_A')).toBe(true)
|
||||
expect(store.widgets.has('TYPE_B')).toBe(true)
|
||||
expect(store.widgets.get('INT')).toBe(ComfyWidgets.INT)
|
||||
})
|
||||
})
|
||||
|
||||
describe('inputIsWidget', () => {
|
||||
it('returns true for known widget type (v1 spec)', () => {
|
||||
const store = useWidgetStore()
|
||||
expect(store.inputIsWidget(v1(['INT', {}]))).toBe(true)
|
||||
expect(store.inputIsWidget(['INT', {}] as const)).toBe(true)
|
||||
})
|
||||
|
||||
it('returns false for unknown type (v1 spec)', () => {
|
||||
const store = useWidgetStore()
|
||||
expect(store.inputIsWidget(v1(['UNKNOWN_TYPE', {}]))).toBe(false)
|
||||
expect(store.inputIsWidget(['UNKNOWN_TYPE', {}] as const)).toBe(false)
|
||||
})
|
||||
|
||||
it('returns true for v2 spec with known type', () => {
|
||||
const store = useWidgetStore()
|
||||
expect(
|
||||
store.inputIsWidget(v2({ type: 'STRING', name: 'test_input' }))
|
||||
store.inputIsWidget({ type: 'STRING', name: 'test_input' })
|
||||
).toBe(true)
|
||||
})
|
||||
|
||||
it('returns false for v2 spec with unknown type', () => {
|
||||
const store = useWidgetStore()
|
||||
expect(
|
||||
store.inputIsWidget(v2({ type: 'LATENT', name: 'test_input' }))
|
||||
store.inputIsWidget({ type: 'LATENT', name: 'test_input' })
|
||||
).toBe(false)
|
||||
})
|
||||
|
||||
@@ -94,7 +69,7 @@ describe('widgetStore', () => {
|
||||
const store = useWidgetStore()
|
||||
store.registerCustomWidgets({ MY_WIDGET: vi.fn() })
|
||||
expect(
|
||||
store.inputIsWidget(v2({ type: 'MY_WIDGET', name: 'test_input' }))
|
||||
store.inputIsWidget({ type: 'MY_WIDGET', name: 'test_input' })
|
||||
).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user