|
| 1 | +import { act, renderHook } from '@testing-library/react'; |
| 2 | +import { ThemeMode } from 'antd-style'; |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import { withSWR } from '~test-utils'; |
| 5 | + |
| 6 | +import { CURRENT_VERSION } from '@/const/version'; |
| 7 | +import { globalService } from '@/services/global'; |
| 8 | +import { useGlobalStore } from '@/store/global'; |
| 9 | +import { initialState } from '@/store/global/initialState'; |
| 10 | +import { switchLang } from '@/utils/client/switchLang'; |
| 11 | + |
| 12 | +vi.mock('@/utils/client/switchLang', () => ({ |
| 13 | + switchLang: vi.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock('@/services/global', () => ({ |
| 17 | + globalService: { |
| 18 | + getLatestVersion: vi.fn(), |
| 19 | + }, |
| 20 | +})); |
| 21 | + |
| 22 | +describe('generalActionSlice', () => { |
| 23 | + beforeEach(() => { |
| 24 | + vi.clearAllMocks(); |
| 25 | + useGlobalStore.setState(initialState); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + vi.restoreAllMocks(); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('updateSystemStatus', () => { |
| 33 | + it('should not update status when not initialized', () => { |
| 34 | + const { result } = renderHook(() => useGlobalStore()); |
| 35 | + |
| 36 | + act(() => { |
| 37 | + result.current.updateSystemStatus({ inputHeight: 200 }); |
| 38 | + }); |
| 39 | + |
| 40 | + expect(result.current.status).toEqual(initialState.status); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should update status when initialized', () => { |
| 44 | + const { result } = renderHook(() => useGlobalStore()); |
| 45 | + |
| 46 | + act(() => { |
| 47 | + useGlobalStore.setState({ isStatusInit: true }); |
| 48 | + result.current.updateSystemStatus({ inputHeight: 200 }); |
| 49 | + }); |
| 50 | + |
| 51 | + expect(result.current.status.inputHeight).toBe(200); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should not update if new status equals current status', () => { |
| 55 | + const { result } = renderHook(() => useGlobalStore()); |
| 56 | + const saveToLocalStorageSpy = vi.spyOn(result.current.statusStorage, 'saveToLocalStorage'); |
| 57 | + |
| 58 | + act(() => { |
| 59 | + useGlobalStore.setState({ isStatusInit: true }); |
| 60 | + result.current.updateSystemStatus({ inputHeight: initialState.status.inputHeight }); |
| 61 | + }); |
| 62 | + |
| 63 | + expect(saveToLocalStorageSpy).not.toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should save to localStorage when status is updated', () => { |
| 67 | + const { result } = renderHook(() => useGlobalStore()); |
| 68 | + const saveToLocalStorageSpy = vi.spyOn(result.current.statusStorage, 'saveToLocalStorage'); |
| 69 | + |
| 70 | + act(() => { |
| 71 | + useGlobalStore.setState({ isStatusInit: true }); |
| 72 | + result.current.updateSystemStatus({ inputHeight: 300 }); |
| 73 | + }); |
| 74 | + |
| 75 | + expect(saveToLocalStorageSpy).toHaveBeenCalledWith( |
| 76 | + expect.objectContaining({ inputHeight: 300 }), |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should merge nested objects correctly', () => { |
| 81 | + const { result } = renderHook(() => useGlobalStore()); |
| 82 | + |
| 83 | + act(() => { |
| 84 | + useGlobalStore.setState({ isStatusInit: true }); |
| 85 | + result.current.updateSystemStatus({ |
| 86 | + expandSessionGroupKeys: ['test1', 'test2'], |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + expect(result.current.status.expandSessionGroupKeys).toEqual(['test1', 'test2']); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('switchLocale', () => { |
| 95 | + it('should update language in system status and call switchLang', () => { |
| 96 | + const { result } = renderHook(() => useGlobalStore()); |
| 97 | + const locale = 'zh-CN'; |
| 98 | + |
| 99 | + act(() => { |
| 100 | + useGlobalStore.setState({ isStatusInit: true }); |
| 101 | + result.current.switchLocale(locale); |
| 102 | + }); |
| 103 | + |
| 104 | + expect(result.current.status.language).toBe(locale); |
| 105 | + expect(switchLang).toHaveBeenCalledWith(locale); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should not update language if status is not initialized', () => { |
| 109 | + const { result } = renderHook(() => useGlobalStore()); |
| 110 | + const locale = 'zh-CN'; |
| 111 | + |
| 112 | + act(() => { |
| 113 | + result.current.switchLocale(locale); |
| 114 | + }); |
| 115 | + |
| 116 | + expect(result.current.status.language).toBeUndefined(); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe('switchThemeMode', () => { |
| 121 | + it('should update theme mode in system status', () => { |
| 122 | + const { result } = renderHook(() => useGlobalStore()); |
| 123 | + const themeMode: ThemeMode = 'dark'; |
| 124 | + |
| 125 | + act(() => { |
| 126 | + useGlobalStore.setState({ isStatusInit: true }); |
| 127 | + result.current.switchThemeMode(themeMode); |
| 128 | + }); |
| 129 | + |
| 130 | + expect(result.current.status.themeMode).toBe(themeMode); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should not update theme mode if status is not initialized', () => { |
| 134 | + const { result } = renderHook(() => useGlobalStore()); |
| 135 | + const themeMode: ThemeMode = 'dark'; |
| 136 | + |
| 137 | + act(() => { |
| 138 | + result.current.switchThemeMode(themeMode); |
| 139 | + }); |
| 140 | + |
| 141 | + expect(result.current.status.themeMode).toBe(initialState.status.themeMode); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should handle light theme mode', () => { |
| 145 | + const { result } = renderHook(() => useGlobalStore()); |
| 146 | + |
| 147 | + act(() => { |
| 148 | + useGlobalStore.setState({ isStatusInit: true }); |
| 149 | + result.current.switchThemeMode('light'); |
| 150 | + }); |
| 151 | + |
| 152 | + expect(result.current.status.themeMode).toBe('light'); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + describe('useCheckLatestVersion', () => { |
| 157 | + it('should not fetch version when check is disabled', () => { |
| 158 | + const getLatestVersionSpy = vi.spyOn(globalService, 'getLatestVersion'); |
| 159 | + |
| 160 | + renderHook(() => useGlobalStore().useCheckLatestVersion(false), { |
| 161 | + wrapper: withSWR, |
| 162 | + }); |
| 163 | + |
| 164 | + expect(getLatestVersionSpy).not.toHaveBeenCalled(); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should set hasNewVersion when major version is newer', async () => { |
| 168 | + const latestVersion = '999.0.0'; |
| 169 | + vi.spyOn(globalService, 'getLatestVersion').mockResolvedValueOnce(latestVersion); |
| 170 | + |
| 171 | + const { result } = renderHook(() => useGlobalStore().useCheckLatestVersion(), { |
| 172 | + wrapper: withSWR, |
| 173 | + }); |
| 174 | + |
| 175 | + await act(async () => { |
| 176 | + await result.current.data; |
| 177 | + }); |
| 178 | + |
| 179 | + expect(useGlobalStore.getState().hasNewVersion).toBe(true); |
| 180 | + expect(useGlobalStore.getState().latestVersion).toBe(latestVersion); |
| 181 | + }); |
| 182 | + |
| 183 | + it('should not set hasNewVersion for same major.minor version', async () => { |
| 184 | + const currentParts = CURRENT_VERSION.split('.'); |
| 185 | + const latestVersion = `${currentParts[0]}.${currentParts[1]}.999`; |
| 186 | + vi.spyOn(globalService, 'getLatestVersion').mockResolvedValueOnce(latestVersion); |
| 187 | + |
| 188 | + const { result } = renderHook(() => useGlobalStore().useCheckLatestVersion(), { |
| 189 | + wrapper: withSWR, |
| 190 | + }); |
| 191 | + |
| 192 | + await act(async () => { |
| 193 | + await result.current.data; |
| 194 | + }); |
| 195 | + |
| 196 | + // Reset hasNewVersion and latestVersion |
| 197 | + useGlobalStore.setState({ hasNewVersion: undefined, latestVersion: undefined }); |
| 198 | + |
| 199 | + expect(useGlobalStore.getState().hasNewVersion).toBeUndefined(); |
| 200 | + expect(useGlobalStore.getState().latestVersion).toBeUndefined(); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should not set hasNewVersion when version is invalid', async () => { |
| 204 | + vi.spyOn(globalService, 'getLatestVersion').mockResolvedValueOnce('invalid.version'); |
| 205 | + |
| 206 | + const { result } = renderHook(() => useGlobalStore().useCheckLatestVersion(), { |
| 207 | + wrapper: withSWR, |
| 208 | + }); |
| 209 | + |
| 210 | + await act(async () => { |
| 211 | + await result.current.data; |
| 212 | + }); |
| 213 | + |
| 214 | + // Reset hasNewVersion and latestVersion |
| 215 | + useGlobalStore.setState({ hasNewVersion: undefined, latestVersion: undefined }); |
| 216 | + |
| 217 | + expect(useGlobalStore.getState().hasNewVersion).toBeUndefined(); |
| 218 | + expect(useGlobalStore.getState().latestVersion).toBeUndefined(); |
| 219 | + }); |
| 220 | + }); |
| 221 | +}); |
0 commit comments