Skip to content
Myr

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

6 files+138 added−49 removed

modified:__tests__/base.js+69 added-10 removed
1"use strict"2import immerProxy from ".."3import immerEs5 from "../es5"2import * as immerProxy from ".."3import * as immerEs5 from "../es5"4import deepFreeze from "deep-freeze"55runBaseTest("proxy", immerProxy)6runBaseTest("es5", immerEs5)6runBaseTest("proxy (no freeze)", immerProxy, false)7runBaseTest("proxy (autofreeze)", immerProxy, true)8runBaseTest("es5 (no freeze)", immerEs5, false)9runBaseTest("es5 (autofreeze)", immerEs5, true)108function runBaseTest(name, immer) {11function runBaseTest(name, lib, freeze) {12 describe(`base functionality - ${name}`, () => {13 const immer = lib.default14 let baseState15 let origBaseState1617 beforeEach(() => {18 lib.setAutoFreeze(freeze)19 origBaseState = baseState = createBaseState()20 })2122 it("should return the original without modifications", () => {
100 unmodified lines
123 })124 expect(nextState).not.toBe(baseState)125 expect(nextState.anArray).not.toBe(baseState.anArray)126122 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 })135136 it("should support sorting arrays", () => {137 const nextState = immer(baseState, s => {
2 unmodified lines
140 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 })146147 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 })168169 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 })188189 it("should updating inside arrays", () => {190 const nextState = immer(baseState, s => {191 s.anArray[2].test = true192 })
9 unmodified lines
202 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 lines
225 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 lines
310 expect(baseState).toEqual(createBaseState())311 })312313 function createBaseState() {261 return {314 const data = {315 anArray: [3, 2, {c: 3}, 1],316 aProp: "hi",317 anObject: {318 nested: {
1 unmodified line
320 },321 coffee: false322 }323 }324 return freeze ? deepFreeze(data) : data325 }326 })327}328329function enumerableOnly(x) {330 // this can be done better...331 return JSON.parse(JSON.stringify(x))332}
modified:__tests__/frozen.js+47 added-39 removed
modified:changelog.md+4 added
modified:immer.js+13 added
64 unmodified lines
65 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 }8485 // creates a copy for a base object if there ain't one
156 unmodified lines
modified:package.json+1 added
modified:yarn.lock+4 added
923 unmodified lines
924deep-extend@~0.4.0:925 version "0.4.2"926 resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.4.2.tgz#48b699c27e334bf89f10892be432f6e4c7d34a7f"927928deep-freeze@^0.0.1:929 version "0.0.1"930 resolved "https://registry.yarnpkg.com/deep-freeze/-/deep-freeze-0.0.1.tgz#3a0b0005de18672819dfd38cd31f91179c893e84"931932deep-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