Skip to content

Commit

Permalink
test(case): add circular ref case
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Feb 5, 2023
1 parent a24f46d commit 18042a7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
35 changes: 35 additions & 0 deletions test/immer-non-support.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,38 @@ test('Unexpected access to getter property in irrelevant plain objects', () => {
expect(isAgeGetterCalled).toBe(false);
}
});

test('circular reference', () => {
{
const data = { a: { b: { c: 1 } } };
// @ts-expect-error
data.a.b.c1 = data.a.b;

setAutoFreeze(true);

expect(() => {
produce(data, () => {
//
});
}).not.toThrowError();
}

{
const data = { a: { b: { c: 1 } } };
// @ts-expect-error
data.a.b.c1 = data.a.b;
expect(() => {
create(
data,
(draft) => {
//
},
{
enableAutoFreeze: true,
}
);
}).toThrowErrorMatchingInlineSnapshot(
`"Forbids circular reference: ~/a/b"`
);
}
});
7 changes: 4 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2558,9 +2558,10 @@ test('circular reference - set - 2 - 1', () => {

test('circular reference - map - 1', () => {
const base = { a: { b: { c: 1 } } };
const key = Symbol(1);
const data = new Map([
[null, null],
[1, base],
[key, base],
]);
// @ts-ignore
base.a.b.c1 = base.a.b;
Expand All @@ -2569,14 +2570,14 @@ test('circular reference - map - 1', () => {
data,
(draft) => {
// @ts-expect-error
draft.get(1).a.b.c = 2;
draft.get(key).a.b.c = 2;
},
{
enableAutoFreeze: true,
}
);
}).toThrowErrorMatchingInlineSnapshot(
`"Forbids circular reference: ~/1/a/b"`
`"Forbids circular reference: ~/[Symbol(1)]/a/b"`
);
});

Expand Down

0 comments on commit 18042a7

Please sign in to comment.