Skip to content
Myr

base.js

This file

Path

__tests__/base.js

603 lines · 21.3 KiB · JavaScript

Last changed by

Making several improvements at onceMichel Weststratebefore adoption1116ccf

History of this file

Take it

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 2–3why, one step back
2import * as immerProxy from "../src/immer"
3import * as immerEs5 from "../src/es5"

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

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

More repo setupMichel Weststratebefore adoption1637393

line 5why, one step back
5

Making several improvements at onceMichel Weststratebefore adoption1116ccf

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

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

lines 8–11why, one step back
8runBaseTest("proxy (no freeze)", immerProxy, false)
9runBaseTest("proxy (autofreeze)", immerProxy, true)10runBaseTest("es5 (no freeze)", immerEs5, false)11runBaseTest("es5 (autofreeze)", immerEs5, true)

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 12why, one step back
12

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

line 13why, one step back
13function runBaseTest(name, lib, freeze) {

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

line 14why, one step back
14 describe(`base functionality - ${name}`, () => {

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

line 15why, one step back
15 const produce = lib.default

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

lines 16–17why, one step back
16 let baseState
17 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 #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

line 20why, one step back
20 lib.setAutoFreeze(freeze)

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

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

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 23why, one step back
23

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

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

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

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

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

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

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

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

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

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

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

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

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

lines 31–35why, one step back
31 expect(s.aProp).toBe("hi")
32 expect(s.anObject.nested).toMatchObject({yummie: true})33 })34 expect(nextState).toBe(baseState)35 })

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 36why, one step back
36

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

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

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

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

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 43why, one step back
43

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

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

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

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

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

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

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

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

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

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

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

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

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

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

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

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

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

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

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

line 72why, one step back
72 })

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

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

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

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

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

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

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

line 84why, one step back
84

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

line 85why, one step back
85 it("can add 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–92why, one step back
87 s.anObject.cookie = {tasty: true}
88 })89 expect(nextState).not.toBe(baseState)90 expect(nextState.anObject).not.toBe(baseState.anObject)91 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)92 expect(nextState.anObject.cookie).toEqual({tasty: true})

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

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

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

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

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

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

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

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

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

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

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

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

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

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

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

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

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

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

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

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

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

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

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

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

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

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

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

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

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

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

lines 122–125why, one step back
122 s.anArray.slice()
123 })124 expect(nextState.anArray).toBe(baseState.anArray)125 expect(nextState).toBe(baseState)

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 126–127why, one step back
126 })
127

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

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

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

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

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

lines 130–134why, one step back
130 s.anArray[3] = true
131 })132 expect(nextState).not.toBe(baseState)133 expect(nextState.anArray).not.toBe(baseState.anArray)134 expect(nextState.anArray[3]).toEqual(true)

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 135–136why, one step back
135 })
136

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

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

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

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

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

lines 139–142why, one step back
139 s.anArray.splice(1, 1, "a", "b")
140 })141 expect(nextState).not.toBe(baseState)142 expect(nextState.anArray).not.toBe(baseState.anArray)

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 143why, one step back
143

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

lines 144–150why, one step back
144 expect(enumerableOnly(nextState.anArray)).toEqual([
145 3,146 "a",147 "b",148 {c: 3},149 1150 ])

Add support for arraysMichel Weststratebefore adoptiond3696cb

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

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

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

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

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

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

lines 155–162why, one step back
155 s.anArray.length = 3
156 })157 expect(nextState).not.toBe(baseState)158 expect(nextState.anObject).toBe(baseState.anObject)159 expect(nextState.anArray).not.toBe(baseState.anArray)160 expect(nextState.anArray).toEqual([3, 2, {c: 3}])161 })162

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

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

`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–170why, one step back
165 s.anArray[2].c = 4
166 s.anArray.sort()167 s.anArray[3].c = 5168 })169 expect(nextState).not.toBe(baseState)170 expect(nextState.anArray).not.toBe(baseState.anArray)

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

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

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

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

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

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

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

lines 176–196why, one step back
176 expect(Object.getOwnPropertyDescriptor(s, 0)).toBe(undefined)
177 s.unshift("x")178 expect(Object.getOwnPropertyDescriptor(s, 0)).toEqual({179 configurable: true,180 enumerable: true,181 value: "x",182 writable: true183 })184 expect(s.length).toBe(1)185 expect(s[0] === "x").toBe(true)186 })187 expect(nextState).toEqual(["x"])188 expect(Object.getOwnPropertyDescriptor(nextState, 0)).toEqual({189 configurable: !freeze,190 enumerable: true,191 value: "x",192 writable: !freeze193 })194 })195196 it("should support sorting arrays - 2", () => {

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

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

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

lines 198–215why, one step back
198 s.anArray.unshift("x")
199 s.anArray[3].c = 4200 s.anArray.sort()201 s.anArray[3].c = 5202 s.anArray.unshift("y")203 })204 expect(nextState).not.toBe(baseState)205 expect(nextState.anArray).not.toBe(baseState.anArray)206 expect(enumerableOnly(nextState.anArray)).toEqual([207 "y",208 1,209 2,210 3,211 {c: 5},212 "x"213 ])214 })215

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

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

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

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

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

lines 218–222why, one step back
218 s.anArray[2].test = true
219 })220 expect(nextState).not.toBe(baseState)221 expect(nextState.anArray).not.toBe(baseState.anArray)222 expect(nextState.anArray).toEqual([3, 2, {c: 3, test: true}, 1])

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 223–224why, one step back
223 })
224

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

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

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

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

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

lines 227–232why, one step back
227 const obj = s.anObject
228 delete s.anObject229 s.messy = obj230 })231 expect(nextState).not.toBe(baseState)232 expect(nextState.anArray).toBe(baseState.anArray)

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

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

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

lines 234–243why, one step back
234 anArray: [3, 2, {c: 3}, 1],
235 aProp: "hi",236 messy: {237 nested: {238 yummie: true239 },240 coffee: false241 }242 })243 expect(nextState.messy.nested).toBe(baseState.anObject.nested)

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 244–245why, one step back
244 })
245

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

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

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

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

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

lines 248–255why, one step back
248 const obj = s.anObject
249 s.aProp = "hello"250 delete s.anObject251 obj.coffee = true252 s.messy = obj253 })254 expect(nextState).not.toBe(baseState)255 expect(nextState.anArray).toBe(baseState.anArray)

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

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

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

lines 257–266why, one step back
257 anArray: [3, 2, {c: 3}, 1],
258 aProp: "hello",259 messy: {260 nested: {261 yummie: true262 },263 coffee: true264 }265 })266 expect(nextState.messy.nested).toBe(baseState.anObject.nested)

Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e

lines 267–268why, one step back
267 })
268

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

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

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

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

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

lines 271–275why, one step back
271 s.aProp = undefined
272 })273 expect(nextState).not.toBe(baseState)274 expect(baseState.aProp).toBe("hi")275 expect(nextState.aProp).toBe(undefined)

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

lines 276–277why, one step back
276 })
277

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

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

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

line 279why, one step back
279 if (lib === immerProxy) {

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

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

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

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

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

lines 283–288why, one step back
283 proxy = s
284 s.aProp = "hello"285 })286 expect(nextState).not.toBe(baseState)287 expect(baseState.aProp).toBe("hi")288 expect(nextState.aProp).toBe("hello")

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

line 289why, one step back
289

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

lines 290–295why, one step back
290 expect(() => {
291 proxy.aProp = "Hallo"292 }).toThrowError(/revoked/)293 expect(() => {294 const aProp = proxy.aProp295 }).toThrowError(/revoked/)

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

line 296why, one step back
296

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

lines 297–304why, one step back
297 expect(nextState).not.toBe(baseState)
298 expect(baseState.aProp).toBe("hi")299 expect(nextState.aProp).toBe("hello")300 })301 }302303 it("should revoke the proxy of the baseState after immer function is executed - 2", () => {304 let proxy

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

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

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

lines 306–315why, one step back
306 proxy = s.anObject
307 })308 expect(nextState).toBe(baseState)309 expect(() => {310 // In ES5 implemenation only protects existing props, but alas..311 proxy.coffee = "Hallo"312 }).toThrowError(/revoked/)313 expect(() => {314 const test = proxy.coffee315 }).toThrowError(/revoked/)

Updated changelogMichel Weststratebefore adoption8f44348

lines 316–317why, one step back
316 })
317

Added a testcaseMichel Weststratebefore adoption1e468fd

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

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

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

Added a testcaseMichel Weststratebefore adoption1e468fd

lines 320–333why, one step back
320 draft.anArray[0] = 5
321 draft.anArray.unshift("test")322 // sliced here; jest will also compare non-enumerable keys, which would include the immer Symbols323 expect(draft.anArray.slice()).toMatchObject([324 "test",325 5,326 2,327 {c: 3},328 1329 ])330 draft.stuffz = "coffee"331 expect(draft.stuffz).toBe("coffee")332 })333 })

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

lines 334–431why, one step back
334
335 it("should be able to get property descriptors from objects", () => {336 produce({a: 1}, draft => {337 expect("a" in draft).toBe(true)338 expect("b" in draft).toBe(false)339 expect(340 Reflect.ownKeys(draft).filter(x => typeof x === "string")341 ).toEqual(["a"])342343 expect(344 Object.getOwnPropertyDescriptor(draft, "a")345 ).toMatchObject({346 configurable: true,347 enumerable: true348 })349 draft.a = 2350 expect(351 Object.getOwnPropertyDescriptor(draft, "a")352 ).toMatchObject({353 configurable: true,354 enumerable: true355 })356 expect(357 Object.getOwnPropertyDescriptor(draft, "b")358 ).toBeUndefined()359 draft.b = 2360 expect(361 Object.getOwnPropertyDescriptor(draft, "b")362 ).toMatchObject({363 configurable: true,364 enumerable: true365 })366 expect("a" in draft).toBe(true)367 expect("b" in draft).toBe(true)368 expect(369 Reflect.ownKeys(draft).filter(x => typeof x === "string")370 ).toEqual(["a", "b"])371 })372 })373374 it("should be able to get property descriptors from arrays", () => {375 produce([1], draft => {376 expect(0 in draft).toBe(true)377 expect(1 in draft).toBe(false)378 expect("0" in draft).toBe(true)379 expect("1" in draft).toBe(false)380 expect(length in draft).toBe(true)381 expect(382 Reflect.ownKeys(draft).filter(x => typeof x === "string")383 ).toEqual(["0", "length"])384385 expect(386 Object.getOwnPropertyDescriptor(draft, "length")387 ).toMatchObject({388 configurable: false,389 enumerable: false390 })391 draft[0] = 2392 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(393 {394 configurable: true,395 enumerable: true396 }397 )398 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(399 {400 configurable: true,401 enumerable: true402 }403 )404 expect(405 Object.getOwnPropertyDescriptor(draft, 1)406 ).toBeUndefined()407 draft[1] = 2408 expect(Object.getOwnPropertyDescriptor(draft, 1)).toMatchObject(409 {410 configurable: true,411 enumerable: true412 }413 )414 expect(415 Reflect.ownKeys(draft).filter(x => typeof x === "string")416 ).toEqual(["0", "1", "length"])417 })418 })419420 if (lib === immerProxy)421 it("should not be possible to set property descriptors", () => {422 expect(() => {423 produce({}, draft => {424 Object.defineProperty(draft, "xx", {425 enumerable: true,426 writeable: true,427 value: 2428 })429 })430 }).toThrowError(/not support/)431 })

initial solutionMichel Weststratebefore adoptiond618d28

line 432why, one step back
432

Faster, simpler implementationMichel Weststratebefore adoptionc420f0d

lines 433–437why, one step back
433 it("should not throw error, see #53 - 1", () => {
434 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}435 const result = produce(base, draft => {436 draft.arr = draft.arr.filter(item => item.count > 2)437 })

initial solutionMichel Weststratebefore adoptiond618d28

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

Faster, simpler implementationMichel Weststratebefore adoptionc420f0d

lines 439–469why, one step back
439 expect(result).toEqual({
440 arr: [{count: 3}]441 })442 expect(result.arr[0]).toBe(base.arr[2])443 })444445 it("should not throw error, see #53 - 2", () => {446 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}447 const result = produce(base, draft => {448 draft.newArr = draft.arr.filter(item => item.count > 2)449 })450 expect(result.newArr[0].count).toEqual(3)451 expect(result.arr).toBe(base.arr)452 expect(result).toEqual({453 arr: [454 {455 count: 1456 },457 {458 count: 2459 },460 {461 count: 3462 }463 ],464 newArr: [465 {466 count: 3467 }468 ]469 })

Making several improvements at onceMichel Weststratebefore adoption1116ccf

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

Faster, simpler implementationMichel Weststratebefore adoptionc420f0d

lines 472–484why, one step back
472 })
473474 it("should not throw error, see #53 - 3", () => {475 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}476 const result = produce(base, draft => {477 draft.newArr = draft.arr.filter(item => item.count > 2)478 delete draft.arr479 })480 expect(result.newArr[0].count).toEqual(3)481 expect(result).toEqual({482 newArr: [{count: 3}]483 })484 expect(result.newArr[0]).toBe(base.arr[2])

initial solutionMichel Weststratebefore adoptiond618d28

line 485why, one step back
485 })

Added a testcaseMichel Weststratebefore adoption1e468fd

line 486why, one step back
486

Making several improvements at onceMichel Weststratebefore adoption1116ccf

lines 487–578why, one step back
487 it("should not throw error, see #53 - 4", () => {
488 debugger489 const base = {bear: {age: 10}}490 const result = produce(base, draft => {491 draft.bear.legs = 4492 draft.room = {elephant: {kiddo: draft.bear}}493 })494 expect(result).toEqual({495 bear: {age: 10, legs: 4},496 room: {elephant: {kiddo: {age: 10, legs: 4}}}497 })498499 const result2 = produce(result, draft => {500 draft.bear.age = 11501 draft.room.elephant.kiddo.legs = 5502 })503 expect(result2).toEqual({504 bear: {age: 11, legs: 4},505 room: {elephant: {kiddo: {age: 10, legs: 5}}}506 })507 })508509 it("should handle dates correctly", () => {510 const data = {date: new Date()}511 const next = produce(data, draft => {512 draft.x = true513 })514 expect(next).toEqual({x: true, date: data.date})515 expect(next.date).toBe(data.date)516 const next2 = produce(next, draft => {517 draft.date.setYear(2015)518 })519 // This still holds; because produce won't proxy Date objects520 // and the original is actually modified!521 expect(next2).toEqual({x: true, date: data.date})522 expect(next2.date).toBe(next.date)523 expect(next2.date).toBe(data.date)524 expect(next2).toBe(next)525 })526527 it("should handle equality correctly - 1", () => {528 const base = {529 y: 3 / 0,530 z: NaN531 }532 const next = produce(base, draft => {533 draft.y = 4 / 0534 draft.z = NaN535 })536 expect(next).toEqual(base)537 expect(next).toBe(base)538 })539540 it("should handle equality correctly - 2", () => {541 const base = {542 x: -0543 }544 const next = produce(base, draft => {545 draft.x = +1546 })547 expect(next).not.toEqual(base)548 expect(next).not.toBe(base)549 })550551 it("should handle nested immer calls correctly - 1", () => {552 const bear = {paw: {honey: true}}553 const next = produce(bear, draft => {554 const paw2 = produce(bear.paw, draft => {555 draft.honey = false556 })557 expect(paw2.honey).toBe(false)558 expect(draft.paw.honey).toBe(true) // effects should not be visible outside559 })560 expect(next.paw.honey).toBe(true)561 expect(next).toBe(bear)562 })563564 it("should handle nested immer calls correctly - 2", () => {565 const bear = {paw: {honey: true}}566 const next = produce(bear, draft => {567 const paw2 = produce(bear.paw, draft => {568 draft.honey = false569 })570 expect(paw2.honey).toBe(false)571 expect(draft.paw.honey).toBe(true)572 draft.paw = paw2573 expect(draft.paw.honey).toBe(false)574 })575 expect(next.paw.honey).toBe(false)576 expect(next).not.toBe(bear)577 })578

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

lines 579–582why, one step back
579 afterEach(() => {
580 expect(baseState).toBe(origBaseState)581 expect(baseState).toEqual(createBaseState())582 })

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 583why, one step back
583

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

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

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

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

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

lines 586–593why, one step back
586 anArray: [3, 2, {c: 3}, 1],
587 aProp: "hi",588 anObject: {589 nested: {590 yummie: true591 },592 coffee: false593 }

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

line 594why, one step back
594 }

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

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

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 596why, one step back
596 }

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

line 597why, one step back
597 })

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

lines 598–602why, one step back
598}
599600function enumerableOnly(x) {601 // this can be done better...602 return JSON.parse(JSON.stringify(x))

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

line 603why, one step back
603}