base.js
This file
Path
__tests__/base.js
950 lines · 32.8 KiB · JavaScript
Last changed by
Merge pull request #158 from stefanwille/base-test-uses-if-instead-of-itbenbraoubefore adoption0d04d62
History of this fileTake it
Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 1why, one step back1"use strict"Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 2why, one step back2import produce, {setAutoFreeze, setUseProxies} from "../src/immer"Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 3why, one step back3import deepFreeze from "deep-freeze"Reproduced #66Michel Weststratebefore adoptiond5de885
line 4why, one step back4import cloneDeep from "lodash.clonedeep"added some additional tests to verify solution correctness of #86Michel Weststratebefore adoption681b162
line 5why, one step back5import * as lodash from "lodash"More repo setupMichel Weststratebefore adoption1637393
line 6why, one step back6Making several improvements at onceMichel Weststratebefore adoption1116ccf
lines 7–8why, one step back7jest.setTimeout(1000)8Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 9–12why, one step back9runBaseTest("proxy (no freeze)", true, false)10runBaseTest("proxy (autofreeze)", true, true)11runBaseTest("es5 (no freeze)", false, false)12runBaseTest("es5 (autofreeze)", false, true)Make debugging workMichel Weststratebefore adoptiond0ef4ed
line 13why, one step back13Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 14why, one step back14function runBaseTest(name, useProxies, freeze) {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 15–17why, one step back15 describe(`base functionality - ${name}`, () => {16 let baseState17 let origBaseStateMake debugging workMichel Weststratebefore adoptiond0ef4ed
line 18why, one step back18Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 19why, one step back19 beforeEach(() => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 20–21why, one step back20 setAutoFreeze(freeze)21 setUseProxies(useProxies)Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 22–23why, one step back22 origBaseState = baseState = createBaseState()23 })Make debugging workMichel Weststratebefore adoptiond0ef4ed
line 24why, one step back24Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 25why, one step back25 it("should return the original without modifications", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 26why, one step back26 const nextState = produce(baseState, () => {})Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 27why, one step back27 expect(nextState).toBe(baseState)Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 28–29why, one step back28 })29Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 30why, one step back30 it("should return the original without modifications when reading stuff", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 31why, one step back31 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 32–36why, one step back32 expect(s.aProp).toBe("hi")33 expect(s.anObject.nested).toMatchObject({yummie: true})34 })35 expect(nextState).toBe(baseState)36 })Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
line 37why, one step back37Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 38why, one step back38 it("should return a copy when modifying stuff", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 39why, one step back39 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 40–46why, one step back40 s.aProp = "hello world"41 })42 expect(nextState).not.toBe(baseState)43 expect(baseState.aProp).toBe("hi")44 expect(nextState.aProp).toBe("hello world")45 // structural sharing?46 expect(nextState.nested).toBe(baseState.nested)Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 47–48why, one step back47 })48Merge pull request #158 from stefanwille/base-test-uses-if-instead-of-itbenbraoubefore adoption0d04d62
lines 49–56why, one step back49 it("should preserve type", () => {50 const nextState = produce(baseState, s => {51 expect(Object.getPrototypeOf(s)).toBe(Object.prototype)52 expect(Array.isArray(s.anArray)).toBe(true)53 s.anArray.push(3)54 s.aProp = "hello world"55 expect(Object.getPrototypeOf(s)).toBe(Object.prototype)56 expect(Array.isArray(s.anArray)).toBe(true)Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 57why, one step back57 })Merge pull request #158 from stefanwille/base-test-uses-if-instead-of-itbenbraoubefore adoption0d04d62
lines 58–64why, one step back58 expect(Object.getPrototypeOf(nextState)).toBe(Object.prototype)59 expect(Array.isArray(nextState.anArray)).toBe(true)60 })6162 it("deep change bubbles up", () => {63 const nextState = produce(baseState, s => {64 s.anObject.nested.yummie = falseMerge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 65why, one step back65 })Merge pull request #158 from stefanwille/base-test-uses-if-instead-of-itbenbraoubefore adoption0d04d62
lines 66–71why, one step back66 expect(nextState).not.toBe(baseState)67 expect(nextState.anObject).not.toBe(baseState.anObject)68 expect(baseState.anObject.nested.yummie).toBe(true)69 expect(nextState.anObject.nested.yummie).toBe(false)70 expect(nextState.anArray).toBe(baseState.anArray)71 })Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
line 72why, one step back72Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 73why, one step back73 it("can add props", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 74why, one step back74 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 75–80why, one step back75 s.anObject.cookie = {tasty: true}76 })77 expect(nextState).not.toBe(baseState)78 expect(nextState.anObject).not.toBe(baseState.anObject)79 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)80 expect(nextState.anObject.cookie).toEqual({tasty: true})Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 81–82why, one step back81 })82Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 83why, one step back83 it("can delete props", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 84why, one step back84 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 85–89why, one step back85 delete s.anObject.nested86 })87 expect(nextState).not.toBe(baseState)88 expect(nextState.anObject).not.toBe(baseState.anObject)89 expect(nextState.anObject.nested).toBe(undefined)Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 90–91why, one step back90 })91Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 92why, one step back92 it("ignores single non-modification", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 93why, one step back93 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 94–96why, one step back94 s.aProp = "hi"95 })96 expect(nextState).toBe(baseState)Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 97–98why, one step back97 })98Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 99why, one step back99 it("processes single modification", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 100why, one step back100 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 101–105why, one step back101 s.aProp = "hello"102 s.aProp = "hi"103 })104 expect(nextState).not.toBe(baseState)105 expect(nextState).toEqual(baseState)Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 106–107why, one step back106 })107Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85
lines 108–143why, one step back108 it("processes with for loop", () => {109 const base = [{id: 1, a: 1}, {id: 2, a: 1}]110 const findById = (collection, id) => {111 for (const item of collection) {112 if (item.id === id) return item113 }114 return null115 }116 const result = produce(base, draft => {117 const obj1 = findById(draft, 1)118 const obj2 = findById(draft, 2)119 obj1.a = 2120 obj2.a = 2121 })122 expect(result[0].a).toEqual(2)123 expect(result[1].a).toEqual(2)124 })125126 it("works with objects without proto", () => {127 const base = Object.create(null)128 base.x = 1129 base.y = Object.create(null)130 base.y.y = 2131 expect(base.__proto__).toBe(undefined)132 const next = produce(base, draft => {133 draft.y.z = 3134 draft.y.y++135 draft.x++136 })137 expect(next).toEqual({138 x: 2,139 y: {y: 3, z: 3}140 })141 expect(next.__proto__).toBe(undefined)142 })143Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 144why, one step back144 it("should support reading arrays", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 145why, one step back145 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 146–149why, one step back146 s.anArray.slice()147 })148 expect(nextState.anArray).toBe(baseState.anArray)149 expect(nextState).toBe(baseState)Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 150–151why, one step back150 })151Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 152why, one step back152 it("should support changing arrays", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 153why, one step back153 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 154–158why, one step back154 s.anArray[3] = true155 })156 expect(nextState).not.toBe(baseState)157 expect(nextState.anArray).not.toBe(baseState.anArray)158 expect(nextState.anArray[3]).toEqual(true)Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 159–160why, one step back159 })160Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 161why, one step back161 it("should support changing arrays - 2", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 162why, one step back162 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 163–166why, one step back163 s.anArray.splice(1, 1, "a", "b")164 })165 expect(nextState).not.toBe(baseState)166 expect(nextState.anArray).not.toBe(baseState.anArray)Add support for arraysMichel Weststratebefore adoptiond3696cb
line 167why, one step back167Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
lines 168–174why, one step back168 expect(enumerableOnly(nextState.anArray)).toEqual([169 3,170 "a",171 "b",172 {c: 3},173 1174 ])Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 175–176why, one step back175 })176Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 177why, one step back177 it("can delete array items", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 178why, one step back178 const nextState = produce(baseState, s => {Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 179–186why, one step back179 s.anArray.length = 3180 })181 expect(nextState).not.toBe(baseState)182 expect(nextState.anObject).toBe(baseState.anObject)183 expect(nextState.anArray).not.toBe(baseState.anArray)184 expect(nextState.anArray).toEqual([3, 2, {c: 3}])185 })186Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 187why, one step back187 it("should support sorting arrays", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 188why, one step back188 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 189–194why, one step back189 s.anArray[2].c = 4190 s.anArray.sort()191 s.anArray[3].c = 5192 })193 expect(nextState).not.toBe(baseState)194 expect(nextState.anArray).not.toBe(baseState.anArray)Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 195why, one step back195 expect(enumerableOnly(nextState.anArray)).toEqual([1, 2, 3, {c: 5}])Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 196–197why, one step back196 })197Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 198why, one step back198 it("should expose property descriptors", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 199why, one step back199 const nextState = produce([], s => {Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
lines 200–220why, one step back200 expect(Object.getOwnPropertyDescriptor(s, 0)).toBe(undefined)201 s.unshift("x")202 expect(Object.getOwnPropertyDescriptor(s, 0)).toEqual({203 configurable: true,204 enumerable: true,205 value: "x",206 writable: true207 })208 expect(s.length).toBe(1)209 expect(s[0] === "x").toBe(true)210 })211 expect(nextState).toEqual(["x"])212 expect(Object.getOwnPropertyDescriptor(nextState, 0)).toEqual({213 configurable: !freeze,214 enumerable: true,215 value: "x",216 writable: !freeze217 })218 })219220 it("should support sorting arrays - 2", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 221why, one step back221 const nextState = produce(baseState, s => {Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
lines 222–239why, one step back222 s.anArray.unshift("x")223 s.anArray[3].c = 4224 s.anArray.sort()225 s.anArray[3].c = 5226 s.anArray.unshift("y")227 })228 expect(nextState).not.toBe(baseState)229 expect(nextState.anArray).not.toBe(baseState.anArray)230 expect(enumerableOnly(nextState.anArray)).toEqual([231 "y",232 1,233 2,234 3,235 {c: 5},236 "x"237 ])238 })239Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 240why, one step back240 it("should updating inside arrays", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 241why, one step back241 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 242–246why, one step back242 s.anArray[2].test = true243 })244 expect(nextState).not.toBe(baseState)245 expect(nextState.anArray).not.toBe(baseState.anArray)246 expect(nextState.anArray).toEqual([3, 2, {c: 3, test: true}, 1])Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 247–248why, one step back247 })248Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 249why, one step back249 it("reusing object should work", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 250why, one step back250 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 251–256why, one step back251 const obj = s.anObject252 delete s.anObject253 s.messy = obj254 })255 expect(nextState).not.toBe(baseState)256 expect(nextState.anArray).toBe(baseState.anArray)Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 257why, one step back257 expect(enumerableOnly(nextState)).toEqual({Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 258–267why, one step back258 anArray: [3, 2, {c: 3}, 1],259 aProp: "hi",260 messy: {261 nested: {262 yummie: true263 },264 coffee: false265 }266 })267 expect(nextState.messy.nested).toBe(baseState.anObject.nested)Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 268–269why, one step back268 })269Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 270why, one step back270 it("refs should be transparent", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 271why, one step back271 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 272–279why, one step back272 const obj = s.anObject273 s.aProp = "hello"274 delete s.anObject275 obj.coffee = true276 s.messy = obj277 })278 expect(nextState).not.toBe(baseState)279 expect(nextState.anArray).toBe(baseState.anArray)Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 280why, one step back280 expect(enumerableOnly(nextState)).toEqual({Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 281–290why, one step back281 anArray: [3, 2, {c: 3}, 1],282 aProp: "hello",283 messy: {284 nested: {285 yummie: true286 },287 coffee: true288 }289 })290 expect(nextState.messy.nested).toBe(baseState.anObject.nested)Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e
lines 291–292why, one step back291 })292Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 293why, one step back293 it("should allow setting to undefined a defined draft property", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 294why, one step back294 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 295–299why, one step back295 s.aProp = undefined296 })297 expect(nextState).not.toBe(baseState)298 expect(baseState.aProp).toBe("hi")299 expect(nextState.aProp).toBe(undefined)Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
lines 300–301why, one step back300 })301Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 302why, one step back302 // ES implementation does't protect against all outside modifications, just some..Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 303why, one step back303 if (useProxies) {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 304–305why, one step back304 it("should revoke the proxy of the baseState after immer function is executed", () => {305 let proxy`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 306why, one step back306 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 307–312why, one step back307 proxy = s308 s.aProp = "hello"309 })310 expect(nextState).not.toBe(baseState)311 expect(baseState.aProp).toBe("hi")312 expect(nextState.aProp).toBe("hello")Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
line 313why, one step back313Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 314–319why, one step back314 expect(() => {315 proxy.aProp = "Hallo"316 }).toThrowError(/revoked/)317 expect(() => {318 const aProp = proxy.aProp319 }).toThrowError(/revoked/)Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 320why, one step back320Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 321–328why, one step back321 expect(nextState).not.toBe(baseState)322 expect(baseState.aProp).toBe("hi")323 expect(nextState.aProp).toBe("hello")324 })325 }326327 it("should revoke the proxy of the baseState after immer function is executed - 2", () => {328 let proxy`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 329why, one step back329 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 330–339why, one step back330 proxy = s.anObject331 })332 expect(nextState).toBe(baseState)333 expect(() => {334 // In ES5 implemenation only protects existing props, but alas..335 proxy.coffee = "Hallo"336 }).toThrowError(/revoked/)337 expect(() => {338 const test = proxy.coffee339 }).toThrowError(/revoked/)Updated changelogMichel Weststratebefore adoption8f44348
lines 340–341why, one step back340 })341Added a testcaseMichel Weststratebefore adoption1e468fd
line 342why, one step back342 it("should reflect all changes made in the draft immediately", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 343why, one step back343 produce(baseState, draft => {Added a testcaseMichel Weststratebefore adoption1e468fd
lines 344–357why, one step back344 draft.anArray[0] = 5345 draft.anArray.unshift("test")346 // sliced here; jest will also compare non-enumerable keys, which would include the immer Symbols347 expect(draft.anArray.slice()).toMatchObject([348 "test",349 5,350 2,351 {c: 3},352 1353 ])354 draft.stuffz = "coffee"355 expect(draft.stuffz).toBe("coffee")356 })357 })Fixed configurability of array.length on proxies. Fixes #50Michel Weststratebefore adoption1c7df26
lines 358–443why, one step back358359 it("should be able to get property descriptors from objects", () => {360 produce({a: 1}, draft => {361 expect("a" in draft).toBe(true)362 expect("b" in draft).toBe(false)363 expect(364 Reflect.ownKeys(draft).filter(x => typeof x === "string")365 ).toEqual(["a"])366367 expect(368 Object.getOwnPropertyDescriptor(draft, "a")369 ).toMatchObject({370 configurable: true,371 enumerable: true372 })373 draft.a = 2374 expect(375 Object.getOwnPropertyDescriptor(draft, "a")376 ).toMatchObject({377 configurable: true,378 enumerable: true379 })380 expect(381 Object.getOwnPropertyDescriptor(draft, "b")382 ).toBeUndefined()383 draft.b = 2384 expect(385 Object.getOwnPropertyDescriptor(draft, "b")386 ).toMatchObject({387 configurable: true,388 enumerable: true389 })390 expect("a" in draft).toBe(true)391 expect("b" in draft).toBe(true)392 expect(393 Reflect.ownKeys(draft).filter(x => typeof x === "string")394 ).toEqual(["a", "b"])395 })396 })397398 it("should be able to get property descriptors from arrays", () => {399 produce([1], draft => {400 expect(0 in draft).toBe(true)401 expect(1 in draft).toBe(false)402 expect("0" in draft).toBe(true)403 expect("1" in draft).toBe(false)404 expect(length in draft).toBe(true)405 expect(406 Reflect.ownKeys(draft).filter(x => typeof x === "string")407 ).toEqual(["0", "length"])408409 expect(410 Object.getOwnPropertyDescriptor(draft, "length")411 ).toMatchObject({412 configurable: false,413 enumerable: false414 })415 draft[0] = 2416 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(417 {418 configurable: true,419 enumerable: true420 }421 )422 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(423 {424 configurable: true,425 enumerable: true426 }427 )428 expect(429 Object.getOwnPropertyDescriptor(draft, 1)430 ).toBeUndefined()431 draft[1] = 2432 expect(Object.getOwnPropertyDescriptor(draft, 1)).toMatchObject(433 {434 configurable: true,435 enumerable: true436 }437 )438 expect(439 Reflect.ownKeys(draft).filter(x => typeof x === "string")440 ).toEqual(["0", "1", "length"])441 })442 })443Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 444why, one step back444 if (useProxies === true) {Fixed configurability of array.length on proxies. Fixes #50Michel Weststratebefore adoption1c7df26
lines 445–455why, one step back445 it("should not be possible to set property descriptors", () => {446 expect(() => {447 produce({}, draft => {448 Object.defineProperty(draft, "xx", {449 enumerable: true,450 writeable: true,451 value: 2452 })453 })454 }).toThrowError(/not support/)455 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 456why, one step back456 }initial solutionMichel Weststratebefore adoptiond618d28
line 457why, one step back457Faster, simpler implementationMichel Weststratebefore adoptionc420f0d
lines 458–462why, one step back458 it("should not throw error, see #53 - 1", () => {459 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}460 const result = produce(base, draft => {461 draft.arr = draft.arr.filter(item => item.count > 2)462 })initial solutionMichel Weststratebefore adoptiond618d28
line 463why, one step back463 expect(result.arr[0].count).toEqual(3)Faster, simpler implementationMichel Weststratebefore adoptionc420f0d
lines 464–494why, one step back464 expect(result).toEqual({465 arr: [{count: 3}]466 })467 expect(result.arr[0]).toBe(base.arr[2])468 })469470 it("should not throw error, see #53 - 2", () => {471 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}472 const result = produce(base, draft => {473 draft.newArr = draft.arr.filter(item => item.count > 2)474 })475 expect(result.newArr[0].count).toEqual(3)476 expect(result.arr).toBe(base.arr)477 expect(result).toEqual({478 arr: [479 {480 count: 1481 },482 {483 count: 2484 },485 {486 count: 3487 }488 ],489 newArr: [490 {491 count: 3492 }493 ]494 })Making several improvements at onceMichel Weststratebefore adoption1116ccf
lines 495–496why, one step back495 expect(result.newArr[0]).toBe(base.arr[2])496 expect(result.arr[2]).toBe(base.arr[2])Faster, simpler implementationMichel Weststratebefore adoptionc420f0d
lines 497–509why, one step back497 })498499 it("should not throw error, see #53 - 3", () => {500 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}501 const result = produce(base, draft => {502 draft.newArr = draft.arr.filter(item => item.count > 2)503 delete draft.arr504 })505 expect(result.newArr[0].count).toEqual(3)506 expect(result).toEqual({507 newArr: [{count: 3}]508 })509 expect(result.newArr[0]).toBe(base.arr[2])initial solutionMichel Weststratebefore adoptiond618d28
line 510why, one step back510 })Added a testcaseMichel Weststratebefore adoption1e468fd
line 511why, one step back511Making several improvements at onceMichel Weststratebefore adoption1116ccf
lines 512–532why, one step back512 it("should not throw error, see #53 - 4", () => {513 const base = {bear: {age: 10}}514 const result = produce(base, draft => {515 draft.bear.legs = 4516 draft.room = {elephant: {kiddo: draft.bear}}517 })518 expect(result).toEqual({519 bear: {age: 10, legs: 4},520 room: {elephant: {kiddo: {age: 10, legs: 4}}}521 })522523 const result2 = produce(result, draft => {524 draft.bear.age = 11525 draft.room.elephant.kiddo.legs = 5526 })527 expect(result2).toEqual({528 bear: {age: 11, legs: 4},529 room: {elephant: {kiddo: {age: 10, legs: 5}}}530 })531 })532Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 533–581why, one step back533 it("should not throw error #78", () => {534 const base = {535 arr: [536 {537 id: "100",538 arr: [{id: "1", no: 1}, {id: "2", no: 2}]539 },540 {id: "3", no: 3}541 ]542 }543 const result = produce(base, draft => {544 const base = draft.arr[0]545 draft.arr = draft.arr.slice(0, 1)546 const newArr = {547 id: "101",548 arr: [base, {id: "3"}]549 }550 draft.arr.splice(0, 1, newArr)551 draft.arr[0].arr[1].no = 3552 })553 expect(result).toEqual({554 arr: [555 {556 id: "101",557 arr: [558 {559 id: "100",560 arr: [{id: "1", no: 1}, {id: "2", no: 2}]561 },562 {id: "3", no: 3}563 ]564 }565 ]566 })567 })568569 it("should not throw error #78 - 2", () => {570 const base = {571 arr: [{count: 1}, {count: 2}]572 }573 const result = produce(base, draft => {574 draft.arr = [draft.arr[1]]575 draft.arr[0].count = 1576 })577 expect(result).toEqual({578 arr: [{count: 1}]579 })580 })581Merge pull request #81 from iruca3/fix/treating_constructor_on_proxyMichel Weststratebefore adoptionad5a86a
lines 582–594why, one step back582 it("should handle constructor correctly", () => {583 const base = {584 arr: new Array(),585 obj: new Object()586 }587 const result = produce(base, draft => {588 draft.arrConstructed = draft.arr.constructor(1)589 draft.objConstructed = draft.obj.constructor(1)590 })591 expect(result.arrConstructed).toEqual(new Array().constructor(1))592 expect(result.objConstructed).toEqual(new Object().constructor(1))593 })594Making several improvements at onceMichel Weststratebefore adoption1116ccf
lines 595–662why, one step back595 it("should handle dates correctly", () => {596 const data = {date: new Date()}597 const next = produce(data, draft => {598 draft.x = true599 })600 expect(next).toEqual({x: true, date: data.date})601 expect(next.date).toBe(data.date)602 const next2 = produce(next, draft => {603 draft.date.setYear(2015)604 })605 // This still holds; because produce won't proxy Date objects606 // and the original is actually modified!607 expect(next2).toEqual({x: true, date: data.date})608 expect(next2.date).toBe(next.date)609 expect(next2.date).toBe(data.date)610 expect(next2).toBe(next)611 })612613 it("should handle equality correctly - 1", () => {614 const base = {615 y: 3 / 0,616 z: NaN617 }618 const next = produce(base, draft => {619 draft.y = 4 / 0620 draft.z = NaN621 })622 expect(next).toEqual(base)623 expect(next).toBe(base)624 })625626 it("should handle equality correctly - 2", () => {627 const base = {628 x: -0629 }630 const next = produce(base, draft => {631 draft.x = +1632 })633 expect(next).not.toEqual(base)634 expect(next).not.toBe(base)635 })636637 it("should handle nested immer calls correctly - 1", () => {638 const bear = {paw: {honey: true}}639 const next = produce(bear, draft => {640 const paw2 = produce(bear.paw, draft => {641 draft.honey = false642 })643 expect(paw2.honey).toBe(false)644 expect(draft.paw.honey).toBe(true) // effects should not be visible outside645 })646 expect(next.paw.honey).toBe(true)647 expect(next).toBe(bear)648 })649650 it("should handle nested immer calls correctly - 2", () => {651 const bear = {paw: {honey: true}}652 const next = produce(bear, draft => {653 const paw2 = produce(bear.paw, draft => {654 draft.honey = false655 })656 expect(paw2.honey).toBe(false)657 expect(draft.paw.honey).toBe(true)658 draft.paw = paw2659 expect(draft.paw.honey).toBe(false)660 })661 expect(next.paw.honey).toBe(false)662 expect(next).not.toBe(bear)Reproduced #66Michel Weststratebefore adoptiond5de885
lines 663–686why, one step back663 })664665 it("should not try to change immutable data, see #66", () => {666 const user = require("./test-data")667668 const base = {}669 const next = produce(base, draft => {670 draft.user = user671 })672 expect(next.user).toBe(user)673 expect(next).not.toBe(base)674 expect(next.user).toEqual(user)675 })676677 it("should not try to change immutable data, see #66 - 2", () => {678 const user = deepFreeze(cloneDeep(require("./test-data")))679680 const base = {}681 const next = produce(base, draft => {682 draft.user = user683 })684 expect(next.user).toBe(user)685 expect(next).not.toBe(base)686 expect(next.user).toEqual(user)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 687–724why, one step back687 })688689 if (freeze)690 it("should freeze new data well", () => {691 const base = {}692 const next = produce(base, draft => {693 draft.x = {y: [{z: true}]}694 })695 expect(Object.isFrozen(next)).toBe(true)696 expect(Object.isFrozen(next.x)).toBe(true)697 expect(Object.isFrozen(next.x.y)).toBe(true)698 expect(Object.isFrozen(next.x.y[0].z)).toBe(true)699 })700701 it("should structurally share identical objects in the tree", () => {702 const base = {bear: {legs: 4}, eagle: {legs: 3}}703 const next = produce(base, draft => {704 const animal = draft.bear705 animal.legs = animal.legs + 1706 draft.bear = animal707 draft.eagle = animal708 draft.cow = animal709 draft.kiddo = animal710 })711 expect(next).toEqual({712 bear: {legs: 5},713 eagle: {legs: 5},714 cow: {legs: 5},715 kiddo: {legs: 5}716 })717 expect(next.bear).toBe(next.cow)718 expect(next.kiddo).toBe(next.cow)719 })720721 if (useProxies)722 it("should not allow changing prototype", () => {723 produce({}, draft => {724 expect(() => Object.setPrototypeOf(draft, Array)).toThrow(Fixed unit testMichel Weststratebefore adoption41bb8b9
line 725why, one step back725 /does not support `setPrototype/Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 726–729why, one step back726 )727 })728 })729Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
line 730why, one step back730 it("'in' should work", () => {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 731–747why, one step back731 produce(createBaseState(), draft => {732 expect("anArray" in draft).toBe(true)733 expect(Reflect.has(draft, "anArray")).toBe(true)734735 expect("bla" in draft).toBe(false)736 expect(Reflect.has(draft, "bla")).toBe(false)737738 expect(0 in draft.anArray).toBe(true)739 expect("0" in draft.anArray).toBe(true)740 expect(Reflect.has(draft.anArray, 0)).toBe(true)741 expect(Reflect.has(draft.anArray, "0")).toBe(true)742743 expect(17 in draft.anArray).toBe(false)744 expect("17" in draft.anArray).toBe(false)745 expect(Reflect.has(draft.anArray, 17)).toBe(false)746 expect(Reflect.has(draft.anArray, "17")).toBe(false)747 })Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
lines 748–766why, one step back748 })749750 it("'this' should work - 1", () => {751 const base = {x: 3}752 const next1 = produce(base, function() {753 this.x = 4754 })755 expect(next1).not.toBe(base)756 expect(next1.x).toBe(4)757 })758759 it("'this' should work - 1", () => {760 const base = {x: 3}761 const incrementor = produce(function() {762 this.x = 4763 })764 const next1 = incrementor(base)765 expect(next1).not.toBe(base)766 expect(next1.x).toBe(4)Making several improvements at onceMichel Weststratebefore adoption1116ccf
lines 767–768why, one step back767 })768added test for #89Michel Weststratebefore adoptionf68a31a
lines 769–791why, one step back769 it("issue #89", () => {770 const state = {771 users: {772 existingID: {773 loading: false,774 value: "val"775 }776 }777 }778 const nextState = produce(state, draft => {779 draft.users.existingID = {780 ...draft.users.existingID,781 loading: true782 }783 })784 expect(nextState).toEqual({785 users: {786 existingID: {787 loading: true,788 value: "val"789 }790 }791 })added some additional tests to verify solution correctness of #86Michel Weststratebefore adoption681b162
lines 792–810why, one step back792 })793794 it("processes with lodash.set", () => {795 const base = [{id: 1, a: 1}]796 const result = produce(base, draft => {797 lodash.set(draft, "[0].a", 2)798 })799 expect(base[0].a).toEqual(1)800 expect(result[0].a).toEqual(2)801 })802803 it("processes with lodash.find", () => {804 const base = [{id: 1, a: 1}]805 const result = produce(base, draft => {806 const obj1 = lodash.find(draft, {id: 1})807 lodash.set(obj1, "a", 2)808 })809 expect(base[0].a).toEqual(1)810 expect(result[0].a).toEqual(2)Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 811–848why, one step back811 })812813 it("can produce from no state", () => {814 expect(815 produce(3, draft => {816 expect(draft).toBe(3)817 return 5818 })819 ).toBe(5)820 })821822 it("can return something new ", () => {823 const base = {x: 3}824 const res = produce(base, draft => {825 return {x: draft.x + 1}826 })827 expect(res).not.toBe(base)828 expect(res).toEqual({x: 4})829 })830831 it("can return the draft new ", () => {832 const base = {x: 3}833 const res = produce(base, draft => {834 draft.x = 4835 return draft836 })837 expect(res).not.toBe(base)838 expect(res).toEqual({x: 4})839 })840841 it("should throw if modifying the draft and returning something new", () => {842 const base = {x: 3}843 expect(() => {844 produce(base, draft => {845 draft.x = 4846 return {x: 5}847 })848 }).toThrow(/An immer producer returned a new value/)added test for #89Michel Weststratebefore adoptionf68a31a
lines 849–850why, one step back849 })850Fixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df
lines 851–858why, one step back851 it("should fix #116", () => {852 const nextState = produce([1, 2, 3], s => {853 s.pop()854 s.push(100)855 })856 expect(nextState).toEqual([1, 2, 100])857 })858Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
lines 859–905why, one step back859 it("should fix #117", () => {860 const reducer = (state, action) =>861 produce(state, draft => {862 switch (action.type) {863 case "SET_STARTING_DOTS":864 return draft.availableStartingDots.map(a => a)865 default:866 break867 }868 })869 const base = {870 availableStartingDots: [871 {dots: 4, count: 1},872 {dots: 3, count: 2},873 {dots: 2, count: 3},874 {dots: 1, count: 4}875 ]876 }877 const next = reducer(base, {type: "SET_STARTING_DOTS"})878 expect(next).toEqual(base.availableStartingDots)879 expect(next).not.toBe(base.availableStartingDots)880 })881882 it("should fix #117 - 2", () => {883 const reducer = (state, action) =>884 produce(state, draft => {885 switch (action.type) {886 case "SET_STARTING_DOTS":887 return {888 dots: draft.availableStartingDots.map(a => a)889 }890 default:891 break892 }893 })894 const base = {895 availableStartingDots: [896 {dots: 4, count: 1},897 {dots: 3, count: 2},898 {dots: 2, count: 3},899 {dots: 1, count: 4}900 ]901 }902 const next = reducer(base, {type: "SET_STARTING_DOTS"})903 expect(next).toEqual({dots: base.availableStartingDots})904 })905Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
lines 906–921why, one step back906 it("should return an unmodified primitive baseState (#148)", () => {907 const baseState = "some string"908 const nextState = produce(baseState, () => {909 /* no modification */910 })911 expect(nextState).toBe(baseState)912 })913914 it("should return an unmodified null baseState (#148)", () => {915 const baseState = null916 const nextState = produce(baseState, () => {917 /* no modification */918 })919 expect(nextState).toBe(baseState)920 })921added `np` for easier releasingMichel Weststratebefore adoption23bfaa2
lines 922–925why, one step back922 it("immer should have no dependencies", () => {923 expect(require("../package.json").dependencies).toEqual(undefined)924 })925Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 926–929why, one step back926 afterEach(() => {927 expect(baseState).toBe(origBaseState)928 expect(baseState).toEqual(createBaseState())929 })Make debugging workMichel Weststratebefore adoptiond0ef4ed
line 930why, one step back930Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 931why, one step back931 function createBaseState() {Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 932why, one step back932 const data = {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 933–940why, one step back933 anArray: [3, 2, {c: 3}, 1],934 aProp: "hi",935 anObject: {936 nested: {937 yummie: true938 },939 coffee: false940 }No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31
line 941why, one step back941 }Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 942why, one step back942 return freeze ? deepFreeze(data) : dataMake debugging workMichel Weststratebefore adoptiond0ef4ed
line 943why, one step back943 }Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 944why, one step back944 })Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
lines 945–949why, one step back945}946947function enumerableOnly(x) {948 // this can be done better...949 return JSON.parse(JSON.stringify(x))Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 950why, one step back950}