Skip to content
Myr

Merge branch 'dalcib-getters' into immer-7

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 · parents b896a3a, f33d6b6 · Browse files · 4 files+130 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+130 added−13 removed

  • tests:+116 added−9 removed
  • other:+14 added−4 removed

4 files+130 added−13 removed

modified:__tests__/base.js+116 added-9 removed
19 unmodified lines
20test("immer should have no dependencies", () => {21 expect(require("../package.json").dependencies).toBeUndefined()22})2324runBaseTest("proxy (no freeze)", true, false)25runBaseTest("proxy (autofreeze)", true, true)26runBaseTest("proxy (patch listener)", true, false, true)27runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)24// runBaseTest("proxy (no freeze)", true, false)25// runBaseTest("proxy (autofreeze)", true, true)26// runBaseTest("proxy (patch listener)", true, false, true)27// runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)2829runBaseTest("es5 (no freeze)", false, false)30runBaseTest("es5 (autofreeze)", false, true)31runBaseTest("es5 (patch listener)", false, false, true)32runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)30// runBaseTest("es5 (autofreeze)", false, true)31// runBaseTest("es5 (patch listener)", false, false, true)32// runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)3334function runBaseTest(name, useProxies, autoFreeze, useListener) {35 const listener = useListener ? function() {} : undefined36 const {produce, produceWithPatches} = createPatchedImmer({
896 unmodified lines
933 expect(nextState.foo).toBeTruthy()934 expect(isEnumerable(nextState, "foo")).toBeFalsy()935 })936937 it("throws on computed properties", () => {937 it("does not throws on computed properties", () => {938 const baseState = {}939 Object.defineProperty(baseState, "foo", {940 get: () => {},941 enumerable: true
4 unmodified lines
946 if (useProxies) {947 s.modified = true948 }949 })950 }).toThrowErrorMatchingSnapshot()950 }).not.toThrow()951 })952953 it("can work with computed props", () => {954 const baseState = {955 x: 1,956 set y(v) {957 this.x = v958 },959 get y() {960 return this.x961 }962 }963964 debugger965 const nextState = produce(baseState, d => {966 expect(d.y).toBe(1)967 d.y = 2968 expect(d.x).toBe(2)969 expect(d.y).toBe(2)970 })971 expect(baseState.x).toBe(1)972 expect(baseState.y).toBe(1)973974 expect(nextState.x).toBe(2)975 expect(nextState.y).toBe(2)976 })977978 it("can work with class with computed props", () => {979 class State {980 [immerable] = true981982 x = 1983984 set y(v) {985 this.x = v986 }987988 get y() {989 return this.x990 }991 }992993 const baseState = new State()994995 const nextState = produce(baseState, d => {996 expect(d.y).toBe(1)997 d.y = 2998 expect(d.x).toBe(2)999 expect(d.y).toBe(2)1000 expect(Object.hasOwnProperty(d, "y")).toBeNull()1001 })1002 expect(baseState.x).toBe(1)1003 expect(baseState.y).toBe(1)10041005 expect(nextState.x).toBe(2)1006 expect(nextState.y).toBe(2)1007 expect(Object.hasOwnProperty(nextState, "y")).toBeNull()1008 })10091010 it("allows inherited computed properties", () => {1011 const proto = {}1012 Object.defineProperty(proto, "foo", {1013 get() {
1126 unmodified lines
2140 expect(result[0]).toBe(random)2141 })2142 })2143 }21442145 describe("class with getters", () => {2146 class State {2147 [immerable] = true2148 _bar = {baz: 1}2149 foo2150 get bar() {2151 return this._bar2152 }2153 syncFoo() {2154 this.foo = this.bar.baz2155 this.bar.baz++2156 }2157 }2158 const state = new State()21592160 it("should use a method to assing a field using a getter that return a non primitive object", () => {2161 const newState = produce(state, draft => {2162 draft.syncFoo()2163 })2164 expect(newState.foo).toEqual(1)2165 expect(newState.bar).toEqual({baz: 2})2166 expect(state.bar).toEqual({baz: 1})2167 })2168 })21692170 describe("class with setters", () => {2171 class State {2172 [immerable] = true2173 _bar = 02174 get bar() {2175 return this._bar2176 }2177 set bar(x) {2178 this._bar = x2179 }2180 }2181 const state = new State()21822183 it("should define a field with a setter", () => {2184 const newState3 = produce(state, d => {2185 d.bar = 12186 expect(d._bar).toEqual(1)2187 })2188 expect(newState3._bar).toEqual(1)2189 expect(newState3.bar).toEqual(1)2190 expect(state._bar).toEqual(0)2191 expect(state.bar).toEqual(0)2192 })2193 })2194}21952196function testLiteralTypes(produce) {2197 class Foo {}
52 unmodified lines
modified:src/core/immerClass.ts-2 removed
3 unmodified lines
4 ImmerState,5 Drafted,6 isDraftable,7 processResult,8 NOTHING,8 Patch,9 Objectish,10 DRAFT_STATE,11 Draft,
2 unmodified lines
14 isMap,15 isSet,16 markChangedProxy,17 createProxyProxy,19 freeze,18 getPlugin,19 die,20 hasProxies,21 isMinified,
207 unmodified lines
modified:src/core/proxy.ts+10 added-1 removed
17 unmodified lines
18 createProxy,19 ProxyTypeProxyObject,20 ProxyTypeProxyArray21} from "../internal"22import {isPlainObject} from "../utils/common"2324interface ProxyBaseState extends ImmerBaseState {25 assigned_: {26 [property: string]: boolean
87 unmodified lines
114 if (!state.modified_ && has(drafts, prop)) {115 return drafts![prop as any]116 }117117 const value = latest(state)[prop]118 const l = latest(state)119 const desc = Object.getOwnPropertyDescriptor(120 isPlainObject(l) ? l : Object.getPrototypeOf(l),121 prop122 )123 // If the prop is a getter, don't proxy the full prop, but invoke the getter with the correct scope. Fixes #558124 if (desc?.get) return desc.get.call(state.draft_)125126 const value = l[prop]127 if (state.finalized_ || !isDraftable(value)) {128 return value129 }130
137 unmodified lines
modified:src/utils/common.ts+4 added-1 removed