Skip to content
Myr

base.js

This file

Path

__tests__/base.js

602 lines · 21.3 KiB · JavaScript

Last changed by

Fixed debug setupMichel Weststratebefore adoption9fd1654

History of this file

Take it

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

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

One module to rule them all WIPMichel Weststratebefore adoptionb7647e4

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

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

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

More repo setupMichel Weststratebefore adoption1637393

line 4why, one step back
4

Making several improvements at onceMichel Weststratebefore adoption1116ccf

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

One module to rule them all WIPMichel Weststratebefore adoptionb7647e4

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

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 11why, one step back
11

One module to rule them all WIPMichel Weststratebefore adoptionb7647e4

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

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

lines 13–15why, one step back
13 describe(`base functionality - ${name}`, () => {
14 let baseState15 let origBaseState

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 16why, one step back
16

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

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

One module to rule them all WIPMichel Weststratebefore adoptionb7647e4

lines 18–19why, one step back
18 setAutoFreeze(freeze)
19 setUseProxies(useProxies)

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

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

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 22why, one step back
22

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

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

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

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

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

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

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

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

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

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

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

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

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

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

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 35why, one step back
35

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

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

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

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

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 42why, one step back
42

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

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

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

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

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

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

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

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

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

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

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

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

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

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

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

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

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

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

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

line 71why, one step back
71 })

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

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

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

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

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

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

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

line 83why, one step back
83

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

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

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

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

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

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

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("can delete props", () => {

`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–100why, one step back
96 delete s.anObject.nested
97 })98 expect(nextState).not.toBe(baseState)99 expect(nextState.anObject).not.toBe(baseState.anObject)100 expect(nextState.anObject.nested).toBe(undefined)

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

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

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

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

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

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

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

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

Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d

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

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

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

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

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

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

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

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

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

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

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

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

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

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

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

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

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

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

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

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

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

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

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

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

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

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

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

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 142why, one step back
142

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

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

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

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

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

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

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

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

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

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

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

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

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

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

lines 164–169why, one step back
164 s.anArray[2].c = 4
165 s.anArray.sort()166 s.anArray[3].c = 5167 })168 expect(nextState).not.toBe(baseState)169 expect(nextState.anArray).not.toBe(baseState.anArray)

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

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

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

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

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

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

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

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

lines 226–231why, one step back
226 const obj = s.anObject
227 delete s.anObject228 s.messy = obj229 })230 expect(nextState).not.toBe(baseState)231 expect(nextState.anArray).toBe(baseState.anArray)

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

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

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

lines 233–242why, one step back
233 anArray: [3, 2, {c: 3}, 1],
234 aProp: "hi",235 messy: {236 nested: {237 yummie: true238 },239 coffee: false240 }241 })242 expect(nextState.messy.nested).toBe(baseState.anObject.nested)

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

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

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

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

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

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

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

lines 247–254why, one step back
247 const obj = s.anObject
248 s.aProp = "hello"249 delete s.anObject250 obj.coffee = true251 s.messy = obj252 })253 expect(nextState).not.toBe(baseState)254 expect(nextState.anArray).toBe(baseState.anArray)

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

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

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

lines 256–265why, one step back
256 anArray: [3, 2, {c: 3}, 1],
257 aProp: "hello",258 messy: {259 nested: {260 yummie: true261 },262 coffee: true263 }264 })265 expect(nextState.messy.nested).toBe(baseState.anObject.nested)

Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e

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

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

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

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

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

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

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

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

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

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

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

One module to rule them all WIPMichel Weststratebefore adoptionb7647e4

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

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

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

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

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

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

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

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

line 288why, one step back
288

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

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

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

line 295why, one step back
295

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

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

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

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

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

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

Updated changelogMichel Weststratebefore adoption8f44348

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

Added a testcaseMichel Weststratebefore adoption1e468fd

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

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

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

Added a testcaseMichel Weststratebefore adoption1e468fd

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

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

lines 333–418why, one step back
333
334 it("should be able to get property descriptors from objects", () => {335 produce({a: 1}, draft => {336 expect("a" in draft).toBe(true)337 expect("b" in draft).toBe(false)338 expect(339 Reflect.ownKeys(draft).filter(x => typeof x === "string")340 ).toEqual(["a"])341342 expect(343 Object.getOwnPropertyDescriptor(draft, "a")344 ).toMatchObject({345 configurable: true,346 enumerable: true347 })348 draft.a = 2349 expect(350 Object.getOwnPropertyDescriptor(draft, "a")351 ).toMatchObject({352 configurable: true,353 enumerable: true354 })355 expect(356 Object.getOwnPropertyDescriptor(draft, "b")357 ).toBeUndefined()358 draft.b = 2359 expect(360 Object.getOwnPropertyDescriptor(draft, "b")361 ).toMatchObject({362 configurable: true,363 enumerable: true364 })365 expect("a" in draft).toBe(true)366 expect("b" in draft).toBe(true)367 expect(368 Reflect.ownKeys(draft).filter(x => typeof x === "string")369 ).toEqual(["a", "b"])370 })371 })372373 it("should be able to get property descriptors from arrays", () => {374 produce([1], draft => {375 expect(0 in draft).toBe(true)376 expect(1 in draft).toBe(false)377 expect("0" in draft).toBe(true)378 expect("1" in draft).toBe(false)379 expect(length in draft).toBe(true)380 expect(381 Reflect.ownKeys(draft).filter(x => typeof x === "string")382 ).toEqual(["0", "length"])383384 expect(385 Object.getOwnPropertyDescriptor(draft, "length")386 ).toMatchObject({387 configurable: false,388 enumerable: false389 })390 draft[0] = 2391 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(392 {393 configurable: true,394 enumerable: true395 }396 )397 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(398 {399 configurable: true,400 enumerable: true401 }402 )403 expect(404 Object.getOwnPropertyDescriptor(draft, 1)405 ).toBeUndefined()406 draft[1] = 2407 expect(Object.getOwnPropertyDescriptor(draft, 1)).toMatchObject(408 {409 configurable: true,410 enumerable: true411 }412 )413 expect(414 Reflect.ownKeys(draft).filter(x => typeof x === "string")415 ).toEqual(["0", "1", "length"])416 })417 })418

One module to rule them all WIPMichel Weststratebefore adoptionb7647e4

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

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

lines 420–430why, one step back
420 it("should not be possible to set property descriptors", () => {
421 expect(() => {422 produce({}, draft => {423 Object.defineProperty(draft, "xx", {424 enumerable: true,425 writeable: true,426 value: 2427 })428 })429 }).toThrowError(/not support/)430 })

One module to rule them all WIPMichel Weststratebefore adoptionb7647e4

line 431why, one step back
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–577why, one step back
487 it("should not throw error, see #53 - 4", () => {
488 const base = {bear: {age: 10}}489 const result = produce(base, draft => {490 draft.bear.legs = 4491 draft.room = {elephant: {kiddo: draft.bear}}492 })493 expect(result).toEqual({494 bear: {age: 10, legs: 4},495 room: {elephant: {kiddo: {age: 10, legs: 4}}}496 })497498 const result2 = produce(result, draft => {499 draft.bear.age = 11500 draft.room.elephant.kiddo.legs = 5501 })502 expect(result2).toEqual({503 bear: {age: 11, legs: 4},504 room: {elephant: {kiddo: {age: 10, legs: 5}}}505 })506 })507508 it("should handle dates correctly", () => {509 const data = {date: new Date()}510 const next = produce(data, draft => {511 draft.x = true512 })513 expect(next).toEqual({x: true, date: data.date})514 expect(next.date).toBe(data.date)515 const next2 = produce(next, draft => {516 draft.date.setYear(2015)517 })518 // This still holds; because produce won't proxy Date objects519 // and the original is actually modified!520 expect(next2).toEqual({x: true, date: data.date})521 expect(next2.date).toBe(next.date)522 expect(next2.date).toBe(data.date)523 expect(next2).toBe(next)524 })525526 it("should handle equality correctly - 1", () => {527 const base = {528 y: 3 / 0,529 z: NaN530 }531 const next = produce(base, draft => {532 draft.y = 4 / 0533 draft.z = NaN534 })535 expect(next).toEqual(base)536 expect(next).toBe(base)537 })538539 it("should handle equality correctly - 2", () => {540 const base = {541 x: -0542 }543 const next = produce(base, draft => {544 draft.x = +1545 })546 expect(next).not.toEqual(base)547 expect(next).not.toBe(base)548 })549550 it("should handle nested immer calls correctly - 1", () => {551 const bear = {paw: {honey: true}}552 const next = produce(bear, draft => {553 const paw2 = produce(bear.paw, draft => {554 draft.honey = false555 })556 expect(paw2.honey).toBe(false)557 expect(draft.paw.honey).toBe(true) // effects should not be visible outside558 })559 expect(next.paw.honey).toBe(true)560 expect(next).toBe(bear)561 })562563 it("should handle nested immer calls correctly - 2", () => {564 const bear = {paw: {honey: true}}565 const next = produce(bear, draft => {566 const paw2 = produce(bear.paw, draft => {567 draft.honey = false568 })569 expect(paw2.honey).toBe(false)570 expect(draft.paw.honey).toBe(true)571 draft.paw = paw2572 expect(draft.paw.honey).toBe(false)573 })574 expect(next.paw.honey).toBe(false)575 expect(next).not.toBe(bear)576 })577

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

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

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 582why, one step back
582

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

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

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

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

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

lines 585–592why, one step back
585 anArray: [3, 2, {c: 3}, 1],
586 aProp: "hi",587 anObject: {588 nested: {589 yummie: true590 },591 coffee: false592 }

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

line 593why, one step back
593 }

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

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

Make debugging workMichel Weststratebefore adoptiond0ef4ed

line 595why, one step back
595 }

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

line 596why, one step back
596 })

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

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

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

line 602why, one step back
602}