mirror of
https://github.com/langgenius/dify.git
synced 2026-06-26 14:51:13 +08:00
feat: router push to agent details page when create agent
This commit is contained in:
parent
c5558b562c
commit
1ce65bf1c9
@ -1,4 +1,4 @@
|
||||
import { render, screen, waitFor, within } from '@testing-library/react'
|
||||
import { act, render, screen, waitFor, within } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { CreateAgentDialog } from '../create-agent-dialog'
|
||||
|
||||
@ -12,6 +12,8 @@ const toastMock = vi.hoisted(() => ({
|
||||
success: vi.fn(),
|
||||
}))
|
||||
|
||||
const routerPushMock = vi.hoisted(() => vi.fn())
|
||||
|
||||
vi.mock('@tanstack/react-query', () => ({
|
||||
useMutation: () => ({
|
||||
isPending: mutationMock.isPending,
|
||||
@ -23,6 +25,12 @@ vi.mock('@langgenius/dify-ui/toast', () => ({
|
||||
toast: toastMock,
|
||||
}))
|
||||
|
||||
vi.mock('@/next/navigation', () => ({
|
||||
useRouter: () => ({
|
||||
push: routerPushMock,
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('@/service/client', () => ({
|
||||
consoleQuery: {
|
||||
agent: {
|
||||
@ -67,6 +75,26 @@ describe('CreateAgentDialog', () => {
|
||||
expect(mutationOptions).not.toHaveProperty('onError')
|
||||
})
|
||||
|
||||
it('navigates to the new agent configure page after creating an agent', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(<CreateAgentDialog />)
|
||||
|
||||
await user.click(screen.getByRole('button', { name: /agentV2\.roster\.createAgent/ }))
|
||||
|
||||
const dialog = await screen.findByRole('dialog', { name: 'agentV2.roster.createDialog.title' })
|
||||
await user.type(within(dialog).getByRole('textbox', { name: 'agentV2.roster.createForm.nameLabel' }), 'Research Agent')
|
||||
await user.type(within(dialog).getByRole('textbox', { name: 'agentV2.roster.createForm.roleLabel' }), 'Research Assistant')
|
||||
await user.click(within(dialog).getByRole('button', { name: 'common.operation.create' }))
|
||||
|
||||
const mutationOptions = mutationMock.mutate.mock.calls[0]?.[1]
|
||||
await act(async () => {
|
||||
mutationOptions.onSuccess({ id: 'agent-1' })
|
||||
})
|
||||
|
||||
expect(toastMock.success).toHaveBeenCalledWith('agentV2.roster.createSuccess')
|
||||
expect(routerPushMock).toHaveBeenCalledWith('/roster/agent/agent-1/configure')
|
||||
})
|
||||
|
||||
it('shows a field error when creating with an empty name', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(<CreateAgentDialog />)
|
||||
|
||||
@ -10,13 +10,16 @@ import { useMutation } from '@tanstack/react-query'
|
||||
import { useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import AppIconPicker from '@/app/components/base/app-icon-picker'
|
||||
import { useRouter } from '@/next/navigation'
|
||||
import { consoleQuery } from '@/service/client'
|
||||
import { getAgentDetailPath } from '../../agent-detail/routes'
|
||||
import { defaultAgentIcon } from './agent-form'
|
||||
import { AgentFormFields } from './agent-form-fields'
|
||||
|
||||
export function CreateAgentDialog() {
|
||||
const { t } = useTranslation('agentV2')
|
||||
const { t: tCommon } = useTranslation('common')
|
||||
const router = useRouter()
|
||||
const [open, setOpen] = useState(false)
|
||||
const [formKey, setFormKey] = useState(0)
|
||||
const [name, setName] = useState('')
|
||||
@ -59,9 +62,10 @@ export function CreateAgentDialog() {
|
||||
createAgentMutation.mutate({
|
||||
body,
|
||||
}, {
|
||||
onSuccess: () => {
|
||||
onSuccess: (createdAgent) => {
|
||||
toast.success(t('roster.createSuccess'))
|
||||
handleOpenChange(false)
|
||||
router.push(getAgentDetailPath(createdAgent.id, 'configure'))
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user