From ac9083fbf1ada2cbf3dfd77bdfd746a7a70aa902 Mon Sep 17 00:00:00 2001 From: JzoNg Date: Wed, 27 May 2026 21:24:07 +0800 Subject: [PATCH] fix(web): tooltip of snippet saved --- .../__tests__/use-snippet-publish.spec.ts | 55 +++---------------- .../components/hooks/use-snippet-publish.ts | 2 +- web/i18n/en-US/snippet.json | 1 + web/i18n/zh-Hans/snippet.json | 1 + 4 files changed, 11 insertions(+), 48 deletions(-) diff --git a/web/app/components/snippets/components/hooks/__tests__/use-snippet-publish.spec.ts b/web/app/components/snippets/components/hooks/__tests__/use-snippet-publish.spec.ts index 0a3383b21dc..801c86c6243 100644 --- a/web/app/components/snippets/components/hooks/__tests__/use-snippet-publish.spec.ts +++ b/web/app/components/snippets/components/hooks/__tests__/use-snippet-publish.spec.ts @@ -1,19 +1,13 @@ import { toast } from '@langgenius/dify-ui/toast' -import { act, renderHook, waitFor } from '@testing-library/react' +import { act, renderHook } from '@testing-library/react' import { useSnippetPublish } from '../use-snippet-publish' const mockMutateAsync = vi.fn() -const mockUseKeyPress = vi.fn() const mockSetPublishedAt = vi.fn() const mockSetQueryData = vi.fn() const mockHandleCheckBeforePublish = vi.fn<() => Promise>() let isPending = false -let shortcutHandler: ((event: KeyboardEvent) => void) | undefined - -vi.mock('ahooks', () => ({ - useKeyPress: (...args: Parameters) => mockUseKeyPress(...args), -})) vi.mock('@langgenius/dify-ui/toast', () => ({ toast: { @@ -53,12 +47,8 @@ describe('useSnippetPublish', () => { beforeEach(() => { vi.clearAllMocks() isPending = false - shortcutHandler = undefined mockHandleCheckBeforePublish.mockResolvedValue(true) mockMutateAsync.mockResolvedValue({ created_at: 1_712_345_678 }) - mockUseKeyPress.mockImplementation((_key, handler) => { - shortcutHandler = handler - }) }) describe('Publish action', () => { @@ -81,7 +71,7 @@ describe('useSnippetPublish', () => { const updateSnippetDetail = setQueryDataCall![1] as (old: { is_published: boolean }) => { is_published: boolean } expect(updateSnippetDetail({ is_published: false })).toEqual({ is_published: true }) expect(mockSetPublishedAt).toHaveBeenCalledWith(1_712_345_678) - expect(toast.success).toHaveBeenCalledWith('snippet.publishSuccess') + expect(toast.success).toHaveBeenCalledWith('snippet.saveSuccess') }) it('should not publish the snippet when checklist validation fails', async () => { @@ -117,42 +107,13 @@ describe('useSnippetPublish', () => { }) }) - describe('Keyboard shortcut', () => { - it('should trigger publish on ctrl+shift+p in the orchestrate section', async () => { - renderHook(() => useSnippetPublish({ - snippetId: 'snippet-1', - })) + it('should expose publishing pending state', () => { + isPending = true - const event = new KeyboardEvent('keydown') - const preventDefault = vi.spyOn(event, 'preventDefault') + const { result } = renderHook(() => useSnippetPublish({ + snippetId: 'snippet-1', + })) - act(() => { - shortcutHandler?.(event) - }) - - await waitFor(() => { - expect(mockMutateAsync).toHaveBeenCalledWith({ - params: { snippetId: 'snippet-1' }, - }) - }) - expect(preventDefault).toHaveBeenCalledTimes(1) - }) - - it('should ignore the shortcut while publishing is pending', () => { - isPending = true - renderHook(() => useSnippetPublish({ - snippetId: 'snippet-1', - })) - - const event = new KeyboardEvent('keydown') - const preventDefault = vi.spyOn(event, 'preventDefault') - - act(() => { - shortcutHandler?.(event) - }) - - expect(mockMutateAsync).not.toHaveBeenCalled() - expect(preventDefault).not.toHaveBeenCalled() - }) + expect(result.current.isPublishing).toBe(true) }) }) diff --git a/web/app/components/snippets/components/hooks/use-snippet-publish.ts b/web/app/components/snippets/components/hooks/use-snippet-publish.ts index 1b852713c16..4756ca7bc12 100644 --- a/web/app/components/snippets/components/hooks/use-snippet-publish.ts +++ b/web/app/components/snippets/components/hooks/use-snippet-publish.ts @@ -39,7 +39,7 @@ export const useSnippetPublish = ({ old => old ? { ...old, is_published: true } : old, ) workflowStore.getState().setPublishedAt(publishedWorkflow.created_at) - toast.success(t('publishSuccess')) + toast.success(t('saveSuccess')) } catch (error) { toast.error(error instanceof Error ? error.message : t('publishFailed')) diff --git a/web/i18n/en-US/snippet.json b/web/i18n/en-US/snippet.json index 49a3f11af44..26046d54074 100644 --- a/web/i18n/en-US/snippet.json +++ b/web/i18n/en-US/snippet.json @@ -36,6 +36,7 @@ "publishMenuCurrentDraft": "Current draft unpublished", "publishSuccess": "Snippet published", "save": "Save", + "saveSuccess": "Snippet saved", "sectionOrchestrate": "Orchestrate", "testRunButton": "Test run", "typeLabel": "Snippet", diff --git a/web/i18n/zh-Hans/snippet.json b/web/i18n/zh-Hans/snippet.json index 7ceb44b483e..894b8dc4bd0 100644 --- a/web/i18n/zh-Hans/snippet.json +++ b/web/i18n/zh-Hans/snippet.json @@ -36,6 +36,7 @@ "publishMenuCurrentDraft": "当前草稿未发布", "publishSuccess": "Snippet 已发布", "save": "保存", + "saveSuccess": "Snippet 保存成功", "sectionOrchestrate": "编排", "testRunButton": "测试运行", "typeLabel": "Snippet",