Skip to content
Myr

Merge branch 'getters' of https://github.com/dalcib/immer into dalcib-getters

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 d64c2bd, 081fff1 · Browse files · 2 files+58 added−2 removed
This commit

State

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

Diff stats

2 files+58 added−2 removed

  • tests:+50 added−0 removed
  • other:+8 added−2 removed

2 files+58 added−2 removed

modified:__tests__/base.js+50 added
2088 unmodified lines
2089 expect(result[0]).toBe(random)2090 })2091 })2092 }20932094 describe("class with getters", () => {2095 class State {2096 [immerable] = true2097 _bar = {baz: 1}2098 foo2099 get bar() {2100 return this._bar2101 }2102 syncFoo() {2103 this.foo = this.bar.baz2104 }2105 }2106 const state = new State()21072108 it("should use a method to assing a field using a getter that return a non primitive object", () => {2109 const newState = produce(state, draft => {2110 draft.syncFoo()2111 expect(draft.bar).toEqual({baz: 1})2112 })2113 expect(newState.foo).toEqual(1)2114 expect(newState.bar).toEqual({baz: 1})2115 expect(state.bar).toEqual({baz: 1})2116 })2117 })21182119 describe("class with setters", () => {2120 class State {2121 [immerable] = true2122 _bar = 02123 get bar() {2124 return this._bar2125 }2126 set bar(x) {2127 this._bar = x2128 }2129 }2130 const state = new State()21312132 it("should define a field with a setter", () => {2133 const newState3 = produce(state, d => {2134 d.bar = 12135 expect(d._bar).toEqual(1)2136 })2137 expect(newState3._bar).toEqual(1)2138 expect(newState3.bar).toEqual(1)2139 expect(state._bar).toEqual(0)2140 expect(state.bar).toEqual(0)2141 })2142 })2143}21442145function testLiteralTypes(produce) {2146 class Foo {}
66 unmodified lines
modified:src/core/proxy.ts+8 added-2 removed
237 unmodified lines
238 state.type_ === ProxyTypeProxyArray239 ) {240 const copy = (state.copy_ = shallowCopy(state.base_))241 each(state.drafts_!, (key, value) => {242 const descProto = Object.getOwnPropertyDescriptor(243 Object.getPrototypeOf(copy),244 key245 )246 if (!(descProto && descProto.get && !descProto.set)) {242 // @ts-ignore243 copy[key] = value247 // @ts-ignore248 copy[key] = value249 }250 })251 state.drafts_ = undefined252 }253
11 unmodified lines