Skip to content

Commit 4df625b

Browse files
committed
add test with object cycles
1 parent 1c0f138 commit 4df625b

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

tests/17_cycles.spec.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { createProxy, isChanged } from 'proxy-compare';
3+
4+
const noop = (_arg: unknown) => {
5+
// do nothing
6+
};
7+
8+
describe('object with cycles', () => {
9+
interface S {
10+
a: string;
11+
s?: S;
12+
}
13+
14+
it('without cache', () => {
15+
const s1: S = { a: 'a' };
16+
s1.s = s1;
17+
const s2: S = { a: 'a' };
18+
s2.s = s2;
19+
const a1 = new WeakMap();
20+
const p1 = createProxy(s1, a1);
21+
noop(p1.s?.a);
22+
expect(() => isChanged(s1, s2, a1)).toThrow();
23+
});
24+
25+
it('with cache', () => {
26+
const s1: S = { a: 'a' };
27+
s1.s = s1;
28+
const s2: S = { a: 'a' };
29+
s2.s = s2;
30+
const a1 = new WeakMap();
31+
const p1 = createProxy(s1, a1);
32+
noop(p1.s?.a);
33+
expect(isChanged(s1, s2, a1, new WeakMap())).toBe(false);
34+
});
35+
36+
it('with cache with a change', () => {
37+
const s1: S = { a: 'a' };
38+
s1.s = s1;
39+
const s2: S = { a: 'aa' };
40+
s2.s = s2;
41+
const a1 = new WeakMap();
42+
const p1 = createProxy(s1, a1);
43+
noop(p1.s?.a);
44+
expect(isChanged(s1, s2, a1, new WeakMap())).toBe(true);
45+
});
46+
});

0 commit comments

Comments
 (0)