|
| 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; |
0 commit comments