@taicode/common-web 3.7.7 → 3.7.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/output/components/alert-dialog.d.ts +20 -0
- package/output/components/alert-dialog.d.ts.map +1 -0
- package/output/components/alert-dialog.js +65 -0
- package/output/components/alert.d.ts +11 -0
- package/output/components/alert.d.ts.map +1 -0
- package/output/components/alert.js +38 -0
- package/output/components/avatar.d.ts +13 -0
- package/output/components/avatar.d.ts.map +1 -0
- package/output/components/avatar.js +40 -0
- package/output/components/badge.d.ts +11 -0
- package/output/components/badge.d.ts.map +1 -0
- package/output/components/badge.js +39 -0
- package/output/components/button.d.ts +12 -0
- package/output/components/button.d.ts.map +1 -0
- package/output/components/button.js +48 -0
- package/output/components/calendar.d.ts +13 -0
- package/output/components/calendar.d.ts.map +1 -0
- package/output/components/calendar.js +57 -0
- package/output/components/card.d.ts +13 -0
- package/output/components/card.d.ts.map +1 -0
- package/output/components/card.js +43 -0
- package/output/components/chart.d.ts +56 -0
- package/output/components/chart.d.ts.map +1 -0
- package/output/components/chart.js +127 -0
- package/output/components/checkbox.d.ts +5 -0
- package/output/components/checkbox.d.ts.map +1 -0
- package/output/components/checkbox.js +21 -0
- package/output/components/command.d.ts +20 -0
- package/output/components/command.d.ts.map +1 -0
- package/output/components/command.js +55 -0
- package/output/components/dialog.d.ts +20 -0
- package/output/components/dialog.d.ts.map +1 -0
- package/output/components/dialog.js +58 -0
- package/output/components/direction.d.ts +9 -0
- package/output/components/direction.d.ts.map +1 -0
- package/output/components/direction.js +10 -0
- package/output/components/drawer.d.ts +14 -0
- package/output/components/drawer.d.ts.map +1 -0
- package/output/components/drawer.js +56 -0
- package/output/components/dropdown-menu.d.ts +29 -0
- package/output/components/dropdown-menu.d.ts.map +1 -0
- package/output/components/dropdown-menu.js +78 -0
- package/output/components/field.d.ts +39 -0
- package/output/components/field.d.ts.map +1 -0
- package/output/components/field.js +102 -0
- package/output/components/footer/index.d.ts +27 -0
- package/output/components/footer/index.d.ts.map +1 -0
- package/output/components/footer/index.js +17 -0
- package/output/components/form.d.ts +26 -0
- package/output/components/form.d.ts.map +1 -0
- package/output/components/form.js +69 -0
- package/output/components/index.d.ts +46 -0
- package/output/components/index.d.ts.map +1 -0
- package/output/components/index.js +45 -0
- package/output/components/input-group.d.ts +17 -0
- package/output/components/input-group.d.ts.map +1 -0
- package/output/components/input-group.js +75 -0
- package/output/components/input.d.ts +4 -0
- package/output/components/input.d.ts.map +1 -0
- package/output/components/input.js +19 -0
- package/output/components/item.d.ts +25 -0
- package/output/components/item.d.ts.map +1 -0
- package/output/components/item.js +89 -0
- package/output/components/json-view/index.d.ts +11 -0
- package/output/components/json-view/index.d.ts.map +1 -0
- package/output/components/json-view/index.js +52 -0
- package/output/components/json-view/lazy/index.d.ts +18 -0
- package/output/components/json-view/lazy/index.d.ts.map +1 -0
- package/output/components/json-view/lazy/index.js +22 -0
- package/output/components/kbd.d.ts +4 -0
- package/output/components/kbd.d.ts.map +1 -0
- package/output/components/kbd.js +22 -0
- package/output/components/label.d.ts +5 -0
- package/output/components/label.d.ts.map +1 -0
- package/output/components/label.js +20 -0
- package/output/components/list-actions/index.d.ts +23 -0
- package/output/components/list-actions/index.d.ts.map +1 -0
- package/output/components/list-actions/index.js +15 -0
- package/output/components/loading-button/index.d.ts +44 -0
- package/output/components/loading-button/index.d.ts.map +1 -0
- package/output/components/loading-button/index.js +75 -0
- package/output/components/loading-button/index.test.d.ts +2 -0
- package/output/components/loading-button/index.test.d.ts.map +1 -0
- package/output/components/loading-button/index.test.js +203 -0
- package/output/components/native-select.d.ts +9 -0
- package/output/components/native-select.d.ts.map +1 -0
- package/output/components/native-select.js +28 -0
- package/output/components/page-back-button/index.d.ts +8 -0
- package/output/components/page-back-button/index.d.ts.map +1 -0
- package/output/components/page-back-button/index.js +7 -0
- package/output/components/page-state.d.ts +19 -0
- package/output/components/page-state.d.ts.map +1 -0
- package/output/components/page-state.js +13 -0
- package/output/components/pagination/index.d.ts +34 -0
- package/output/components/pagination/index.d.ts.map +1 -0
- package/output/components/pagination/index.js +75 -0
- package/output/components/popover.d.ts +11 -0
- package/output/components/popover.d.ts.map +1 -0
- package/output/components/popover.js +44 -0
- package/output/components/progress.d.ts +5 -0
- package/output/components/progress.d.ts.map +1 -0
- package/output/components/progress.js +21 -0
- package/output/components/radio-group.d.ts +6 -0
- package/output/components/radio-group.d.ts.map +1 -0
- package/output/components/radio-group.js +25 -0
- package/output/components/route-pending-indicator/index.d.ts +20 -0
- package/output/components/route-pending-indicator/index.d.ts.map +1 -0
- package/output/components/route-pending-indicator/index.js +17 -0
- package/output/components/scroll-area.d.ts +6 -0
- package/output/components/scroll-area.d.ts.map +1 -0
- package/output/components/scroll-area.js +24 -0
- package/output/components/searchable-select/index.d.ts +41 -0
- package/output/components/searchable-select/index.d.ts.map +1 -0
- package/output/components/searchable-select/index.js +46 -0
- package/output/components/select.d.ts +17 -0
- package/output/components/select.d.ts.map +1 -0
- package/output/components/select.js +58 -0
- package/output/components/separator.d.ts +5 -0
- package/output/components/separator.d.ts.map +1 -0
- package/output/components/separator.js +20 -0
- package/output/components/sheet.d.ts +16 -0
- package/output/components/sheet.d.ts.map +1 -0
- package/output/components/sheet.js +58 -0
- package/output/components/sidebar.d.ts +78 -0
- package/output/components/sidebar.d.ts.map +1 -0
- package/output/components/sidebar.js +233 -0
- package/output/components/size-provider/index.d.ts +2 -0
- package/output/components/size-provider/index.d.ts.map +1 -0
- package/output/components/size-provider/index.js +1 -0
- package/output/components/size-provider/size-provider.d.ts +9 -0
- package/output/components/size-provider/size-provider.d.ts.map +1 -0
- package/output/components/size-provider/size-provider.js +29 -0
- package/output/components/skeleton.d.ts +3 -0
- package/output/components/skeleton.d.ts.map +1 -0
- package/output/components/skeleton.js +18 -0
- package/output/components/sonner.d.ts +8 -0
- package/output/components/sonner.d.ts.map +1 -0
- package/output/components/sonner.js +34 -0
- package/output/components/spinner.d.ts +3 -0
- package/output/components/spinner.d.ts.map +1 -0
- package/output/components/spinner.js +19 -0
- package/output/components/switch.d.ts +8 -0
- package/output/components/switch.d.ts.map +1 -0
- package/output/components/switch.js +20 -0
- package/output/components/table.d.ts +11 -0
- package/output/components/table.d.ts.map +1 -0
- package/output/components/table.js +47 -0
- package/output/components/tabs.d.ts +12 -0
- package/output/components/tabs.d.ts.map +1 -0
- package/output/components/tabs.js +45 -0
- package/output/components/textarea.d.ts +4 -0
- package/output/components/textarea.d.ts.map +1 -0
- package/output/components/textarea.js +19 -0
- package/output/components/theme-switcher.d.ts +6 -0
- package/output/components/theme-switcher.d.ts.map +1 -0
- package/output/components/theme-switcher.js +19 -0
- package/output/components/toaster/index.d.ts +16 -0
- package/output/components/toaster/index.d.ts.map +1 -0
- package/output/components/toaster/index.js +70 -0
- package/output/components/toggle-group.d.ts +12 -0
- package/output/components/toggle-group.d.ts.map +1 -0
- package/output/components/toggle-group.js +38 -0
- package/output/components/toggle.d.ts +10 -0
- package/output/components/toggle.d.ts.map +1 -0
- package/output/components/toggle.js +38 -0
- package/output/components/tooltip.d.ts +8 -0
- package/output/components/tooltip.d.ts.map +1 -0
- package/output/components/tooltip.js +33 -0
- package/output/features/confirm-dialog/index.d.ts +41 -0
- package/output/features/confirm-dialog/index.d.ts.map +1 -0
- package/output/features/confirm-dialog/index.js +19 -0
- package/output/features/input-confirm/index.d.ts +26 -0
- package/output/features/input-confirm/index.d.ts.map +1 -0
- package/output/features/input-confirm/index.js +59 -0
- package/output/features/signin/context.d.ts +14 -0
- package/output/features/signin/context.d.ts.map +1 -0
- package/output/features/signin/context.js +13 -0
- package/output/features/signin/index.d.ts +58 -0
- package/output/features/signin/index.d.ts.map +1 -0
- package/output/features/signin/index.js +137 -0
- package/output/features/signin/service.d.ts +92 -0
- package/output/features/signin/service.d.ts.map +1 -0
- package/output/features/signin/service.js +519 -0
- package/output/features/signin/service.test.d.ts +2 -0
- package/output/features/signin/service.test.d.ts.map +1 -0
- package/output/features/signin/service.test.js +211 -0
- package/output/features/signin/types.d.ts +63 -0
- package/output/features/signin/types.d.ts.map +1 -0
- package/output/features/signin/types.js +1 -0
- package/output/hooks/use-loading/index.d.ts +6 -0
- package/output/hooks/use-loading/index.d.ts.map +1 -0
- package/output/hooks/use-loading/index.js +14 -0
- package/output/hooks/use-loading/index.test.d.ts +2 -0
- package/output/hooks/use-loading/index.test.d.ts.map +1 -0
- package/output/hooks/use-loading/index.test.js +24 -0
- package/output/hooks/use-mobile/index.d.ts +2 -0
- package/output/hooks/use-mobile/index.d.ts.map +1 -0
- package/output/hooks/use-mobile/index.js +16 -0
- package/output/hooks/use-observer/index.d.ts +2 -0
- package/output/hooks/use-observer/index.d.ts.map +1 -0
- package/output/hooks/use-observer/index.js +1 -0
- package/output/hooks/use-observer/use-observer.d.ts +3 -0
- package/output/hooks/use-observer/use-observer.d.ts.map +1 -0
- package/output/hooks/use-observer/use-observer.js +16 -0
- package/output/hooks/use-observer/use-observer.test.d.ts +2 -0
- package/output/hooks/use-observer/use-observer.test.d.ts.map +1 -0
- package/output/hooks/use-observer/use-observer.test.js +134 -0
- package/output/hooks/use-offline/index.d.ts +6 -0
- package/output/hooks/use-offline/index.d.ts.map +1 -0
- package/output/hooks/use-offline/index.js +19 -0
- package/output/layout/app/index.d.ts +34 -0
- package/output/layout/app/index.d.ts.map +1 -0
- package/output/layout/app/index.js +34 -0
- package/output/layout/dashboard/index.d.ts +76 -0
- package/output/layout/dashboard/index.d.ts.map +1 -0
- package/output/layout/dashboard/index.js +84 -0
- package/output/layout/index.d.ts +3 -0
- package/output/layout/index.d.ts.map +1 -0
- package/output/layout/index.js +2 -0
- package/output/services/cache-api/cache-api.d.ts +23 -0
- package/output/services/cache-api/cache-api.d.ts.map +1 -0
- package/output/services/cache-api/cache-api.js +145 -0
- package/output/services/cache-api/cache-api.test.d.ts +2 -0
- package/output/services/cache-api/cache-api.test.d.ts.map +1 -0
- package/output/services/cache-api/cache-api.test.js +610 -0
- package/output/services/cache-api/index.d.ts +2 -0
- package/output/services/cache-api/index.d.ts.map +1 -0
- package/output/services/cache-api/index.js +1 -0
- package/output/services/cached-fetch/index.d.ts +50 -0
- package/output/services/cached-fetch/index.d.ts.map +1 -0
- package/output/services/cached-fetch/index.js +196 -0
- package/output/services/service/index.d.ts +2 -0
- package/output/services/service/index.d.ts.map +1 -0
- package/output/services/service/index.js +1 -0
- package/output/services/service/service.d.ts +135 -0
- package/output/services/service/service.d.ts.map +1 -0
- package/output/services/service/service.js +276 -0
- package/output/services/service/service.test.d.ts +2 -0
- package/output/services/service/service.test.d.ts.map +1 -0
- package/output/services/service/service.test.js +1399 -0
- package/output/services/side-cache/index.d.ts +2 -0
- package/output/services/side-cache/index.d.ts.map +1 -0
- package/output/services/side-cache/index.js +1 -0
- package/output/services/side-cache/side-cache.d.ts +11 -0
- package/output/services/side-cache/side-cache.d.ts.map +1 -0
- package/output/services/side-cache/side-cache.js +161 -0
- package/output/services/side-cache/side-cache.test.d.ts +2 -0
- package/output/services/side-cache/side-cache.test.d.ts.map +1 -0
- package/output/services/side-cache/side-cache.test.js +271 -0
- package/output/utils/avatar/index.d.ts +28 -0
- package/output/utils/avatar/index.d.ts.map +1 -0
- package/output/utils/avatar/index.js +35 -0
- package/output/utils/delay/index.d.ts +2 -0
- package/output/utils/delay/index.d.ts.map +1 -0
- package/output/utils/delay/index.js +5 -0
- package/output/utils/form/index.d.ts +3 -0
- package/output/utils/form/index.d.ts.map +1 -0
- package/output/utils/form/index.js +1 -0
- package/output/utils/merge-class-names/index.d.ts +3 -0
- package/output/utils/merge-class-names/index.d.ts.map +1 -0
- package/output/utils/merge-class-names/index.js +5 -0
- package/output/utils/merge-class-names/index.test.d.ts +2 -0
- package/output/utils/merge-class-names/index.test.d.ts.map +1 -0
- package/output/utils/merge-class-names/index.test.js +7 -0
- package/output/utils/performance/index.d.ts +82 -0
- package/output/utils/performance/index.d.ts.map +1 -0
- package/output/utils/performance/index.js +129 -0
- package/output/utils/redirect/index.d.ts +6 -0
- package/output/utils/redirect/index.d.ts.map +1 -0
- package/output/utils/redirect/index.js +32 -0
- package/package.json +2 -2
|
@@ -0,0 +1,610 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { runInAction, configure } from 'mobx';
|
|
3
|
+
import { CacheApi } from './cache-api.js';
|
|
4
|
+
// 配置 MobX 在测试环境中的行为
|
|
5
|
+
configure({
|
|
6
|
+
enforceActions: 'never',
|
|
7
|
+
computedRequiresReaction: false,
|
|
8
|
+
reactionRequiresObservable: false,
|
|
9
|
+
observableRequiresReaction: false,
|
|
10
|
+
disableErrorBoundaries: true
|
|
11
|
+
});
|
|
12
|
+
describe('CacheApi', () => {
|
|
13
|
+
let mockApiFunc;
|
|
14
|
+
let cacheApi;
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
mockApiFunc = vi.fn();
|
|
17
|
+
cacheApi = new CacheApi(mockApiFunc);
|
|
18
|
+
});
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
vi.clearAllMocks();
|
|
21
|
+
});
|
|
22
|
+
describe('构造函数', () => {
|
|
23
|
+
it('应该正确初始化 CacheApi 实例', () => {
|
|
24
|
+
expect(cacheApi).toBeInstanceOf(CacheApi);
|
|
25
|
+
expect(cacheApi.value).toBeUndefined();
|
|
26
|
+
});
|
|
27
|
+
it('应该正确绑定 send 方法', () => {
|
|
28
|
+
const { call: send } = cacheApi;
|
|
29
|
+
expect(typeof send).toBe('function');
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
describe('value getter', () => {
|
|
33
|
+
it('初始状态下应该返回 undefined', () => {
|
|
34
|
+
expect(cacheApi.value).toBeUndefined();
|
|
35
|
+
});
|
|
36
|
+
it('应该在成功调用后返回缓存的值', async () => {
|
|
37
|
+
const expectedResult = 'test-result';
|
|
38
|
+
mockApiFunc.mockResolvedValue(expectedResult);
|
|
39
|
+
await cacheApi.call('param1', 123);
|
|
40
|
+
expect(cacheApi.value).toBe(expectedResult);
|
|
41
|
+
});
|
|
42
|
+
it('应该在多次调用后返回最新的缓存值', async () => {
|
|
43
|
+
const result1 = 'result-1';
|
|
44
|
+
const result2 = 'result-2';
|
|
45
|
+
mockApiFunc.mockResolvedValueOnce(result1);
|
|
46
|
+
await cacheApi.call('param1', 123);
|
|
47
|
+
expect(cacheApi.value).toBe(result1);
|
|
48
|
+
mockApiFunc.mockResolvedValueOnce(result2);
|
|
49
|
+
await cacheApi.call('param2', 456);
|
|
50
|
+
expect(cacheApi.value).toBe(result2);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
describe('empty getter', () => {
|
|
54
|
+
it('初始状态下应该返回 true', () => {
|
|
55
|
+
expect(cacheApi.empty).toBe(true);
|
|
56
|
+
});
|
|
57
|
+
it('成功调用后应该返回 false', async () => {
|
|
58
|
+
mockApiFunc.mockResolvedValue('test-result');
|
|
59
|
+
await cacheApi.call('param1', 123);
|
|
60
|
+
expect(cacheApi.empty).toBe(false);
|
|
61
|
+
});
|
|
62
|
+
it('即使调用失败也应该保持 true', async () => {
|
|
63
|
+
const error = new Error('API Error');
|
|
64
|
+
mockApiFunc.mockRejectedValue(error);
|
|
65
|
+
try {
|
|
66
|
+
await cacheApi.call('param1', 123);
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
// 忽略错误
|
|
70
|
+
}
|
|
71
|
+
expect(cacheApi.empty).toBe(true);
|
|
72
|
+
});
|
|
73
|
+
it('empty 应该是一个 computed 属性', () => {
|
|
74
|
+
const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(cacheApi), 'empty');
|
|
75
|
+
expect(descriptor).toBeDefined();
|
|
76
|
+
expect(descriptor === null || descriptor === void 0 ? void 0 : descriptor.get).toBeDefined();
|
|
77
|
+
expect(descriptor === null || descriptor === void 0 ? void 0 : descriptor.set).toBeUndefined();
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
describe('send 方法', () => {
|
|
81
|
+
it('应该调用传入的 API 函数', async () => {
|
|
82
|
+
const expectedResult = 'api-result';
|
|
83
|
+
mockApiFunc.mockResolvedValue(expectedResult);
|
|
84
|
+
const result = await cacheApi.call('test', 42);
|
|
85
|
+
expect(mockApiFunc).toHaveBeenCalledTimes(1);
|
|
86
|
+
expect(mockApiFunc).toHaveBeenCalledWith('test', 42);
|
|
87
|
+
expect(result).toBe(expectedResult);
|
|
88
|
+
});
|
|
89
|
+
it('应该正确传递多个参数', async () => {
|
|
90
|
+
const params = ['hello', 999];
|
|
91
|
+
mockApiFunc.mockResolvedValue('success');
|
|
92
|
+
await cacheApi.call(...params);
|
|
93
|
+
expect(mockApiFunc).toHaveBeenCalledWith(...params);
|
|
94
|
+
});
|
|
95
|
+
it('应该返回 API 函数的结果', async () => {
|
|
96
|
+
const expectedResult = { data: 'test', status: 'ok' };
|
|
97
|
+
mockApiFunc.mockResolvedValue(expectedResult);
|
|
98
|
+
const result = await cacheApi.call('param', 1);
|
|
99
|
+
expect(result).toEqual(expectedResult);
|
|
100
|
+
});
|
|
101
|
+
it('应该处理 API 函数抛出的异常', async () => {
|
|
102
|
+
const error = new Error('API Error');
|
|
103
|
+
mockApiFunc.mockRejectedValue(error);
|
|
104
|
+
await expect(cacheApi.call('param', 1)).rejects.toThrow('API Error');
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
describe('缓存功能', () => {
|
|
108
|
+
it('相同参数应该使用缓存', async () => {
|
|
109
|
+
const result = 'cached-result';
|
|
110
|
+
mockApiFunc.mockResolvedValue(result);
|
|
111
|
+
// 第一次调用
|
|
112
|
+
const firstResult = await cacheApi.call('same', 123);
|
|
113
|
+
expect(firstResult).toBe(result);
|
|
114
|
+
expect(cacheApi.value).toBe(result);
|
|
115
|
+
// 第二次调用相同参数,应该从缓存返回
|
|
116
|
+
const secondResult = await cacheApi.call('same', 123);
|
|
117
|
+
expect(secondResult).toBe(result);
|
|
118
|
+
expect(cacheApi.value).toBe(result);
|
|
119
|
+
// API 函数应该被调用两次(SideCache 的 handle 方法会每次都调用)
|
|
120
|
+
expect(mockApiFunc).toHaveBeenCalledTimes(2);
|
|
121
|
+
});
|
|
122
|
+
it('不同参数应该触发新的 API 调用', async () => {
|
|
123
|
+
mockApiFunc.mockResolvedValueOnce('result1');
|
|
124
|
+
mockApiFunc.mockResolvedValueOnce('result2');
|
|
125
|
+
await cacheApi.call('param1', 1);
|
|
126
|
+
await cacheApi.call('param2', 2);
|
|
127
|
+
expect(mockApiFunc).toHaveBeenCalledTimes(2);
|
|
128
|
+
expect(mockApiFunc).toHaveBeenNthCalledWith(1, 'param1', 1);
|
|
129
|
+
expect(mockApiFunc).toHaveBeenNthCalledWith(2, 'param2', 2);
|
|
130
|
+
});
|
|
131
|
+
it('应该正确更新当前缓存的 key', async () => {
|
|
132
|
+
mockApiFunc.mockResolvedValueOnce('value1');
|
|
133
|
+
mockApiFunc.mockResolvedValueOnce('value2');
|
|
134
|
+
await cacheApi.call('key1', 1);
|
|
135
|
+
expect(cacheApi.value).toBe('value1');
|
|
136
|
+
await cacheApi.call('key2', 2);
|
|
137
|
+
expect(cacheApi.value).toBe('value2');
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
describe('异步操作', () => {
|
|
141
|
+
it('应该正确处理并发调用', async () => {
|
|
142
|
+
let resolveCount = 0;
|
|
143
|
+
mockApiFunc.mockImplementation(async (param) => {
|
|
144
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
145
|
+
return `result-${param}-${++resolveCount}`;
|
|
146
|
+
});
|
|
147
|
+
const [result1, result2, result3] = await Promise.all([
|
|
148
|
+
cacheApi.call('param1', 1),
|
|
149
|
+
cacheApi.call('param2', 2),
|
|
150
|
+
cacheApi.call('param3', 3)
|
|
151
|
+
]);
|
|
152
|
+
expect(result1).toMatch(/result-param1-\d/);
|
|
153
|
+
expect(result2).toMatch(/result-param2-\d/);
|
|
154
|
+
expect(result3).toMatch(/result-param3-\d/);
|
|
155
|
+
expect(mockApiFunc).toHaveBeenCalledTimes(3);
|
|
156
|
+
});
|
|
157
|
+
it('应该在异步操作完成后更新 value', async () => {
|
|
158
|
+
const delay = 50;
|
|
159
|
+
mockApiFunc.mockImplementation(async () => {
|
|
160
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
161
|
+
return 'delayed-result';
|
|
162
|
+
});
|
|
163
|
+
// 开始异步操作,此时 value 应该还是之前的值
|
|
164
|
+
const promise = cacheApi.call('test', 1);
|
|
165
|
+
// 等待一小段时间,但还没完成
|
|
166
|
+
await new Promise(resolve => setTimeout(resolve, delay / 2));
|
|
167
|
+
// 完成异步操作
|
|
168
|
+
const result = await promise;
|
|
169
|
+
expect(result).toBe('delayed-result');
|
|
170
|
+
expect(cacheApi.value).toBe('delayed-result');
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
describe('边缘情况', () => {
|
|
174
|
+
it('应该处理 null 返回值', async () => {
|
|
175
|
+
mockApiFunc.mockResolvedValue(null);
|
|
176
|
+
const result = await cacheApi.call('test', 1);
|
|
177
|
+
expect(result).toBeNull();
|
|
178
|
+
expect(cacheApi.value).toBeNull();
|
|
179
|
+
});
|
|
180
|
+
it('应该处理 undefined 返回值', async () => {
|
|
181
|
+
mockApiFunc.mockResolvedValue(undefined);
|
|
182
|
+
const result = await cacheApi.call('test', 1);
|
|
183
|
+
expect(result).toBeUndefined();
|
|
184
|
+
expect(cacheApi.value).toBeUndefined();
|
|
185
|
+
});
|
|
186
|
+
it('应该处理复杂对象参数', async () => {
|
|
187
|
+
const complexParam = { nested: { value: 'test' }, array: [1, 2, 3] };
|
|
188
|
+
const complexMockFunc = vi.fn();
|
|
189
|
+
complexMockFunc.mockResolvedValue('complex-result');
|
|
190
|
+
// 创建一个接受复杂参数的 CacheApi
|
|
191
|
+
const complexCacheApi = new CacheApi(complexMockFunc);
|
|
192
|
+
const result = await complexCacheApi.call(complexParam);
|
|
193
|
+
expect(result).toBe('complex-result');
|
|
194
|
+
expect(complexMockFunc).toHaveBeenCalledWith(complexParam);
|
|
195
|
+
});
|
|
196
|
+
it('should handle no parameters', async () => {
|
|
197
|
+
const noParamApi = new CacheApi(() => Promise.resolve('no-param-result'));
|
|
198
|
+
const result = await noParamApi.call();
|
|
199
|
+
expect(result).toBe('no-param-result');
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
describe('MobX 集成', () => {
|
|
203
|
+
it('value 应该是一个 computed 属性', () => {
|
|
204
|
+
// 通过检查属性描述符来验证这是一个 computed 属性
|
|
205
|
+
const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(cacheApi), 'value');
|
|
206
|
+
expect(descriptor).toBeDefined();
|
|
207
|
+
expect(descriptor === null || descriptor === void 0 ? void 0 : descriptor.get).toBeDefined();
|
|
208
|
+
expect(descriptor === null || descriptor === void 0 ? void 0 : descriptor.set).toBeUndefined();
|
|
209
|
+
});
|
|
210
|
+
it('value 变化应该触发 MobX 反应', async () => {
|
|
211
|
+
let reactionCount = 0;
|
|
212
|
+
let lastValue = undefined;
|
|
213
|
+
// 创建一个简单的反应来监听 value 变化
|
|
214
|
+
const disposer = runInAction(() => {
|
|
215
|
+
const reaction = () => {
|
|
216
|
+
reactionCount++;
|
|
217
|
+
lastValue = cacheApi.value;
|
|
218
|
+
};
|
|
219
|
+
// 初始调用
|
|
220
|
+
reaction();
|
|
221
|
+
return reaction;
|
|
222
|
+
});
|
|
223
|
+
mockApiFunc.mockResolvedValue('new-value');
|
|
224
|
+
await cacheApi.call('test', 1);
|
|
225
|
+
// 清理
|
|
226
|
+
if (typeof disposer === 'function') {
|
|
227
|
+
disposer();
|
|
228
|
+
}
|
|
229
|
+
expect(lastValue).toBe('new-value');
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
describe('类型安全', () => {
|
|
233
|
+
it('应该保持参数类型安全', () => {
|
|
234
|
+
// 这个测试主要是在编译时进行类型检查
|
|
235
|
+
// 如果类型不匹配,TypeScript 编译器会报错
|
|
236
|
+
const typedApi = new CacheApi((str, num, bool) => {
|
|
237
|
+
return Promise.resolve({ result: `${str}-${num}-${bool}` });
|
|
238
|
+
});
|
|
239
|
+
// 正确的调用
|
|
240
|
+
expect(() => typedApi.call('test', 42, true)).not.toThrow();
|
|
241
|
+
// 以下调用在 TypeScript 中会产生编译错误(在运行时测试中跳过)
|
|
242
|
+
// typedApi.send('test', 'wrong-type', true) // 第二个参数应该是 number
|
|
243
|
+
// typedApi.send('test', 42) // 缺少第三个参数
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
describe('loading 状态', () => {
|
|
247
|
+
it('初始状态下 loading 应该为 false', () => {
|
|
248
|
+
expect(cacheApi.loading).toBe(false);
|
|
249
|
+
});
|
|
250
|
+
it('发送请求时 loading 应该为 true', async () => {
|
|
251
|
+
let resolveFn;
|
|
252
|
+
const promise = new Promise((resolve) => {
|
|
253
|
+
resolveFn = resolve;
|
|
254
|
+
});
|
|
255
|
+
mockApiFunc.mockReturnValue(promise);
|
|
256
|
+
// 开始请求
|
|
257
|
+
const sendPromise = cacheApi.call('test', 1);
|
|
258
|
+
// 此时应该是 loading 状态
|
|
259
|
+
expect(cacheApi.loading).toBe(true);
|
|
260
|
+
// 完成请求
|
|
261
|
+
resolveFn('result');
|
|
262
|
+
await sendPromise;
|
|
263
|
+
// 请求完成后 loading 应该为 false
|
|
264
|
+
expect(cacheApi.loading).toBe(false);
|
|
265
|
+
});
|
|
266
|
+
it('请求失败后 loading 应该为 false', async () => {
|
|
267
|
+
const error = new Error('API Error');
|
|
268
|
+
mockApiFunc.mockRejectedValue(error);
|
|
269
|
+
try {
|
|
270
|
+
await cacheApi.call('test', 1);
|
|
271
|
+
}
|
|
272
|
+
catch (e) {
|
|
273
|
+
// 忽略错误
|
|
274
|
+
}
|
|
275
|
+
expect(cacheApi.loading).toBe(false);
|
|
276
|
+
});
|
|
277
|
+
it('并发请求时 loading 状态应该正确处理', async () => {
|
|
278
|
+
const resolvers = [];
|
|
279
|
+
mockApiFunc.mockImplementation((param) => {
|
|
280
|
+
return new Promise((resolve) => {
|
|
281
|
+
resolvers.push((value) => resolve(`${param}-${value}`));
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
// 启动三个并发请求
|
|
285
|
+
const promise1 = cacheApi.call('req1', 1);
|
|
286
|
+
const promise2 = cacheApi.call('req2', 2);
|
|
287
|
+
const promise3 = cacheApi.call('req3', 3);
|
|
288
|
+
// 此时应该有三个请求正在进行,loading 为 true
|
|
289
|
+
expect(cacheApi.loading).toBe(true);
|
|
290
|
+
// 完成第一个请求
|
|
291
|
+
resolvers[0]('result1');
|
|
292
|
+
await promise1;
|
|
293
|
+
// 还有两个请求在进行,loading 仍为 true
|
|
294
|
+
expect(cacheApi.loading).toBe(true);
|
|
295
|
+
// 完成第二个请求
|
|
296
|
+
resolvers[1]('result2');
|
|
297
|
+
await promise2;
|
|
298
|
+
// 还有一个请求在进行,loading 仍为 true
|
|
299
|
+
expect(cacheApi.loading).toBe(true);
|
|
300
|
+
// 完成第三个请求
|
|
301
|
+
resolvers[2]('result3');
|
|
302
|
+
await promise3;
|
|
303
|
+
// 所有请求完成,loading 为 false
|
|
304
|
+
expect(cacheApi.loading).toBe(false);
|
|
305
|
+
});
|
|
306
|
+
it('部分并发请求失败时 loading 状态应该正确', async () => {
|
|
307
|
+
let resolveSuccess;
|
|
308
|
+
let rejectError;
|
|
309
|
+
mockApiFunc.mockImplementation((param) => {
|
|
310
|
+
if (param === 'success') {
|
|
311
|
+
return new Promise((resolve) => {
|
|
312
|
+
resolveSuccess = resolve;
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
return new Promise((_, reject) => {
|
|
317
|
+
rejectError = reject;
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
// 启动两个请求,一个成功一个失败
|
|
322
|
+
const successPromise = cacheApi.call('success', 1);
|
|
323
|
+
const errorPromise = cacheApi.call('error', 2);
|
|
324
|
+
expect(cacheApi.loading).toBe(true);
|
|
325
|
+
// 先让失败的请求完成
|
|
326
|
+
rejectError(new Error('Failed'));
|
|
327
|
+
try {
|
|
328
|
+
await errorPromise;
|
|
329
|
+
}
|
|
330
|
+
catch (e) {
|
|
331
|
+
// 忽略错误
|
|
332
|
+
}
|
|
333
|
+
// 还有一个成功的请求在进行,loading 仍为 true
|
|
334
|
+
expect(cacheApi.loading).toBe(true);
|
|
335
|
+
// 完成成功的请求
|
|
336
|
+
resolveSuccess('success-result');
|
|
337
|
+
await successPromise;
|
|
338
|
+
// 所有请求完成,loading 为 false
|
|
339
|
+
expect(cacheApi.loading).toBe(false);
|
|
340
|
+
});
|
|
341
|
+
it('loading 应该是一个 computed 属性', () => {
|
|
342
|
+
const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(cacheApi), 'loading');
|
|
343
|
+
expect(descriptor).toBeDefined();
|
|
344
|
+
expect(descriptor === null || descriptor === void 0 ? void 0 : descriptor.get).toBeDefined();
|
|
345
|
+
expect(descriptor === null || descriptor === void 0 ? void 0 : descriptor.set).toBeUndefined();
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
describe('refresh 方法', () => {
|
|
349
|
+
it('应该在没有调用过 call 时抛出错误', async () => {
|
|
350
|
+
await expect(cacheApi.refresh()).rejects.toThrow('没有可用的参数来刷新数据,请先调用 call 方法');
|
|
351
|
+
});
|
|
352
|
+
it('应该使用最近一次的参数重新调用 API', async () => {
|
|
353
|
+
const expectedResult1 = 'first-result';
|
|
354
|
+
const expectedResult2 = 'refreshed-result';
|
|
355
|
+
mockApiFunc.mockResolvedValueOnce(expectedResult1);
|
|
356
|
+
mockApiFunc.mockResolvedValueOnce(expectedResult2);
|
|
357
|
+
// 第一次调用
|
|
358
|
+
await cacheApi.call('param1', 123);
|
|
359
|
+
expect(cacheApi.value).toBe(expectedResult1);
|
|
360
|
+
expect(mockApiFunc).toHaveBeenCalledTimes(1);
|
|
361
|
+
expect(mockApiFunc).toHaveBeenCalledWith('param1', 123);
|
|
362
|
+
// 刷新
|
|
363
|
+
const refreshResult = await cacheApi.refresh();
|
|
364
|
+
expect(refreshResult).toBe(expectedResult2);
|
|
365
|
+
expect(cacheApi.value).toBe(expectedResult2);
|
|
366
|
+
expect(mockApiFunc).toHaveBeenCalledTimes(2);
|
|
367
|
+
expect(mockApiFunc).toHaveBeenNthCalledWith(2, 'param1', 123);
|
|
368
|
+
});
|
|
369
|
+
it('应该在多次调用后使用最新的参数进行刷新', async () => {
|
|
370
|
+
mockApiFunc.mockResolvedValue('result');
|
|
371
|
+
// 第一次调用
|
|
372
|
+
await cacheApi.call('param1', 123);
|
|
373
|
+
// 第二次调用,参数不同
|
|
374
|
+
await cacheApi.call('param2', 456);
|
|
375
|
+
// 刷新应该使用最新的参数
|
|
376
|
+
await cacheApi.refresh();
|
|
377
|
+
expect(mockApiFunc).toHaveBeenCalledTimes(3);
|
|
378
|
+
expect(mockApiFunc).toHaveBeenNthCalledWith(3, 'param2', 456);
|
|
379
|
+
});
|
|
380
|
+
it('应该在 force=false 时正常刷新(默认行为)', async () => {
|
|
381
|
+
const result1 = 'cached-result';
|
|
382
|
+
const result2 = 'refreshed-result';
|
|
383
|
+
mockApiFunc.mockResolvedValueOnce(result1);
|
|
384
|
+
mockApiFunc.mockResolvedValueOnce(result2);
|
|
385
|
+
await cacheApi.call('test', 1);
|
|
386
|
+
const refreshResult = await cacheApi.refresh(false);
|
|
387
|
+
expect(refreshResult).toBe(result2);
|
|
388
|
+
expect(mockApiFunc).toHaveBeenCalledTimes(2);
|
|
389
|
+
});
|
|
390
|
+
it('应该在 force=true 时强制刷新并清除缓存', async () => {
|
|
391
|
+
const result1 = 'cached-result';
|
|
392
|
+
const result2 = 'force-refreshed-result';
|
|
393
|
+
mockApiFunc.mockResolvedValueOnce(result1);
|
|
394
|
+
mockApiFunc.mockResolvedValueOnce(result2);
|
|
395
|
+
await cacheApi.call('test', 1);
|
|
396
|
+
const refreshResult = await cacheApi.refresh(true);
|
|
397
|
+
expect(refreshResult).toBe(result2);
|
|
398
|
+
expect(mockApiFunc).toHaveBeenCalledTimes(2);
|
|
399
|
+
});
|
|
400
|
+
it('应该正确处理刷新时的异常', async () => {
|
|
401
|
+
const error = new Error('Refresh failed');
|
|
402
|
+
mockApiFunc.mockResolvedValueOnce('initial-result');
|
|
403
|
+
mockApiFunc.mockRejectedValueOnce(error);
|
|
404
|
+
// 第一次调用成功
|
|
405
|
+
await cacheApi.call('test', 1);
|
|
406
|
+
expect(cacheApi.value).toBe('initial-result');
|
|
407
|
+
// 刷新时失败
|
|
408
|
+
await expect(cacheApi.refresh()).rejects.toThrow('Refresh failed');
|
|
409
|
+
// 值应该保持不变
|
|
410
|
+
expect(cacheApi.value).toBe('initial-result');
|
|
411
|
+
});
|
|
412
|
+
it('应该在刷新期间正确更新 loading 状态', async () => {
|
|
413
|
+
let resolveFn;
|
|
414
|
+
const promise = new Promise((resolve) => {
|
|
415
|
+
resolveFn = resolve;
|
|
416
|
+
});
|
|
417
|
+
mockApiFunc.mockResolvedValueOnce('initial');
|
|
418
|
+
mockApiFunc.mockReturnValueOnce(promise);
|
|
419
|
+
// 初始调用
|
|
420
|
+
await cacheApi.call('test', 1);
|
|
421
|
+
expect(cacheApi.loading).toBe(false);
|
|
422
|
+
// 开始刷新
|
|
423
|
+
const refreshPromise = cacheApi.refresh();
|
|
424
|
+
expect(cacheApi.loading).toBe(true);
|
|
425
|
+
// 完成刷新
|
|
426
|
+
resolveFn('refreshed');
|
|
427
|
+
await refreshPromise;
|
|
428
|
+
expect(cacheApi.loading).toBe(false);
|
|
429
|
+
});
|
|
430
|
+
it('应该正确处理复杂参数的刷新', async () => {
|
|
431
|
+
const complexParam = { id: 1, data: { nested: 'value' } };
|
|
432
|
+
const complexMockFunc = vi.fn();
|
|
433
|
+
complexMockFunc.mockResolvedValue('complex-result');
|
|
434
|
+
const complexCacheApi = new CacheApi(complexMockFunc);
|
|
435
|
+
await complexCacheApi.call(complexParam);
|
|
436
|
+
await complexCacheApi.refresh();
|
|
437
|
+
expect(complexMockFunc).toHaveBeenCalledTimes(2);
|
|
438
|
+
expect(complexMockFunc).toHaveBeenNthCalledWith(2, complexParam);
|
|
439
|
+
});
|
|
440
|
+
it('应该支持无参数函数的刷新', async () => {
|
|
441
|
+
const noParamMockFunc = vi.fn();
|
|
442
|
+
noParamMockFunc.mockResolvedValue('no-param-result');
|
|
443
|
+
const noParamCacheApi = new CacheApi(noParamMockFunc);
|
|
444
|
+
await noParamCacheApi.call();
|
|
445
|
+
await noParamCacheApi.refresh();
|
|
446
|
+
expect(noParamMockFunc).toHaveBeenCalledTimes(2);
|
|
447
|
+
});
|
|
448
|
+
});
|
|
449
|
+
describe('reset 方法', () => {
|
|
450
|
+
it('应该清除所有缓存数据', async () => {
|
|
451
|
+
mockApiFunc.mockResolvedValue('test-result');
|
|
452
|
+
// 先调用 API 产生缓存
|
|
453
|
+
await cacheApi.call('param1', 123);
|
|
454
|
+
expect(cacheApi.value).toBe('test-result');
|
|
455
|
+
expect(cacheApi.empty).toBe(false);
|
|
456
|
+
// 重置
|
|
457
|
+
await cacheApi.reset();
|
|
458
|
+
// 验证缓存被清除
|
|
459
|
+
expect(cacheApi.value).toBeUndefined();
|
|
460
|
+
expect(cacheApi.empty).toBe(true);
|
|
461
|
+
});
|
|
462
|
+
it('应该清除 lastArgs 参数记录', async () => {
|
|
463
|
+
mockApiFunc.mockResolvedValue('test-result');
|
|
464
|
+
// 先调用 API
|
|
465
|
+
await cacheApi.call('param1', 123);
|
|
466
|
+
// 重置
|
|
467
|
+
await cacheApi.reset();
|
|
468
|
+
// 尝试刷新应该抛出错误,因为 lastArgs 已被清除
|
|
469
|
+
await expect(cacheApi.refresh()).rejects.toThrow('没有可用的参数来刷新数据,请先调用 call 方法');
|
|
470
|
+
});
|
|
471
|
+
it('应该在没有任何调用时也能正常重置', async () => {
|
|
472
|
+
// 直接重置,不应该抛出错误
|
|
473
|
+
await expect(cacheApi.reset()).resolves.toBeUndefined();
|
|
474
|
+
// 验证状态
|
|
475
|
+
expect(cacheApi.value).toBeUndefined();
|
|
476
|
+
expect(cacheApi.empty).toBe(true);
|
|
477
|
+
expect(cacheApi.loading).toBe(false);
|
|
478
|
+
});
|
|
479
|
+
it('应该等待所有进行中的请求完成后再重置', async () => {
|
|
480
|
+
let resolveFn;
|
|
481
|
+
const promise = new Promise((resolve) => {
|
|
482
|
+
resolveFn = resolve;
|
|
483
|
+
});
|
|
484
|
+
mockApiFunc.mockReturnValue(promise);
|
|
485
|
+
// 启动一个异步请求
|
|
486
|
+
const callPromise = cacheApi.call('test', 1);
|
|
487
|
+
expect(cacheApi.loading).toBe(true);
|
|
488
|
+
// 开始重置(应该等待请求完成)
|
|
489
|
+
const resetPromise = cacheApi.reset();
|
|
490
|
+
// 此时请求还在进行中,缓存应该还有值
|
|
491
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
492
|
+
// 完成请求
|
|
493
|
+
resolveFn('result');
|
|
494
|
+
await callPromise;
|
|
495
|
+
// 等待重置完成
|
|
496
|
+
await resetPromise;
|
|
497
|
+
// 验证重置后的状态
|
|
498
|
+
expect(cacheApi.value).toBeUndefined();
|
|
499
|
+
expect(cacheApi.empty).toBe(true);
|
|
500
|
+
expect(cacheApi.loading).toBe(false);
|
|
501
|
+
});
|
|
502
|
+
it('应该等待多个并发请求都完成后再重置', async () => {
|
|
503
|
+
const resolvers = [];
|
|
504
|
+
mockApiFunc.mockImplementation((param) => {
|
|
505
|
+
return new Promise((resolve) => {
|
|
506
|
+
resolvers.push((value) => resolve(`${param}-${value}`));
|
|
507
|
+
});
|
|
508
|
+
});
|
|
509
|
+
// 启动三个并发请求
|
|
510
|
+
const promise1 = cacheApi.call('req1', 1);
|
|
511
|
+
const promise2 = cacheApi.call('req2', 2);
|
|
512
|
+
const promise3 = cacheApi.call('req3', 3);
|
|
513
|
+
expect(cacheApi.loading).toBe(true);
|
|
514
|
+
// 开始重置
|
|
515
|
+
const resetPromise = cacheApi.reset();
|
|
516
|
+
// 等待一下
|
|
517
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
518
|
+
// 依次完成所有请求
|
|
519
|
+
resolvers[0]('result1');
|
|
520
|
+
await promise1;
|
|
521
|
+
resolvers[1]('result2');
|
|
522
|
+
await promise2;
|
|
523
|
+
resolvers[2]('result3');
|
|
524
|
+
await promise3;
|
|
525
|
+
// 等待重置完成
|
|
526
|
+
await resetPromise;
|
|
527
|
+
// 验证重置后的状态
|
|
528
|
+
expect(cacheApi.value).toBeUndefined();
|
|
529
|
+
expect(cacheApi.empty).toBe(true);
|
|
530
|
+
expect(cacheApi.loading).toBe(false);
|
|
531
|
+
});
|
|
532
|
+
it('应该在请求失败后也能正常重置', async () => {
|
|
533
|
+
const error = new Error('API Error');
|
|
534
|
+
mockApiFunc.mockRejectedValue(error);
|
|
535
|
+
// 调用失败的请求
|
|
536
|
+
try {
|
|
537
|
+
await cacheApi.call('test', 1);
|
|
538
|
+
}
|
|
539
|
+
catch (e) {
|
|
540
|
+
// 忽略错误
|
|
541
|
+
}
|
|
542
|
+
// 重置应该成功
|
|
543
|
+
await expect(cacheApi.reset()).resolves.toBeUndefined();
|
|
544
|
+
// 验证状态
|
|
545
|
+
expect(cacheApi.value).toBeUndefined();
|
|
546
|
+
expect(cacheApi.empty).toBe(true);
|
|
547
|
+
expect(cacheApi.loading).toBe(false);
|
|
548
|
+
});
|
|
549
|
+
it('重置后应该能够正常调用新的请求', async () => {
|
|
550
|
+
mockApiFunc.mockResolvedValueOnce('first-result');
|
|
551
|
+
mockApiFunc.mockResolvedValueOnce('second-result');
|
|
552
|
+
// 第一次调用
|
|
553
|
+
await cacheApi.call('param1', 123);
|
|
554
|
+
expect(cacheApi.value).toBe('first-result');
|
|
555
|
+
// 重置
|
|
556
|
+
await cacheApi.reset();
|
|
557
|
+
expect(cacheApi.value).toBeUndefined();
|
|
558
|
+
// 重置后再次调用
|
|
559
|
+
await cacheApi.call('param2', 456);
|
|
560
|
+
expect(cacheApi.value).toBe('second-result');
|
|
561
|
+
expect(cacheApi.empty).toBe(false);
|
|
562
|
+
});
|
|
563
|
+
it('重置后 refresh 应该需要先调用 call', async () => {
|
|
564
|
+
mockApiFunc.mockResolvedValue('test-result');
|
|
565
|
+
// 先调用并刷新
|
|
566
|
+
await cacheApi.call('test', 1);
|
|
567
|
+
await cacheApi.refresh();
|
|
568
|
+
// 重置
|
|
569
|
+
await cacheApi.reset();
|
|
570
|
+
// 重置后 refresh 应该抛出错误
|
|
571
|
+
await expect(cacheApi.refresh()).rejects.toThrow('没有可用的参数来刷新数据,请先调用 call 方法');
|
|
572
|
+
});
|
|
573
|
+
it('应该正确处理重置期间的 loading 状态', async () => {
|
|
574
|
+
let resolveFn;
|
|
575
|
+
const promise = new Promise((resolve) => {
|
|
576
|
+
resolveFn = resolve;
|
|
577
|
+
});
|
|
578
|
+
mockApiFunc.mockReturnValue(promise);
|
|
579
|
+
// 启动请求
|
|
580
|
+
const callPromise = cacheApi.call('test', 1);
|
|
581
|
+
expect(cacheApi.loading).toBe(true);
|
|
582
|
+
// 启动重置(会等待请求完成)
|
|
583
|
+
const resetPromise = cacheApi.reset();
|
|
584
|
+
// loading 应该还是 true
|
|
585
|
+
expect(cacheApi.loading).toBe(true);
|
|
586
|
+
// 完成请求
|
|
587
|
+
resolveFn('result');
|
|
588
|
+
await callPromise;
|
|
589
|
+
// loading 变为 false
|
|
590
|
+
expect(cacheApi.loading).toBe(false);
|
|
591
|
+
// 等待重置完成
|
|
592
|
+
await resetPromise;
|
|
593
|
+
// 重置后 loading 应该仍然是 false
|
|
594
|
+
expect(cacheApi.loading).toBe(false);
|
|
595
|
+
});
|
|
596
|
+
it('应该清除所有缓存的 key', async () => {
|
|
597
|
+
mockApiFunc.mockResolvedValue('result');
|
|
598
|
+
// 调用多次产生多个缓存
|
|
599
|
+
await cacheApi.call('param1', 1);
|
|
600
|
+
await cacheApi.call('param2', 2);
|
|
601
|
+
await cacheApi.call('param3', 3);
|
|
602
|
+
expect(cacheApi.empty).toBe(false);
|
|
603
|
+
// 重置
|
|
604
|
+
await cacheApi.reset();
|
|
605
|
+
// 验证所有缓存都被清除
|
|
606
|
+
expect(cacheApi.empty).toBe(true);
|
|
607
|
+
expect(cacheApi.value).toBeUndefined();
|
|
608
|
+
});
|
|
609
|
+
});
|
|
610
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../source/services/cache-api/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CacheApi } from './cache-api.js';
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Service } from '@taicode/common-base/service';
|
|
2
|
+
/**
|
|
3
|
+
* 带 TTL 缓存和 in-flight 请求去重的数据获取基类
|
|
4
|
+
*
|
|
5
|
+
* 子类通过实现 `fetch` 方法定义数据获取逻辑,基类负责:
|
|
6
|
+
* - 基于 cache key 的 TTL 缓存
|
|
7
|
+
* - 相同 key 的并发请求去重
|
|
8
|
+
* - observable 状态管理
|
|
9
|
+
*
|
|
10
|
+
* @typeParam TData - 缓存的数据类型
|
|
11
|
+
* @typeParam TParams - 查询参数类型(用于生成 cache key)
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* class UserService extends CachedFetchService<User[], QueryParams> {
|
|
16
|
+
* protected async fetch(params: QueryParams) {
|
|
17
|
+
* const result = await api.queryUsers(params)
|
|
18
|
+
* return result.data.list
|
|
19
|
+
* }
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare abstract class CachedFetchService<TData, TParams = void> extends Service {
|
|
24
|
+
protected cacheTTLMs: number;
|
|
25
|
+
accessor inited: boolean;
|
|
26
|
+
accessor loading: boolean;
|
|
27
|
+
accessor data: TData | undefined;
|
|
28
|
+
private cache;
|
|
29
|
+
private inflight;
|
|
30
|
+
private lastKey;
|
|
31
|
+
/**
|
|
32
|
+
* 子类实现:实际的数据获取逻辑
|
|
33
|
+
*/
|
|
34
|
+
protected abstract fetch(params: TParams): Promise<TData>;
|
|
35
|
+
/**
|
|
36
|
+
* 生成缓存 key,默认使用 JSON.stringify
|
|
37
|
+
*/
|
|
38
|
+
protected cacheKey(params: TParams): string;
|
|
39
|
+
private now;
|
|
40
|
+
/**
|
|
41
|
+
* 执行查询(带缓存和去重)
|
|
42
|
+
*/
|
|
43
|
+
query(params: TParams): Promise<TData>;
|
|
44
|
+
/**
|
|
45
|
+
* 清除缓存
|
|
46
|
+
*/
|
|
47
|
+
clearCache(): void;
|
|
48
|
+
init(): Promise<boolean>;
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../source/services/cached-fetch/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,8BAA8B,CAAA;AAOtD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,8BAAsB,kBAAkB,CACtC,KAAK,EACL,OAAO,GAAG,IAAI,CACd,SAAQ,OAAO;IACf,SAAS,CAAC,UAAU,SAAQ;IAE5B,SACgB,MAAM,UAAQ;IAE9B,SACgB,OAAO,UAAQ;IAE/B,SACgB,IAAI,EAAE,KAAK,GAAG,SAAS,CAAA;IAEvC,OAAO,CAAC,KAAK,CAAyC;IACtD,OAAO,CAAC,QAAQ,CAAoC;IACpD,OAAO,CAAC,OAAO,CAAsB;IAErC;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC;IAEzD;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM;IAI3C,OAAO,CAAC,GAAG;IAIX;;OAEG;IAEU,KAAK,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC;IAoDnD;;OAEG;IAEI,UAAU;IAMJ,IAAI,IAAI,OAAO,CAAC,OAAO,CAAC;CAMtC"}
|