-
-
Notifications
You must be signed in to change notification settings - Fork 851
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
class_only
strict mode setting
- Loading branch information
1 parent
7f1b3b9
commit 85a8f7b
Showing
4 changed files
with
110 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,74 @@ | ||
import {produce, setUseStrictShallowCopy} from "../src/immer" | ||
import { | ||
immerable, | ||
produce, | ||
setUseStrictShallowCopy, | ||
setAutoFreeze | ||
} from "../src/immer" | ||
|
||
describe("setUseStrictShallowCopy(true)", () => { | ||
test("keep descriptors", () => { | ||
setUseStrictShallowCopy(true) | ||
describe.each([true, false, "class_only" as const])( | ||
"setUseStrictShallowCopy(true)", | ||
(strictMode: boolean | "class_only") => { | ||
test("keep descriptors, mode: " + strictMode, () => { | ||
setUseStrictShallowCopy(strictMode) | ||
|
||
const base: Record<string, unknown> = {} | ||
Object.defineProperty(base, "foo", { | ||
value: "foo", | ||
writable: false, | ||
configurable: false | ||
const base: Record<string, unknown> = {} | ||
Object.defineProperty(base, "foo", { | ||
value: "foo", | ||
writable: false, | ||
configurable: false | ||
}) | ||
const copy = produce(base, (draft: any) => { | ||
draft.bar = "bar" | ||
}) | ||
if (strictMode === true) { | ||
expect(Object.getOwnPropertyDescriptor(copy, "foo")).toStrictEqual( | ||
Object.getOwnPropertyDescriptor(base, "foo") | ||
) | ||
} else { | ||
expect(Object.getOwnPropertyDescriptor(copy, "foo")).toBeUndefined() | ||
} | ||
}) | ||
const copy = produce(base, (draft: any) => { | ||
draft.bar = "bar" | ||
}) | ||
expect(Object.getOwnPropertyDescriptor(copy, "foo")).toStrictEqual( | ||
Object.getOwnPropertyDescriptor(base, "foo") | ||
) | ||
}) | ||
}) | ||
|
||
describe("setUseStrictShallowCopy(false)", () => { | ||
test("ignore descriptors", () => { | ||
setUseStrictShallowCopy(false) | ||
|
||
const base: Record<string, unknown> = {} | ||
Object.defineProperty(base, "foo", { | ||
value: "foo", | ||
writable: false, | ||
configurable: false | ||
}) | ||
const copy = produce(base, (draft: any) => { | ||
draft.bar = "bar" | ||
|
||
test("keep non-enumerable class descriptors, mode: " + strictMode, () => { | ||
setUseStrictShallowCopy(strictMode) | ||
setAutoFreeze(false) | ||
|
||
class X { | ||
[immerable] = true | ||
foo = "foo" | ||
bar!: string | ||
constructor() { | ||
Object.defineProperty(this, "bar", { | ||
get() { | ||
return this.foo + "bar" | ||
}, | ||
configurable: false, | ||
enumerable: false | ||
}) | ||
} | ||
|
||
get baz() { | ||
return this.foo + "baz" | ||
} | ||
} | ||
|
||
const copy = produce(new X(), (draft: any) => { | ||
draft.foo = "FOO" | ||
}) | ||
|
||
const strict = strictMode === true || strictMode === "class_only" | ||
|
||
// descriptors on the prototype are unaffected, so this is still a getter | ||
expect(copy.baz).toBe("FOObaz") | ||
// descriptors on the instance are found, even when non-enumerable, and read during copy | ||
// so new values won't be reflected | ||
expect(copy.bar).toBe(strict ? "foobar" : undefined) | ||
|
||
copy.foo = "fluff" | ||
// not updated, the own prop became a value | ||
expect(copy.bar).toBe(strict ? "foobar" : undefined) | ||
// updated, it is still a getter | ||
expect(copy.baz).toBe("fluffbaz") | ||
}) | ||
expect(Object.getOwnPropertyDescriptor(copy, "foo")).toBeUndefined() | ||
}) | ||
}) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters