Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(theme): add tests for alert component #549

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/docs/pages/ThemePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Flowbite } from '../../lib/components';
import type { CustomFlowbiteTheme } from '../../lib/components/Flowbite/FlowbiteTheme';

const ThemePage: FC = () => {
const theme: CustomFlowbiteTheme = { alert: { root: { color: { primary: 'bg-primary' } } } };
const theme: CustomFlowbiteTheme = { alert: { root: { color: { info: 'bg-primary' } } } };

return (
<div className="flex flex-col max-w-4xl gap-8 mx-auto dark:text-white">
Expand Down
129 changes: 126 additions & 3 deletions src/lib/components/Alert/Alert.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { FC, useState } from 'react';
import { HiEye, HiInformationCircle } from 'react-icons/hi';
import { HiEye, HiHeart, HiInformationCircle } from 'react-icons/hi';
import { describe, expect, it, vi } from 'vitest';
import { Alert } from './Alert';
import { Flowbite } from '../Flowbite';

import { Alert, AlertProps } from './Alert';

describe.concurrent('Components / Alert', () => {
describe.concurrent('A11y', () => {
Expand All @@ -12,6 +14,122 @@ describe.concurrent('Components / Alert', () => {

expect(alert()).toBeInTheDocument();
});

describe('Theme', () => {
it('should use custom `base` classes', () => {
const theme = {
alert: {
root: {
base: 'text-purple-100',
},
},
};
render(
<Flowbite theme={{ theme }}>
<TestAlert />
</Flowbite>,
);

expect(alert()).toHaveClass('text-purple-100');
});

it('should use custom `borderAccent` classes', () => {
const theme = {
alert: {
root: {
borderAccent: 'border-t-4 border-purple-500',
},
},
};
render(
<Flowbite theme={{ theme }}>
<TestAlert withBorderAccent />
</Flowbite>,
);

expect(alert()).toHaveClass('border-t-4 border-purple-500');
});

it('should use custom `wrapper` classes', () => {
const theme = {
alert: {
root: {
wrapper: 'flex items-center',
},
},
};
render(
<Flowbite theme={{ theme }}>
<TestAlert />
</Flowbite>,
);

expect(wrapper()).toHaveClass('flex items-center');
});

it('should use custom `color` classes', () => {
const theme = {
alert: {
root: {
color: {
info: 'text-purple-700 bg-purple-100 border-purple-500 dark:bg-purple-200 dark:text-purple-800',
},
},
closeButton: {
color: {
info: 'text-purple-500 hover:bg-purple-200 dark:text-purple-600 dark:hover:text-purple-300',
},
},
},
};
render(
<Flowbite theme={{ theme }}>
<TestAlert />
</Flowbite>,
);

expect(alert()).toHaveClass(
'text-purple-700 bg-purple-100 border-purple-500 dark:bg-purple-200 dark:text-purple-800',
);
expect(dismiss()).toHaveClass(
'text-purple-500 hover:bg-purple-200 dark:text-purple-600 dark:hover:text-purple-300',
);
});

it('should use custom `icon`', () => {
const theme = {
alert: {
root: {
icon: 'alert-custom-icon',
},
},
};
render(
<Flowbite theme={{ theme }}>
<TestAlert icon={HiHeart} />
</Flowbite>,
);

expect(icon()).toHaveClass('alert-custom-icon');
});

it('should show custom `rounded` class', () => {
const theme = {
alert: {
root: {
rounded: 'rounded',
},
},
};
render(
<Flowbite theme={{ theme }}>
<TestAlert />
</Flowbite>,
);

expect(alert()).toHaveClass('rounded');
});
});
});

describe.concurrent('Keyboard interactions', () => {
Expand Down Expand Up @@ -45,11 +163,12 @@ describe.concurrent('Components / Alert', () => {
});
});

const TestAlert: FC = () => {
const TestAlert: FC<AlertProps> = (props: AlertProps) => {
const [isDismissed, setDismissed] = useState(false);

return (
<Alert
{...props}
additionalContent={
<>
<div className="mt-2 mb-4 text-sm text-blue-700 dark:text-blue-800">
Expand Down Expand Up @@ -86,4 +205,8 @@ const TestAlert: FC = () => {

const alert = () => screen.getByRole('alert');

const wrapper = () => screen.getByTestId('flowbite-alert-wrapper');

const icon = () => screen.getByTestId('flowbite-alert-icon');

const dismiss = () => screen.getByLabelText('Dismiss');
4 changes: 2 additions & 2 deletions src/lib/components/Alert/Alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export const Alert: FC<AlertProps> = ({
)}
role="alert"
>
<div className={theme.root.wrapper}>
{Icon && <Icon className={theme.root.icon} />}
<div className={theme.root.wrapper} data-testid="flowbite-alert-wrapper">
{Icon && <Icon className={theme.root.icon} data-testid="flowbite-alert-icon" />}
<div>{children}</div>
{typeof onDismiss === 'function' && (
<button
Expand Down