Skip to content
Myr

base.js

This file

Path

__tests__/base.js

850 lines · 29.5 KiB · JavaScript

Last changed by

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

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

lines 38–39why, one step back
38 it("should not return any value: thunk", () => {
39 const warning = jest.spyOn(console, "warn")

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

lines 40–43why, one step back
40 produce(baseState, () => ({bad: "don't do this"}))
41 produce(baseState, () => [1, 2, 3])42 produce(baseState, () => false)43 produce(baseState, () => "")

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 44why, one step back
44

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

lines 45–46why, one step back
45 expect(warning).toHaveBeenCalledTimes(4)
46 warning.mockClear()

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 47–48why, one step back
47 })
48

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

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

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

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

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

lines 51–57why, one step back
51 s.aProp = "hello world"
52 })53 expect(nextState).not.toBe(baseState)54 expect(baseState.aProp).toBe("hi")55 expect(nextState.aProp).toBe("hello world")56 // structural sharing?57 expect(nextState.nested).toBe(baseState.nested)

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 58–59why, one step back
58 })
59

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

lines 60–62why, one step back
60 if (
61 ("should preserve type",62 () => {

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

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

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

lines 64–72why, one step back
64 expect(Array.isArray(s)).toBe(true)
65 expect(s.protoType).toBe(Object)66 s.anArray.push(3)67 s.aProp = "hello world"68 expect(Array.isArray(s)).toBe(true)69 expect(s.protoType).toBe(Object)70 })71 expect(Array.isArray(nextState)).toBe(true)72 expect(nextState.protoType).toBe(Object)

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

line 73why, one step back
73 })

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

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

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

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

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

lines 77–84why, one step back
77 s.anObject.nested.yummie = false
78 })79 expect(nextState).not.toBe(baseState)80 expect(nextState.anObject).not.toBe(baseState.anObject)81 expect(baseState.anObject.nested.yummie).toBe(true)82 expect(nextState.anObject.nested.yummie).toBe(false)83 expect(nextState.anArray).toBe(baseState.anArray)84 })

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

line 85why, one step back
85

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

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

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

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

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

lines 88–93why, one step back
88 s.anObject.cookie = {tasty: true}
89 })90 expect(nextState).not.toBe(baseState)91 expect(nextState.anObject).not.toBe(baseState.anObject)92 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)93 expect(nextState.anObject.cookie).toEqual({tasty: true})

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 94–95why, one step back
94 })
95

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

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

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

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

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

lines 98–102why, one step back
98 delete s.anObject.nested
99 })100 expect(nextState).not.toBe(baseState)101 expect(nextState.anObject).not.toBe(baseState.anObject)102 expect(nextState.anObject.nested).toBe(undefined)

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 103–104why, one step back
103 })
104

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

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

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

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

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

lines 107–109why, one step back
107 s.aProp = "hi"
108 })109 expect(nextState).toBe(baseState)

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

lines 110–111why, one step back
110 })
111

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

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

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

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

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

lines 114–118why, one step back
114 s.aProp = "hello"
115 s.aProp = "hi"116 })117 expect(nextState).not.toBe(baseState)118 expect(nextState).toEqual(baseState)

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 119–120why, one step back
119 })
120

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

lines 121–156why, one step back
121 it("processes with for loop", () => {
122 const base = [{id: 1, a: 1}, {id: 2, a: 1}]123 const findById = (collection, id) => {124 for (const item of collection) {125 if (item.id === id) return item126 }127 return null128 }129 const result = produce(base, draft => {130 const obj1 = findById(draft, 1)131 const obj2 = findById(draft, 2)132 obj1.a = 2133 obj2.a = 2134 })135 expect(result[0].a).toEqual(2)136 expect(result[1].a).toEqual(2)137 })138139 it("works with objects without proto", () => {140 const base = Object.create(null)141 base.x = 1142 base.y = Object.create(null)143 base.y.y = 2144 expect(base.__proto__).toBe(undefined)145 const next = produce(base, draft => {146 draft.y.z = 3147 draft.y.y++148 draft.x++149 })150 expect(next).toEqual({151 x: 2,152 y: {y: 3, z: 3}153 })154 expect(next.__proto__).toBe(undefined)155 })156

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

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

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

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

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

lines 159–162why, one step back
159 s.anArray.slice()
160 })161 expect(nextState.anArray).toBe(baseState.anArray)162 expect(nextState).toBe(baseState)

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 163–164why, one step back
163 })
164

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

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

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

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

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

lines 167–171why, one step back
167 s.anArray[3] = true
168 })169 expect(nextState).not.toBe(baseState)170 expect(nextState.anArray).not.toBe(baseState.anArray)171 expect(nextState.anArray[3]).toEqual(true)

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 172–173why, one step back
172 })
173

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

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

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

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

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

lines 176–179why, one step back
176 s.anArray.splice(1, 1, "a", "b")
177 })178 expect(nextState).not.toBe(baseState)179 expect(nextState.anArray).not.toBe(baseState.anArray)

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 180why, one step back
180

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

lines 181–187why, one step back
181 expect(enumerableOnly(nextState.anArray)).toEqual([
182 3,183 "a",184 "b",185 {c: 3},186 1187 ])

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 188–189why, one step back
188 })
189

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

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

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

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

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

lines 192–199why, one step back
192 s.anArray.length = 3
193 })194 expect(nextState).not.toBe(baseState)195 expect(nextState.anObject).toBe(baseState.anObject)196 expect(nextState.anArray).not.toBe(baseState.anArray)197 expect(nextState.anArray).toEqual([3, 2, {c: 3}])198 })199

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

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

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

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

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

lines 202–207why, one step back
202 s.anArray[2].c = 4
203 s.anArray.sort()204 s.anArray[3].c = 5205 })206 expect(nextState).not.toBe(baseState)207 expect(nextState.anArray).not.toBe(baseState.anArray)

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

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 209–210why, one step back
209 })
210

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

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

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

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

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

lines 213–233why, one step back
213 expect(Object.getOwnPropertyDescriptor(s, 0)).toBe(undefined)
214 s.unshift("x")215 expect(Object.getOwnPropertyDescriptor(s, 0)).toEqual({216 configurable: true,217 enumerable: true,218 value: "x",219 writable: true220 })221 expect(s.length).toBe(1)222 expect(s[0] === "x").toBe(true)223 })224 expect(nextState).toEqual(["x"])225 expect(Object.getOwnPropertyDescriptor(nextState, 0)).toEqual({226 configurable: !freeze,227 enumerable: true,228 value: "x",229 writable: !freeze230 })231 })232233 it("should support sorting arrays - 2", () => {

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

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

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

lines 235–252why, one step back
235 s.anArray.unshift("x")
236 s.anArray[3].c = 4237 s.anArray.sort()238 s.anArray[3].c = 5239 s.anArray.unshift("y")240 })241 expect(nextState).not.toBe(baseState)242 expect(nextState.anArray).not.toBe(baseState.anArray)243 expect(enumerableOnly(nextState.anArray)).toEqual([244 "y",245 1,246 2,247 3,248 {c: 5},249 "x"250 ])251 })252

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

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

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

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

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

lines 255–259why, one step back
255 s.anArray[2].test = true
256 })257 expect(nextState).not.toBe(baseState)258 expect(nextState.anArray).not.toBe(baseState.anArray)259 expect(nextState.anArray).toEqual([3, 2, {c: 3, test: true}, 1])

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 260–261why, one step back
260 })
261

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

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

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

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

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

lines 264–269why, one step back
264 const obj = s.anObject
265 delete s.anObject266 s.messy = obj267 })268 expect(nextState).not.toBe(baseState)269 expect(nextState.anArray).toBe(baseState.anArray)

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

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

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

lines 271–280why, one step back
271 anArray: [3, 2, {c: 3}, 1],
272 aProp: "hi",273 messy: {274 nested: {275 yummie: true276 },277 coffee: false278 }279 })280 expect(nextState.messy.nested).toBe(baseState.anObject.nested)

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 281–282why, one step back
281 })
282

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

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

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

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

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

lines 285–292why, one step back
285 const obj = s.anObject
286 s.aProp = "hello"287 delete s.anObject288 obj.coffee = true289 s.messy = obj290 })291 expect(nextState).not.toBe(baseState)292 expect(nextState.anArray).toBe(baseState.anArray)

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

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

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

lines 294–303why, one step back
294 anArray: [3, 2, {c: 3}, 1],
295 aProp: "hello",296 messy: {297 nested: {298 yummie: true299 },300 coffee: true301 }302 })303 expect(nextState.messy.nested).toBe(baseState.anObject.nested)

Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e

lines 304–305why, one step back
304 })
305

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

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

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

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

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

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

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

lines 313–314why, one step back
313 })
314

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

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

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

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

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

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

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

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

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

lines 320–325why, one step back
320 proxy = s
321 s.aProp = "hello"322 })323 expect(nextState).not.toBe(baseState)324 expect(baseState.aProp).toBe("hi")325 expect(nextState.aProp).toBe("hello")

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

line 326why, one step back
326

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

lines 327–332why, one step back
327 expect(() => {
328 proxy.aProp = "Hallo"329 }).toThrowError(/revoked/)330 expect(() => {331 const aProp = proxy.aProp332 }).toThrowError(/revoked/)

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

line 333why, one step back
333

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

lines 334–341why, one step back
334 expect(nextState).not.toBe(baseState)
335 expect(baseState.aProp).toBe("hi")336 expect(nextState.aProp).toBe("hello")337 })338 }339340 it("should revoke the proxy of the baseState after immer function is executed - 2", () => {341 let proxy

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

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

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

lines 343–352why, one step back
343 proxy = s.anObject
344 })345 expect(nextState).toBe(baseState)346 expect(() => {347 // In ES5 implemenation only protects existing props, but alas..348 proxy.coffee = "Hallo"349 }).toThrowError(/revoked/)350 expect(() => {351 const test = proxy.coffee352 }).toThrowError(/revoked/)

Updated changelogMichel Weststratebefore adoption8f44348

lines 353–354why, one step back
353 })
354

Added a testcaseMichel Weststratebefore adoption1e468fd

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

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

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

Added a testcaseMichel Weststratebefore adoption1e468fd

lines 357–370why, one step back
357 draft.anArray[0] = 5
358 draft.anArray.unshift("test")359 // sliced here; jest will also compare non-enumerable keys, which would include the immer Symbols360 expect(draft.anArray.slice()).toMatchObject([361 "test",362 5,363 2,364 {c: 3},365 1366 ])367 draft.stuffz = "coffee"368 expect(draft.stuffz).toBe("coffee")369 })370 })

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

lines 371–456why, one step back
371
372 it("should be able to get property descriptors from objects", () => {373 produce({a: 1}, draft => {374 expect("a" in draft).toBe(true)375 expect("b" in draft).toBe(false)376 expect(377 Reflect.ownKeys(draft).filter(x => typeof x === "string")378 ).toEqual(["a"])379380 expect(381 Object.getOwnPropertyDescriptor(draft, "a")382 ).toMatchObject({383 configurable: true,384 enumerable: true385 })386 draft.a = 2387 expect(388 Object.getOwnPropertyDescriptor(draft, "a")389 ).toMatchObject({390 configurable: true,391 enumerable: true392 })393 expect(394 Object.getOwnPropertyDescriptor(draft, "b")395 ).toBeUndefined()396 draft.b = 2397 expect(398 Object.getOwnPropertyDescriptor(draft, "b")399 ).toMatchObject({400 configurable: true,401 enumerable: true402 })403 expect("a" in draft).toBe(true)404 expect("b" in draft).toBe(true)405 expect(406 Reflect.ownKeys(draft).filter(x => typeof x === "string")407 ).toEqual(["a", "b"])408 })409 })410411 it("should be able to get property descriptors from arrays", () => {412 produce([1], draft => {413 expect(0 in draft).toBe(true)414 expect(1 in draft).toBe(false)415 expect("0" in draft).toBe(true)416 expect("1" in draft).toBe(false)417 expect(length in draft).toBe(true)418 expect(419 Reflect.ownKeys(draft).filter(x => typeof x === "string")420 ).toEqual(["0", "length"])421422 expect(423 Object.getOwnPropertyDescriptor(draft, "length")424 ).toMatchObject({425 configurable: false,426 enumerable: false427 })428 draft[0] = 2429 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(430 {431 configurable: true,432 enumerable: true433 }434 )435 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(436 {437 configurable: true,438 enumerable: true439 }440 )441 expect(442 Object.getOwnPropertyDescriptor(draft, 1)443 ).toBeUndefined()444 draft[1] = 2445 expect(Object.getOwnPropertyDescriptor(draft, 1)).toMatchObject(446 {447 configurable: true,448 enumerable: true449 }450 )451 expect(452 Reflect.ownKeys(draft).filter(x => typeof x === "string")453 ).toEqual(["0", "1", "length"])454 })455 })456

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

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

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

lines 458–468why, one step back
458 it("should not be possible to set property descriptors", () => {
459 expect(() => {460 produce({}, draft => {461 Object.defineProperty(draft, "xx", {462 enumerable: true,463 writeable: true,464 value: 2465 })466 })467 }).toThrowError(/not support/)468 })

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

line 469why, one step back
469 }

initial solutionMichel Weststratebefore adoptiond618d28

line 470why, one step back
470

Faster, simpler implementationMichel Weststratebefore adoptionc420f0d

lines 471–475why, one step back
471 it("should not throw error, see #53 - 1", () => {
472 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}473 const result = produce(base, draft => {474 draft.arr = draft.arr.filter(item => item.count > 2)475 })

initial solutionMichel Weststratebefore adoptiond618d28

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

Faster, simpler implementationMichel Weststratebefore adoptionc420f0d

lines 477–507why, one step back
477 expect(result).toEqual({
478 arr: [{count: 3}]479 })480 expect(result.arr[0]).toBe(base.arr[2])481 })482483 it("should not throw error, see #53 - 2", () => {484 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}485 const result = produce(base, draft => {486 draft.newArr = draft.arr.filter(item => item.count > 2)487 })488 expect(result.newArr[0].count).toEqual(3)489 expect(result.arr).toBe(base.arr)490 expect(result).toEqual({491 arr: [492 {493 count: 1494 },495 {496 count: 2497 },498 {499 count: 3500 }501 ],502 newArr: [503 {504 count: 3505 }506 ]507 })

Making several improvements at onceMichel Weststratebefore adoption1116ccf

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

Faster, simpler implementationMichel Weststratebefore adoptionc420f0d

lines 510–522why, one step back
510 })
511512 it("should not throw error, see #53 - 3", () => {513 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}514 const result = produce(base, draft => {515 draft.newArr = draft.arr.filter(item => item.count > 2)516 delete draft.arr517 })518 expect(result.newArr[0].count).toEqual(3)519 expect(result).toEqual({520 newArr: [{count: 3}]521 })522 expect(result.newArr[0]).toBe(base.arr[2])

initial solutionMichel Weststratebefore adoptiond618d28

line 523why, one step back
523 })

Added a testcaseMichel Weststratebefore adoption1e468fd

line 524why, one step back
524

Making several improvements at onceMichel Weststratebefore adoption1116ccf

lines 525–545why, one step back
525 it("should not throw error, see #53 - 4", () => {
526 const base = {bear: {age: 10}}527 const result = produce(base, draft => {528 draft.bear.legs = 4529 draft.room = {elephant: {kiddo: draft.bear}}530 })531 expect(result).toEqual({532 bear: {age: 10, legs: 4},533 room: {elephant: {kiddo: {age: 10, legs: 4}}}534 })535536 const result2 = produce(result, draft => {537 draft.bear.age = 11538 draft.room.elephant.kiddo.legs = 5539 })540 expect(result2).toEqual({541 bear: {age: 11, legs: 4},542 room: {elephant: {kiddo: {age: 10, legs: 5}}}543 })544 })545

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

lines 546–594why, one step back
546 it("should not throw error #78", () => {
547 const base = {548 arr: [549 {550 id: "100",551 arr: [{id: "1", no: 1}, {id: "2", no: 2}]552 },553 {id: "3", no: 3}554 ]555 }556 const result = produce(base, draft => {557 const base = draft.arr[0]558 draft.arr = draft.arr.slice(0, 1)559 const newArr = {560 id: "101",561 arr: [base, {id: "3"}]562 }563 draft.arr.splice(0, 1, newArr)564 draft.arr[0].arr[1].no = 3565 })566 expect(result).toEqual({567 arr: [568 {569 id: "101",570 arr: [571 {572 id: "100",573 arr: [{id: "1", no: 1}, {id: "2", no: 2}]574 },575 {id: "3", no: 3}576 ]577 }578 ]579 })580 })581582 it("should not throw error #78 - 2", () => {583 const base = {584 arr: [{count: 1}, {count: 2}]585 }586 const result = produce(base, draft => {587 draft.arr = [draft.arr[1]]588 draft.arr[0].count = 1589 })590 expect(result).toEqual({591 arr: [{count: 1}]592 })593 })594

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

lines 595–607why, one step back
595 it("should handle constructor correctly", () => {
596 const base = {597 arr: new Array(),598 obj: new Object()599 }600 const result = produce(base, draft => {601 draft.arrConstructed = draft.arr.constructor(1)602 draft.objConstructed = draft.obj.constructor(1)603 })604 expect(result.arrConstructed).toEqual(new Array().constructor(1))605 expect(result.objConstructed).toEqual(new Object().constructor(1))606 })607

Making several improvements at onceMichel Weststratebefore adoption1116ccf

lines 608–675why, one step back
608 it("should handle dates correctly", () => {
609 const data = {date: new Date()}610 const next = produce(data, draft => {611 draft.x = true612 })613 expect(next).toEqual({x: true, date: data.date})614 expect(next.date).toBe(data.date)615 const next2 = produce(next, draft => {616 draft.date.setYear(2015)617 })618 // This still holds; because produce won't proxy Date objects619 // and the original is actually modified!620 expect(next2).toEqual({x: true, date: data.date})621 expect(next2.date).toBe(next.date)622 expect(next2.date).toBe(data.date)623 expect(next2).toBe(next)624 })625626 it("should handle equality correctly - 1", () => {627 const base = {628 y: 3 / 0,629 z: NaN630 }631 const next = produce(base, draft => {632 draft.y = 4 / 0633 draft.z = NaN634 })635 expect(next).toEqual(base)636 expect(next).toBe(base)637 })638639 it("should handle equality correctly - 2", () => {640 const base = {641 x: -0642 }643 const next = produce(base, draft => {644 draft.x = +1645 })646 expect(next).not.toEqual(base)647 expect(next).not.toBe(base)648 })649650 it("should handle nested immer calls correctly - 1", () => {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) // effects should not be visible outside658 })659 expect(next.paw.honey).toBe(true)660 expect(next).toBe(bear)661 })662663 it("should handle nested immer calls correctly - 2", () => {664 const bear = {paw: {honey: true}}665 const next = produce(bear, draft => {666 const paw2 = produce(bear.paw, draft => {667 draft.honey = false668 })669 expect(paw2.honey).toBe(false)670 expect(draft.paw.honey).toBe(true)671 draft.paw = paw2672 expect(draft.paw.honey).toBe(false)673 })674 expect(next.paw.honey).toBe(false)675 expect(next).not.toBe(bear)

Reproduced #66Michel Weststratebefore adoptiond5de885

lines 676–699why, one step back
676 })
677678 it("should not try to change immutable data, see #66", () => {679 const user = require("./test-data")680681 const base = {}682 const next = produce(base, draft => {683 draft.user = user684 })685 expect(next.user).toBe(user)686 expect(next).not.toBe(base)687 expect(next.user).toEqual(user)688 })689690 it("should not try to change immutable data, see #66 - 2", () => {691 const user = deepFreeze(cloneDeep(require("./test-data")))692693 const base = {}694 const next = produce(base, draft => {695 draft.user = user696 })697 expect(next.user).toBe(user)698 expect(next).not.toBe(base)699 expect(next.user).toEqual(user)

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

lines 700–742why, one step back
700 })
701702 if (freeze)703 it("should freeze new data well", () => {704 const base = {}705 const next = produce(base, draft => {706 draft.x = {y: [{z: true}]}707 })708 expect(Object.isFrozen(next)).toBe(true)709 expect(Object.isFrozen(next.x)).toBe(true)710 expect(Object.isFrozen(next.x.y)).toBe(true)711 expect(Object.isFrozen(next.x.y[0].z)).toBe(true)712 })713714 it("should structurally share identical objects in the tree", () => {715 const base = {bear: {legs: 4}, eagle: {legs: 3}}716 const next = produce(base, draft => {717 const animal = draft.bear718 animal.legs = animal.legs + 1719 draft.bear = animal720 draft.eagle = animal721 draft.cow = animal722 draft.kiddo = animal723 })724 expect(next).toEqual({725 bear: {legs: 5},726 eagle: {legs: 5},727 cow: {legs: 5},728 kiddo: {legs: 5}729 })730 expect(next.bear).toBe(next.cow)731 expect(next.kiddo).toBe(next.cow)732 })733734 if (useProxies)735 it("should not allow changing prototype", () => {736 produce({}, draft => {737 expect(() => Object.setPrototypeOf(draft, Array)).toThrow(738 /try this/739 )740 })741 })742

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

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

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

lines 744–760why, one step back
744 produce(createBaseState(), draft => {
745 expect("anArray" in draft).toBe(true)746 expect(Reflect.has(draft, "anArray")).toBe(true)747748 expect("bla" in draft).toBe(false)749 expect(Reflect.has(draft, "bla")).toBe(false)750751 expect(0 in draft.anArray).toBe(true)752 expect("0" in draft.anArray).toBe(true)753 expect(Reflect.has(draft.anArray, 0)).toBe(true)754 expect(Reflect.has(draft.anArray, "0")).toBe(true)755756 expect(17 in draft.anArray).toBe(false)757 expect("17" in draft.anArray).toBe(false)758 expect(Reflect.has(draft.anArray, 17)).toBe(false)759 expect(Reflect.has(draft.anArray, "17")).toBe(false)760 })

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

lines 761–779why, one step back
761 })
762763 it("'this' should work - 1", () => {764 const base = {x: 3}765 const next1 = produce(base, function() {766 this.x = 4767 })768 expect(next1).not.toBe(base)769 expect(next1.x).toBe(4)770 })771772 it("'this' should work - 1", () => {773 const base = {x: 3}774 const incrementor = produce(function() {775 this.x = 4776 })777 const next1 = incrementor(base)778 expect(next1).not.toBe(base)779 expect(next1.x).toBe(4)

Making several improvements at onceMichel Weststratebefore adoption1116ccf

lines 780–781why, one step back
780 })
781

added test for #89Michel Weststratebefore adoptionf68a31a

lines 782–804why, one step back
782 it("issue #89", () => {
783 const state = {784 users: {785 existingID: {786 loading: false,787 value: "val"788 }789 }790 }791 const nextState = produce(state, draft => {792 draft.users.existingID = {793 ...draft.users.existingID,794 loading: true795 }796 })797 expect(nextState).toEqual({798 users: {799 existingID: {800 loading: true,801 value: "val"802 }803 }804 })

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

lines 805–823why, one step back
805 })
806807 it("processes with lodash.set", () => {808 const base = [{id: 1, a: 1}]809 const result = produce(base, draft => {810 lodash.set(draft, "[0].a", 2)811 })812 expect(base[0].a).toEqual(1)813 expect(result[0].a).toEqual(2)814 })815816 it("processes with lodash.find", () => {817 const base = [{id: 1, a: 1}]818 const result = produce(base, draft => {819 const obj1 = lodash.find(draft, {id: 1})820 lodash.set(obj1, "a", 2)821 })822 expect(base[0].a).toEqual(1)823 expect(result[0].a).toEqual(2)

added test for #89Michel Weststratebefore adoptionf68a31a

lines 824–825why, one step back
824 })
825

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

lines 826–829why, one step back
826 afterEach(() => {
827 expect(baseState).toBe(origBaseState)828 expect(baseState).toEqual(createBaseState())829 })

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 830why, one step back
830

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

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

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

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

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

lines 833–840why, one step back
833 anArray: [3, 2, {c: 3}, 1],
834 aProp: "hi",835 anObject: {836 nested: {837 yummie: true838 },839 coffee: false840 }

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

line 841why, one step back
841 }

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

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

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 843why, one step back
843 }

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

line 844why, one step back
844 })

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

lines 845–849why, one step back
845}
846847function enumerableOnly(x) {848 // this can be done better...849 return JSON.parse(JSON.stringify(x))

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

line 850why, one step back
850}