Files
dify/web/app/components/workflow/skill/hooks/use-skill-auto-save.spec.tsx
yyh 30b9295156 Merge remote-tracking branch 'origin/build/feat/hitl' into wip/hitl-merge-web-conflicts-20260206-175434
# Conflicts:
#	api/.env.example
#	api/core/app/apps/advanced_chat/app_generator.py
#	api/core/app/apps/advanced_chat/app_runner.py
#	api/core/app/apps/advanced_chat/generate_task_pipeline.py
#	api/core/app/apps/workflow/app_generator.py
#	api/core/app/apps/workflow/app_runner.py
#	api/core/app/entities/queue_entities.py
#	api/core/workflow/node_events/__init__.py
#	api/core/workflow/runtime/graph_runtime_state.py
#	api/fields/message_fields.py
#	api/services/workflow_service.py
#	web/app/components/app/app-publisher/index.tsx
#	web/app/components/base/chat/chat/answer/index.tsx
#	web/app/components/base/chat/chat/hooks.ts
#	web/app/components/base/chat/chat/type.ts
#	web/app/components/base/prompt-editor/index.tsx
#	web/app/components/rag-pipeline/hooks/use-available-nodes-meta-data.ts
#	web/app/components/share/text-generation/result/header.tsx
#	web/app/components/workflow-app/components/workflow-header/features-trigger.tsx
#	web/app/components/workflow-app/hooks/use-workflow-run.ts
#	web/app/components/workflow/hooks/use-checklist.ts
#	web/app/components/workflow/hooks/use-fetch-workflow-inspect-vars.ts
#	web/app/components/workflow/hooks/use-workflow.ts
#	web/app/components/workflow/nodes/_base/components/variable/var-reference-picker.tsx
#	web/app/components/workflow/nodes/_base/components/variable/var-reference-vars.tsx
#	web/app/components/workflow/nodes/_base/node.tsx
#	web/app/components/workflow/nodes/tool/components/mixed-variable-text-input/components/placeholder.tsx
#	web/app/components/workflow/panel/debug-and-preview/hooks.ts
#	web/app/components/workflow/panel/workflow-preview.tsx
#	web/app/components/workflow/store/workflow/workflow-slice.ts
#	web/eslint-suppressions.json
#	web/i18n/en-US/common.json
#	web/i18n/zh-Hans/common.json
#	web/i18n/zh-Hant/common.json
#	web/service/workflow.ts
2026-02-06 19:13:51 +08:00

81 lines
1.9 KiB
TypeScript

import { act, renderHook } from '@testing-library/react'
import { useSkillAutoSave } from './use-skill-auto-save'
const { mockSaveAllDirty } = vi.hoisted(() => ({
mockSaveAllDirty: vi.fn(),
}))
vi.mock('./skill-save-context', () => ({
useSkillSaveManager: () => ({
saveAllDirty: mockSaveAllDirty,
}),
}))
const setVisibilityState = (state: DocumentVisibilityState) => {
Object.defineProperty(document, 'visibilityState', {
configurable: true,
value: state,
})
}
describe('useSkillAutoSave', () => {
beforeEach(() => {
vi.clearAllMocks()
setVisibilityState('visible')
})
it('should save all dirty files on unmount', () => {
const { unmount } = renderHook(() => useSkillAutoSave())
unmount()
expect(mockSaveAllDirty).toHaveBeenCalledTimes(1)
})
it('should save all dirty files when document becomes hidden', () => {
renderHook(() => useSkillAutoSave())
setVisibilityState('hidden')
act(() => {
document.dispatchEvent(new Event('visibilitychange'))
})
expect(mockSaveAllDirty).toHaveBeenCalledTimes(1)
})
it('should not save when document becomes visible', () => {
renderHook(() => useSkillAutoSave())
setVisibilityState('visible')
act(() => {
document.dispatchEvent(new Event('visibilitychange'))
})
expect(mockSaveAllDirty).not.toHaveBeenCalled()
})
it('should save all dirty files before unload', () => {
renderHook(() => useSkillAutoSave())
act(() => {
window.dispatchEvent(new Event('beforeunload'))
})
expect(mockSaveAllDirty).toHaveBeenCalledTimes(1)
})
it('should remove listeners after unmount', () => {
const { unmount } = renderHook(() => useSkillAutoSave())
unmount()
setVisibilityState('hidden')
act(() => {
document.dispatchEvent(new Event('visibilitychange'))
window.dispatchEvent(new Event('beforeunload'))
})
expect(mockSaveAllDirty).toHaveBeenCalledTimes(1)
})
})