-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathweakRef.test.ts
More file actions
178 lines (144 loc) · 5.46 KB
/
weakRef.test.ts
File metadata and controls
178 lines (144 loc) · 5.46 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
import { describe, expect, it, vi } from 'vitest';
import { derefWeakRef, makeWeakRef, type MaybeWeakRef } from '../../../src/utils/weakRef';
describe('Unit | util | weakRef', () => {
describe('makeWeakRef', () => {
it('creates a WeakRef when available', () => {
const obj = { foo: 'bar' };
const ref = makeWeakRef(obj);
// Should be a WeakRef, not the direct object
expect(ref).toBeInstanceOf(WeakRef);
expect((ref as WeakRef<typeof obj>).deref()).toBe(obj);
});
it('returns the object directly when WeakRef is not available', () => {
const originalWeakRef = globalThis.WeakRef;
(globalThis as any).WeakRef = undefined;
try {
const obj = { foo: 'bar' };
const ref = makeWeakRef(obj);
// Should be the direct object
expect(ref).toBe(obj);
} finally {
(globalThis as any).WeakRef = originalWeakRef;
}
});
it('returns the object directly when WeakRef constructor throws', () => {
const originalWeakRef = globalThis.WeakRef;
(globalThis as any).WeakRef = function () {
throw new Error('WeakRef not supported');
};
try {
const obj = { foo: 'bar' };
const ref = makeWeakRef(obj);
// Should fall back to the direct object
expect(ref).toBe(obj);
} finally {
(globalThis as any).WeakRef = originalWeakRef;
}
});
it('works with different object types', () => {
const plainObject = { key: 'value' };
const array = [1, 2, 3];
const func = () => 'test';
const date = new Date();
expect(derefWeakRef(makeWeakRef(plainObject))).toBe(plainObject);
expect(derefWeakRef(makeWeakRef(array))).toBe(array);
expect(derefWeakRef(makeWeakRef(func))).toBe(func);
expect(derefWeakRef(makeWeakRef(date))).toBe(date);
});
});
describe('derefWeakRef', () => {
it('returns undefined for undefined input', () => {
expect(derefWeakRef(undefined)).toBeUndefined();
});
it('correctly dereferences a WeakRef', () => {
const obj = { foo: 'bar' };
const weakRef = new WeakRef(obj);
expect(derefWeakRef(weakRef)).toBe(obj);
});
it('returns the direct object when not a WeakRef', () => {
const obj = { foo: 'bar' };
// Passing a direct object (fallback case)
expect(derefWeakRef(obj as MaybeWeakRef<typeof obj>)).toBe(obj);
});
it('returns undefined when WeakRef.deref() returns undefined (simulating GC)', () => {
const mockWeakRef = {
deref: vi.fn().mockReturnValue(undefined),
};
expect(derefWeakRef(mockWeakRef as MaybeWeakRef<object>)).toBeUndefined();
expect(mockWeakRef.deref).toHaveBeenCalled();
});
it('returns undefined when WeakRef.deref() throws an error', () => {
const mockWeakRef = {
deref: vi.fn().mockImplementation(() => {
throw new Error('deref failed');
}),
};
expect(derefWeakRef(mockWeakRef as MaybeWeakRef<object>)).toBeUndefined();
expect(mockWeakRef.deref).toHaveBeenCalled();
});
it('handles objects with a non-function deref property', () => {
const objWithDerefProperty = {
deref: 'not a function',
actualData: 'test',
};
// Should treat it as a direct object since deref is not a function
expect(derefWeakRef(objWithDerefProperty as unknown as MaybeWeakRef<object>)).toBe(objWithDerefProperty);
});
});
describe('roundtrip (makeWeakRef + derefWeakRef)', () => {
it('preserves object identity with WeakRef available', () => {
const obj = { nested: { data: [1, 2, 3] } };
const ref = makeWeakRef(obj);
const retrieved = derefWeakRef(ref);
expect(retrieved).toBe(obj);
expect(retrieved?.nested.data).toEqual([1, 2, 3]);
});
it('preserves object identity with WeakRef unavailable', () => {
const originalWeakRef = globalThis.WeakRef;
(globalThis as any).WeakRef = undefined;
try {
const obj = { nested: { data: [1, 2, 3] } };
const ref = makeWeakRef(obj);
const retrieved = derefWeakRef(ref);
expect(retrieved).toBe(obj);
expect(retrieved?.nested.data).toEqual([1, 2, 3]);
} finally {
(globalThis as any).WeakRef = originalWeakRef;
}
});
it('allows multiple refs to the same object', () => {
const obj = { id: 'shared' };
const ref1 = makeWeakRef(obj);
const ref2 = makeWeakRef(obj);
expect(derefWeakRef(ref1)).toBe(obj);
expect(derefWeakRef(ref2)).toBe(obj);
expect(derefWeakRef(ref1)).toBe(derefWeakRef(ref2));
});
});
describe('type safety', () => {
it('preserves generic type information', () => {
interface TestInterface {
id: number;
name: string;
}
const obj: TestInterface = { id: 1, name: 'test' };
const ref: MaybeWeakRef<TestInterface> = makeWeakRef(obj);
const retrieved: TestInterface | undefined = derefWeakRef(ref);
expect(retrieved?.id).toBe(1);
expect(retrieved?.name).toBe('test');
});
it('works with class instances', () => {
class TestClass {
constructor(public value: string) {}
getValue(): string {
return this.value;
}
}
const instance = new TestClass('hello');
const ref = makeWeakRef(instance);
const retrieved = derefWeakRef(ref);
expect(retrieved).toBeInstanceOf(TestClass);
expect(retrieved?.getValue()).toBe('hello');
});
});
});