refactor(web): migrate remaining toast usage (#34433)

This commit is contained in:
yyh
2026-04-02 12:16:50 +08:00
committed by GitHub
parent 2d29345f26
commit f9d9ad7a38
273 changed files with 3491 additions and 6996 deletions

View File

@@ -17,16 +17,36 @@ vi.mock('@/next/navigation', () => ({
}),
}))
// Mock use-context-selector with stable mockNotify reference for tracking calls
const toastMocks = vi.hoisted(() => {
const record = vi.fn()
const api = vi.fn((message: unknown, options?: Record<string, unknown>) => record({ message, ...options }))
return {
record,
api: Object.assign(api, {
success: vi.fn((message: unknown, options?: Record<string, unknown>) => record({ type: 'success', message, ...options })),
error: vi.fn((message: unknown, options?: Record<string, unknown>) => record({ type: 'error', message, ...options })),
warning: vi.fn((message: unknown, options?: Record<string, unknown>) => record({ type: 'warning', message, ...options })),
info: vi.fn((message: unknown, options?: Record<string, unknown>) => record({ type: 'info', message, ...options })),
dismiss: vi.fn(),
update: vi.fn(),
promise: vi.fn(),
}),
}
})
vi.mock('@/app/components/base/ui/toast', () => ({
toast: toastMocks.api,
}))
// Mock use-context-selector with stable toast reference for tracking calls
// Include createContext for components that use it (like Toast)
const mockNotify = vi.fn()
vi.mock('use-context-selector', () => ({
createContext: <T,>(defaultValue: T) => React.createContext(defaultValue),
useContext: () => ({
notify: mockNotify,
notify: toastMocks.api,
}),
useContextSelector: (_context: unknown, selector: (state: Record<string, unknown>) => unknown) => selector({
notify: mockNotify,
notify: toastMocks.api,
}),
}))
@@ -591,7 +611,7 @@ describe('AppCard', () => {
await waitFor(() => {
expect(mockDeleteAppMutation).toHaveBeenCalled()
expect(mockNotify).toHaveBeenCalledWith({ type: 'error', message: expect.stringContaining('Delete failed') })
expect(toastMocks.record).toHaveBeenCalledWith({ type: 'error', message: expect.stringContaining('Delete failed') })
})
})
@@ -670,7 +690,7 @@ describe('AppCard', () => {
await waitFor(() => {
expect(appsService.copyApp).toHaveBeenCalled()
expect(mockNotify).toHaveBeenCalledWith({ type: 'error', message: 'app.newApp.appCreateFailed' })
expect(toastMocks.record).toHaveBeenCalledWith({ type: 'error', message: 'app.newApp.appCreateFailed' })
})
})
@@ -699,7 +719,7 @@ describe('AppCard', () => {
await waitFor(() => {
expect(appsService.exportAppConfig).toHaveBeenCalled()
expect(mockNotify).toHaveBeenCalledWith({ type: 'error', message: 'app.exportFailed' })
expect(toastMocks.record).toHaveBeenCalledWith({ type: 'error', message: 'app.exportFailed' })
})
})
})
@@ -945,7 +965,7 @@ describe('AppCard', () => {
await waitFor(() => {
expect(appsService.updateAppInfo).toHaveBeenCalled()
expect(mockNotify).toHaveBeenCalledWith({ type: 'error', message: expect.stringContaining('Edit failed') })
expect(toastMocks.record).toHaveBeenCalledWith({ type: 'error', message: expect.stringContaining('Edit failed') })
})
})
@@ -998,7 +1018,7 @@ describe('AppCard', () => {
await waitFor(() => {
expect(workflowService.fetchWorkflowDraft).toHaveBeenCalled()
expect(mockNotify).toHaveBeenCalledWith({ type: 'error', message: 'app.exportFailed' })
expect(toastMocks.record).toHaveBeenCalledWith({ type: 'error', message: 'app.exportFailed' })
})
})
})