Skip to content

Commit

Permalink
Merge 2409fe5 into 39c6e72
Browse files Browse the repository at this point in the history
  • Loading branch information
dalcib committed Oct 31, 2019
2 parents 39c6e72 + 2409fe5 commit 136bd8f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
42 changes: 42 additions & 0 deletions __tests__/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,48 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) {
})
})

describe('class with getters and methods', () => {
class State {
constructor() {
this[immerable] = true
this.word1 = "bar"
this.word2 = "foo"
this.foo = 0
this._bar = {baz: 1}
}
get chars() {
return this.word1.split("")
}
get bar() {
return this._bar
}
setWord2() {
let mix = [...this.chars].slice(0, 3)
mix[2] = "z"
this.word2 = mix.join("")
}
syncFoo() {
return produce(this, state => {
state.foo = state.bar.baz
})
}
}

const state = new State()

it("should work without creating a proxy for a getter property", () => {
expect(state.chars).toEqual(['b', 'a', 'r'])
const newState1 = produce(state, d => d.setWord2())
expect(newState1.word2).toEqual('baz')

expect(state.bar).toEqual({ baz: 1 })
const newState2 = state.syncFoo()
expect(newState2.foo).toEqual(1)
expect(newState2.bar).toEqual({baz: 1})
})

});

describe(`complex nesting map / set / object`, () => {
const a = {a: 1}
const b = {b: 2}
Expand Down
4 changes: 4 additions & 0 deletions src/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ const objectTraps = {
// Store drafts on the copy (when one exists).
drafts = state.copy
}

if (!Object.keys(state.base).includes(prop)) {
return value
}

return (drafts[prop] = createProxy(value, state))
},
Expand Down

0 comments on commit 136bd8f

Please sign in to comment.