Skip to content

Commit 95e02fb

Browse files
committed
test(flowbite): adds test cases for the flowbite theme provider and useTheme hook
#443
1 parent 1ac4e68 commit 95e02fb

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { act, render } from '@testing-library/react';
2+
import { afterEach, describe, expect, it } from 'vitest';
3+
import { mergeDeep } from '../../helpers/mergeDeep';
4+
import defaultTheme from '../../theme/default';
5+
import { Flowbite, useTheme } from '../Flowbite';
6+
import { ThemeContextProps } from './ThemeContext';
7+
8+
afterEach(() => {
9+
localStorage.removeItem('theme');
10+
});
11+
12+
describe('Components / Flowbite', () => {
13+
describe('hook / useTheme', () => {
14+
it('should return default values', () => {
15+
render(<TestComponent />);
16+
17+
const { theme, mode, toggleMode } = context;
18+
19+
expect(theme).toEqual(defaultTheme);
20+
expect(mode).toBeUndefined();
21+
expect(toggleMode).toBeUndefined();
22+
});
23+
});
24+
25+
describe('Flowbite', () => {
26+
it('should return default values', () => {
27+
render(
28+
<Flowbite>
29+
<TestComponent />
30+
</Flowbite>,
31+
);
32+
33+
const { theme, mode, toggleMode } = context;
34+
expect(theme).toEqual(defaultTheme);
35+
expect(mode).toBe('light');
36+
expect(toggleMode).not.toBeUndefined();
37+
});
38+
39+
it('should return custom theme', () => {
40+
render(
41+
<Flowbite theme={{ theme: customTheme }}>
42+
<TestComponent />
43+
</Flowbite>,
44+
);
45+
46+
const { theme } = context;
47+
const mergedTheme = mergeDeep(theme, customTheme);
48+
49+
expect(theme).toEqual(mergedTheme);
50+
});
51+
52+
it('should return darkmode', () => {
53+
render(
54+
<Flowbite theme={{ dark: true }}>
55+
<TestComponent />
56+
</Flowbite>,
57+
);
58+
59+
const { mode } = context;
60+
61+
expect(mode).toBe('dark');
62+
expect(documentEl()).toHaveClass('dark');
63+
});
64+
65+
it('should toggle mode', () => {
66+
render(
67+
<Flowbite>
68+
<TestComponent />
69+
</Flowbite>,
70+
);
71+
72+
const { mode, toggleMode } = context;
73+
74+
expect(mode).toBe('light');
75+
expect(documentEl()).not.toHaveClass('dark');
76+
77+
act(() => {
78+
if (toggleMode) toggleMode();
79+
});
80+
81+
const { mode: mode2 } = context;
82+
83+
expect(mode2).toBe('dark');
84+
expect(documentEl()).toHaveClass('dark');
85+
});
86+
});
87+
});
88+
89+
let context: ThemeContextProps;
90+
const TestComponent = () => {
91+
context = useTheme();
92+
return null;
93+
};
94+
95+
const customTheme = {
96+
avatar: {
97+
size: {
98+
xxl: 'h-64 w-64',
99+
},
100+
},
101+
};
102+
103+
const documentEl = () => document.documentElement;

src/lib/components/Flowbite/ThemeContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { FlowbiteTheme } from './FlowbiteTheme';
77

88
export type Mode = 'light' | 'dark';
99

10-
interface ThemeContextProps {
10+
export interface ThemeContextProps {
1111
theme: FlowbiteTheme;
1212
mode?: Mode;
1313
toggleMode?: () => void | null;

0 commit comments

Comments
 (0)