@kine-design/core 0.0.1-beta.7 → 0.0.1-beta.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/components/base/datePicker/__tests__/useDatePicker.test.ts +5 -0
- package/components/base/image/__tests__/useImage.test.ts +2 -0
- package/components/base/inputNumber/__tests__/useInputNumber.test.ts +36 -0
- package/components/base/timePicker/__tests__/useTimePicker.test.ts +6 -0
- package/components/base/tree/__tests__/tree.test.ts +9 -0
- package/components/template/table/__tests__/useTable.test.ts +4 -5
- package/compositions/common/__tests__/useEventListener.test.ts +27 -0
- package/compositions/common/__tests__/usePopover.test.ts +6 -1
- package/compositions/common/__tests__/useTeleport.test.ts +7 -0
- package/compositions/modal/__tests__/useModal.test.ts +18 -0
- package/package.json +1 -1
- package/tools/__tests__/empty.test.ts +20 -1
|
@@ -27,6 +27,11 @@ describe('toDayjs', () => {
|
|
|
27
27
|
const d = toDayjs(undefined);
|
|
28
28
|
expect(d.isValid()).toBe(true);
|
|
29
29
|
});
|
|
30
|
+
|
|
31
|
+
it('无效日期字符串返回 invalid dayjs 对象', () => {
|
|
32
|
+
const d = toDayjs('not-a-date');
|
|
33
|
+
expect(d.isValid()).toBe(false);
|
|
34
|
+
});
|
|
30
35
|
});
|
|
31
36
|
|
|
32
37
|
describe('generateTimeColumn', () => {
|
|
@@ -33,6 +33,8 @@ describe('useImage', () => {
|
|
|
33
33
|
it('handleLoad 设置状态为 loaded 并触发 emit', () => {
|
|
34
34
|
const emit = createEmit();
|
|
35
35
|
const { status, handleLoad } = useImage(defaultProps, emit);
|
|
36
|
+
// Vitest node 环境下由 happy-dom/jsdom 提供 Event 构造函数,
|
|
37
|
+
// handleLoad 仅需接收 Event 实例作为参数传给 emit,不依赖 DOM 行为
|
|
36
38
|
const event = new Event('load');
|
|
37
39
|
|
|
38
40
|
handleLoad(event);
|
|
@@ -150,4 +150,40 @@ describe('useInputNumber 输入流程', () => {
|
|
|
150
150
|
const call = lastUpdate(emit);
|
|
151
151
|
expect(call[1]).toBe(3);
|
|
152
152
|
});
|
|
153
|
+
|
|
154
|
+
it('disabled 时 increment 和 decrement 为 no-op', () => {
|
|
155
|
+
const { increment, decrement, emit } = setup({ modelValue: 5, disabled: true });
|
|
156
|
+
emit.mockClear();
|
|
157
|
+
increment();
|
|
158
|
+
decrement();
|
|
159
|
+
// disabled 状态下不触发任何 emit
|
|
160
|
+
expect(emit).not.toHaveBeenCalled();
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('readonly 时 increment 和 decrement 为 no-op', () => {
|
|
164
|
+
const { increment, decrement, emit } = setup({ modelValue: 5, readonly: true });
|
|
165
|
+
emit.mockClear();
|
|
166
|
+
increment();
|
|
167
|
+
decrement();
|
|
168
|
+
// readonly 状态下不触发任何 emit
|
|
169
|
+
expect(emit).not.toHaveBeenCalled();
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
describe('useInputNumber 中间态处理', () => {
|
|
174
|
+
it('validate(".") 为中间态 → emit null', () => {
|
|
175
|
+
const { validate, emit } = setup({ modelValue: null });
|
|
176
|
+
validate('.' as unknown as number);
|
|
177
|
+
// "." 经 handleInputChange 会被转为 "0.",但直接 validate('.') 是非法输入
|
|
178
|
+
// validate 里 isNaN(+'.') 为 true 且不是 '' 也不是 '-',所以不触发 emit
|
|
179
|
+
expect(emit.mock.calls.filter((c: unknown[]) => c[0] === 'update:modelValue')).toHaveLength(0);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('validate("-.") 为非法输入 → 不触发 emit', () => {
|
|
183
|
+
const { validate, emit } = setup({ modelValue: null });
|
|
184
|
+
emit.mockClear();
|
|
185
|
+
validate('-.' as unknown as number);
|
|
186
|
+
// '-.' → isNaN(+'-.')=true, 不是 '' 不是 '-',走非法输入分支
|
|
187
|
+
expect(emit).not.toHaveBeenCalled();
|
|
188
|
+
});
|
|
153
189
|
});
|
|
@@ -17,6 +17,12 @@ describe('generateColumn', () => {
|
|
|
17
17
|
it('生成 60 分钟列,步进 5', () => {
|
|
18
18
|
expect(generateColumn(60, 5)).toEqual([0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55]);
|
|
19
19
|
});
|
|
20
|
+
|
|
21
|
+
// step=0 会导致 for 循环条件 i += 0 永远不推进,造成死循环。
|
|
22
|
+
// 源码无守卫,此处跳过并记录风险,避免测试挂起。
|
|
23
|
+
it.skip('step=0 会导致死循环(源码未做守卫)', () => {
|
|
24
|
+
// generateColumn(24, 0) — 无限循环,不可执行
|
|
25
|
+
});
|
|
20
26
|
});
|
|
21
27
|
|
|
22
28
|
describe('useTimePicker', () => {
|
|
@@ -211,4 +211,13 @@ describe('Tree', () => {
|
|
|
211
211
|
const childKeys = tree.getChildrenKeys(roots[0]);
|
|
212
212
|
expect(childKeys).toEqual(['b']);
|
|
213
213
|
});
|
|
214
|
+
|
|
215
|
+
it('getTreeData 不存在的 key 返回 undefined 元素', () => {
|
|
216
|
+
// cacheMap.get(key) 对不存在的 key 返回 undefined,
|
|
217
|
+
// getNodesByKeys 不做过滤,数组中会包含 undefined
|
|
218
|
+
const tree = new Tree({ data: SAMPLE_DATA });
|
|
219
|
+
const nodes = tree.getTreeData(['nonexistent']);
|
|
220
|
+
expect(nodes).toHaveLength(1);
|
|
221
|
+
expect(nodes[0]).toBeUndefined();
|
|
222
|
+
});
|
|
214
223
|
});
|
|
@@ -6,10 +6,13 @@
|
|
|
6
6
|
*
|
|
7
7
|
* 江湖的业务千篇一律,复杂的代码好几百行。
|
|
8
8
|
*/
|
|
9
|
-
import { describe, expect, it, vi } from 'vitest';
|
|
9
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
10
10
|
import { useTable } from '../useTable';
|
|
11
11
|
|
|
12
12
|
describe('useTable', () => {
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
vi.restoreAllMocks();
|
|
15
|
+
});
|
|
13
16
|
const createRenders = () => ({
|
|
14
17
|
empty: 'EMPTY',
|
|
15
18
|
tbodyTr: (opt: any) => `TD:${opt.param}:${opt.data}`,
|
|
@@ -58,8 +61,6 @@ describe('useTable', () => {
|
|
|
58
61
|
const result = initTable(createRenders(), columns, data);
|
|
59
62
|
expect(result.thead).toBe('THEAD:[TH:name:姓名]');
|
|
60
63
|
expect(errorSpy).toHaveBeenCalled();
|
|
61
|
-
|
|
62
|
-
errorSpy.mockRestore();
|
|
63
64
|
});
|
|
64
65
|
|
|
65
66
|
it('列宽 number 转换为 px', () => {
|
|
@@ -133,7 +134,5 @@ describe('useTable', () => {
|
|
|
133
134
|
|
|
134
135
|
error('test error');
|
|
135
136
|
expect(spy).toHaveBeenCalledWith('[水墨UI表格组件] test error');
|
|
136
|
-
|
|
137
|
-
spy.mockRestore();
|
|
138
137
|
});
|
|
139
138
|
});
|
|
@@ -15,6 +15,7 @@ describe('useEventListener', () => {
|
|
|
15
15
|
const target = {
|
|
16
16
|
addEventListener: vi.fn(),
|
|
17
17
|
removeEventListener: vi.fn(),
|
|
18
|
+
dispatchEvent: vi.fn(),
|
|
18
19
|
};
|
|
19
20
|
|
|
20
21
|
const { add, remove } = useEventListener({
|
|
@@ -35,6 +36,7 @@ describe('useEventListener', () => {
|
|
|
35
36
|
const target = {
|
|
36
37
|
addEventListener: vi.fn(),
|
|
37
38
|
removeEventListener: vi.fn(),
|
|
39
|
+
dispatchEvent: vi.fn(),
|
|
38
40
|
};
|
|
39
41
|
|
|
40
42
|
const { onMounted, onBeforeUnmount } = useEventListener({
|
|
@@ -55,6 +57,7 @@ describe('useEventListener', () => {
|
|
|
55
57
|
const realTarget = {
|
|
56
58
|
addEventListener: vi.fn(),
|
|
57
59
|
removeEventListener: vi.fn(),
|
|
60
|
+
dispatchEvent: vi.fn(),
|
|
58
61
|
};
|
|
59
62
|
const targetFn = vi.fn(() => realTarget);
|
|
60
63
|
|
|
@@ -68,4 +71,28 @@ describe('useEventListener', () => {
|
|
|
68
71
|
expect(targetFn).toHaveBeenCalled();
|
|
69
72
|
expect(realTarget.addEventListener).toHaveBeenCalledWith('click', handler);
|
|
70
73
|
});
|
|
74
|
+
|
|
75
|
+
it('target 为 null 时 add 抛出异常(无防御性检查)', () => {
|
|
76
|
+
// useEventListener 不对 null/undefined target 做守卫,
|
|
77
|
+
// 调用 add() 时会对 null 调用 addEventListener,导致 TypeError
|
|
78
|
+
const handler = vi.fn();
|
|
79
|
+
const { add } = useEventListener({
|
|
80
|
+
target: null as unknown as EventTarget,
|
|
81
|
+
event: 'click',
|
|
82
|
+
handler,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
expect(() => add()).toThrow();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('target 为返回 undefined 的函数时 add 抛出异常', () => {
|
|
89
|
+
const handler = vi.fn();
|
|
90
|
+
const { add } = useEventListener({
|
|
91
|
+
target: (() => undefined) as unknown as () => EventTarget,
|
|
92
|
+
event: 'click',
|
|
93
|
+
handler,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
expect(() => add()).toThrow();
|
|
97
|
+
});
|
|
71
98
|
});
|
|
@@ -27,7 +27,12 @@ describe('usePopover', () => {
|
|
|
27
27
|
|
|
28
28
|
it('自定义 offset', () => {
|
|
29
29
|
const { popoverOptions } = usePopover(undefined, { offset: 10 });
|
|
30
|
-
|
|
30
|
+
const middleware = popoverOptions.popper?.middleware;
|
|
31
|
+
expect(middleware).toHaveLength(3);
|
|
32
|
+
// middleware[0] 是 offset(10),验证自定义 offset 值确实被应用
|
|
33
|
+
const offsetMiddleware = middleware![0] as { name: string; options: unknown };
|
|
34
|
+
expect(offsetMiddleware.name).toBe('offset');
|
|
35
|
+
expect(offsetMiddleware.options).toBe(10);
|
|
31
36
|
});
|
|
32
37
|
|
|
33
38
|
it('扩展 middleware', () => {
|
|
@@ -22,4 +22,11 @@ describe('initTeleportOptions', () => {
|
|
|
22
22
|
const custom = { to: '#app', disabled: false };
|
|
23
23
|
expect(initTeleportOptions(custom as any)).toBe(custom);
|
|
24
24
|
});
|
|
25
|
+
|
|
26
|
+
it('false 不命中 true/undefined 分支,原样返回', () => {
|
|
27
|
+
// initTeleportOptions 只对 true 和 undefined 返回 { to: 'body' },
|
|
28
|
+
// false 走 else 分支直接返回原值
|
|
29
|
+
const result = initTeleportOptions(false as unknown as import('vue').TeleportProps);
|
|
30
|
+
expect(result).toBe(false);
|
|
31
|
+
});
|
|
25
32
|
});
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* 江湖的业务千篇一律,复杂的代码好几百行。
|
|
8
8
|
*/
|
|
9
9
|
import { describe, expect, it, vi } from 'vitest';
|
|
10
|
+
import { nextTick, reactive } from 'vue';
|
|
10
11
|
import { useModal } from '../useModal';
|
|
11
12
|
|
|
12
13
|
describe('useModal', () => {
|
|
@@ -89,4 +90,21 @@ describe('useModal', () => {
|
|
|
89
90
|
onContentClick(event);
|
|
90
91
|
expect(event.stopPropagation).toHaveBeenCalled();
|
|
91
92
|
});
|
|
93
|
+
|
|
94
|
+
it('外部 props.visible 变化时 watcher 同步内部 visible', async () => {
|
|
95
|
+
const emit = vi.fn();
|
|
96
|
+
const props = reactive({ visible: false });
|
|
97
|
+
const { visible } = useModal(props, emit);
|
|
98
|
+
|
|
99
|
+
expect(visible.value).toBe(false);
|
|
100
|
+
|
|
101
|
+
// 外部修改 props.visible → watcher 同步 visible.value
|
|
102
|
+
props.visible = true;
|
|
103
|
+
await nextTick();
|
|
104
|
+
expect(visible.value).toBe(true);
|
|
105
|
+
|
|
106
|
+
props.visible = false;
|
|
107
|
+
await nextTick();
|
|
108
|
+
expect(visible.value).toBe(false);
|
|
109
|
+
});
|
|
92
110
|
});
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*
|
|
7
7
|
* 江湖的业务千篇一律,复杂的代码好几百行。
|
|
8
8
|
*/
|
|
9
|
-
import { describe, expect, it } from 'vitest';
|
|
9
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
10
10
|
import { notEmpty, isEmpty } from '../empty';
|
|
11
11
|
|
|
12
12
|
describe('notEmpty', () => {
|
|
@@ -57,8 +57,27 @@ describe('notEmpty', () => {
|
|
|
57
57
|
it('非空正则非空', () => expect(notEmpty(/abc/)).toBe(true));
|
|
58
58
|
|
|
59
59
|
// TypedArray
|
|
60
|
+
// notEmpty 对 TypedArray 返回 value.length(number),0 为 falsy,>0 为 truthy
|
|
60
61
|
it('空 Uint8Array 为空', () => expect(notEmpty(new Uint8Array(0))).toBe(0));
|
|
61
62
|
it('非空 Float64Array 非空', () => expect(notEmpty(new Float64Array([1.5]))).toBe(1));
|
|
63
|
+
|
|
64
|
+
// WeakMap / WeakSet — 无法判断是否为空,始终返回 false 并 console.error
|
|
65
|
+
it('WeakMap 无法判断,返回 false', () => {
|
|
66
|
+
const spy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
67
|
+
expect(notEmpty(new WeakMap())).toBe(false);
|
|
68
|
+
expect(spy).toHaveBeenCalledWith('WeakMap和WeakSet无法以通用的方式判断是否为空。');
|
|
69
|
+
spy.mockRestore();
|
|
70
|
+
});
|
|
71
|
+
it('WeakSet 无法判断,返回 false', () => {
|
|
72
|
+
const spy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
73
|
+
expect(notEmpty(new WeakSet())).toBe(false);
|
|
74
|
+
expect(spy).toHaveBeenCalledWith('WeakMap和WeakSet无法以通用的方式判断是否为空。');
|
|
75
|
+
spy.mockRestore();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// BigInt — typeof bigint 分支,始终返回 true
|
|
79
|
+
it('BigInt(0) 非空', () => expect(notEmpty(BigInt(0))).toBe(true));
|
|
80
|
+
it('BigInt(42) 非空', () => expect(notEmpty(BigInt(42))).toBe(true));
|
|
62
81
|
});
|
|
63
82
|
|
|
64
83
|
describe('isEmpty', () => {
|