mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-01-31 21:39:54 +00:00
## Summary Migrates all unit tests from `tests-ui/` to colocate with their source files in `src/`, improving discoverability and maintainability. ## Changes - **What**: Relocated all unit tests to be adjacent to the code they test, following the `<source>.test.ts` naming convention - **Config**: Updated `vitest.config.ts` to remove `tests-ui` include pattern and `@tests-ui` alias - **Docs**: Moved testing documentation to `docs/testing/` with updated paths and patterns ## Review Focus - Migration patterns documented in `temp/plans/migrate-tests-ui-to-src.md` - Tests use `@/` path aliases instead of relative imports - Shared fixtures placed in `__fixtures__/` directories ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-7811-chore-migrate-tests-from-tests-ui-to-colocate-with-source-files-2da6d73d36508147a4cce85365dee614) by [Unito](https://www.unito.io) --------- Co-authored-by: Amp <amp@ampcode.com> Co-authored-by: GitHub Action <action@github.com>
122 lines
3.2 KiB
TypeScript
122 lines
3.2 KiB
TypeScript
import { useEventListener } from '@vueuse/core'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { nextTick } from 'vue'
|
|
|
|
import { useServerLogs } from '@/composables/useServerLogs'
|
|
import type { LogsWsMessage } from '@/schemas/apiSchema'
|
|
import { api } from '@/scripts/api'
|
|
|
|
vi.mock('@/scripts/api', () => ({
|
|
api: {
|
|
subscribeLogs: vi.fn(),
|
|
addEventListener: vi.fn(),
|
|
removeEventListener: vi.fn()
|
|
}
|
|
}))
|
|
|
|
vi.mock('@vueuse/core', () => ({
|
|
useEventListener: vi.fn().mockReturnValue(vi.fn())
|
|
}))
|
|
|
|
describe('useServerLogs', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('should initialize with empty logs array', () => {
|
|
const { logs } = useServerLogs()
|
|
expect(logs.value).toEqual([])
|
|
})
|
|
|
|
it('should not subscribe to logs by default', () => {
|
|
useServerLogs()
|
|
expect(api.subscribeLogs).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('should subscribe to logs when immediate is true', () => {
|
|
useServerLogs({ immediate: true })
|
|
expect(api.subscribeLogs).toHaveBeenCalledWith(true)
|
|
})
|
|
|
|
it('should start listening when startListening is called', async () => {
|
|
const { startListening } = useServerLogs()
|
|
|
|
await startListening()
|
|
|
|
expect(api.subscribeLogs).toHaveBeenCalledWith(true)
|
|
})
|
|
|
|
it('should stop listening when stopListening is called', async () => {
|
|
const { startListening, stopListening } = useServerLogs()
|
|
|
|
await startListening()
|
|
await stopListening()
|
|
|
|
expect(api.subscribeLogs).toHaveBeenCalledWith(false)
|
|
})
|
|
|
|
it('should register event listener when starting', async () => {
|
|
const { startListening } = useServerLogs()
|
|
|
|
await startListening()
|
|
|
|
expect(vi.mocked(useEventListener)).toHaveBeenCalledWith(
|
|
api,
|
|
'logs',
|
|
expect.any(Function)
|
|
)
|
|
})
|
|
|
|
it('should handle log messages correctly', async () => {
|
|
const { logs, startListening } = useServerLogs()
|
|
|
|
await startListening()
|
|
|
|
// Get the callback that was registered with useEventListener
|
|
const eventCallback = vi.mocked(useEventListener).mock.calls[0][2] as (
|
|
event: CustomEvent<LogsWsMessage>
|
|
) => void
|
|
|
|
// Simulate receiving a log event
|
|
const mockEvent = new CustomEvent('logs', {
|
|
detail: {
|
|
type: 'logs',
|
|
entries: [{ m: 'Log message 1' }, { m: 'Log message 2' }]
|
|
} as unknown as LogsWsMessage
|
|
}) as CustomEvent<LogsWsMessage>
|
|
|
|
eventCallback(mockEvent)
|
|
await nextTick()
|
|
|
|
expect(logs.value).toEqual(['Log message 1', 'Log message 2'])
|
|
})
|
|
|
|
it('should use the message filter if provided', async () => {
|
|
const { logs, startListening } = useServerLogs({
|
|
messageFilter: (msg) => msg !== 'remove me'
|
|
})
|
|
|
|
await startListening()
|
|
|
|
const eventCallback = vi.mocked(useEventListener).mock.calls[0][2] as (
|
|
event: CustomEvent<LogsWsMessage>
|
|
) => void
|
|
|
|
const mockEvent = new CustomEvent('logs', {
|
|
detail: {
|
|
type: 'logs',
|
|
entries: [
|
|
{ m: 'Log message 1 dont remove me' },
|
|
{ m: 'remove me' },
|
|
{ m: '' }
|
|
]
|
|
} as unknown as LogsWsMessage
|
|
}) as CustomEvent<LogsWsMessage>
|
|
|
|
eventCallback(mockEvent)
|
|
await nextTick()
|
|
|
|
expect(logs.value).toEqual(['Log message 1 dont remove me', ''])
|
|
})
|
|
})
|