mirror of
https://github.com/langgenius/dify.git
synced 2026-04-28 03:36:36 +08:00
fix: unittests
This commit is contained in:
parent
af8c399e94
commit
d35bd79d6d
@ -1,4 +1,5 @@
|
|||||||
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
|
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
|
||||||
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
import AppPublisher from '@/app/components/app/app-publisher'
|
import AppPublisher from '@/app/components/app/app-publisher'
|
||||||
import { AccessMode } from '@/models/access-control'
|
import { AccessMode } from '@/models/access-control'
|
||||||
@ -23,6 +24,27 @@ let mockAppDetail: {
|
|||||||
}
|
}
|
||||||
} | null = null
|
} | null = null
|
||||||
|
|
||||||
|
const createTestQueryClient = () =>
|
||||||
|
new QueryClient({
|
||||||
|
defaultOptions: {
|
||||||
|
queries: {
|
||||||
|
retry: false,
|
||||||
|
},
|
||||||
|
mutations: {
|
||||||
|
retry: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const renderWithQueryClient = (ui: React.ReactElement) => {
|
||||||
|
const queryClient = createTestQueryClient()
|
||||||
|
return render(
|
||||||
|
<QueryClientProvider client={queryClient}>
|
||||||
|
{ui}
|
||||||
|
</QueryClientProvider>,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
vi.mock('react-i18next', () => ({
|
vi.mock('react-i18next', () => ({
|
||||||
useTranslation: () => ({
|
useTranslation: () => ({
|
||||||
t: (key: string, options?: { ns?: string }) => options?.ns ? `${options.ns}.${key}` : key,
|
t: (key: string, options?: { ns?: string }) => options?.ns ? `${options.ns}.${key}` : key,
|
||||||
@ -76,6 +98,18 @@ vi.mock('@/app/components/app/overview/embedded', () => ({
|
|||||||
default: () => null,
|
default: () => null,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
vi.mock('@/app/components/workflow/collaboration/core/websocket-manager', () => ({
|
||||||
|
webSocketClient: {
|
||||||
|
getSocket: vi.fn(() => null),
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
|
vi.mock('@/app/components/workflow/collaboration/core/collaboration-manager', () => ({
|
||||||
|
collaborationManager: {
|
||||||
|
onAppPublishUpdate: vi.fn(() => vi.fn()),
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
vi.mock('@/app/components/app/app-access-control', () => ({
|
vi.mock('@/app/components/app/app-access-control', () => ({
|
||||||
default: ({
|
default: ({
|
||||||
onConfirm,
|
onConfirm,
|
||||||
@ -115,7 +149,7 @@ describe('App Access Control Flow', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('refreshes app detail after confirming access control updates', async () => {
|
it('refreshes app detail after confirming access control updates', async () => {
|
||||||
render(<AppPublisher publishedAt={1700000000} />)
|
renderWithQueryClient(<AppPublisher publishedAt={1700000000} />)
|
||||||
|
|
||||||
fireEvent.click(screen.getByRole('button', { name: 'workflow.common.publish' }))
|
fireEvent.click(screen.getByRole('button', { name: 'workflow.common.publish' }))
|
||||||
fireEvent.click(screen.getByText('app.accessControlDialog.accessItems.specific'))
|
fireEvent.click(screen.getByText('app.accessControlDialog.accessItems.specific'))
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
|
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
|
||||||
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
import AppPublisher from '@/app/components/app/app-publisher'
|
import AppPublisher from '@/app/components/app/app-publisher'
|
||||||
import { AccessMode } from '@/models/access-control'
|
import { AccessMode } from '@/models/access-control'
|
||||||
@ -27,6 +28,27 @@ let mockAppDetail: {
|
|||||||
}
|
}
|
||||||
} | null = null
|
} | null = null
|
||||||
|
|
||||||
|
const createTestQueryClient = () =>
|
||||||
|
new QueryClient({
|
||||||
|
defaultOptions: {
|
||||||
|
queries: {
|
||||||
|
retry: false,
|
||||||
|
},
|
||||||
|
mutations: {
|
||||||
|
retry: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const renderWithQueryClient = (ui: React.ReactElement) => {
|
||||||
|
const queryClient = createTestQueryClient()
|
||||||
|
return render(
|
||||||
|
<QueryClientProvider client={queryClient}>
|
||||||
|
{ui}
|
||||||
|
</QueryClientProvider>,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
vi.mock('react-i18next', () => ({
|
vi.mock('react-i18next', () => ({
|
||||||
useTranslation: () => ({
|
useTranslation: () => ({
|
||||||
t: (key: string) => key,
|
t: (key: string) => key,
|
||||||
@ -106,6 +128,18 @@ vi.mock('@/app/components/app/overview/embedded', () => ({
|
|||||||
),
|
),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
vi.mock('@/app/components/workflow/collaboration/core/websocket-manager', () => ({
|
||||||
|
webSocketClient: {
|
||||||
|
getSocket: vi.fn(() => null),
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
|
vi.mock('@/app/components/workflow/collaboration/core/collaboration-manager', () => ({
|
||||||
|
collaborationManager: {
|
||||||
|
onAppPublishUpdate: vi.fn(() => vi.fn()),
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
vi.mock('@/app/components/app/app-access-control', () => ({
|
vi.mock('@/app/components/app/app-access-control', () => ({
|
||||||
default: () => <div data-testid="app-access-control" />,
|
default: () => <div data-testid="app-access-control" />,
|
||||||
}))
|
}))
|
||||||
@ -183,7 +217,7 @@ describe('App Publisher Flow', () => {
|
|||||||
it('publishes from the summary panel and tracks the publish event', async () => {
|
it('publishes from the summary panel and tracks the publish event', async () => {
|
||||||
const onPublish = vi.fn().mockResolvedValue(undefined)
|
const onPublish = vi.fn().mockResolvedValue(undefined)
|
||||||
|
|
||||||
render(
|
renderWithQueryClient(
|
||||||
<AppPublisher
|
<AppPublisher
|
||||||
publishedAt={1700000000}
|
publishedAt={1700000000}
|
||||||
onPublish={onPublish}
|
onPublish={onPublish}
|
||||||
@ -210,7 +244,7 @@ describe('App Publisher Flow', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('opens embedded modal and resolves the installed explore target', async () => {
|
it('opens embedded modal and resolves the installed explore target', async () => {
|
||||||
render(<AppPublisher publishedAt={1700000000} />)
|
renderWithQueryClient(<AppPublisher publishedAt={1700000000} />)
|
||||||
|
|
||||||
fireEvent.click(screen.getByText('common.publish'))
|
fireEvent.click(screen.getByText('common.publish'))
|
||||||
fireEvent.click(screen.getByText('common.embedIntoSite'))
|
fireEvent.click(screen.getByText('common.embedIntoSite'))
|
||||||
@ -231,7 +265,7 @@ describe('App Publisher Flow', () => {
|
|||||||
installed_apps: [],
|
installed_apps: [],
|
||||||
})
|
})
|
||||||
|
|
||||||
render(<AppPublisher publishedAt={1700000000} />)
|
renderWithQueryClient(<AppPublisher publishedAt={1700000000} />)
|
||||||
|
|
||||||
fireEvent.click(screen.getByText('common.publish'))
|
fireEvent.click(screen.getByText('common.publish'))
|
||||||
fireEvent.click(screen.getByText('common.openInExplore'))
|
fireEvent.click(screen.getByText('common.openInExplore'))
|
||||||
|
|||||||
@ -47,6 +47,36 @@ vi.mock('@/hooks/use-breakpoints', () => ({
|
|||||||
default: vi.fn(),
|
default: vi.fn(),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
vi.mock('@/context/global-public-context', async (importOriginal) => {
|
||||||
|
const actual = await importOriginal<typeof import('@/context/global-public-context')>()
|
||||||
|
const systemFeatures = {
|
||||||
|
...actual.useGlobalPublicStore.getState().systemFeatures,
|
||||||
|
webapp_auth: {
|
||||||
|
...actual.useGlobalPublicStore.getState().systemFeatures.webapp_auth,
|
||||||
|
enabled: true,
|
||||||
|
},
|
||||||
|
branding: {
|
||||||
|
...actual.useGlobalPublicStore.getState().systemFeatures.branding,
|
||||||
|
enabled: false,
|
||||||
|
},
|
||||||
|
enable_marketplace: true,
|
||||||
|
enable_collaboration_mode: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
useGlobalPublicStore: (selector: (state: Record<string, unknown>) => unknown) => selector({
|
||||||
|
systemFeatures,
|
||||||
|
}),
|
||||||
|
useSystemFeaturesQuery: () => ({
|
||||||
|
data: systemFeatures,
|
||||||
|
isPending: false,
|
||||||
|
isLoading: false,
|
||||||
|
isFetching: false,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
vi.mock('@/app/components/header/account-setting/model-provider-page/hooks', () => ({
|
vi.mock('@/app/components/header/account-setting/model-provider-page/hooks', () => ({
|
||||||
useDefaultModel: vi.fn(() => ({ data: null, isLoading: false })),
|
useDefaultModel: vi.fn(() => ({ data: null, isLoading: false })),
|
||||||
useUpdateDefaultModel: vi.fn(() => ({ trigger: vi.fn() })),
|
useUpdateDefaultModel: vi.fn(() => ({ trigger: vi.fn() })),
|
||||||
@ -54,6 +84,7 @@ vi.mock('@/app/components/header/account-setting/model-provider-page/hooks', ()
|
|||||||
useInvalidateDefaultModel: vi.fn(() => vi.fn()),
|
useInvalidateDefaultModel: vi.fn(() => vi.fn()),
|
||||||
useModelList: vi.fn(() => ({ data: [], isLoading: false })),
|
useModelList: vi.fn(() => ({ data: [], isLoading: false })),
|
||||||
useSystemDefaultModelAndModelList: vi.fn(() => [null, vi.fn()]),
|
useSystemDefaultModelAndModelList: vi.fn(() => [null, vi.fn()]),
|
||||||
|
useMarketplaceAllPlugins: vi.fn(() => ({ plugins: [], isLoading: false })),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
vi.mock('@/app/components/header/account-setting/model-provider-page/atoms', () => ({
|
vi.mock('@/app/components/header/account-setting/model-provider-page/atoms', () => ({
|
||||||
@ -70,6 +101,11 @@ vi.mock('@/service/use-common', () => ({
|
|||||||
useProviderContext: vi.fn(),
|
useProviderContext: vi.fn(),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
vi.mock('@/app/components/billing/billing-page', () => ({
|
||||||
|
__esModule: true,
|
||||||
|
default: () => <div data-testid="billing-page" />,
|
||||||
|
}))
|
||||||
|
|
||||||
const baseAppContextValue: AppContextValue = {
|
const baseAppContextValue: AppContextValue = {
|
||||||
userProfile: {
|
userProfile: {
|
||||||
id: '1',
|
id: '1',
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import type { LoroMap } from 'loro-crdt'
|
import type { LoroMap } from 'loro-crdt/base64'
|
||||||
import type { Node } from '@/app/components/workflow/types'
|
import type { Node } from '@/app/components/workflow/types'
|
||||||
import { LoroDoc } from 'loro-crdt'
|
import { LoroDoc } from 'loro-crdt/base64'
|
||||||
import { BlockEnum } from '@/app/components/workflow/types'
|
import { BlockEnum } from '@/app/components/workflow/types'
|
||||||
import { CollaborationManager } from '../collaboration-manager'
|
import { CollaborationManager } from '../collaboration-manager'
|
||||||
|
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
import type { LoroMap } from 'loro-crdt'
|
import type { LoroMap } from 'loro-crdt/base64'
|
||||||
import type {
|
import type {
|
||||||
NodePanelPresenceMap,
|
NodePanelPresenceMap,
|
||||||
NodePanelPresenceUser,
|
NodePanelPresenceUser,
|
||||||
} from '@/app/components/workflow/collaboration/types/collaboration'
|
} from '@/app/components/workflow/collaboration/types/collaboration'
|
||||||
import type { CommonNodeType, Edge, Node } from '@/app/components/workflow/types'
|
import type { CommonNodeType, Edge, Node } from '@/app/components/workflow/types'
|
||||||
import { LoroDoc } from 'loro-crdt'
|
import { LoroDoc } from 'loro-crdt/base64'
|
||||||
import { Position } from 'reactflow'
|
import { Position } from 'reactflow'
|
||||||
import { CollaborationManager } from '@/app/components/workflow/collaboration/core/collaboration-manager'
|
import { CollaborationManager } from '@/app/components/workflow/collaboration/core/collaboration-manager'
|
||||||
import { BlockEnum } from '@/app/components/workflow/types'
|
import { BlockEnum } from '@/app/components/workflow/types'
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import type { LoroDoc } from 'loro-crdt'
|
import type { LoroDoc } from 'loro-crdt/base64'
|
||||||
import type { Socket } from 'socket.io-client'
|
import type { Socket } from 'socket.io-client'
|
||||||
import { CRDTProvider } from '../crdt-provider'
|
import { CRDTProvider } from '../crdt-provider'
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user