fix(web): create snippet from workflow

This commit is contained in:
JzoNg
2026-05-29 14:49:00 +08:00
parent a62c616664
commit efe98b1e52
3 changed files with 10 additions and 40 deletions

View File

@ -6,7 +6,6 @@ const mockPush = vi.fn()
const mockMutateAsync = vi.fn()
const mockToastSuccess = vi.fn()
const mockToastError = vi.fn()
const mockSyncDraftWorkflow = vi.fn()
vi.mock('@/next/navigation', () => ({
useRouter: () => ({ push: mockPush }),
@ -19,14 +18,6 @@ vi.mock('@/service/use-snippets', () => ({
}),
}))
vi.mock('@/service/client', () => ({
consoleClient: {
snippets: {
syncDraftWorkflow: (...args: unknown[]) => mockSyncDraftWorkflow(...args),
},
},
}))
vi.mock('@langgenius/dify-ui/toast', () => ({
toast: {
success: (...args: unknown[]) => mockToastSuccess(...args),
@ -56,9 +47,13 @@ describe('useCreateSnippet', () => {
})
describe('Create Flow', () => {
it('should create snippet, sync draft workflow, and navigate on success', async () => {
it('should create snippet with graph and navigate on success', async () => {
mockMutateAsync.mockResolvedValue({ id: 'snippet-123' })
mockSyncDraftWorkflow.mockResolvedValue(undefined)
const graph = {
nodes: [],
edges: [],
viewport: { x: 0, y: 0, zoom: 1 },
}
const { result } = renderHook(() => useCreateSnippet())
@ -78,11 +73,7 @@ describe('useCreateSnippet', () => {
required: true,
},
],
graph: {
nodes: [],
edges: [],
viewport: { x: 0, y: 0, zoom: 1 },
},
graph,
})
})
@ -90,6 +81,7 @@ describe('useCreateSnippet', () => {
body: {
name: 'My snippet',
description: 'desc',
graph,
input_fields: [
{
label: 'topic',
@ -100,24 +92,6 @@ describe('useCreateSnippet', () => {
],
},
})
expect(mockSyncDraftWorkflow).toHaveBeenCalledWith({
params: { snippetId: 'snippet-123' },
body: {
input_fields: [
{
label: 'topic',
variable: 'topic',
type: PipelineInputVarType.textInput,
required: true,
},
],
graph: {
nodes: [],
edges: [],
viewport: { x: 0, y: 0, zoom: 1 },
},
},
})
expect(mockToastSuccess).toHaveBeenCalledWith('workflow.snippet.createSuccess')
expect(mockPush).toHaveBeenCalledWith('/snippets/snippet-123/orchestrate')
expect(result.current.isCreateSnippetDialogOpen).toBe(false)

View File

@ -3,7 +3,6 @@ import { toast } from '@langgenius/dify-ui/toast'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useRouter } from '@/next/navigation'
import { consoleClient } from '@/service/client'
import { useCreateSnippetMutation } from '@/service/use-snippets'
export const useCreateSnippet = () => {
@ -34,15 +33,11 @@ export const useCreateSnippet = () => {
body: {
name,
description: description || undefined,
graph,
input_fields,
},
})
await consoleClient.snippets.syncDraftWorkflow({
params: { snippetId: snippet.id },
body: { graph, input_fields },
})
toast.success(t('snippet.createSuccess', { ns: 'workflow' }))
handleCloseCreateSnippetDialog()
push(`/snippets/${snippet.id}/orchestrate`)

View File

@ -34,6 +34,7 @@ export type CreateSnippetPayload = {
name: string
description?: string
type?: SnippetType
graph?: Record<string, unknown>
input_fields?: SnippetInputField[]
}