Skip to content

Commit a62e84f

Browse files
emilchevarluders
andauthored
feat(theme): add tests for alert component (#549)
* feat(theme): add tests for alert component * feat(theme): fix prettier formatting Co-authored-by: Ricardo Lüders <[email protected]>
1 parent 5f01946 commit a62e84f

File tree

3 files changed

+129
-6
lines changed

3 files changed

+129
-6
lines changed

src/docs/pages/ThemePage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Flowbite } from '../../lib/components';
88
import type { CustomFlowbiteTheme } from '../../lib/components/Flowbite/FlowbiteTheme';
99

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

1313
return (
1414
<div className="flex flex-col max-w-4xl gap-8 mx-auto dark:text-white">

src/lib/components/Alert/Alert.spec.tsx

Lines changed: 126 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { render, screen, waitFor } from '@testing-library/react';
22
import userEvent from '@testing-library/user-event';
33
import { FC, useState } from 'react';
4-
import { HiEye, HiInformationCircle } from 'react-icons/hi';
4+
import { HiEye, HiHeart, HiInformationCircle } from 'react-icons/hi';
55
import { describe, expect, it, vi } from 'vitest';
6-
import { Alert } from './Alert';
6+
import { Flowbite } from '../Flowbite';
7+
8+
import { Alert, AlertProps } from './Alert';
79

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

1315
expect(alert()).toBeInTheDocument();
1416
});
17+
18+
describe('Theme', () => {
19+
it('should use custom `base` classes', () => {
20+
const theme = {
21+
alert: {
22+
root: {
23+
base: 'text-purple-100',
24+
},
25+
},
26+
};
27+
render(
28+
<Flowbite theme={{ theme }}>
29+
<TestAlert />
30+
</Flowbite>,
31+
);
32+
33+
expect(alert()).toHaveClass('text-purple-100');
34+
});
35+
36+
it('should use custom `borderAccent` classes', () => {
37+
const theme = {
38+
alert: {
39+
root: {
40+
borderAccent: 'border-t-4 border-purple-500',
41+
},
42+
},
43+
};
44+
render(
45+
<Flowbite theme={{ theme }}>
46+
<TestAlert withBorderAccent />
47+
</Flowbite>,
48+
);
49+
50+
expect(alert()).toHaveClass('border-t-4 border-purple-500');
51+
});
52+
53+
it('should use custom `wrapper` classes', () => {
54+
const theme = {
55+
alert: {
56+
root: {
57+
wrapper: 'flex items-center',
58+
},
59+
},
60+
};
61+
render(
62+
<Flowbite theme={{ theme }}>
63+
<TestAlert />
64+
</Flowbite>,
65+
);
66+
67+
expect(wrapper()).toHaveClass('flex items-center');
68+
});
69+
70+
it('should use custom `color` classes', () => {
71+
const theme = {
72+
alert: {
73+
root: {
74+
color: {
75+
info: 'text-purple-700 bg-purple-100 border-purple-500 dark:bg-purple-200 dark:text-purple-800',
76+
},
77+
},
78+
closeButton: {
79+
color: {
80+
info: 'text-purple-500 hover:bg-purple-200 dark:text-purple-600 dark:hover:text-purple-300',
81+
},
82+
},
83+
},
84+
};
85+
render(
86+
<Flowbite theme={{ theme }}>
87+
<TestAlert />
88+
</Flowbite>,
89+
);
90+
91+
expect(alert()).toHaveClass(
92+
'text-purple-700 bg-purple-100 border-purple-500 dark:bg-purple-200 dark:text-purple-800',
93+
);
94+
expect(dismiss()).toHaveClass(
95+
'text-purple-500 hover:bg-purple-200 dark:text-purple-600 dark:hover:text-purple-300',
96+
);
97+
});
98+
99+
it('should use custom `icon`', () => {
100+
const theme = {
101+
alert: {
102+
root: {
103+
icon: 'alert-custom-icon',
104+
},
105+
},
106+
};
107+
render(
108+
<Flowbite theme={{ theme }}>
109+
<TestAlert icon={HiHeart} />
110+
</Flowbite>,
111+
);
112+
113+
expect(icon()).toHaveClass('alert-custom-icon');
114+
});
115+
116+
it('should show custom `rounded` class', () => {
117+
const theme = {
118+
alert: {
119+
root: {
120+
rounded: 'rounded',
121+
},
122+
},
123+
};
124+
render(
125+
<Flowbite theme={{ theme }}>
126+
<TestAlert />
127+
</Flowbite>,
128+
);
129+
130+
expect(alert()).toHaveClass('rounded');
131+
});
132+
});
15133
});
16134

17135
describe.concurrent('Keyboard interactions', () => {
@@ -45,11 +163,12 @@ describe.concurrent('Components / Alert', () => {
45163
});
46164
});
47165

48-
const TestAlert: FC = () => {
166+
const TestAlert: FC<AlertProps> = (props: AlertProps) => {
49167
const [isDismissed, setDismissed] = useState(false);
50168

51169
return (
52170
<Alert
171+
{...props}
53172
additionalContent={
54173
<>
55174
<div className="mt-2 mb-4 text-sm text-blue-700 dark:text-blue-800">
@@ -86,4 +205,8 @@ const TestAlert: FC = () => {
86205

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

208+
const wrapper = () => screen.getByTestId('flowbite-alert-wrapper');
209+
210+
const icon = () => screen.getByTestId('flowbite-alert-icon');
211+
89212
const dismiss = () => screen.getByLabelText('Dismiss');

src/lib/components/Alert/Alert.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ export const Alert: FC<AlertProps> = ({
6464
)}
6565
role="alert"
6666
>
67-
<div className={theme.root.wrapper}>
68-
{Icon && <Icon className={theme.root.icon} />}
67+
<div className={theme.root.wrapper} data-testid="flowbite-alert-wrapper">
68+
{Icon && <Icon className={theme.root.icon} data-testid="flowbite-alert-icon" />}
6969
<div>{children}</div>
7070
{typeof onDismiss === 'function' && (
7171
<button

0 commit comments

Comments
 (0)