-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathuseAppKitTheme.test.tsx
More file actions
188 lines (144 loc) · 5.85 KB
/
useAppKitTheme.test.tsx
File metadata and controls
188 lines (144 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { renderHook, act } from '@testing-library/react-native';
import React from 'react';
import { useAppKitTheme } from '../../hooks/useAppKitTheme';
import { ThemeController } from '@reown/appkit-core-react-native';
import { type AppKitContextType, AppKitContext } from '../../AppKitContext';
import type { AppKit } from '../../AppKit';
// Mock valtio
jest.mock('valtio', () => ({
useSnapshot: jest.fn(state => state)
}));
// Mock ThemeController
jest.mock('@reown/appkit-core-react-native', () => ({
ThemeController: {
state: {
themeMode: 'dark',
themeVariables: {}
},
setThemeMode: jest.fn(),
setThemeVariables: jest.fn()
}
}));
describe('useAppKitTheme', () => {
const mockAppKit = {} as AppKit;
const wrapper = ({ children }: { children: React.ReactNode }) => {
const contextValue: AppKitContextType = { appKit: mockAppKit };
return <AppKitContext.Provider value={contextValue}>{children}</AppKitContext.Provider>;
};
beforeEach(() => {
jest.clearAllMocks();
// Reset ThemeController state
ThemeController.state = {
themeMode: 'dark',
themeVariables: {}
};
});
it('should throw error when used outside AppKitProvider', () => {
// Suppress console.error for this test
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
expect(() => {
renderHook(() => useAppKitTheme());
}).toThrow('AppKit instance is not yet available in context.');
consoleSpy.mockRestore();
});
it('should return initial theme state', () => {
const { result } = renderHook(() => useAppKitTheme(), { wrapper });
expect(result.current.themeMode).toBeUndefined();
expect(result.current.themeVariables).toBe({});
});
it('should return dark theme mode when set', () => {
ThemeController.state = {
themeMode: 'dark',
themeVariables: {}
};
const { result } = renderHook(() => useAppKitTheme(), { wrapper });
expect(result.current.themeMode).toBe('dark');
});
it('should return light theme mode when set', () => {
ThemeController.state = {
themeMode: 'light',
themeVariables: {}
};
const { result } = renderHook(() => useAppKitTheme(), { wrapper });
expect(result.current.themeMode).toBe('light');
});
it('should return theme variables when set', () => {
const themeVariables = { accent: '#00BB7F' };
ThemeController.state = {
themeMode: 'dark',
themeVariables
};
const { result } = renderHook(() => useAppKitTheme(), { wrapper });
expect(result.current.themeVariables).toEqual(themeVariables);
});
it('should call ThemeController.setThemeMode when setThemeMode is called', () => {
const { result } = renderHook(() => useAppKitTheme(), { wrapper });
act(() => {
result.current.setThemeMode('dark');
});
expect(ThemeController.setThemeMode).toHaveBeenCalledWith('dark');
});
it('should call ThemeController.setThemeMode with undefined', () => {
const { result } = renderHook(() => useAppKitTheme(), { wrapper });
act(() => {
result.current.setThemeMode(undefined);
});
expect(ThemeController.setThemeMode).toHaveBeenCalledWith(undefined);
});
it('should call ThemeController.setThemeVariables when setThemeVariables is called', () => {
const { result } = renderHook(() => useAppKitTheme(), { wrapper });
const themeVariables = { accent: '#FF5733' };
act(() => {
result.current.setThemeVariables(themeVariables);
});
expect(ThemeController.setThemeVariables).toHaveBeenCalledWith(themeVariables);
});
it('should call ThemeController.setThemeVariables with undefined', () => {
const { result } = renderHook(() => useAppKitTheme(), { wrapper });
act(() => {
result.current.setThemeVariables(undefined);
});
expect(ThemeController.setThemeVariables).toHaveBeenCalledWith(undefined);
});
it('should return stable function references', () => {
const { result } = renderHook(() => useAppKitTheme(), { wrapper });
const firstSetThemeMode = result.current.setThemeMode;
const firstSetThemeVariables = result.current.setThemeVariables;
// Functions should be stable (same reference)
expect(result.current.setThemeMode).toBe(firstSetThemeMode);
expect(result.current.setThemeVariables).toBe(firstSetThemeVariables);
});
it('should update theme mode and variables together', () => {
ThemeController.state = {
themeMode: 'dark',
themeVariables: { accent: '#00BB7F' }
};
const { result } = renderHook(() => useAppKitTheme(), { wrapper });
expect(result.current.themeMode).toBe('dark');
expect(result.current.themeVariables).toEqual({ accent: '#00BB7F' });
});
it('should handle multiple setThemeMode calls', () => {
const { result } = renderHook(() => useAppKitTheme(), { wrapper });
act(() => {
result.current.setThemeMode('dark');
result.current.setThemeMode('light');
result.current.setThemeMode(undefined);
});
expect(ThemeController.setThemeMode).toHaveBeenCalledTimes(3);
expect(ThemeController.setThemeMode).toHaveBeenNthCalledWith(1, 'dark');
expect(ThemeController.setThemeMode).toHaveBeenNthCalledWith(2, 'light');
expect(ThemeController.setThemeMode).toHaveBeenNthCalledWith(3, undefined);
});
it('should handle multiple setThemeVariables calls', () => {
const { result } = renderHook(() => useAppKitTheme(), { wrapper });
const variables1 = { accent: '#00BB7F' };
const variables2 = { accent: '#FF5733' };
act(() => {
result.current.setThemeVariables(variables1);
result.current.setThemeVariables(variables2);
});
expect(ThemeController.setThemeVariables).toHaveBeenCalledTimes(2);
expect(ThemeController.setThemeVariables).toHaveBeenNthCalledWith(1, variables1);
expect(ThemeController.setThemeVariables).toHaveBeenNthCalledWith(2, variables2);
});
});