mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-02-20 23:04:06 +00:00
## Summary
This PR removes unsafe type assertions ("as unknown as Type") from test
files and improves type safety across the codebase.
### Key Changes
#### Type Safety Improvements
- Removed improper `as unknown as Type` patterns from 17 test files in
Group 8 part 7
- Replaced with proper TypeScript patterns using factory functions and
Mock types
- Fixed createTestingPinia usage in test files (was incorrectly using
createPinia)
- Fixed vi.hoisted pattern for mockSetDirty in viewport tests
- Fixed vi.doMock lint issues with vi.mock and vi.hoisted pattern
- Retained necessary `as unknown as` casts only for complex mock objects
where direct type assertions would fail
### Files Changed
Test files (Group 8 part 7 - services, stores, utils):
- src/services/nodeOrganizationService.test.ts
- src/services/providers/algoliaSearchProvider.test.ts
- src/services/providers/registrySearchProvider.test.ts
- src/stores/comfyRegistryStore.test.ts
- src/stores/domWidgetStore.test.ts
- src/stores/executionStore.test.ts
- src/stores/firebaseAuthStore.test.ts
- src/stores/modelToNodeStore.test.ts
- src/stores/queueStore.test.ts
- src/stores/subgraphNavigationStore.test.ts
- src/stores/subgraphNavigationStore.viewport.test.ts
- src/stores/subgraphStore.test.ts
- src/stores/systemStatsStore.test.ts
- src/stores/workspace/nodeHelpStore.test.ts
- src/utils/colorUtil.test.ts
- src/utils/executableGroupNodeChildDTO.test.ts
Source files:
- src/stores/modelStore.ts - Improved type handling
### Testing
- All TypeScript type checking passes (`pnpm typecheck`)
- All affected test files pass (`pnpm test:unit`)
- Linting passes without errors (`pnpm lint`)
- Code formatting applied (`pnpm format`)
Part of the "Road to No Explicit Any" initiative, cleaning up type
casting issues from branch `fix/remove-any-types-part8`.
### Previous PRs in this series:
- Part 2: #7401
- Part 3: #7935
- Part 4: #7970
- Part 5: #8064
- Part 6: #8083
- Part 7: #8092
- Part 8 Group 1: #8253
- Part 8 Group 2: #8258
- Part 8 Group 3: #8304
- Part 8 Group 4: #8314
- Part 8 Group 5: #8329
- Part 8 Group 6: #8344
- Part 8 Group 7: #8459 (this PR)
┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-8459-Road-to-No-explicit-any-Group-8-part-7-test-files-2f86d73d36508114ad28d82e72a3a5e9)
by [Unito](https://www.unito.io)
154 lines
4.3 KiB
TypeScript
154 lines
4.3 KiB
TypeScript
import { setActivePinia } from 'pinia'
|
|
import { beforeEach, describe, expect, it } from 'vitest'
|
|
|
|
import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
|
|
import { useDomWidgetStore } from '@/stores/domWidgetStore'
|
|
import { createTestingPinia } from '@pinia/testing'
|
|
|
|
// Mock DOM widget for testing
|
|
const createMockDOMWidget = (id: string) => {
|
|
const element = document.createElement('input')
|
|
return {
|
|
id,
|
|
element,
|
|
node: {
|
|
id: 'node-1',
|
|
title: 'Test Node',
|
|
pos: [0, 0],
|
|
size: [200, 100]
|
|
} as Partial<LGraphNode> as LGraphNode,
|
|
name: 'test_widget',
|
|
type: 'text',
|
|
value: 'test',
|
|
options: {},
|
|
y: 0,
|
|
margin: 10,
|
|
isVisible: () => true,
|
|
containerNode: undefined
|
|
}
|
|
}
|
|
|
|
describe('domWidgetStore', () => {
|
|
let store: ReturnType<typeof useDomWidgetStore>
|
|
|
|
beforeEach(() => {
|
|
setActivePinia(createTestingPinia({ stubActions: false }))
|
|
store = useDomWidgetStore()
|
|
})
|
|
|
|
describe('widget registration', () => {
|
|
it('should register a widget with default state', () => {
|
|
const widget = createMockDOMWidget('widget-1')
|
|
|
|
store.registerWidget(widget)
|
|
|
|
expect(store.widgetStates.has('widget-1')).toBe(true)
|
|
const state = store.widgetStates.get('widget-1')
|
|
expect(state).toBeDefined()
|
|
expect(state!.widget).toBe(widget)
|
|
expect(state!.visible).toBe(true)
|
|
expect(state!.active).toBe(true)
|
|
expect(state!.readonly).toBe(false)
|
|
expect(state!.zIndex).toBe(0)
|
|
expect(state!.pos).toEqual([0, 0])
|
|
expect(state!.size).toEqual([0, 0])
|
|
})
|
|
|
|
it('should not register the same widget twice', () => {
|
|
const widget = createMockDOMWidget('widget-1')
|
|
|
|
store.registerWidget(widget)
|
|
store.registerWidget(widget)
|
|
|
|
// Should still only have one entry
|
|
const states = Array.from(store.widgetStates.values())
|
|
expect(states.length).toBe(1)
|
|
})
|
|
})
|
|
|
|
describe('widget unregistration', () => {
|
|
it('should unregister a widget by id', () => {
|
|
const widget = createMockDOMWidget('widget-1')
|
|
|
|
store.registerWidget(widget)
|
|
expect(store.widgetStates.has('widget-1')).toBe(true)
|
|
|
|
store.unregisterWidget('widget-1')
|
|
expect(store.widgetStates.has('widget-1')).toBe(false)
|
|
})
|
|
|
|
it('should handle unregistering non-existent widget gracefully', () => {
|
|
// Should not throw
|
|
expect(() => {
|
|
store.unregisterWidget('non-existent')
|
|
}).not.toThrow()
|
|
})
|
|
})
|
|
|
|
describe('widget state management', () => {
|
|
it('should activate a widget', () => {
|
|
const widget = createMockDOMWidget('widget-1')
|
|
store.registerWidget(widget)
|
|
|
|
// Set to inactive first
|
|
const state = store.widgetStates.get('widget-1')!
|
|
state.active = false
|
|
|
|
store.activateWidget('widget-1')
|
|
expect(state.active).toBe(true)
|
|
})
|
|
|
|
it('should deactivate a widget', () => {
|
|
const widget = createMockDOMWidget('widget-1')
|
|
store.registerWidget(widget)
|
|
|
|
store.deactivateWidget('widget-1')
|
|
const state = store.widgetStates.get('widget-1')
|
|
expect(state!.active).toBe(false)
|
|
})
|
|
|
|
it('should handle activating non-existent widget gracefully', () => {
|
|
expect(() => {
|
|
store.activateWidget('non-existent')
|
|
}).not.toThrow()
|
|
})
|
|
})
|
|
|
|
describe('computed states', () => {
|
|
it('should separate active and inactive widget states', () => {
|
|
const widget1 = createMockDOMWidget('widget-1')
|
|
const widget2 = createMockDOMWidget('widget-2')
|
|
|
|
store.registerWidget(widget1)
|
|
store.registerWidget(widget2)
|
|
|
|
// Deactivate widget2
|
|
store.deactivateWidget('widget-2')
|
|
|
|
expect(store.activeWidgetStates.length).toBe(1)
|
|
expect(store.activeWidgetStates[0].widget.id).toBe('widget-1')
|
|
|
|
expect(store.inactiveWidgetStates.length).toBe(1)
|
|
expect(store.inactiveWidgetStates[0].widget.id).toBe('widget-2')
|
|
})
|
|
})
|
|
|
|
describe('clear functionality', () => {
|
|
it('should clear all widget states', () => {
|
|
const widget1 = createMockDOMWidget('widget-1')
|
|
const widget2 = createMockDOMWidget('widget-2')
|
|
|
|
store.registerWidget(widget1)
|
|
store.registerWidget(widget2)
|
|
|
|
expect(store.widgetStates.size).toBe(2)
|
|
|
|
store.clear()
|
|
|
|
expect(store.widgetStates.size).toBe(0)
|
|
expect(store.activeWidgetStates.length).toBe(0)
|
|
expect(store.inactiveWidgetStates.length).toBe(0)
|
|
})
|
|
})
|
|
})
|