-
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.
Merge pull request #17 from nhanluongoe/test/setup-test
Test/setup test
- Loading branch information
Showing
9 changed files
with
160 additions
and
25 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,119 @@ | ||
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(); | ||
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 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 was deleted.
Oops, something went wrong.
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,15 +1,18 @@ | ||
{ | ||
"compilerOptions": { | ||
"esModuleInterop": true, | ||
"jsx": "react-jsx", | ||
"jsx": "react", | ||
"lib": ["dom", "esnext"], | ||
"declaration": true, | ||
"sourceMap": true, | ||
"module": "ESNext", | ||
"moduleResolution": "node", | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"noEmit": true | ||
"noEmit": true, | ||
"types": ["@testing-library/jest-dom"], | ||
"rootDir": "./src" | ||
}, | ||
"exclude": ["./site"] | ||
"include": ["src", "types", "test"], | ||
"exclude": ["./site", "**/*.stories.tsx"] | ||
} |
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