Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

test(web): add global zustand mock for tests (#31149)

yyhe3b0918dd9b4
diff --git a/web/__mocks__/zustand.ts b/web/__mocks__/zustand.ts
new file mode 100644
index 00000000000..7255e5ef86e
--- /dev/null
+++ b/web/__mocks__/zustand.ts
@@ -0,0 +1,56 @@
+import type * as ZustandExportedTypes from 'zustand'
+import { act } from '@testing-library/react'
+
+export * from 'zustand'
+
+const { create: actualCreate, createStore: actualCreateStore }
+ // eslint-disable-next-line antfu/no-top-level-await
+ = await vi.importActual<typeof ZustandExportedTypes>('zustand')
+
+export const storeResetFns = new Set<() => void>()
+
+const createUncurried = <T>(
+ stateCreator: ZustandExportedTypes.StateCreator<T>,
+) => {
+ const store = actualCreate(stateCreator)
+ const initialState = store.getInitialState()
+ storeResetFns.add(() => {
+ store.setState(initialState, true)
+ })
+ return store
+}
+
+export const create = (<T>(
+ stateCreator: ZustandExportedTypes.StateCreator<T>,
+) => {
+ return typeof stateCreator === 'function'
+ ? createUncurried(stateCreator)
+ : createUncurried
+}) as typeof ZustandExportedTypes.create
+
+const createStoreUncurried = <T>(
+ stateCreator: ZustandExportedTypes.StateCreator<T>,
+) => {
+ const store = actualCreateStore(stateCreator)
+ const initialState = store.getInitialState()
+ storeResetFns.add(() => {
+ store.setState(initialState, true)
+ })
+ return store
+}
+
+export const createStore = (<T>(
+ stateCreator: ZustandExportedTypes.StateCreator<T>,
+) => {
+ return typeof stateCreator === 'function'
+ ? createStoreUncurried(stateCreator)
+ : createStoreUncurried
+}) as typeof ZustandExportedTypes.createStore
+
+afterEach(() => {
+ act(() => {
+ storeResetFns.forEach((resetFn) => {
+ resetFn()
+ })
+ })
+})
diff --git a/web/app/components/app/app-access-control/access-control.spec.tsx b/web/app/components/app/app-access-control/access-control.spec.tsx
index b73ed5c266e..3950bdf7ee7 100644
--- a/web/app/components/app/app-access-control/access-control.spec.tsx
+++ b/web/app/components/app/app-access-control/access-control.spec.tsx
@@ -3,9 +3,7 @@ import type { App } from '@/types/app'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import useAccessControlStore from '@/context/access-control-store'
-import { useGlobalPublicStore } from '@/context/global-public-context'
import { AccessMode, SubjectType } from '@/models/access-control'
-import { defaultSystemFeatures } from '@/types/feature'
import Toast from '../../base/toast'
import AccessControlDialog from './access-control-dialog'
import AccessControlItem from './access-control-item'
@@ -105,22 +103,6 @@ const memberSubject: Subject = {
accountData: baseMember,
} as Subject
-const resetAccessControlStore = () => {
- useAccessControlStore.setState({
- appId: '',
- specificGroups: [],
- specificMembers: [],
- currentMenu: AccessMode.SPECIFIC_GROUPS_MEMBERS,
- selectedGroupsForBreadcrumb: [],
- })
-}
-
-const resetGlobalStore = () => {
- useGlobalPublicStore.setState({
- systemFeatures: defaultSystemFeatures,
- })
-}
-
beforeAll(() => {
class MockIntersectionObserver {
observe = vi.fn(() => undefined)
@@ -132,9 +114,6 @@ beforeAll(() => {
})
beforeEach(() => {
- vi.clearAllMocks()
- resetAccessControlStore()
- resetGlobalStore()
mockMutateAsync.mockResolvedValue(undefined)
mockUseUpdateAccessMode.mockReturnValue({
isPending: false,
diff --git a/web/app/components/plugins/plugin-page/filter-management/index.spec.tsx b/web/app/components/plugins/plugin-page/filter-management/index.spec.tsx
index 58474b47235..b942a360b01 100644
--- a/web/app/components/plugins/plugin-page/filter-management/index.spec.tsx
+++ b/web/app/components/plugins/plugin-page/filter-management/index.spec.tsx
@@ -144,17 +144,6 @@ describe('constant.ts - Type Definitions', () => {
// ==================== store.ts Tests ====================
describe('store.ts - Zustand Store', () => {
- beforeEach(() => {
- // Reset store to initial state
- const { setState } = useStore
- setState({
- tagList: [],
- categoryList: [],
- showTagManagementModal: false,
- showCategoryManagementModal: false,
- })
- })
-
describe('Initial State', () => {
it('should have empty tagList initially', () => {
const { result } = renderHook(() => useStore(state => state.tagList))
diff --git a/web/app/components/plugins/readme-panel/index.spec.tsx b/web/app/components/plugins/readme-panel/index.spec.tsx
index 8d795eac109..5de18c2ed97 100644
--- a/web/app/components/plugins/readme-panel/index.spec.tsx
+++ b/web/app/components/plugins/readme-panel/index.spec.tsx
@@ -134,13 +134,6 @@ describe('BUILTIN_TOOLS_ARRAY', () => {
// Store Tests
// ================================
describe('useReadmePanelStore', () => {
- beforeEach(() => {
- vi.clearAllMocks()
- // Reset store state before each test
- const { setCurrentPluginDetail } = useReadmePanelStore.getState()
- setCurrentPluginDetail()
- })
-
describe('Initial State', () => {
it('should have undefined currentPluginDetail initially', () => {
const { currentPluginDetail } = useReadmePanelStore.getState()
@@ -228,12 +221,6 @@ describe('useReadmePanelStore', () => {
// ReadmeEntrance Component Tests
// ================================
describe('ReadmeEntrance', () => {
- beforeEach(() => {
- vi.clearAllMocks()
- // Reset store state
- const { setCurrentPluginDetail } = useReadmePanelStore.getState()
- setCurrentPluginDetail()
- })
// ================================
// Rendering Tests
@@ -417,11 +404,6 @@ describe('ReadmeEntrance', () => {
// ================================
describe('ReadmePanel', () => {
beforeEach(() => {
- vi.clearAllMocks()
- // Reset store state
- const { setCurrentPluginDetail } = useReadmePanelStore.getState()
- setCurrentPluginDetail()
- // Reset mock
mockUsePluginReadme.mockReturnValue({
data: null,
isLoading: false,
diff --git a/web/tsconfig.json b/web/tsconfig.json
index 78c5930aa24..c7aa998644b 100644
--- a/web/tsconfig.json
+++ b/web/tsconfig.json
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"incremental": true,
- "target": "es2015",
+ "target": "es2022",
"jsx": "preserve",
"lib": [
"dom",
diff --git a/web/vitest.setup.ts b/web/vitest.setup.ts
index 26dc25bbcf7..597ded9559b 100644
--- a/web/vitest.setup.ts
+++ b/web/vitest.setup.ts
@@ -85,6 +85,10 @@ afterEach(() => {
// mock next/image to avoid width/height requirements for data URLs
vi.mock('next/image')
+// mock zustand - auto-resets all stores after each test
+// Based on official Zustand testing guide: https://zustand.docs.pmnd.rs/guides/testing
+vi.mock('zustand')
+
// mock react-i18next
vi.mock('react-i18next', async () => {
const actual = await vi.importActual<typeof import('react-i18next')>('react-i18next')