dify/web/app/components/base/toast/__tests__/index.spec.tsx
Saumya Talwani f50e44b24a
test: improve coverage for some test files (#32916)
Signed-off-by: edvatar <88481784+toroleapinc@users.noreply.github.com>
Signed-off-by: -LAN- <laipz8200@outlook.com>
Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: majiayu000 <1835304752@qq.com>
Co-authored-by: Poojan <poojan@infocusp.com>
Co-authored-by: sahil-infocusp <73810410+sahil-infocusp@users.noreply.github.com>
Co-authored-by: 非法操作 <hjlarry@163.com>
Co-authored-by: Pandaaaa906 <ye.pandaaaa906@gmail.com>
Co-authored-by: Asuka Minato <i@asukaminato.eu.org>
Co-authored-by: heyszt <270985384@qq.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Ijas <ijas.ahmd.ap@gmail.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: 木之本澪 <kinomotomiovo@gmail.com>
Co-authored-by: KinomotoMio <200703522+KinomotoMio@users.noreply.github.com>
Co-authored-by: 不做了睡大觉 <64798754+stakeswky@users.noreply.github.com>
Co-authored-by: User <user@example.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: edvatar <88481784+toroleapinc@users.noreply.github.com>
Co-authored-by: -LAN- <laipz8200@outlook.com>
Co-authored-by: Leilei <138381132+Inlei@users.noreply.github.com>
Co-authored-by: HaKu <104669497+haku-ink@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: wangxiaolei <fatelei@gmail.com>
Co-authored-by: Varun Chawla <34209028+veeceey@users.noreply.github.com>
Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com>
Co-authored-by: yyh <yuanyouhuilyz@gmail.com>
Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com>
Co-authored-by: tda <95275462+tda1017@users.noreply.github.com>
Co-authored-by: root <root@DESKTOP-KQLO90N>
Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Co-authored-by: Niels Kaspers <153818647+nielskaspers@users.noreply.github.com>
Co-authored-by: hj24 <mambahj24@gmail.com>
Co-authored-by: Tyson Cung <45380903+tysoncung@users.noreply.github.com>
Co-authored-by: Stephen Zhou <hi@hyoban.cc>
Co-authored-by: FFXN <31929997+FFXN@users.noreply.github.com>
Co-authored-by: slegarraga <64795732+slegarraga@users.noreply.github.com>
Co-authored-by: 99 <wh2099@pm.me>
Co-authored-by: Br1an <932039080@qq.com>
Co-authored-by: L1nSn0w <l1nsn0w@qq.com>
Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai>
Co-authored-by: akkoaya <151345394+akkoaya@users.noreply.github.com>
Co-authored-by: 盐粒 Yanli <yanli@dify.ai>
Co-authored-by: lif <1835304752@qq.com>
Co-authored-by: weiguang li <codingpunk@gmail.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>
Co-authored-by: HanWenbo <124024253+hwb96@users.noreply.github.com>
Co-authored-by: Coding On Star <447357187@qq.com>
Co-authored-by: CodingOnStar <hanxujiang@dify.com>
Co-authored-by: Stable Genius <stablegenius043@gmail.com>
Co-authored-by: Stable Genius <259448942+stablegenius49@users.noreply.github.com>
Co-authored-by: ふるい <46769295+Echo0ff@users.noreply.github.com>
Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com>
2026-03-06 18:59:16 +08:00

349 lines
10 KiB
TypeScript

import type { ReactNode } from 'react'
import type { ToastHandle } from '../index'
import { act, render, screen, waitFor, within } from '@testing-library/react'
import { noop } from 'es-toolkit/function'
import * as React from 'react'
import Toast, { ToastProvider } from '..'
import { useToastContext } from '../context'
const TestComponent = () => {
const { notify, close } = useToastContext()
return (
<div>
<button type="button" onClick={() => notify({ message: 'Notification message', type: 'info' })}>
Show Toast
</button>
<button type="button" onClick={close}>Close Toast</button>
</div>
)
}
describe('Toast', () => {
const getToastElementByMessage = (message: string): HTMLElement => {
const messageElement = screen.getByText(message)
const toastElement = messageElement.closest('.fixed')
expect(toastElement).toBeInTheDocument()
return toastElement as HTMLElement
}
beforeEach(() => {
vi.useFakeTimers({ shouldAdvanceTime: true })
})
afterEach(() => {
vi.runOnlyPendingTimers()
vi.useRealTimers()
})
describe('Toast Component', () => {
it('renders toast with correct type and message', () => {
render(
<ToastProvider>
<Toast type="success" message="Success message" />
</ToastProvider>,
)
expect(screen.getByText('Success message')).toBeInTheDocument()
})
it('renders with different types', () => {
const { rerender } = render(
<ToastProvider>
<Toast type="success" message="Success message" />
</ToastProvider>,
)
const successToast = getToastElementByMessage('Success message')
const successIcon = within(successToast).getByTestId('toast-icon-success')
expect(successIcon).toHaveClass('text-text-success')
rerender(
<ToastProvider>
<Toast type="error" message="Error message" />
</ToastProvider>,
)
const errorToast = getToastElementByMessage('Error message')
const errorIcon = within(errorToast).getByTestId('toast-icon-error')
expect(errorIcon).toHaveClass('text-text-destructive')
})
it('renders with custom component', () => {
render(
<ToastProvider>
<Toast
message="Message with custom component"
customComponent={<span data-testid="custom-component">Custom</span>}
/>
</ToastProvider>,
)
expect(screen.getByTestId('custom-component')).toBeInTheDocument()
})
it('renders children content', () => {
render(
<ToastProvider>
<Toast message="Message with children">
<span>Additional information</span>
</Toast>
</ToastProvider>,
)
expect(screen.getByText('Additional information')).toBeInTheDocument()
})
it('does not render close button when close is undefined', () => {
// Create a modified context where close is undefined
const CustomToastContext = React.createContext({ notify: noop, close: undefined })
// Create a wrapper component using the custom context
const Wrapper = ({ children }: { children: ReactNode }) => (
<CustomToastContext.Provider value={{ notify: noop, close: undefined }}>
{children}
</CustomToastContext.Provider>
)
render(
<Wrapper>
<Toast message="No close button" type="info" />
</Wrapper>,
)
expect(screen.getByText('No close button')).toBeInTheDocument()
const toastElement = getToastElementByMessage('No close button')
expect(within(toastElement).queryByRole('button')).not.toBeInTheDocument()
})
it('returns null when message is not a string', () => {
const { container } = render(
<ToastProvider>
{/* @ts-expect-error - testing invalid input */}
<Toast message={<div>Invalid</div>} />
</ToastProvider>,
)
// Toast returns null, and provider adds no DOM elements
expect(container.firstChild).toBeNull()
})
it('renders with size sm', () => {
const { rerender } = render(
<ToastProvider>
<Toast type="info" message="Small size" size="sm" />
</ToastProvider>,
)
const infoToast = getToastElementByMessage('Small size')
const infoIcon = within(infoToast).getByTestId('toast-icon-info')
expect(infoIcon).toHaveClass('text-text-accent', 'h-4', 'w-4')
expect(infoIcon.parentElement).toHaveClass('p-1')
rerender(
<ToastProvider>
<Toast type="success" message="Small size" size="sm" />
</ToastProvider>,
)
const successToast = getToastElementByMessage('Small size')
const successIcon = within(successToast).getByTestId('toast-icon-success')
expect(successIcon).toHaveClass('text-text-success', 'h-4', 'w-4')
rerender(
<ToastProvider>
<Toast type="warning" message="Small size" size="sm" />
</ToastProvider>,
)
const warningToast = getToastElementByMessage('Small size')
const warningIcon = within(warningToast).getByTestId('toast-icon-warning')
expect(warningIcon).toHaveClass('text-text-warning-secondary', 'h-4', 'w-4')
rerender(
<ToastProvider>
<Toast type="error" message="Small size" size="sm" />
</ToastProvider>,
)
const errorToast = getToastElementByMessage('Small size')
const errorIcon = within(errorToast).getByTestId('toast-icon-error')
expect(errorIcon).toHaveClass('text-text-destructive', 'h-4', 'w-4')
})
})
describe('ToastProvider and Context', () => {
it('shows and hides toast using context', async () => {
render(
<ToastProvider>
<TestComponent />
</ToastProvider>,
)
// No toast initially
expect(screen.queryByText('Notification message')).not.toBeInTheDocument()
// Show toast
act(() => {
screen.getByText('Show Toast').click()
})
expect(screen.getByText('Notification message')).toBeInTheDocument()
// Close toast
act(() => {
screen.getByText('Close Toast').click()
})
expect(screen.queryByText('Notification message')).not.toBeInTheDocument()
})
it('automatically hides toast after duration', async () => {
render(
<ToastProvider>
<TestComponent />
</ToastProvider>,
)
// Show toast
act(() => {
screen.getByText('Show Toast').click()
})
expect(screen.getByText('Notification message')).toBeInTheDocument()
// Fast-forward timer
act(() => {
vi.advanceTimersByTime(3000) // Default for info type is 3000ms
})
// Toast should be gone
await waitFor(() => {
expect(screen.queryByText('Notification message')).not.toBeInTheDocument()
})
})
it('automatically hides toast after duration for error type in provider', async () => {
const TestComponentError = () => {
const { notify } = useToastContext()
return (
<button type="button" onClick={() => notify({ message: 'Error notify', type: 'error' })}>
Show Error
</button>
)
}
render(
<ToastProvider>
<TestComponentError />
</ToastProvider>,
)
act(() => {
screen.getByText('Show Error').click()
})
expect(screen.getByText('Error notify')).toBeInTheDocument()
// Error type uses 6000ms default
act(() => {
vi.advanceTimersByTime(6000)
})
await waitFor(() => {
expect(screen.queryByText('Error notify')).not.toBeInTheDocument()
})
})
})
describe('Toast.notify static method', () => {
it('creates and removes toast from DOM', async () => {
act(() => {
// Call the static method
Toast.notify({ message: 'Static notification', type: 'warning' })
})
// Toast should be in document
expect(screen.getByText('Static notification')).toBeInTheDocument()
// Fast-forward timer
act(() => {
vi.advanceTimersByTime(6000) // Default for warning type is 6000ms
})
// Toast should be removed
await waitFor(() => {
expect(screen.queryByText('Static notification')).not.toBeInTheDocument()
})
})
it('calls onClose callback after duration', async () => {
const onCloseMock = vi.fn()
act(() => {
Toast.notify({
message: 'Closing notification',
type: 'success',
onClose: onCloseMock,
})
})
// Fast-forward timer
act(() => {
vi.advanceTimersByTime(3000) // Default for success type is 3000ms
})
// onClose should be called
await waitFor(() => {
expect(onCloseMock).toHaveBeenCalled()
})
})
it('closes when close button is clicked in static toast', async () => {
const onCloseMock = vi.fn()
act(() => {
Toast.notify({ message: 'Static close test', type: 'info', onClose: onCloseMock })
})
expect(screen.getByText('Static close test')).toBeInTheDocument()
const toastElement = getToastElementByMessage('Static close test')
const closeButton = within(toastElement).getByRole('button')
act(() => {
closeButton.click()
})
expect(screen.queryByText('Static close test')).not.toBeInTheDocument()
expect(onCloseMock).toHaveBeenCalled()
})
it('does not auto close when duration is 0', async () => {
act(() => {
Toast.notify({ message: 'No auto close', type: 'info', duration: 0 })
})
expect(screen.getByText('No auto close')).toBeInTheDocument()
act(() => {
vi.advanceTimersByTime(10000)
})
expect(screen.getByText('No auto close')).toBeInTheDocument()
// manual clear to clean up
act(() => {
const toastElement = getToastElementByMessage('No auto close')
within(toastElement).getByRole('button').click()
})
})
it('returns a toast handler that can clear the toast', async () => {
let handler: ToastHandle = {}
const onCloseMock = vi.fn()
act(() => {
handler = Toast.notify({ message: 'Clearable toast', type: 'warning', onClose: onCloseMock })
})
expect(screen.getByText('Clearable toast')).toBeInTheDocument()
act(() => {
handler.clear?.()
})
expect(screen.queryByText('Clearable toast')).not.toBeInTheDocument()
expect(onCloseMock).toHaveBeenCalled()
})
})
})