Skip to content

Commit

Permalink
add test with object cycles
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Nov 21, 2024
1 parent 1c0f138 commit 4df625b
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/17_cycles.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, expect, it } from 'vitest';
import { createProxy, isChanged } from 'proxy-compare';

const noop = (_arg: unknown) => {
// do nothing
};

describe('object with cycles', () => {
interface S {
a: string;
s?: S;
}

it('without cache', () => {
const s1: S = { a: 'a' };
s1.s = s1;
const s2: S = { a: 'a' };
s2.s = s2;
const a1 = new WeakMap();
const p1 = createProxy(s1, a1);
noop(p1.s?.a);
expect(() => isChanged(s1, s2, a1)).toThrow();
});

it('with cache', () => {
const s1: S = { a: 'a' };
s1.s = s1;
const s2: S = { a: 'a' };
s2.s = s2;
const a1 = new WeakMap();
const p1 = createProxy(s1, a1);
noop(p1.s?.a);
expect(isChanged(s1, s2, a1, new WeakMap())).toBe(false);
});

it('with cache with a change', () => {
const s1: S = { a: 'a' };
s1.s = s1;
const s2: S = { a: 'aa' };
s2.s = s2;
const a1 = new WeakMap();
const p1 = createProxy(s1, a1);
noop(p1.s?.a);
expect(isChanged(s1, s2, a1, new WeakMap())).toBe(true);
});
});

0 comments on commit 4df625b

Please sign in to comment.