Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

chore: refactor Alert component tests to TS, fix old asserions

Brandon Scott95f97b315b78
diff --git a/packages/ui/src/components/Alert/Alert.test.js b/packages/ui/src/components/Alert/Alert.test.js
deleted file mode 100644
index e6fdee11169..00000000000
--- a/packages/ui/src/components/Alert/Alert.test.js
+++ /dev/null
@@ -1,67 +0,0 @@
-import { fireEvent, render, screen } from '@testing-library/react'
-import Alert from './Alert'
-
-const VARIANTS = ['success', 'danger', 'warning', 'info']
-
-describe('#Alert', () => {
- it('should render elements correctly', () => {
- render(<Alert title="Required Title">{'Description'}</Alert>)
- expect(screen.queryByText('Required Title')).toBeInTheDocument()
- expect(screen.queryByText('Description')).toBeInTheDocument()
- })
-
- it('should add custom classes to container div', () => {
- const { container } = render(
- <Alert title="Required Title" className={'custom classes'}>
- {'Description'}
- </Alert>
- )
- expect(container.querySelector('div')).toHaveClass(
- `sbui-alert-container sbui-alert-container--success custom classes`
- )
- })
-
- it('should close alert when close button clicked', () => {
- render(
- <Alert title="Required Title" closable>
- {'Description'}
- </Alert>
- )
-
- const closeButton = screen.getByRole('button')
-
- fireEvent.click(closeButton)
-
- expect(screen.queryByText('Description')).not.toBeInTheDocument()
- })
-
- it('should render an icon when passed withIcon', () => {
- const { container } = render(
- <Alert title="Required Title" withIcon>
- {'Description'}
- </Alert>
- )
-
- expect(
- container.querySelector('div.flex-shrink-0 > svg')
- ).toBeInTheDocument()
- })
-
- it.each(VARIANTS)(
- 'should have "sbui-alert-[container|description]--%s" class',
- (variant) => {
- const { container } = render(
- <Alert title="Required Title" variant={variant}>
- {'Description'}
- </Alert>
- )
-
- expect(container.querySelector('div')).toHaveClass(
- `sbui-alert-container sbui-alert-container--${variant}`
- )
- expect(screen.queryByText('Description')).toHaveClass(
- `sbui-alert-description sbui-alert-description--${variant}`
- )
- }
- )
-})
diff --git a/packages/ui/src/components/Alert/Alert.test.tsx b/packages/ui/src/components/Alert/Alert.test.tsx
new file mode 100644
index 00000000000..299bcc91bcd
--- /dev/null
+++ b/packages/ui/src/components/Alert/Alert.test.tsx
@@ -0,0 +1,90 @@
+import { fireEvent, render, screen } from '@testing-library/react'
+import Alert, { AlertVariant } from './Alert'
+import defaultTheme from '../../lib/theme/defaultTheme'
+
+const VARIANTS: AlertVariant[] = ['success', 'danger', 'warning', 'info', 'neutral']
+
+describe('#Alert', () => {
+ it('should render title and description', () => {
+ render(<Alert title="Required Title">Description</Alert>)
+
+ expect(screen.queryByText('Required Title')).toBeInTheDocument()
+ expect(screen.queryByText('Description')).toBeInTheDocument()
+ })
+
+ it('should add custom classes to container div', () => {
+ const className = 'custom classes'
+
+ const { container } = render(
+ <Alert title="Required Title" className={className}>
+ Description
+ </Alert>
+ )
+ const alert = container.querySelector('div')
+
+ expect(alert?.className).toStrictEqual(expect.stringContaining(className))
+ })
+
+ it('should close alert when close button clicked', () => {
+ render(
+ <Alert title="Required Title" closable>
+ Description
+ </Alert>
+ )
+
+ const closeButton = screen.getByRole('button')
+ fireEvent.click(closeButton)
+
+ expect(screen.queryByText('Description')).not.toBeInTheDocument()
+ })
+
+ describe('when withIcon is true', () => {
+ describe("when variant is not 'neutral'", () => {
+ it('should render an icon', () => {
+ const { container } = render(
+ <Alert title="Required Title" withIcon={true} variant="danger">
+ Description
+ </Alert>
+ )
+
+ expect(container.querySelector('svg')).toBeInTheDocument()
+ })
+ })
+
+ describe("when variant is 'neutral'", () => {
+ it('should not render an icon', () => {
+ const { container } = render(
+ <Alert title="Required Title" withIcon={true} variant="neutral">
+ Description
+ </Alert>
+ )
+
+ expect(container.querySelector('svg')).toBeNull()
+ })
+ })
+ })
+
+ describe('when withIcon is false', () => {
+ it('should not render an icon', () => {
+ const { container } = render(
+ <Alert title="Required Title" withIcon={false} variant="danger">
+ Description
+ </Alert>
+ )
+
+ expect(container.querySelector('svg')).toBeNull()
+ })
+ })
+
+ it.each(VARIANTS)('should have class %p from theme', (variant) => {
+ const expected = defaultTheme.alert.variant[variant].base
+ const { container } = render(
+ <Alert title="Required Title" variant={variant}>
+ Description
+ </Alert>
+ )
+ const alert = container.querySelector('div')
+
+ expect(alert?.className).toStrictEqual(expect.stringContaining(expected))
+ })
+})
diff --git a/packages/ui/src/components/Alert/Alert.tsx b/packages/ui/src/components/Alert/Alert.tsx
index cf0b96fe810..3df3c6a8a96 100644
--- a/packages/ui/src/components/Alert/Alert.tsx
+++ b/packages/ui/src/components/Alert/Alert.tsx
@@ -9,7 +9,7 @@ import { IconAlertOctagon } from '../Icon/icons/IconAlertOctagon'
import { IconCheckCircle } from '../Icon/icons/IconCheckCircle'
export interface AlertProps {
- variant?: 'success' | 'danger' | 'warning' | 'info' | 'neutral'
+ variant?: AlertVariant
className?: string
title: string | React.ReactNode
withIcon?: boolean
@@ -19,7 +19,9 @@ export interface AlertProps {
actions?: React.ReactNode
}
-const icons: Record<'success' | 'danger' | 'warning' | 'info' | 'neutral', React.ReactElement> = {
+export type AlertVariant = 'success' | 'danger' | 'warning' | 'info' | 'neutral'
+
+const icons: Record<AlertVariant, React.ReactElement> = {
danger: <IconAlertOctagon strokeWidth={1.5} size={18} />,
success: <IconCheckCircle strokeWidth={1.5} size={18} />,
warning: <IconAlertTriangle strokeWidth={1.5} size={18} />,