Repository navigation
Expand file tree
/
Copy pathutils.node.test.ts
More file actions
98 lines (83 loc) · 3.39 KB
/
Copy pathutils.node.test.ts
File metadata and controls
98 lines (83 loc) · 3.39 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
import { describe, expect, it, vi } from "vitest";
import {
createEventInvoker,
isIgnorableWatchChange,
isPlainObject,
isValidArrayIndex,
parseOnEvent,
} from "../src/utils";
describe("utils", () => {
describe("createEventInvoker", () => {
it("uses a stable handler snapshot for each dispatch", () => {
const lateHandler = vi.fn();
const stableHandler = vi.fn();
const handlers: Array<() => void> = [];
handlers.push(() => handlers.splice(1, 1, lateHandler), stableHandler);
const invoke = createEventInvoker(handlers);
if (!invoke) {
throw new Error("Expected an array event invoker.");
}
invoke();
expect(stableHandler).toHaveBeenCalledTimes(1);
expect(lateHandler).not.toHaveBeenCalled();
invoke();
expect(stableHandler).toHaveBeenCalledTimes(1);
expect(lateHandler).toHaveBeenCalledTimes(1);
});
});
describe("parseOnEvent", () => {
it("extracts event name and once flag", () => {
expect(parseOnEvent("onClick")).toEqual({ event: "click", once: false });
expect(parseOnEvent("onZr:click")).toEqual({ event: "zr:click", once: false });
expect(parseOnEvent("onClickOnce")).toEqual({ event: "click", once: true });
expect(parseOnEvent("onNative:clickOnce")).toEqual({ event: "native:click", once: true });
});
it("returns null for non-event attrs", () => {
expect(parseOnEvent("onclick")).toBeNull();
expect(parseOnEvent("on")).toBeNull();
expect(parseOnEvent("foo")).toBeNull();
});
});
describe("isValidArrayIndex", () => {
it("accepts non-negative integer strings", () => {
expect(isValidArrayIndex("0")).toBe(true);
expect(isValidArrayIndex("42")).toBe(true);
expect(isValidArrayIndex("4294967294")).toBe(true);
expect(isValidArrayIndex(" 1")).toBe(false);
});
it("rejects invalid inputs", () => {
expect(isValidArrayIndex("-1")).toBe(false);
expect(isValidArrayIndex("3.14")).toBe(false);
expect(isValidArrayIndex("1e3")).toBe(false);
expect(isValidArrayIndex("foo")).toBe(false);
});
});
describe("isPlainObject", () => {
it("accepts plain objects", () => {
expect(isPlainObject({ foo: "bar" })).toBe(true);
expect(isPlainObject(Object.create(null))).toBe(true);
});
it("rejects arrays, class instances, and primitives", () => {
expect(isPlainObject([])).toBe(false);
expect(isPlainObject(new Date())).toBe(false);
expect(isPlainObject(new (class {})())).toBe(false);
expect(isPlainObject(() => ({ foo: "bar" }))).toBe(false);
expect(isPlainObject(null)).toBe(false);
expect(isPlainObject("foo")).toBe(false);
});
});
describe("isIgnorableWatchChange", () => {
it("ignores stable scalars and shallow-equivalent replacements", () => {
const nested = {};
expect(isIgnorableWatchChange(undefined, undefined)).toBe(true);
expect(isIgnorableWatchChange("dark", "dark")).toBe(true);
expect(isIgnorableWatchChange({ nested }, { nested })).toBe(true);
});
it("preserves deep mutations and meaningful replacements", () => {
const options = {};
expect(isIgnorableWatchChange(options, options)).toBe(false);
expect(isIgnorableWatchChange({ nested: {} }, { nested: {} })).toBe(false);
expect(isIgnorableWatchChange({ renderer: "svg" }, { renderer: "canvas" })).toBe(false);
});
});
});