aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-theme/use-theme.test.tsx
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2023-10-27 18:07:45 +0200
committerArmand Philippot <git@armandphilippot.com>2023-11-11 18:15:27 +0100
commit05f1dfc6896d3affa7c494a1b955f230d836a4b7 (patch)
tree3089d5c3145f241293b88b9a1bfe4bb85e8ca9e0 /src/utils/hooks/use-theme/use-theme.test.tsx
parent757201fdc5c04a3f15504f74bf8ab85bb6018c2b (diff)
feat: replace next-themes with a custom ThemeProvider
To be honest, next-themes was working fine. However since I use a theme provider for Prism code blocks, some code is duplicated between this app and the library. So I prefer to use a custom Provider without the options I don't need.
Diffstat (limited to 'src/utils/hooks/use-theme/use-theme.test.tsx')
-rw-r--r--src/utils/hooks/use-theme/use-theme.test.tsx82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/utils/hooks/use-theme/use-theme.test.tsx b/src/utils/hooks/use-theme/use-theme.test.tsx
new file mode 100644
index 0000000..feaabfa
--- /dev/null
+++ b/src/utils/hooks/use-theme/use-theme.test.tsx
@@ -0,0 +1,82 @@
+import { describe, expect, it } from '@jest/globals';
+import { act, renderHook } from '@testing-library/react';
+import type { FC, ReactNode } from 'react';
+import { ThemeProvider, type ThemeProviderProps } from '../../providers';
+import { useTheme } from './use-theme';
+
+const createWrapper = (
+ Wrapper: FC<ThemeProviderProps>,
+ config: ThemeProviderProps
+) =>
+ function CreatedWrapper({ children }: { children: ReactNode }) {
+ return <Wrapper {...config}>{children}</Wrapper>;
+ };
+
+describe('useTheme', () => {
+ it('should return the default value without provider and prevent update', () => {
+ const defaultTheme = 'system';
+ const { result } = renderHook(() => useTheme());
+
+ expect(result.current.theme).toBe(defaultTheme);
+
+ act(() => result.current.setTheme('dark'));
+
+ expect(result.current.theme).toBe(defaultTheme);
+ });
+
+ it('can update the value', () => {
+ const defaultTheme = 'dark';
+
+ const { result } = renderHook(() => useTheme(), {
+ wrapper: createWrapper(ThemeProvider, {
+ attribute: 'magnam',
+ defaultTheme,
+ storageKey: 'repellat',
+ }),
+ });
+
+ expect(result.current.theme).toBe(defaultTheme);
+
+ const newTheme = 'light';
+
+ act(() => result.current.setTheme(newTheme));
+
+ expect(result.current.theme).toBe(newTheme);
+ });
+
+ it('can toggle the theme from dark to light', () => {
+ const defaultTheme = 'dark';
+
+ const { result } = renderHook(() => useTheme(), {
+ wrapper: createWrapper(ThemeProvider, {
+ attribute: 'voluptatibus',
+ defaultTheme,
+ storageKey: 'qui',
+ }),
+ });
+
+ expect(result.current.theme).toBe(defaultTheme);
+
+ act(() => result.current.toggleTheme());
+
+ expect(result.current.theme).toBe('light');
+ });
+
+ it('can toggle the theme from light to dark', () => {
+ const defaultTheme = 'light';
+
+ const { result } = renderHook(() => useTheme(), {
+ wrapper: createWrapper(ThemeProvider, {
+ attribute: 'sed',
+ defaultTheme,
+ storageKey: 'ut',
+ }),
+ });
+
+ expect(result.current.theme).toBe(defaultTheme);
+
+ act(() => result.current.toggleTheme());
+
+ expect(result.current.theme).toBe('dark');
+ });
+});