Skip to content

Commit 0922fa3

Browse files
authored
feat: Add Success Variant Toast (#3639)
1 parent 1eaa0d6 commit 0922fa3

File tree

10 files changed

+79
-13
lines changed

10 files changed

+79
-13
lines changed

src/services/toast/ErrorToast/ErrorToast.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { render, screen } from '@testing-library/react'
22

3-
import ErrorToast from './ErrorToast'
3+
import { ErrorToast } from './ErrorToast'
44

55
describe('ErrorToast', () => {
66
it('renders the title', () => {
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import type { ToastProps } from '../renderToast'
22

3-
const ErrorToast: React.FC<ToastProps> = ({ title, content }) => (
3+
export const ErrorToast: React.FC<ToastProps> = ({ title, content }) => (
44
<div className="min-w-[300px] bg-ds-gray-secondary p-4">
55
<h3 className="text-base font-semibold">&#9940; {title}</h3>
66
<p className="whitespace-pre-line text-sm">{content}</p>
77
</div>
88
)
9-
10-
export default ErrorToast

src/services/toast/ErrorToast/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/services/toast/GenericToast/GenericToast.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { render, screen } from '@testing-library/react'
22

3-
import GenericToast from './GenericToast'
3+
import { GenericToast } from './GenericToast'
44

55
describe('GenericToast', () => {
66
it('renders the title', () => {
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import type { ToastProps } from '../renderToast'
22

3-
const GenericToast: React.FC<ToastProps> = ({ title, content }) => (
3+
export const GenericToast: React.FC<ToastProps> = ({ title, content }) => (
44
<div className="min-w-[300px] bg-ds-gray-secondary p-4">
55
<h3 className="text-base font-semibold">&#127881; {title}</h3>
66
<p className="whitespace-pre-line text-sm">{content}</p>
77
</div>
88
)
9-
10-
export default GenericToast

src/services/toast/GenericToast/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { render, screen } from '@testing-library/react'
2+
3+
import { SuccessToast } from './SuccessToast'
4+
5+
describe('SuccessToast', () => {
6+
it('renders the title', () => {
7+
render(<SuccessToast title="Success Title" content="Success Content" />)
8+
9+
const title = screen.getByRole('heading', { name: /Success Title/ })
10+
expect(title).toBeInTheDocument()
11+
expect(title).toHaveClass('font-semibold')
12+
})
13+
14+
it('renders the content', () => {
15+
render(<SuccessToast title="Success Title" content="Success Content" />)
16+
17+
const content = screen.getByText(/Success Content/)
18+
expect(content).toBeInTheDocument()
19+
})
20+
})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { ToastProps } from '../renderToast'
2+
3+
export const SuccessToast: React.FC<ToastProps> = ({ title, content }) => (
4+
<div className="min-w-[300px] bg-ds-gray-secondary p-4">
5+
<h3 className="text-base font-semibold">&#x2705; {title}</h3>
6+
<p className="whitespace-pre-line text-sm">{content}</p>
7+
</div>
8+
)

src/services/toast/renderToast.test.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,44 @@ describe('renderToast', () => {
152152
})
153153
})
154154
})
155+
156+
describe('triggering success toast', () => {
157+
describe('with options and type passed', () => {
158+
it('renders toast', async () => {
159+
const { user } = setup()
160+
161+
render(<TestComponent type="success" options={{ duration: 5000 }} />)
162+
163+
const button = screen.getByRole('button', { name: 'click me' })
164+
expect(button).toBeInTheDocument()
165+
await user.click(button)
166+
167+
const title = screen.getByRole('heading', { name: /Cool title/ })
168+
expect(title).toBeInTheDocument()
169+
170+
const clearToasts = screen.getByRole('button', { name: 'clear toasts' })
171+
expect(clearToasts).toBeInTheDocument()
172+
await user.click(clearToasts)
173+
})
174+
})
175+
176+
describe('no options are passed', () => {
177+
it('renders toast', async () => {
178+
const { user } = setup()
179+
180+
render(<TestComponent type="success" />)
181+
182+
const button = screen.getByRole('button', { name: 'click me' })
183+
expect(button).toBeInTheDocument()
184+
await user.click(button)
185+
186+
const title = screen.getByRole('heading', { name: /Cool title/ })
187+
expect(title).toBeInTheDocument()
188+
189+
const clearToasts = screen.getByRole('button', { name: 'clear toasts' })
190+
expect(clearToasts).toBeInTheDocument()
191+
await user.click(clearToasts)
192+
})
193+
})
194+
})
155195
})

src/services/toast/renderToast.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { toast, type ToastOptions } from 'react-hot-toast'
22

3-
import ErrorToast from './ErrorToast'
4-
import GenericToast from './GenericToast'
3+
import { ErrorToast } from './ErrorToast/ErrorToast'
4+
import { GenericToast } from './GenericToast/GenericToast'
5+
import { SuccessToast } from './SuccessToast/SuccessToast'
56

67
const TOAST_DURATION = 4000
78

@@ -10,7 +11,7 @@ export interface ToastProps {
1011
content: string
1112
}
1213

13-
export type ToastTypes = 'generic' | 'error'
14+
export type ToastTypes = 'generic' | 'error' | 'success'
1415

1516
export interface ToastArgs {
1617
title: string
@@ -31,6 +32,9 @@ export const renderToast = ({
3132
case 'error':
3233
component = <ErrorToast title={title} content={content} />
3334
break
35+
case 'success':
36+
component = <SuccessToast title={title} content={content} />
37+
break
3438
default:
3539
component = <GenericToast title={title} content={content} />
3640
}

0 commit comments

Comments
 (0)