Skip to content
Myr

base.js

This file

Path

__tests__/base.js

877 lines · 30.2 KiB · JavaScript

Last changed by

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

History of this file

Take it

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

line 1why, one step back
1"use strict"

Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667

line 2why, one step back
2import produce, {setAutoFreeze, setUseProxies} from "../src/immer"

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

line 3why, one step back
3import deepFreeze from "deep-freeze"

Reproduced #66Michel Weststratebefore adoptiond5de885

line 4why, one step back
4import cloneDeep from "lodash.clonedeep"

added some additional tests to verify solution correctness of #86Michel Weststratebefore adoption681b162

line 5why, one step back
5import * as lodash from "lodash"

More repo setupMichel Weststratebefore adoption1637393

line 6why, one step back
6

Making several improvements at onceMichel Weststratebefore adoption1116ccf

lines 7–8why, one step back
7jest.setTimeout(1000)
8

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 9–12why, one step back
9runBaseTest("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 back
13

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 14why, one step back
14function runBaseTest(name, useProxies, freeze) {

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 15–17why, one step back
15 describe(`base functionality - ${name}`, () => {
16 let baseState17 let origBaseState

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 18why, one step back
18

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 19why, one step back
19 beforeEach(() => {

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 20–21why, one step back
20 setAutoFreeze(freeze)
21 setUseProxies(useProxies)

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 22–23why, one step back
22 origBaseState = baseState = createBaseState()
23 })

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 24why, one step back
24

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 25why, one step back
25 it("should return the original without modifications", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 26why, one step back
26 const nextState = produce(baseState, () => {})

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 27why, one step back
27 expect(nextState).toBe(baseState)

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 28–29why, one step back
28 })
29

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 30why, one step back
30 it("should return the original without modifications when reading stuff", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 31why, one step back
31 const nextState = produce(baseState, s => {

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 32–36why, one step back
32 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 back
37

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 38why, one step back
38 it("should return a copy when modifying stuff", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 39why, one step back
39 const nextState = produce(baseState, s => {

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 40–46why, one step back
40 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 back
47 })
48

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 49–51why, one step back
49 if (
50 ("should preserve type",51 () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 52why, one step back
52 const nextState = produce(baseState, s => {

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 53–61why, one step back
53 expect(Array.isArray(s)).toBe(true)
54 expect(s.protoType).toBe(Object)55 s.anArray.push(3)56 s.aProp = "hello world"57 expect(Array.isArray(s)).toBe(true)58 expect(s.protoType).toBe(Object)59 })60 expect(Array.isArray(nextState)).toBe(true)61 expect(nextState.protoType).toBe(Object)

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 62why, one step back
62 })

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 63–64why, one step back
63 )
64 it("deep change bubbles up", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 65why, one step back
65 const nextState = produce(baseState, s => {

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 66–73why, one step back
66 s.anObject.nested.yummie = false
67 })68 expect(nextState).not.toBe(baseState)69 expect(nextState.anObject).not.toBe(baseState.anObject)70 expect(baseState.anObject.nested.yummie).toBe(true)71 expect(nextState.anObject.nested.yummie).toBe(false)72 expect(nextState.anArray).toBe(baseState.anArray)73 })

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

line 74why, one step back
74

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 75why, one step back
75 it("can add props", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 76why, one step back
76 const nextState = produce(baseState, s => {

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 77–82why, one step back
77 s.anObject.cookie = {tasty: true}
78 })79 expect(nextState).not.toBe(baseState)80 expect(nextState.anObject).not.toBe(baseState.anObject)81 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)82 expect(nextState.anObject.cookie).toEqual({tasty: true})

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 83–84why, one step back
83 })
84

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 85why, one step back
85 it("can delete props", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 86why, one step back
86 const nextState = produce(baseState, s => {

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 87–91why, one step back
87 delete s.anObject.nested
88 })89 expect(nextState).not.toBe(baseState)90 expect(nextState.anObject).not.toBe(baseState.anObject)91 expect(nextState.anObject.nested).toBe(undefined)

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 92–93why, one step back
92 })
93

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 94why, one step back
94 it("ignores single non-modification", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 95why, one step back
95 const nextState = produce(baseState, s => {

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 96–98why, one step back
96 s.aProp = "hi"
97 })98 expect(nextState).toBe(baseState)

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 99–100why, one step back
99 })
100

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 101why, one step back
101 it("processes single modification", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 102why, one step back
102 const nextState = produce(baseState, s => {

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 103–107why, one step back
103 s.aProp = "hello"
104 s.aProp = "hi"105 })106 expect(nextState).not.toBe(baseState)107 expect(nextState).toEqual(baseState)

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 108–109why, one step back
108 })
109

Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85

lines 110–145why, one step back
110 it("processes with for loop", () => {
111 const base = [{id: 1, a: 1}, {id: 2, a: 1}]112 const findById = (collection, id) => {113 for (const item of collection) {114 if (item.id === id) return item115 }116 return null117 }118 const result = produce(base, draft => {119 const obj1 = findById(draft, 1)120 const obj2 = findById(draft, 2)121 obj1.a = 2122 obj2.a = 2123 })124 expect(result[0].a).toEqual(2)125 expect(result[1].a).toEqual(2)126 })127128 it("works with objects without proto", () => {129 const base = Object.create(null)130 base.x = 1131 base.y = Object.create(null)132 base.y.y = 2133 expect(base.__proto__).toBe(undefined)134 const next = produce(base, draft => {135 draft.y.z = 3136 draft.y.y++137 draft.x++138 })139 expect(next).toEqual({140 x: 2,141 y: {y: 3, z: 3}142 })143 expect(next.__proto__).toBe(undefined)144 })145

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 146why, one step back
146 it("should support reading arrays", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 147why, one step back
147 const nextState = produce(baseState, s => {

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 148–151why, one step back
148 s.anArray.slice()
149 })150 expect(nextState.anArray).toBe(baseState.anArray)151 expect(nextState).toBe(baseState)

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 152–153why, one step back
152 })
153

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 154why, one step back
154 it("should support changing arrays", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 155why, one step back
155 const nextState = produce(baseState, s => {

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 156–160why, one step back
156 s.anArray[3] = true
157 })158 expect(nextState).not.toBe(baseState)159 expect(nextState.anArray).not.toBe(baseState.anArray)160 expect(nextState.anArray[3]).toEqual(true)

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 161–162why, one step back
161 })
162

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 163why, one step back
163 it("should support changing arrays - 2", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 164why, one step back
164 const nextState = produce(baseState, s => {

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 165–168why, one step back
165 s.anArray.splice(1, 1, "a", "b")
166 })167 expect(nextState).not.toBe(baseState)168 expect(nextState.anArray).not.toBe(baseState.anArray)

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 169why, one step back
169

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

lines 170–176why, one step back
170 expect(enumerableOnly(nextState.anArray)).toEqual([
171 3,172 "a",173 "b",174 {c: 3},175 1176 ])

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 177–178why, one step back
177 })
178

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

line 179why, one step back
179 it("can delete array items", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 180why, one step back
180 const nextState = produce(baseState, s => {

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 181–188why, one step back
181 s.anArray.length = 3
182 })183 expect(nextState).not.toBe(baseState)184 expect(nextState.anObject).toBe(baseState.anObject)185 expect(nextState.anArray).not.toBe(baseState.anArray)186 expect(nextState.anArray).toEqual([3, 2, {c: 3}])187 })188

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 189why, one step back
189 it("should support sorting arrays", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 190why, one step back
190 const nextState = produce(baseState, s => {

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 191–196why, one step back
191 s.anArray[2].c = 4
192 s.anArray.sort()193 s.anArray[3].c = 5194 })195 expect(nextState).not.toBe(baseState)196 expect(nextState.anArray).not.toBe(baseState.anArray)

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

line 197why, one step back
197 expect(enumerableOnly(nextState.anArray)).toEqual([1, 2, 3, {c: 5}])

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 198–199why, one step back
198 })
199

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

line 200why, one step back
200 it("should expose property descriptors", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 201why, one step back
201 const nextState = produce([], s => {

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

lines 202–222why, one step back
202 expect(Object.getOwnPropertyDescriptor(s, 0)).toBe(undefined)
203 s.unshift("x")204 expect(Object.getOwnPropertyDescriptor(s, 0)).toEqual({205 configurable: true,206 enumerable: true,207 value: "x",208 writable: true209 })210 expect(s.length).toBe(1)211 expect(s[0] === "x").toBe(true)212 })213 expect(nextState).toEqual(["x"])214 expect(Object.getOwnPropertyDescriptor(nextState, 0)).toEqual({215 configurable: !freeze,216 enumerable: true,217 value: "x",218 writable: !freeze219 })220 })221222 it("should support sorting arrays - 2", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 223why, one step back
223 const nextState = produce(baseState, s => {

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

lines 224–241why, one step back
224 s.anArray.unshift("x")
225 s.anArray[3].c = 4226 s.anArray.sort()227 s.anArray[3].c = 5228 s.anArray.unshift("y")229 })230 expect(nextState).not.toBe(baseState)231 expect(nextState.anArray).not.toBe(baseState.anArray)232 expect(enumerableOnly(nextState.anArray)).toEqual([233 "y",234 1,235 2,236 3,237 {c: 5},238 "x"239 ])240 })241

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 242why, one step back
242 it("should updating inside arrays", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 243why, one step back
243 const nextState = produce(baseState, s => {

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 244–248why, one step back
244 s.anArray[2].test = true
245 })246 expect(nextState).not.toBe(baseState)247 expect(nextState.anArray).not.toBe(baseState.anArray)248 expect(nextState.anArray).toEqual([3, 2, {c: 3, test: true}, 1])

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 249–250why, one step back
249 })
250

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 251why, one step back
251 it("reusing object should work", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 252why, one step back
252 const nextState = produce(baseState, s => {

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 253–258why, one step back
253 const obj = s.anObject
254 delete s.anObject255 s.messy = obj256 })257 expect(nextState).not.toBe(baseState)258 expect(nextState.anArray).toBe(baseState.anArray)

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

line 259why, one step back
259 expect(enumerableOnly(nextState)).toEqual({

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 260–269why, one step back
260 anArray: [3, 2, {c: 3}, 1],
261 aProp: "hi",262 messy: {263 nested: {264 yummie: true265 },266 coffee: false267 }268 })269 expect(nextState.messy.nested).toBe(baseState.anObject.nested)

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 270–271why, one step back
270 })
271

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 272why, one step back
272 it("refs should be transparent", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 273why, one step back
273 const nextState = produce(baseState, s => {

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 274–281why, one step back
274 const obj = s.anObject
275 s.aProp = "hello"276 delete s.anObject277 obj.coffee = true278 s.messy = obj279 })280 expect(nextState).not.toBe(baseState)281 expect(nextState.anArray).toBe(baseState.anArray)

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

line 282why, one step back
282 expect(enumerableOnly(nextState)).toEqual({

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 283–292why, one step back
283 anArray: [3, 2, {c: 3}, 1],
284 aProp: "hello",285 messy: {286 nested: {287 yummie: true288 },289 coffee: true290 }291 })292 expect(nextState.messy.nested).toBe(baseState.anObject.nested)

Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e

lines 293–294why, one step back
293 })
294

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 295why, one step back
295 it("should allow setting to undefined a defined draft property", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 296why, one step back
296 const nextState = produce(baseState, s => {

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 297–301why, one step back
297 s.aProp = undefined
298 })299 expect(nextState).not.toBe(baseState)300 expect(baseState.aProp).toBe("hi")301 expect(nextState.aProp).toBe(undefined)

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

lines 302–303why, one step back
302 })
303

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 304why, one step back
304 // ES implementation does't protect against all outside modifications, just some..

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 305why, one step back
305 if (useProxies) {

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 306–307why, one step back
306 it("should revoke the proxy of the baseState after immer function is executed", () => {
307 let proxy

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 308why, one step back
308 const nextState = produce(baseState, s => {

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 309–314why, one step back
309 proxy = s
310 s.aProp = "hello"311 })312 expect(nextState).not.toBe(baseState)313 expect(baseState.aProp).toBe("hi")314 expect(nextState.aProp).toBe("hello")

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

line 315why, one step back
315

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 316–321why, one step back
316 expect(() => {
317 proxy.aProp = "Hallo"318 }).toThrowError(/revoked/)319 expect(() => {320 const aProp = proxy.aProp321 }).toThrowError(/revoked/)

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

line 322why, one step back
322

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 323–330why, one step back
323 expect(nextState).not.toBe(baseState)
324 expect(baseState.aProp).toBe("hi")325 expect(nextState.aProp).toBe("hello")326 })327 }328329 it("should revoke the proxy of the baseState after immer function is executed - 2", () => {330 let proxy

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 331why, one step back
331 const nextState = produce(baseState, s => {

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 332–341why, one step back
332 proxy = s.anObject
333 })334 expect(nextState).toBe(baseState)335 expect(() => {336 // In ES5 implemenation only protects existing props, but alas..337 proxy.coffee = "Hallo"338 }).toThrowError(/revoked/)339 expect(() => {340 const test = proxy.coffee341 }).toThrowError(/revoked/)

Updated changelogMichel Weststratebefore adoption8f44348

lines 342–343why, one step back
342 })
343

Added a testcaseMichel Weststratebefore adoption1e468fd

line 344why, one step back
344 it("should reflect all changes made in the draft immediately", () => {

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 345why, one step back
345 produce(baseState, draft => {

Added a testcaseMichel Weststratebefore adoption1e468fd

lines 346–359why, one step back
346 draft.anArray[0] = 5
347 draft.anArray.unshift("test")348 // sliced here; jest will also compare non-enumerable keys, which would include the immer Symbols349 expect(draft.anArray.slice()).toMatchObject([350 "test",351 5,352 2,353 {c: 3},354 1355 ])356 draft.stuffz = "coffee"357 expect(draft.stuffz).toBe("coffee")358 })359 })

Fixed configurability of array.length on proxies. Fixes #50Michel Weststratebefore adoption1c7df26

lines 360–445why, one step back
360
361 it("should be able to get property descriptors from objects", () => {362 produce({a: 1}, draft => {363 expect("a" in draft).toBe(true)364 expect("b" in draft).toBe(false)365 expect(366 Reflect.ownKeys(draft).filter(x => typeof x === "string")367 ).toEqual(["a"])368369 expect(370 Object.getOwnPropertyDescriptor(draft, "a")371 ).toMatchObject({372 configurable: true,373 enumerable: true374 })375 draft.a = 2376 expect(377 Object.getOwnPropertyDescriptor(draft, "a")378 ).toMatchObject({379 configurable: true,380 enumerable: true381 })382 expect(383 Object.getOwnPropertyDescriptor(draft, "b")384 ).toBeUndefined()385 draft.b = 2386 expect(387 Object.getOwnPropertyDescriptor(draft, "b")388 ).toMatchObject({389 configurable: true,390 enumerable: true391 })392 expect("a" in draft).toBe(true)393 expect("b" in draft).toBe(true)394 expect(395 Reflect.ownKeys(draft).filter(x => typeof x === "string")396 ).toEqual(["a", "b"])397 })398 })399400 it("should be able to get property descriptors from arrays", () => {401 produce([1], draft => {402 expect(0 in draft).toBe(true)403 expect(1 in draft).toBe(false)404 expect("0" in draft).toBe(true)405 expect("1" in draft).toBe(false)406 expect(length in draft).toBe(true)407 expect(408 Reflect.ownKeys(draft).filter(x => typeof x === "string")409 ).toEqual(["0", "length"])410411 expect(412 Object.getOwnPropertyDescriptor(draft, "length")413 ).toMatchObject({414 configurable: false,415 enumerable: false416 })417 draft[0] = 2418 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(419 {420 configurable: true,421 enumerable: true422 }423 )424 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(425 {426 configurable: true,427 enumerable: true428 }429 )430 expect(431 Object.getOwnPropertyDescriptor(draft, 1)432 ).toBeUndefined()433 draft[1] = 2434 expect(Object.getOwnPropertyDescriptor(draft, 1)).toMatchObject(435 {436 configurable: true,437 enumerable: true438 }439 )440 expect(441 Reflect.ownKeys(draft).filter(x => typeof x === "string")442 ).toEqual(["0", "1", "length"])443 })444 })445

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 446why, one step back
446 if (useProxies === true) {

Fixed configurability of array.length on proxies. Fixes #50Michel Weststratebefore adoption1c7df26

lines 447–457why, one step back
447 it("should not be possible to set property descriptors", () => {
448 expect(() => {449 produce({}, draft => {450 Object.defineProperty(draft, "xx", {451 enumerable: true,452 writeable: true,453 value: 2454 })455 })456 }).toThrowError(/not support/)457 })

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 458why, one step back
458 }

initial solutionMichel Weststratebefore adoptiond618d28

line 459why, one step back
459

Faster, simpler implementationMichel Weststratebefore adoptionc420f0d

lines 460–464why, one step back
460 it("should not throw error, see #53 - 1", () => {
461 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}462 const result = produce(base, draft => {463 draft.arr = draft.arr.filter(item => item.count > 2)464 })

initial solutionMichel Weststratebefore adoptiond618d28

line 465why, one step back
465 expect(result.arr[0].count).toEqual(3)

Faster, simpler implementationMichel Weststratebefore adoptionc420f0d

lines 466–496why, one step back
466 expect(result).toEqual({
467 arr: [{count: 3}]468 })469 expect(result.arr[0]).toBe(base.arr[2])470 })471472 it("should not throw error, see #53 - 2", () => {473 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}474 const result = produce(base, draft => {475 draft.newArr = draft.arr.filter(item => item.count > 2)476 })477 expect(result.newArr[0].count).toEqual(3)478 expect(result.arr).toBe(base.arr)479 expect(result).toEqual({480 arr: [481 {482 count: 1483 },484 {485 count: 2486 },487 {488 count: 3489 }490 ],491 newArr: [492 {493 count: 3494 }495 ]496 })

Making several improvements at onceMichel Weststratebefore adoption1116ccf

lines 497–498why, one step back
497 expect(result.newArr[0]).toBe(base.arr[2])
498 expect(result.arr[2]).toBe(base.arr[2])

Faster, simpler implementationMichel Weststratebefore adoptionc420f0d

lines 499–511why, one step back
499 })
500501 it("should not throw error, see #53 - 3", () => {502 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}503 const result = produce(base, draft => {504 draft.newArr = draft.arr.filter(item => item.count > 2)505 delete draft.arr506 })507 expect(result.newArr[0].count).toEqual(3)508 expect(result).toEqual({509 newArr: [{count: 3}]510 })511 expect(result.newArr[0]).toBe(base.arr[2])

initial solutionMichel Weststratebefore adoptiond618d28

line 512why, one step back
512 })

Added a testcaseMichel Weststratebefore adoption1e468fd

line 513why, one step back
513

Making several improvements at onceMichel Weststratebefore adoption1116ccf

lines 514–534why, one step back
514 it("should not throw error, see #53 - 4", () => {
515 const base = {bear: {age: 10}}516 const result = produce(base, draft => {517 draft.bear.legs = 4518 draft.room = {elephant: {kiddo: draft.bear}}519 })520 expect(result).toEqual({521 bear: {age: 10, legs: 4},522 room: {elephant: {kiddo: {age: 10, legs: 4}}}523 })524525 const result2 = produce(result, draft => {526 draft.bear.age = 11527 draft.room.elephant.kiddo.legs = 5528 })529 expect(result2).toEqual({530 bear: {age: 11, legs: 4},531 room: {elephant: {kiddo: {age: 10, legs: 5}}}532 })533 })534

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

lines 535–583why, one step back
535 it("should not throw error #78", () => {
536 const base = {537 arr: [538 {539 id: "100",540 arr: [{id: "1", no: 1}, {id: "2", no: 2}]541 },542 {id: "3", no: 3}543 ]544 }545 const result = produce(base, draft => {546 const base = draft.arr[0]547 draft.arr = draft.arr.slice(0, 1)548 const newArr = {549 id: "101",550 arr: [base, {id: "3"}]551 }552 draft.arr.splice(0, 1, newArr)553 draft.arr[0].arr[1].no = 3554 })555 expect(result).toEqual({556 arr: [557 {558 id: "101",559 arr: [560 {561 id: "100",562 arr: [{id: "1", no: 1}, {id: "2", no: 2}]563 },564 {id: "3", no: 3}565 ]566 }567 ]568 })569 })570571 it("should not throw error #78 - 2", () => {572 const base = {573 arr: [{count: 1}, {count: 2}]574 }575 const result = produce(base, draft => {576 draft.arr = [draft.arr[1]]577 draft.arr[0].count = 1578 })579 expect(result).toEqual({580 arr: [{count: 1}]581 })582 })583

Merge pull request #81 from iruca3/fix/treating_constructor_on_proxyMichel Weststratebefore adoptionad5a86a

lines 584–596why, one step back
584 it("should handle constructor correctly", () => {
585 const base = {586 arr: new Array(),587 obj: new Object()588 }589 const result = produce(base, draft => {590 draft.arrConstructed = draft.arr.constructor(1)591 draft.objConstructed = draft.obj.constructor(1)592 })593 expect(result.arrConstructed).toEqual(new Array().constructor(1))594 expect(result.objConstructed).toEqual(new Object().constructor(1))595 })596

Making several improvements at onceMichel Weststratebefore adoption1116ccf

lines 597–664why, one step back
597 it("should handle dates correctly", () => {
598 const data = {date: new Date()}599 const next = produce(data, draft => {600 draft.x = true601 })602 expect(next).toEqual({x: true, date: data.date})603 expect(next.date).toBe(data.date)604 const next2 = produce(next, draft => {605 draft.date.setYear(2015)606 })607 // This still holds; because produce won't proxy Date objects608 // and the original is actually modified!609 expect(next2).toEqual({x: true, date: data.date})610 expect(next2.date).toBe(next.date)611 expect(next2.date).toBe(data.date)612 expect(next2).toBe(next)613 })614615 it("should handle equality correctly - 1", () => {616 const base = {617 y: 3 / 0,618 z: NaN619 }620 const next = produce(base, draft => {621 draft.y = 4 / 0622 draft.z = NaN623 })624 expect(next).toEqual(base)625 expect(next).toBe(base)626 })627628 it("should handle equality correctly - 2", () => {629 const base = {630 x: -0631 }632 const next = produce(base, draft => {633 draft.x = +1634 })635 expect(next).not.toEqual(base)636 expect(next).not.toBe(base)637 })638639 it("should handle nested immer calls correctly - 1", () => {640 const bear = {paw: {honey: true}}641 const next = produce(bear, draft => {642 const paw2 = produce(bear.paw, draft => {643 draft.honey = false644 })645 expect(paw2.honey).toBe(false)646 expect(draft.paw.honey).toBe(true) // effects should not be visible outside647 })648 expect(next.paw.honey).toBe(true)649 expect(next).toBe(bear)650 })651652 it("should handle nested immer calls correctly - 2", () => {653 const bear = {paw: {honey: true}}654 const next = produce(bear, draft => {655 const paw2 = produce(bear.paw, draft => {656 draft.honey = false657 })658 expect(paw2.honey).toBe(false)659 expect(draft.paw.honey).toBe(true)660 draft.paw = paw2661 expect(draft.paw.honey).toBe(false)662 })663 expect(next.paw.honey).toBe(false)664 expect(next).not.toBe(bear)

Reproduced #66Michel Weststratebefore adoptiond5de885

lines 665–688why, one step back
665 })
666667 it("should not try to change immutable data, see #66", () => {668 const user = require("./test-data")669670 const base = {}671 const next = produce(base, draft => {672 draft.user = user673 })674 expect(next.user).toBe(user)675 expect(next).not.toBe(base)676 expect(next.user).toEqual(user)677 })678679 it("should not try to change immutable data, see #66 - 2", () => {680 const user = deepFreeze(cloneDeep(require("./test-data")))681682 const base = {}683 const next = produce(base, draft => {684 draft.user = user685 })686 expect(next.user).toBe(user)687 expect(next).not.toBe(base)688 expect(next.user).toEqual(user)

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 689–731why, one step back
689 })
690691 if (freeze)692 it("should freeze new data well", () => {693 const base = {}694 const next = produce(base, draft => {695 draft.x = {y: [{z: true}]}696 })697 expect(Object.isFrozen(next)).toBe(true)698 expect(Object.isFrozen(next.x)).toBe(true)699 expect(Object.isFrozen(next.x.y)).toBe(true)700 expect(Object.isFrozen(next.x.y[0].z)).toBe(true)701 })702703 it("should structurally share identical objects in the tree", () => {704 const base = {bear: {legs: 4}, eagle: {legs: 3}}705 const next = produce(base, draft => {706 const animal = draft.bear707 animal.legs = animal.legs + 1708 draft.bear = animal709 draft.eagle = animal710 draft.cow = animal711 draft.kiddo = animal712 })713 expect(next).toEqual({714 bear: {legs: 5},715 eagle: {legs: 5},716 cow: {legs: 5},717 kiddo: {legs: 5}718 })719 expect(next.bear).toBe(next.cow)720 expect(next.kiddo).toBe(next.cow)721 })722723 if (useProxies)724 it("should not allow changing prototype", () => {725 produce({}, draft => {726 expect(() => Object.setPrototypeOf(draft, Array)).toThrow(727 /try this/728 )729 })730 })731

Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1

line 732why, one step back
732 it("'in' should work", () => {

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 733–749why, one step back
733 produce(createBaseState(), draft => {
734 expect("anArray" in draft).toBe(true)735 expect(Reflect.has(draft, "anArray")).toBe(true)736737 expect("bla" in draft).toBe(false)738 expect(Reflect.has(draft, "bla")).toBe(false)739740 expect(0 in draft.anArray).toBe(true)741 expect("0" in draft.anArray).toBe(true)742 expect(Reflect.has(draft.anArray, 0)).toBe(true)743 expect(Reflect.has(draft.anArray, "0")).toBe(true)744745 expect(17 in draft.anArray).toBe(false)746 expect("17" in draft.anArray).toBe(false)747 expect(Reflect.has(draft.anArray, 17)).toBe(false)748 expect(Reflect.has(draft.anArray, "17")).toBe(false)749 })

Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1

lines 750–768why, one step back
750 })
751752 it("'this' should work - 1", () => {753 const base = {x: 3}754 const next1 = produce(base, function() {755 this.x = 4756 })757 expect(next1).not.toBe(base)758 expect(next1.x).toBe(4)759 })760761 it("'this' should work - 1", () => {762 const base = {x: 3}763 const incrementor = produce(function() {764 this.x = 4765 })766 const next1 = incrementor(base)767 expect(next1).not.toBe(base)768 expect(next1.x).toBe(4)

Making several improvements at onceMichel Weststratebefore adoption1116ccf

lines 769–770why, one step back
769 })
770

added test for #89Michel Weststratebefore adoptionf68a31a

lines 771–793why, one step back
771 it("issue #89", () => {
772 const state = {773 users: {774 existingID: {775 loading: false,776 value: "val"777 }778 }779 }780 const nextState = produce(state, draft => {781 draft.users.existingID = {782 ...draft.users.existingID,783 loading: true784 }785 })786 expect(nextState).toEqual({787 users: {788 existingID: {789 loading: true,790 value: "val"791 }792 }793 })

added some additional tests to verify solution correctness of #86Michel Weststratebefore adoption681b162

lines 794–812why, one step back
794 })
795796 it("processes with lodash.set", () => {797 const base = [{id: 1, a: 1}]798 const result = produce(base, draft => {799 lodash.set(draft, "[0].a", 2)800 })801 expect(base[0].a).toEqual(1)802 expect(result[0].a).toEqual(2)803 })804805 it("processes with lodash.find", () => {806 const base = [{id: 1, a: 1}]807 const result = produce(base, draft => {808 const obj1 = lodash.find(draft, {id: 1})809 lodash.set(obj1, "a", 2)810 })811 expect(base[0].a).toEqual(1)812 expect(result[0].a).toEqual(2)

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

lines 813–850why, one step back
813 })
814815 it("can produce from no state", () => {816 expect(817 produce(3, draft => {818 expect(draft).toBe(3)819 return 5820 })821 ).toBe(5)822 })823824 it("can return something new ", () => {825 const base = {x: 3}826 const res = produce(base, draft => {827 return {x: draft.x + 1}828 })829 expect(res).not.toBe(base)830 expect(res).toEqual({x: 4})831 })832833 it("can return the draft new ", () => {834 const base = {x: 3}835 const res = produce(base, draft => {836 draft.x = 4837 return draft838 })839 expect(res).not.toBe(base)840 expect(res).toEqual({x: 4})841 })842843 it("should throw if modifying the draft and returning something new", () => {844 const base = {x: 3}845 expect(() => {846 produce(base, draft => {847 draft.x = 4848 return {x: 5}849 })850 }).toThrow(/An immer producer returned a new value/)

added test for #89Michel Weststratebefore adoptionf68a31a

lines 851–852why, one step back
851 })
852

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 853–856why, one step back
853 afterEach(() => {
854 expect(baseState).toBe(origBaseState)855 expect(baseState).toEqual(createBaseState())856 })

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 857why, one step back
857

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 858why, one step back
858 function createBaseState() {

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

line 859why, one step back
859 const data = {

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

lines 860–867why, one step back
860 anArray: [3, 2, {c: 3}, 1],
861 aProp: "hi",862 anObject: {863 nested: {864 yummie: true865 },866 coffee: false867 }

No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31

line 868why, one step back
868 }

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

line 869why, one step back
869 return freeze ? deepFreeze(data) : data

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 870why, one step back
870 }

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 871why, one step back
871 })

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

lines 872–876why, one step back
872}
873874function enumerableOnly(x) {875 // this can be done better...876 return JSON.parse(JSON.stringify(x))

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 877why, one step back
877}