wip
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent d2a47b6 · Browse files · 5 files·+74 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+74 added−15 removed
- tests:+66 added−9 removed
- other:+8 added−6 removed
Provenance
- Commit
f33d6b6- Tree
693cf8a- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
d2a47b6
5 files changed+74 added−15 removed
modified:__tests__/base.js+66 added-9 removed
18 unmodified lines19 test("immer should have no dependencies", () => {20 expect(require("../package.json").dependencies).toBeUndefined()21 })22 23-runBaseTest("proxy (no freeze)", true, false)24-runBaseTest("proxy (autofreeze)", true, true)25-runBaseTest("proxy (patch listener)", true, false, true)26-runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)23+// runBaseTest("proxy (no freeze)", true, false)24+// runBaseTest("proxy (autofreeze)", true, true)25+// runBaseTest("proxy (patch listener)", true, false, true)26+// runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)27 28 runBaseTest("es5 (no freeze)", false, false)29-runBaseTest("es5 (autofreeze)", false, true)30-runBaseTest("es5 (patch listener)", false, false, true)31-runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)29+// runBaseTest("es5 (autofreeze)", false, true)30+// runBaseTest("es5 (patch listener)", false, false, true)31+// runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)32 33 function runBaseTest(name, useProxies, autoFreeze, useListener) {34 const listener = useListener ? function() {} : undefined35 const {produce, produceWithPatches} = createPatchedImmer({896 unmodified lines932 expect(nextState.foo).toBeTruthy()933 expect(isEnumerable(nextState, "foo")).toBeFalsy()934 })935 936- it("throws on computed properties", () => {936+ it("does not throws on computed properties", () => {937 const baseState = {}938 Object.defineProperty(baseState, "foo", {939 get: () => {},940 enumerable: true4 unmodified lines945 if (useProxies) {946 s.modified = true947 }948 })949- }).toThrowErrorMatchingSnapshot()949+ }).not.toThrow()950 })951 952+ it("can work with computed props", () => {953+ const baseState = {954+ x: 1,955+ set y(v) {956+ this.x = v;957+ },958+ get y() {959+ return this.x960+ }961+ }962+963+ debugger;964+ const nextState = produce(baseState, d => {965+ expect(d.y).toBe(1)966+ d.y = 2;967+ expect(d.x).toBe(2)968+ expect(d.y).toBe(2)969+ })970+ expect(baseState.x).toBe(1)971+ expect(baseState.y).toBe(1)972+973+ expect(nextState.x).toBe(2)974+ expect(nextState.y).toBe(2)975+ })976+977+ it("can work with class with computed props", () => {978+ class State {979+ [immerable]= true980+981+ x= 1,982+983+ set y(v) {984+ this.x = v;985+ },986+987+ get y() {988+ return this.x989+ }990+ }991+992+ const baseState = new State();993+994+ const nextState = produce(baseState, d => {995+ expect(d.y).toBe(1)996+ d.y = 2;997+ expect(d.x).toBe(2)998+ expect(d.y).toBe(2)999+ expect(Object.hasOwnProperty(d, "y")).toBeNull()1000+ })1001+ expect(baseState.x).toBe(1)1002+ expect(baseState.y).toBe(1)1003+1004+ expect(nextState.x).toBe(2)1005+ expect(nextState.y).toBe(2)1006+ expect(Object.hasOwnProperty(nextState, "y")).toBeNull()1007+ })1008+1009 it("allows inherited computed properties", () => {1010 const proto = {}1011 Object.defineProperty(proto, "foo", {1012 get() {1257 unmodified linesmodified:src/core/immerClass.ts+1 added
114 unmodified lines115 }116 usePatchesInScope(scope, patchListener)117 return processResult(result, scope)118 } else {119+ // TODO: simply throw!120 result = recipe(base)121 if (result === NOTHING) return undefined122 if (result === undefined) result = base123 if (this.autoFreeze_) freeze(result, true)113 unmodified linesmodified:src/core/proxy.ts-4 removed
135 unmodified lines136 // @ts-ignore137 drafts = state.copy_138 }139 140- // const desc = Object.getOwnPropertyDescriptor(latest(state), prop)141- // Don't create and store proxies for getters (they might return drafts though)142- // See #558143- // if (!desc || desc.get) return value140 return (drafts![prop as any] = createProxy(141 state.scope_.immer_,142 value,143 state124 unmodified linesmodified:src/utils/common.ts+4 added-1 removed
157 unmodified linesExpand all15815916016116216216316416516616716816923 unmodified lines157 unmodified linesExpand all } const desc = Object.getOwnPropertyDescriptor(base, key)! let {value} = desc if (desc.get) { if (!invokeGetters) die(1) if (!invokeGetters) { Object.defineProperty(clone, key, desc); return; } value = desc.get.call(base) } if (desc.enumerable) { clone[key] = value23 unmodified linesmodified:src/utils/errors.ts+3 added-1 removed
27 unmodified lines28 },29 18(plugin: string) {30 return `The plugin for '${plugin}' has not been loaded into Immer. To enable the plugin, import and call \`enable${plugin}()\` when initializing your application.`31 },32- 19: "plugin not loaded",32+ 19(plugin: string) {33+ return `Plugin not loaded: ${plugin}`34+ },35 20: "Cannot use proxies if Proxy, Proxy.revocable or Reflect are not available"36 } as const37 38 export function die(error: keyof typeof errors, ...args: any[]): never {15 unmodified lines