|
1 | | -import {produce, setUseStrictShallowCopy} from "../src/immer" |
| 1 | +import { |
| 2 | + immerable, |
| 3 | + produce, |
| 4 | + setUseStrictShallowCopy, |
| 5 | + setAutoFreeze, |
| 6 | + StrictMode |
| 7 | +} from "../src/immer" |
2 | 8 |
|
3 | | -describe("setUseStrictShallowCopy(true)", () => { |
4 | | - test("keep descriptors", () => { |
5 | | - setUseStrictShallowCopy(true) |
| 9 | +describe.each([true, false, "class_only" as const])( |
| 10 | + "setUseStrictShallowCopy(true)", |
| 11 | + (strictMode: StrictMode) => { |
| 12 | + test("keep descriptors, mode: " + strictMode, () => { |
| 13 | + setUseStrictShallowCopy(strictMode) |
6 | 14 |
|
7 | | - const base: Record<string, unknown> = {} |
8 | | - Object.defineProperty(base, "foo", { |
9 | | - value: "foo", |
10 | | - writable: false, |
11 | | - configurable: false |
| 15 | + const base: Record<string, unknown> = {} |
| 16 | + Object.defineProperty(base, "foo", { |
| 17 | + value: "foo", |
| 18 | + writable: false, |
| 19 | + configurable: false |
| 20 | + }) |
| 21 | + const copy = produce(base, (draft: any) => { |
| 22 | + draft.bar = "bar" |
| 23 | + }) |
| 24 | + if (strictMode === true) { |
| 25 | + expect(Object.getOwnPropertyDescriptor(copy, "foo")).toStrictEqual( |
| 26 | + Object.getOwnPropertyDescriptor(base, "foo") |
| 27 | + ) |
| 28 | + } else { |
| 29 | + expect(Object.getOwnPropertyDescriptor(copy, "foo")).toBeUndefined() |
| 30 | + } |
12 | 31 | }) |
13 | | - const copy = produce(base, (draft: any) => { |
14 | | - draft.bar = "bar" |
15 | | - }) |
16 | | - expect(Object.getOwnPropertyDescriptor(copy, "foo")).toStrictEqual( |
17 | | - Object.getOwnPropertyDescriptor(base, "foo") |
18 | | - ) |
19 | | - }) |
20 | | -}) |
21 | | - |
22 | | -describe("setUseStrictShallowCopy(false)", () => { |
23 | | - test("ignore descriptors", () => { |
24 | | - setUseStrictShallowCopy(false) |
25 | | - |
26 | | - const base: Record<string, unknown> = {} |
27 | | - Object.defineProperty(base, "foo", { |
28 | | - value: "foo", |
29 | | - writable: false, |
30 | | - configurable: false |
31 | | - }) |
32 | | - const copy = produce(base, (draft: any) => { |
33 | | - draft.bar = "bar" |
| 32 | + |
| 33 | + test("keep non-enumerable class descriptors, mode: " + strictMode, () => { |
| 34 | + setUseStrictShallowCopy(strictMode) |
| 35 | + setAutoFreeze(false) |
| 36 | + |
| 37 | + class X { |
| 38 | + [immerable] = true |
| 39 | + foo = "foo" |
| 40 | + bar!: string |
| 41 | + constructor() { |
| 42 | + Object.defineProperty(this, "bar", { |
| 43 | + get() { |
| 44 | + return this.foo + "bar" |
| 45 | + }, |
| 46 | + configurable: false, |
| 47 | + enumerable: false |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + get baz() { |
| 52 | + return this.foo + "baz" |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + const copy = produce(new X(), (draft: any) => { |
| 57 | + draft.foo = "FOO" |
| 58 | + }) |
| 59 | + |
| 60 | + const strict = strictMode === true || strictMode === "class_only" |
| 61 | + |
| 62 | + // descriptors on the prototype are unaffected, so this is still a getter |
| 63 | + expect(copy.baz).toBe("FOObaz") |
| 64 | + // descriptors on the instance are found, even when non-enumerable, and read during copy |
| 65 | + // so new values won't be reflected |
| 66 | + expect(copy.bar).toBe(strict ? "foobar" : undefined) |
| 67 | + |
| 68 | + copy.foo = "fluff" |
| 69 | + // not updated, the own prop became a value |
| 70 | + expect(copy.bar).toBe(strict ? "foobar" : undefined) |
| 71 | + // updated, it is still a getter |
| 72 | + expect(copy.baz).toBe("fluffbaz") |
34 | 73 | }) |
35 | | - expect(Object.getOwnPropertyDescriptor(copy, "foo")).toBeUndefined() |
36 | | - }) |
37 | | -}) |
| 74 | + } |
| 75 | +) |
0 commit comments