Merge pull request #33 from mweststrate/fix/32
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix reflection on proxies
authored by Michel Weststrate · committed by GitHub · parents c3fcf65, c6454ac · Browse files · 6 files·+138 added−49 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+138 added−49 removed
- tests:+116 added−49 removed
- other:+22 added−0 removed
Provenance
- Commit
fae0957- Tree
9da6464- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
c3fcf65- Parent 2
c6454ac
6 files changed+138 added−49 removed
modified:__tests__/base.js+69 added-10 removed
1 "use strict"2-import immerProxy from ".."3-import immerEs5 from "../es5"2+import * as immerProxy from ".."3+import * as immerEs5 from "../es5"4+import deepFreeze from "deep-freeze"5 5-runBaseTest("proxy", immerProxy)6-runBaseTest("es5", immerEs5)6+runBaseTest("proxy (no freeze)", immerProxy, false)7+runBaseTest("proxy (autofreeze)", immerProxy, true)8+runBaseTest("es5 (no freeze)", immerEs5, false)9+runBaseTest("es5 (autofreeze)", immerEs5, true)10 8-function runBaseTest(name, immer) {11+function runBaseTest(name, lib, freeze) {12 describe(`base functionality - ${name}`, () => {13+ const immer = lib.default14 let baseState15 let origBaseState16 17 beforeEach(() => {18+ lib.setAutoFreeze(freeze)19 origBaseState = baseState = createBaseState()20 })21 22 it("should return the original without modifications", () => {100 unmodified lines123 })124 expect(nextState).not.toBe(baseState)125 expect(nextState.anArray).not.toBe(baseState.anArray)126 122- expect(nextState.anArray).toEqual([3, "a", "b", {c: 3}, 1])127+ expect(enumerableOnly(nextState.anArray)).toEqual([128+ 3,129+ "a",130+ "b",131+ {c: 3},132+ 1133+ ])134 })135 136 it("should support sorting arrays", () => {137 const nextState = immer(baseState, s => {2 unmodified lines140 s.anArray[3].c = 5141 })142 expect(nextState).not.toBe(baseState)143 expect(nextState.anArray).not.toBe(baseState.anArray)133- expect(nextState.anArray).toEqual([1, 2, 3, {c: 5}])144+ expect(enumerableOnly(nextState.anArray)).toEqual([1, 2, 3, {c: 5}])145 })146 147+ it("should expose property descriptors", () => {148+ const nextState = immer([], s => {149+ expect(Object.getOwnPropertyDescriptor(s, 0)).toBe(undefined)150+ s.unshift("x")151+ expect(Object.getOwnPropertyDescriptor(s, 0)).toEqual({152+ configurable: true,153+ enumerable: true,154+ value: "x",155+ writable: true156+ })157+ expect(s.length).toBe(1)158+ expect(s[0] === "x").toBe(true)159+ })160+ expect(nextState).toEqual(["x"])161+ expect(Object.getOwnPropertyDescriptor(nextState, 0)).toEqual({162+ configurable: !freeze,163+ enumerable: true,164+ value: "x",165+ writable: !freeze166+ })167+ })168+169+ it("should support sorting arrays - 2", () => {170+ const nextState = immer(baseState, s => {171+ s.anArray.unshift("x")172+ s.anArray[3].c = 4173+ s.anArray.sort()174+ s.anArray[3].c = 5175+ s.anArray.unshift("y")176+ })177+ expect(nextState).not.toBe(baseState)178+ expect(nextState.anArray).not.toBe(baseState.anArray)179+ expect(enumerableOnly(nextState.anArray)).toEqual([180+ "y",181+ 1,182+ 2,183+ 3,184+ {c: 5},185+ "x"186+ ])187+ })188+189 it("should updating inside arrays", () => {190 const nextState = immer(baseState, s => {191 s.anArray[2].test = true192 })9 unmodified lines202 s.messy = obj203 })204 expect(nextState).not.toBe(baseState)205 expect(nextState.anArray).toBe(baseState.anArray)153- expect(nextState).toEqual({206+ expect(enumerableOnly(nextState)).toEqual({207 anArray: [3, 2, {c: 3}, 1],208 aProp: "hi",209 messy: {210 nested: {14 unmodified lines225 s.messy = obj226 })227 expect(nextState).not.toBe(baseState)228 expect(nextState.anArray).toBe(baseState.anArray)176- expect(nextState).toEqual({229+ expect(enumerableOnly(nextState)).toEqual({230 anArray: [3, 2, {c: 3}, 1],231 aProp: "hello",232 messy: {233 nested: {76 unmodified lines310 expect(baseState).toEqual(createBaseState())311 })312 313 function createBaseState() {261- return {314+ const data = {315 anArray: [3, 2, {c: 3}, 1],316 aProp: "hi",317 anObject: {318 nested: {1 unmodified line320 },321 coffee: false322 }323 }324+ return freeze ? deepFreeze(data) : data325 }326 })327 }328+329+function enumerableOnly(x) {330+ // this can be done better...331+ return JSON.parse(JSON.stringify(x))332+}modified:__tests__/frozen.js+47 added-39 removed
122345456786781011121314151617910111213141516192021221718192021222324252425262726272829293031303334353631323334353637383938394041424344454041424344454647484950515253475455564957"use strict"import immer from ".."import * as immerProxy from ".."import * as immerEs5 from "../es5"import deepFreeze from "deep-freeze"
describe("auto freeze", () => { const baseState = { object: {a: 1}, array: [1, 2] }runBaseTest("proxy", immerProxy)runBaseTest("es5", immerEs5)
it("should freeze objects after modifications", () => { expect(Object.isFrozen(baseState.object)).toBe(false) // initially not frozen const next = immer(baseState, draft => { draft.object.c = 2 }) expect(Object.isFrozen(next.object)).toBe(true) expect(Object.isFrozen(next)).toBe(true) expect(Object.isFrozen(next.array)).toBe(false)function runBaseTest(name, lib) { describe("auto freeze - " + name, () => { const immer = lib.default const baseState = { object: {a: 1}, array: [1, 2] }
expect(() => { next.object.a = 2 }).toThrow(/Cannot assign to read only property/) }) it("should freeze objects after modifications", () => { expect(Object.isFrozen(baseState.object)).toBe(false) // initially not frozen const next = immer(baseState, draft => { draft.object.c = 2 }) expect(Object.isFrozen(next.object)).toBe(true) expect(Object.isFrozen(next)).toBe(true) expect(Object.isFrozen(next.array)).toBe(false)
it("should freeze arrays after modifications", () => { expect(Object.isFrozen(baseState.object)).toBe(false) // initially not frozen const next = immer(baseState, draft => { draft.array.push(3) expect(() => { next.object.a = 2 }).toThrow(/Cannot assign to read only property/) }) expect(Object.isFrozen(next.object)).toBe(false) // not touched expect(Object.isFrozen(next)).toBe(true) expect(Object.isFrozen(next.array)).toBe(true)
expect(() => { next.array.shift() }).toThrow(/Cannot add\/remove sealed array elements/) }) it("should freeze arrays after modifications", () => { expect(Object.isFrozen(baseState.object)).toBe(false) // initially not frozen const next = immer(baseState, draft => { draft.array.push(3) }) expect(Object.isFrozen(next.object)).toBe(false) // not touched expect(Object.isFrozen(next)).toBe(true) expect(Object.isFrozen(next.array)).toBe(true)
it("can handle already frozen trees", () => { const a = [] const b = {a: a} Object.freeze(a) Object.freeze(b) const n = immer(b, draft => { draft.c = true draft.a.push(3) expect(() => { next.array.shift() }).toThrow(/Cannot add\/remove sealed array elements/) })
it("can handle already frozen trees", () => { const a = [] const b = {a: a} Object.freeze(a) Object.freeze(b) const n = immer(b, draft => { draft.c = true draft.a.push(3) }) expect(n).toEqual({c: true, a: [3]}) expect(n).toEqual({c: true, a: [3]}) }) })})}modified:changelog.md+4 added
1234567891024 unmodified lines# Changelog
### 0.2.2
* Fixed [#32](https://github.com/mweststrate/immer/issue/32): changes are not properly reflected in proxies when using for example `console.dir`
### 0.2.1
* Fixed: `immer/es5.js` was not packed into the package. PR [#28](https://github.com/mweststrate/immer/pull/28) by [Nicolas Lepage](https://github.com/nlepage)
24 unmodified linesmodified:immer.js+13 added
64 unmodified lines65 deleteProperty(target, property) {66 const copy = getOrCreateCopy(target)67 delete copy[property]68 return true69+ },70+ getOwnPropertyDescriptor(target, prop) {71+ return Reflect.getOwnPropertyDescriptor(72+ getCurrentSource(target),73+ prop74+ )75+ },76+ defineProperty(target, property, descriptor) {77+ Object.defineProperty(getOrCreateCopy(target), property, descriptor)78+ return true79+ },80+ setPrototypeOf() {81+ throw new Error("Don't even try this...")82 }83 }84 85 // creates a copy for a base object if there ain't one156 unmodified linesmodified:package.json+1 added
35 unmodified lines36373839404142434410 unmodified lines35 unmodified lines "@types/jest": "^22.0.0", "babel-core": "^6.26.0", "babel-jest": "^22.0.4", "babel-preset-env": "^1.6.1", "deep-freeze": "^0.0.1", "immutable": "^3.8.2", "jest": "^22.0.4", "lodash.clonedeep": "^4.5.0", "pre-commit": "^1.2.2",10 unmodified linesmodified:yarn.lock+4 added
923 unmodified lines924 deep-extend@~0.4.0:925 version "0.4.2"926 resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.4.2.tgz#48b699c27e334bf89f10892be432f6e4c7d34a7f"927 928+deep-freeze@^0.0.1:929+ version "0.0.1"930+ resolved "https://registry.yarnpkg.com/deep-freeze/-/deep-freeze-0.0.1.tgz#3a0b0005de18672819dfd38cd31f91179c893e84"931+932 deep-is@~0.1.3:933 version "0.1.3"934 resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.3.tgz#b369d6fb5dbc13eecf524f91b070feedc357cf34"935 2186 unmodified lines