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

@@ -4,20 +4,24 @@ import { act, render, renderHook, waitFor } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { PROGRESS_ERROR, PROGRESS_NOT_STARTED } from '../../constants'
// Mock notify function - defined before mocks
const mockNotify = vi.fn()
const mockClose = vi.fn()
// Mock ToastContext with factory function
vi.mock('@/app/components/base/toast/context', async () => {
const { createContext, useContext } = await import('use-context-selector')
const context = createContext({ notify: mockNotify, close: mockClose })
return {
ToastContext: context,
useToastContext: () => useContext(context),
}
const { mockNotify, mockToast } = vi.hoisted(() => {
const mockNotify = vi.fn()
const mockToast = Object.assign(mockNotify, {
success: vi.fn((message, options) => mockNotify({ type: 'success', message, ...options })),
error: vi.fn((message, options) => mockNotify({ type: 'error', message, ...options })),
warning: vi.fn((message, options) => mockNotify({ type: 'warning', message, ...options })),
info: vi.fn((message, options) => mockNotify({ type: 'info', message, ...options })),
dismiss: vi.fn(),
update: vi.fn(),
promise: vi.fn(),
})
return { mockNotify, mockToast }
})
vi.mock('@/app/components/base/ui/toast', () => ({
toast: mockToast,
}))
// Mock file uploader utils
vi.mock('@/app/components/base/file-uploader/utils', () => ({
getFileUploadErrorMessage: (e: Error, defaultMsg: string) => e.message || defaultMsg,
@@ -87,13 +91,12 @@ vi.mock('@/service/base', () => ({
// Import after all mocks are set up
const { useLocalFileUpload } = await import('../use-local-file-upload')
const { ToastContext } = await import('@/app/components/base/toast/context')
const createWrapper = () => {
return ({ children }: { children: ReactNode }) => (
<ToastContext.Provider value={{ notify: mockNotify, close: mockClose }}>
<>
{children}
</ToastContext.Provider>
</>
)
}
@@ -555,9 +558,9 @@ describe('useLocalFileUpload', () => {
it('should set dragging true on dragenter', async () => {
const { getByTestId } = await act(async () =>
render(
<ToastContext.Provider value={{ notify: mockNotify, close: mockClose }}>
<>
<TestDropzone allowedExtensions={['pdf']} />
</ToastContext.Provider>,
</>,
),
)
@@ -574,9 +577,9 @@ describe('useLocalFileUpload', () => {
it('should handle dragover event', async () => {
const { getByTestId } = await act(async () =>
render(
<ToastContext.Provider value={{ notify: mockNotify, close: mockClose }}>
<>
<TestDropzone allowedExtensions={['pdf']} />
</ToastContext.Provider>,
</>,
),
)
@@ -594,9 +597,9 @@ describe('useLocalFileUpload', () => {
it('should set dragging false on dragleave from drag overlay', async () => {
const { getByTestId, queryByTestId } = await act(async () =>
render(
<ToastContext.Provider value={{ notify: mockNotify, close: mockClose }}>
<>
<TestDropzone allowedExtensions={['pdf']} />
</ToastContext.Provider>,
</>,
),
)
@@ -626,9 +629,9 @@ describe('useLocalFileUpload', () => {
const { getByTestId } = await act(async () =>
render(
<ToastContext.Provider value={{ notify: mockNotify, close: mockClose }}>
<>
<TestDropzone allowedExtensions={['pdf']} />
</ToastContext.Provider>,
</>,
),
)
@@ -658,9 +661,9 @@ describe('useLocalFileUpload', () => {
it('should handle drop without dataTransfer', async () => {
const { getByTestId } = await act(async () =>
render(
<ToastContext.Provider value={{ notify: mockNotify, close: mockClose }}>
<>
<TestDropzone allowedExtensions={['pdf']} />
</ToastContext.Provider>,
</>,
),
)
@@ -682,9 +685,9 @@ describe('useLocalFileUpload', () => {
const { getByTestId } = await act(async () =>
render(
<ToastContext.Provider value={{ notify: mockNotify, close: mockClose }}>
<>
<TestDropzone allowedExtensions={['pdf']} supportBatchUpload={false} />
</ToastContext.Provider>,
</>,
),
)