-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e4bf5d
commit 8dbf389
Showing
3 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,119 @@ | ||
import { act, render, screen } from '@testing-library/react'; | ||
import { beforeEach, expect, it, vitest } from 'vitest'; | ||
import toast, { Toaster } from '../src'; | ||
import { act, fireEvent, render, screen } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { afterEach, beforeEach, expect, it, vi } from 'vitest'; | ||
import toast, { Toaster } from '../src'; | ||
|
||
beforeEach(() => { | ||
toast.remove(); | ||
vitest.useFakeTimers(); | ||
vi.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
act(() => { | ||
vi.runAllTimers(); | ||
vi.useRealTimers(); | ||
}); | ||
}); | ||
|
||
it('renders success toast correctly', () => { | ||
render(<Toaster />); | ||
|
||
act(() => { | ||
toast.success({ title: 'Success!' }); | ||
}); | ||
|
||
expect(screen.getByText(/success/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders error toast correctly', () => { | ||
render(<Toaster />); | ||
|
||
act(() => { | ||
toast.error({ title: 'Error!' }); | ||
}); | ||
|
||
expect(screen.getByText(/error/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders warning toast correctly', () => { | ||
render(<Toaster />); | ||
|
||
act(() => { | ||
toast.warning({ title: 'Warning!' }); | ||
}); | ||
|
||
expect(screen.getByText(/warning/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders loading toast correctly', () => { | ||
render(<Toaster />); | ||
|
||
act(() => { | ||
toast.loading({ title: 'Loading' }); | ||
}); | ||
|
||
expect(screen.getByText(/loading/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders different types of toast correctly', () => { | ||
it('renders stacked toasts correctly', () => { | ||
render(<Toaster />); | ||
|
||
act(() => { | ||
toast.success({ title: 'Success!' }); | ||
toast.error({ title: 'Error!' }); | ||
toast.warning({ title: 'Warning!' }); | ||
}); | ||
|
||
expect(screen.getByText(/success/i)).toBeInTheDocument(); | ||
expect(screen.getByText(/error/i)).toBeInTheDocument(); | ||
expect(screen.getByText(/warning/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders only 3 toasts stacking correctly', () => { | ||
render(<Toaster />); | ||
|
||
act(() => { | ||
toast.success({ title: 'Success!' }); | ||
toast.error({ title: 'Error!' }); | ||
toast.warning({ title: 'Warning!' }); | ||
toast.loading({ title: 'Loading' }); | ||
}); | ||
|
||
expect(screen.getByText(/loading/i)).toBeInTheDocument(); | ||
expect(screen.getByText(/error/i)).toBeInTheDocument(); | ||
expect(screen.getByText(/warning/i)).toBeInTheDocument(); | ||
expect(screen.queryByText(/success/i)).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('renders toasts with custom icon correctly', () => { | ||
render(<Toaster />); | ||
|
||
act(() => { | ||
toast({ title: 'Success!', icon: <div>👍</div> }); | ||
}); | ||
|
||
expect(screen.getByText(/👍/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it('pause toasts correctly', () => { | ||
render(<Toaster />); | ||
|
||
act(() => { | ||
toast({ title: 'hover' }); | ||
}); | ||
|
||
const toastElement = screen.getByText(/hover/i); | ||
expect(toastElement).toBeInTheDocument(); | ||
|
||
fireEvent.mouseEnter(toastElement); | ||
act(() => { | ||
vi.advanceTimersByTime(60 * 1000); | ||
}); | ||
expect(toastElement).toBeInTheDocument(); | ||
|
||
fireEvent.mouseLeave(toastElement); | ||
act(() => { | ||
vi.advanceTimersByTime(5 * 1000); | ||
}); | ||
expect(toastElement).not.toBeInTheDocument(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters