Skip to content
Myr

base.js

This file

Path

__tests__/base.js

1,478 lines · 53.5 KiB · JavaScript

Last changed by

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

History of this file

Take it

The walk stopped after 200 commits: lines older than d671c22 point at it.

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

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

feat: limited class supportaleclarsonbefore adoption9877d64

line 2why, one step back
2import {Immer, nothing, original, isDraft, immerable} from "../src/index"

fix: revert Map supportAlec Larsonbefore adoption47529d3

line 3why, one step back
3import {each, shallowCopy, isEnumerable} from "../src/common"

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 4–9why, one step back
4import deepFreeze from "deep-freeze"
5import cloneDeep from "lodash.clonedeep"6import * as lodash from "lodash"78jest.setTimeout(1000)9

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 10–13why, one step back
10test("immer should have no dependencies", () => {
11 expect(require("../package.json").dependencies).toBeUndefined()12})13

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 14–20why, one step back
14runBaseTest("proxy (no freeze)", true, false)
15runBaseTest("proxy (autofreeze)", true, true)16runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)17runBaseTest("es5 (no freeze)", false, false)18runBaseTest("es5 (autofreeze)", false, true)19runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)20

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 21why, one step back
21function runBaseTest(name, useProxies, autoFreeze, useListener) {

test: patch the `produce` function for interweaved Immer instances tooaleclarsonbefore adoption31bbe64

lines 22–23why, one step back
22 const listener = useListener ? function() {} : undefined
23 const {produce} = createPatchedImmer({

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 24–27why, one step back
24 useProxies,
25 autoFreeze26 })27

test: patch the `produce` function for interweaved Immer instances tooaleclarsonbefore adoption31bbe64

lines 28–31why, one step back
28 // When `useListener` is true, append a function to the arguments of every
29 // uncurried `produce` call in every test. This makes tests easier to read.30 function createPatchedImmer(options) {31 const immer = new Immer(options)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 32why, one step back
32

test: patch the `produce` function for interweaved Immer instances tooaleclarsonbefore adoption31bbe64

lines 33–41why, one step back
33 const {produce} = immer
34 immer.produce = (...args) =>35 typeof args[1] === "function" && args.length < 336 ? produce(...args, listener)37 : produce(...args)3839 return immer40 }41

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 42–49why, one step back
42 describe(`base functionality - ${name}`, () => {
43 let baseState44 let origBaseState4546 beforeEach(() => {47 origBaseState = baseState = createBaseState()48 })49

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 50why, one step back
50 it("returns the original state when no changes are made", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 51–54why, one step back
51 const nextState = produce(baseState, s => {
52 expect(s.aProp).toBe("hi")53 expect(s.anObject.nested).toMatchObject({yummie: true})54 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 55–57why, one step back
55 expect(nextState).toBe(baseState)
56 })57

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 58–59why, one step back
58 it("does structural sharing", () => {
59 const random = Math.random()

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 61why, one step back
61 s.aProp = random

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 62why, one step back
62 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

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

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 64why, one step back
64 expect(nextState.aProp).toBe(random)

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 65–68why, one step back
65 expect(nextState.nested).toBe(baseState.nested)
66 })6768 it("deep change bubbles up", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 69–71why, one step back
69 const nextState = produce(baseState, s => {
70 s.anObject.nested.yummie = false71 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 72–79why, one step back
72 expect(nextState).not.toBe(baseState)
73 expect(nextState.anObject).not.toBe(baseState.anObject)74 expect(baseState.anObject.nested.yummie).toBe(true)75 expect(nextState.anObject.nested.yummie).toBe(false)76 expect(nextState.anArray).toBe(baseState.anArray)77 })7879 it("can add props", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 80–82why, one step back
80 const nextState = produce(baseState, s => {
81 s.anObject.cookie = {tasty: true}82 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 83–89why, one step back
83 expect(nextState).not.toBe(baseState)
84 expect(nextState.anObject).not.toBe(baseState.anObject)85 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)86 expect(nextState.anObject.cookie).toEqual({tasty: true})87 })8889 it("can delete props", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 90–92why, one step back
90 const nextState = produce(baseState, s => {
91 delete s.anObject.nested92 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 93–97why, one step back
93 expect(nextState).not.toBe(baseState)
94 expect(nextState.anObject).not.toBe(baseState.anObject)95 expect(nextState.anObject.nested).toBe(undefined)96 })97

test: deleting props added in the produceraleclarsonbefore adoption691e73b

lines 98–99why, one step back
98 // Found by: https://github.com/mweststrate/immer/pull/267
99 it("can delete props added in the producer", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 100–103why, one step back
100 const nextState = produce(baseState, s => {
101 s.anObject.test = true102 delete s.anObject.test103 })

test: deleting props added in the produceraleclarsonbefore adoption691e73b

lines 104–112why, one step back
104 if (useProxies) {
105 expect(nextState).not.toBe(baseState)106 expect(nextState).toEqual(baseState)107 } else {108 // The copy is avoided in ES5.109 expect(nextState).toBe(baseState)110 }111 })112

test: delete then set an existing propertyAlec Larsonbefore adoption5121d36

lines 113–138why, one step back
113 // Found by: https://github.com/mweststrate/immer/issues/328
114 it("can set a property that was just deleted", () => {115 const baseState = {a: 1}116 const nextState = produce(baseState, s => {117 delete s.a118 s.a = 2119 })120 expect(nextState.a).toBe(2)121 })122123 it("can set a property to its original value after deleting it", () => {124 const baseState = {a: {b: 1}}125 const nextState = produce(baseState, s => {126 const a = s.a127 delete s.a128 s.a = a129 })130 if (useProxies) {131 expect(nextState).not.toBe(baseState)132 expect(nextState).toEqual(baseState)133 } else {134 // The copy is avoided in ES5.135 expect(nextState).toBe(baseState)136 }137 })138

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 139–141why, one step back
139 it("can get property descriptors", () => {
140 const getDescriptor = Object.getOwnPropertyDescriptor141 const baseState = deepFreeze([{a: 1}])

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

lines 142–148why, one step back
142 produce(baseState, arr => {
143 const obj = arr[0]144 const desc = {145 configurable: true,146 enumerable: true,147 ...(useProxies && {writable: true})148 }

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 149why, one step back
149

chore: improve more testsaleclarsonbefore adoption4f59b12

line 150why, one step back
150 // Known property

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

lines 151–152why, one step back
151 expect(getDescriptor(obj, "a")).toMatchObject(desc)
152 expect(getDescriptor(arr, 0)).toMatchObject(desc)

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 153–155why, one step back
153
154 // Deleted property155 delete obj.a

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

line 156why, one step back
156 arr.pop()

chore: improve more testsaleclarsonbefore adoption4f59b12

line 157why, one step back
157 expect(getDescriptor(obj, "a")).toBeUndefined()

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

line 158why, one step back
158 expect(getDescriptor(arr, 0)).toBeUndefined()

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 159–161why, one step back
159
160 // Unknown property161 expect(getDescriptor(obj, "b")).toBeUndefined()

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

line 162why, one step back
162 expect(getDescriptor(arr, 100)).toBeUndefined()

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 163–165why, one step back
163
164 // Added property165 obj.b = 2

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

line 166why, one step back
166 arr[100] = 1

chore: improve more testsaleclarsonbefore adoption4f59b12

line 167why, one step back
167 expect(getDescriptor(obj, "b")).toBeDefined()

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

line 168why, one step back
168 expect(getDescriptor(arr, 100)).toBeDefined()

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 169why, one step back
169 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

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

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 172–178why, one step back
172 describe("array drafts", () => {
173 it("supports Array.isArray()", () => {174 const nextState = produce(baseState, s => {175 expect(Array.isArray(s.anArray)).toBeTruthy()176 s.anArray.push(1)177 })178 expect(Array.isArray(nextState.anArray)).toBeTruthy()

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 179–180why, one step back
179 })
180

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 181–186why, one step back
181 it("supports index access", () => {
182 const value = baseState.anArray[0]183 const nextState = produce(baseState, s => {184 expect(s.anArray[0]).toBe(value)185 })186 expect(nextState).toBe(baseState)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 187why, one step back
187 })

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 188–204why, one step back
188
189 it("supports iteration", () => {190 const base = [{id: 1, a: 1}, {id: 2, a: 1}]191 const findById = (collection, id) => {192 for (const item of collection) {193 if (item.id === id) return item194 }195 return null196 }197 const result = produce(base, draft => {198 const obj1 = findById(draft, 1)199 const obj2 = findById(draft, 2)200 obj1.a = 2201 obj2.a = 2202 })203 expect(result[0].a).toEqual(2)204 expect(result[1].a).toEqual(2)

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 205–206why, one step back
205 })
206

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 207–213why, one step back
207 it("can assign an index via bracket notation", () => {
208 const nextState = produce(baseState, s => {209 s.anArray[3] = true210 })211 expect(nextState).not.toBe(baseState)212 expect(nextState.anArray).not.toBe(baseState.anArray)213 expect(nextState.anArray[3]).toEqual(true)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 214why, one step back
214 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 215why, one step back
215

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 216–222why, one step back
216 it("can use splice() to both add and remove items", () => {
217 const nextState = produce(baseState, s => {218 s.anArray.splice(1, 1, "a", "b")219 })220 expect(nextState.anArray).not.toBe(baseState.anArray)221 expect(nextState.anArray[1]).toBe("a")222 expect(nextState.anArray[2]).toBe("b")

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 223why, one step back
223 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 224why, one step back
224

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 225–231why, one step back
225 it("can truncate via the length property", () => {
226 const baseLength = baseState.anArray.length227 const nextState = produce(baseState, s => {228 s.anArray.length = baseLength - 1229 })230 expect(nextState.anArray).not.toBe(baseState.anArray)231 expect(nextState.anArray.length).toBe(baseLength - 1)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 232why, one step back
232 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 233why, one step back
233

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 234–241why, one step back
234 it("can extend via the length property", () => {
235 const baseLength = baseState.anArray.length236 const nextState = produce(baseState, s => {237 s.anArray.length = baseLength + 1238 })239 expect(nextState.anArray).not.toBe(baseState.anArray)240 expect(nextState.anArray.length).toBe(baseLength + 1)241 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 242why, one step back
242

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 243–249why, one step back
243 // Reported here: https://github.com/mweststrate/immer/issues/116
244 it("can pop then push", () => {245 const nextState = produce([1, 2, 3], s => {246 s.pop()247 s.push(100)248 })249 expect(nextState).toEqual([1, 2, 100])

test: rename and move a testaleclarsonbefore adoption8e0325c

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

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 252–258why, one step back
252 it("can be sorted", () => {
253 const baseState = [3, 1, 2]254 const nextState = produce(baseState, s => {255 s.sort()256 })257 expect(nextState).not.toBe(baseState)258 expect(nextState).toEqual([1, 2, 3])

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 259why, one step back
259 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 260why, one step back
260

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 261–269why, one step back
261 it("supports modifying nested objects", () => {
262 const baseState = [{a: 1}, {}]263 const nextState = produce(baseState, s => {264 s[0].a++265 s[1].a = 0266 })267 expect(nextState).not.toBe(baseState)268 expect(nextState[0].a).toBe(2)269 expect(nextState[1].a).toBe(0)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 270why, one step back
270 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 271why, one step back
271

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 272–276why, one step back
272 it("never preserves non-numeric properties", () => {
273 const baseState = []274 baseState.x = 7275 const nextState = produce(baseState, s => {276 s.push(3)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 277why, one step back
277 })

chore: improve more testsaleclarsonbefore adoption4f59b12

line 278why, one step back
278 expect("x" in nextState).toBeFalsy()

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 279why, one step back
279 })

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 280–286why, one step back
280
281 if (useProxies) {282 it("throws when a non-numeric property is added", () => {283 expect(() => {284 produce([], d => {285 d.x = 3286 })

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

line 287why, one step back
287 }).toThrowErrorMatchingSnapshot()

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 288–296why, one step back
288 })
289290 it("throws when a non-numeric property is deleted", () => {291 expect(() => {292 const baseState = []293 baseState.x = 7294 produce(baseState, d => {295 delete d.x296 })

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

line 297why, one step back
297 }).toThrowErrorMatchingSnapshot()

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 298–299why, one step back
298 })
299 }

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 300–301why, one step back
300 })
301

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 302–500why, one step back
302 if (useProxies) {
303 describe("map drafts", () => {304 it("supports key access", () => {305 const value = baseState.aMap.get("jedi")306 const nextState = produce(baseState, s => {307 expect(s.aMap.get("jedi")).toEqual(value)308 })309 expect(nextState).toBe(baseState)310 })311312 it("supports iteration", () => {313 const base = new Map([314 ["first", {id: 1, a: 1}],315 ["second", {id: 2, a: 1}]316 ])317 const findById = (map, id) => {318 for (const [, item] of map) {319 if (item.id === id) return item320 }321 return null322 }323 const result = produce(base, draft => {324 const obj1 = findById(draft, 1)325 const obj2 = findById(draft, 2)326 obj1.a = 2327 obj2.a = 2328 })329 expect(result).not.toBe(base)330 expect(result.get("first").a).toEqual(2)331 expect(result.get("second").a).toEqual(2)332 })333334 it("supports 'entries'", () => {335 const base = new Map([336 ["first", {id: 1, a: 1}],337 ["second", {id: 2, a: 1}]338 ])339 const findById = (map, id) => {340 for (const [, item] of map.entries()) {341 if (item.id === id) return item342 }343 return null344 }345 const result = produce(base, draft => {346 const obj1 = findById(draft, 1)347 const obj2 = findById(draft, 2)348 obj1.a = 2349 obj2.a = 2350 })351 expect(result).not.toBe(base)352 expect(result.get("first").a).toEqual(2)353 expect(result.get("second").a).toEqual(2)354 })355356 it("supports 'values'", () => {357 const base = new Map([358 ["first", {id: 1, a: 1}],359 ["second", {id: 2, a: 1}]360 ])361 const findById = (map, id) => {362 for (const item of map.values()) {363 if (item.id === id) return item364 }365 return null366 }367 const result = produce(base, draft => {368 const obj1 = findById(draft, 1)369 const obj2 = findById(draft, 2)370 obj1.a = 2371 obj2.a = 2372 })373 expect(result).not.toBe(base)374 expect(result.get("first").a).toEqual(2)375 expect(result.get("second").a).toEqual(2)376 })377378 it("supports 'keys", () => {379 const base = new Map([380 ["first", Symbol()],381 ["second", Symbol()]382 ])383 const result = produce(base, draft => {384 expect([...draft.keys()]).toEqual(["first", "second"])385 draft.set("third", Symbol())386 expect([...draft.keys()]).toEqual([387 "first",388 "second",389 "third"390 ])391 })392 })393394 it("supports forEach", () => {395 const base = new Map([396 ["first", {id: 1, a: 1}],397 ["second", {id: 2, a: 1}]398 ])399 const result = produce(base, draft => {400 let sum1 = 0401 draft.forEach(({a}) => {402 sum1 += a403 })404 expect(sum1).toBe(2)405 let sum2 = 0406 draft.get("first").a = 10407 draft.get("second").a = 20408 draft.forEach(({a}) => {409 sum2 += a410 })411 expect(sum2).toBe(30)412 })413 })414415 it("supports forEach mutation", () => {416 const base = new Map([417 ["first", {id: 1, a: 1}],418 ["second", {id: 2, a: 1}]419 ])420 const result = produce(base, draft => {421 draft.forEach(item => {422 item.a = 100423 })424 })425 expect(result).not.toBe(base)426 expect(result.get("first").a).toEqual(100)427 expect(result.get("second").a).toEqual(100)428 })429430 it("can assign by key", () => {431 const nextState = produce(baseState, s => {432 s.aMap.set("force", true)433 })434 expect(nextState).not.toBe(baseState)435 expect(nextState.aMap).not.toBe(baseState.aMap)436 expect(nextState.aMap.get("force")).toEqual(true)437 })438439 it("returns 'size'", () => {440 const nextState = produce(baseState, s => {441 s.aMap.set("newKey", true)442 expect(s.aMap.size).toBe(baseState.aMap.size + 1)443 })444 expect(nextState).not.toBe(baseState)445 expect(nextState.aMap).not.toBe(baseState.aMap)446 expect(nextState.aMap.get("newKey")).toEqual(true)447 expect(nextState.aMap.size).toEqual(baseState.aMap.size + 1)448 })449450 it("can use 'delete' to remove items", () => {451 const nextState = produce(baseState, s => {452 expect(s.aMap.has("jedi")).toBe(true)453 s.aMap.delete("jedi")454 expect(s.aMap.has("jedi")).toBe(false)455 })456 expect(nextState.aMap).not.toBe(baseState.aMap)457 expect(nextState.aMap.size).toBe(baseState.aMap.size - 1)458 expect(baseState.aMap.has("jedi")).toBe(true)459 expect(nextState.aMap.has("jedi")).toBe(false)460 })461462 it("can use 'clear' to remove items", () => {463 const nextState = produce(baseState, s => {464 expect(s.aMap.size).not.toBe(0)465 s.aMap.clear()466 expect(s.aMap.size).toBe(0)467 })468 expect(nextState.aMap).not.toBe(baseState.aMap)469 expect(nextState.aMap.size).toBe(0)470 })471472 it("support 'has'", () => {473 const nextState = produce(baseState, s => {474 expect(s.aMap.has("newKey")).toBe(false)475 s.aMap.set("newKey", true)476 expect(s.aMap.has("newKey")).toBe(true)477 })478 expect(nextState).not.toBe(baseState)479 expect(nextState.aMap).not.toBe(baseState.aMap)480 expect(nextState.aMap.has("newKey")).toEqual(true)481 })482483 it("supports nested maps", () => {484 const base = new Map([485 ["first", new Map([["second", {prop: "test"}]])]486 ])487 const result = produce(base, draft => {488 draft.get("first").get("second").prop = "test1"489 })490 expect(result).not.toBe(base)491 expect(result.get("first")).not.toBe(base.get("first"))492 expect(result.get("first").get("second")).not.toBe(493 base.get("first").get("second")494 )495 expect(base.get("first").get("second").prop).toBe("test")496 expect(result.get("first").get("second").prop).toBe("test1")497 })498 })499 }500

feat: limited class supportaleclarsonbefore adoption9877d64

lines 501–527why, one step back
501 it("supports `immerable` symbol on constructor", () => {
502 class One {}503 One[immerable] = true504 const baseState = new One()505 const nextState = produce(baseState, draft => {506 expect(draft).not.toBe(baseState)507 draft.foo = true508 })509 expect(nextState).not.toBe(baseState)510 expect(nextState.foo).toBeTruthy()511 })512513 it("preserves symbol properties", () => {514 const test = Symbol("test")515 const baseState = {[test]: true}516 const nextState = produce(baseState, s => {517 expect(s[test]).toBeTruthy()518 s.foo = true519 })520 expect(nextState).toEqual({521 [test]: true,522 foo: true523 })524 })525526 it("preserves non-enumerable properties", () => {527 const baseState = {}

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

line 528why, one step back
528 // Non-enumerable object property

feat: limited class supportaleclarsonbefore adoption9877d64

line 529why, one step back
529 Object.defineProperty(baseState, "foo", {

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

line 530why, one step back
530 value: {a: 1},

feat: limited class supportaleclarsonbefore adoption9877d64

lines 531–532why, one step back
531 enumerable: false
532 })

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

lines 533–537why, one step back
533 // Non-enumerable primitive property
534 Object.defineProperty(baseState, "bar", {535 value: 1,536 enumerable: false537 })

feat: limited class supportaleclarsonbefore adoption9877d64

lines 538–540why, one step back
538 const nextState = produce(baseState, s => {
539 expect(s.foo).toBeTruthy()540 expect(isEnumerable(s, "foo")).toBeFalsy()

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

lines 541–544why, one step back
541 s.bar++
542 expect(isEnumerable(s, "foo")).toBeFalsy()543 s.foo.a++544 expect(isEnumerable(s, "foo")).toBeFalsy()

feat: limited class supportaleclarsonbefore adoption9877d64

lines 545–562why, one step back
545 })
546 expect(nextState.foo).toBeTruthy()547 expect(isEnumerable(nextState, "foo")).toBeFalsy()548 })549550 it("throws on computed properties", () => {551 const baseState = {}552 Object.defineProperty(baseState, "foo", {553 get: () => {},554 enumerable: true555 })556 expect(() => {557 produce(baseState, s => {558 // Proxies only throw once a change is made.559 if (useProxies) {560 s.modified = true561 }562 })

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

line 563why, one step back
563 }).toThrowErrorMatchingSnapshot()

feat: limited class supportaleclarsonbefore adoption9877d64

lines 564–584why, one step back
564 })
565566 it("allows inherited computed properties", () => {567 const proto = {}568 Object.defineProperty(proto, "foo", {569 get() {570 return this.bar571 },572 set(val) {573 this.bar = val574 }575 })576 const baseState = Object.create(proto)577 produce(baseState, s => {578 expect(s.bar).toBeUndefined()579 s.foo = {}580 expect(s.bar).toBeDefined()581 expect(s.foo).toBe(s.bar)582 })583 })584

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 585–606why, one step back
585 it("supports a base state with multiple references to an object", () => {
586 const obj = {}587 const res = produce({a: obj, b: obj}, d => {588 // Two drafts are created for each occurrence of an object in the base state.589 expect(d.a).not.toBe(d.b)590 d.a.z = true591 expect(d.b.z).toBeUndefined()592 })593 expect(res.b).toBe(obj)594 expect(res.a).not.toBe(res.b)595 expect(res.a.z).toBeTruthy()596 })597598 // NOTE: Except the root draft.599 it("supports multiple references to any modified draft", () => {600 const next = produce({a: {b: 1}}, d => {601 d.a.b++602 d.b = d.a603 })604 expect(next.a).toBe(next.b)605 })606

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 607why, one step back
607 it("can rename nested objects (no changes)", () => {

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 608–610why, one step back
608 const nextState = produce({obj: {}}, s => {
609 s.foo = s.obj610 delete s.obj

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 611why, one step back
611 })

chore: improve more testsaleclarsonbefore adoption4f59b12

line 612why, one step back
612 expect(nextState).toEqual({foo: {}})

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 613–614why, one step back
613 })
614

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

lines 615–617why, one step back
615 // Very similar to the test before, but the reused object has one
616 // property changed, one added, and one removed.617 it("can rename nested objects (with changes)", () => {

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 618–621why, one step back
618 const nextState = produce({obj: {a: 1, b: 1}}, s => {
619 s.obj.a = true // change620 delete s.obj.b // delete621 s.obj.c = true // add

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 622why, one step back
622

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 623–624why, one step back
623 s.foo = s.obj
624 delete s.obj

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 625why, one step back
625 })

chore: improve more testsaleclarsonbefore adoption4f59b12

line 626why, one step back
626 expect(nextState).toEqual({foo: {a: true, c: true}})

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 627–628why, one step back
627 })
628

test: draft nested in new objectaleclarsonbefore adoptionf46659b

line 629why, one step back
629 it("can nest a draft in a new object (no changes)", () => {

chore: improve more testsaleclarsonbefore adoption4f59b12

line 630why, one step back
630 const baseState = {obj: {}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 632–633why, one step back
632 s.foo = {bar: s.obj}
633 delete s.obj

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 634why, one step back
634 })

chore: improve more testsaleclarsonbefore adoption4f59b12

line 635why, one step back
635 expect(nextState.foo.bar).toBe(baseState.obj)

test: draft nested in new objectaleclarsonbefore adoptionf46659b

lines 636–637why, one step back
636 })
637

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 638–642why, one step back
638 it("can nest a modified draft in a new object", () => {
639 const nextState = produce({obj: {a: 1, b: 1}}, s => {640 s.obj.a = true // change641 delete s.obj.b // delete642 s.obj.c = true // add

test: draft nested in new objectaleclarsonbefore adoptionf46659b

line 643why, one step back
643

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 644–645why, one step back
644 s.foo = {bar: s.obj}
645 delete s.obj

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 646why, one step back
646 })

chore: improve more testsaleclarsonbefore adoption4f59b12

line 647why, one step back
647 expect(nextState).toEqual({foo: {bar: {a: true, c: true}}})

test: draft nested in new objectaleclarsonbefore adoptionf46659b

lines 648–649why, one step back
648 })
649

chore: improve some testsaleclarsonbefore adoptionefd47f6

line 650why, one step back
650 it("supports assigning undefined to an existing property", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 651–653why, one step back
651 const nextState = produce(baseState, s => {
652 s.aProp = undefined653 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

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

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 655–663why, one step back
655 expect(nextState.aProp).toBe(undefined)
656 })657658 it("supports assigning undefined to a new property", () => {659 const baseState = {}660 const nextState = produce(baseState, s => {661 s.aProp = undefined662 })663 expect(nextState).not.toBe(baseState)

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 664–666why, one step back
664 expect(nextState.aProp).toBe(undefined)
665 })666

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

lines 667–669why, one step back
667 // NOTE: ES5 drafts only protect existing properties when revoked.
668 it("revokes the draft once produce returns", () => {669 const expectRevoked = (fn, shouldThrow = true) => {

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

line 670why, one step back
670 if (shouldThrow) expect(fn).toThrowErrorMatchingSnapshot()

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

lines 671–672why, one step back
671 else expect(fn).not.toThrow()
672 }

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 673why, one step back
673

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

lines 674–679why, one step back
674 // Test object drafts:
675 let draft676 produce({a: 1, b: 1}, s => {677 draft = s678 delete s.b679 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 680why, one step back
680

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

lines 681–683why, one step back
681 // Access known property on object draft.
682 expectRevoked(() => {683 draft.a

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 684–685why, one step back
684 })
685

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

lines 686–688why, one step back
686 // Assign known property on object draft.
687 expectRevoked(() => {688 draft.a = true

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 689why, one step back
689 })

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

lines 690–725why, one step back
690
691 // Access unknown property on object draft.692 expectRevoked(() => {693 draft.z694 }, useProxies)695696 // Assign unknown property on object draft.697 expectRevoked(() => {698 draft.z = true699 }, useProxies)700701 // Test array drafts:702 produce([1, 2], s => {703 draft = s704 s.pop()705 })706707 // Access known index of an array draft.708 expectRevoked(() => {709 draft[0]710 })711712 // Assign known index of an array draft.713 expectRevoked(() => {714 draft[0] = true715 })716717 // Access unknown index of an array draft.718 expectRevoked(() => {719 draft[1]720 }, useProxies)721722 // Assign unknown index of an array draft.723 expectRevoked(() => {724 draft[1] = true725 }, useProxies)

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 726–727why, one step back
726 })
727

test: add missing edge casealeclarsonbefore adoptionc46af36

lines 728–735why, one step back
728 it("can access a child draft that was created before the draft was modified", () => {
729 produce({a: {}}, s => {730 const before = s.a731 s.b = 1732 expect(s.a).toBe(before)733 })734 })735

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 737–739why, one step back
737 produce(baseState, draft => {
738 draft.anArray[0] = 5739 draft.anArray.unshift("test")

test: tiny improvementaleclarsonbefore adoptionee49da3

line 740why, one step back
740 expect(enumerableOnly(draft.anArray)).toEqual([

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 741–749why, one step back
741 "test",
742 5,743 2,744 {c: 3},745 1746 ])747 draft.stuffz = "coffee"748 expect(draft.stuffz).toBe("coffee")749 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 750–751why, one step back
750 })
751

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 752–753why, one step back
752 if (useProxies)
753 it("throws when Object.defineProperty() is used on drafts", () => {

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 754why, one step back
754 expect(() => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 755–761why, one step back
755 produce({}, draft => {
756 Object.defineProperty(draft, "xx", {757 enumerable: true,758 writeable: true,759 value: 2760 })761 })

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

line 762why, one step back
762 }).toThrowErrorMatchingSnapshot()

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 763–765why, one step back
763 })
764765 it("should handle constructor correctly", () => {

chore: improve more testsaleclarsonbefore adoption4f59b12

line 766why, one step back
766 const baseState = {

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 767–769why, one step back
767 arr: new Array(),
768 obj: new Object()769 }

chore: improve more testsaleclarsonbefore adoption4f59b12

line 770why, one step back
770 const result = produce(baseState, draft => {

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 771–778why, one step back
771 draft.arrConstructed = draft.arr.constructor(1)
772 draft.objConstructed = draft.obj.constructor(1)773 })774 expect(result.arrConstructed).toEqual(new Array().constructor(1))775 expect(result.objConstructed).toEqual(new Object().constructor(1))776 })777778 it("should handle equality correctly - 1", () => {

chore: improve more testsaleclarsonbefore adoption4f59b12

line 779why, one step back
779 const baseState = {

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 780–782why, one step back
780 y: 3 / 0,
781 z: NaN782 }

chore: improve more testsaleclarsonbefore adoption4f59b12

line 783why, one step back
783 const nextState = produce(baseState, draft => {

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 784–786why, one step back
784 draft.y = 4 / 0
785 draft.z = NaN786 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 788–790why, one step back
788 })
789790 it("should handle equality correctly - 2", () => {

chore: improve more testsaleclarsonbefore adoption4f59b12

line 791why, one step back
791 const baseState = {

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 792–793why, one step back
792 x: -0
793 }

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 794–795why, one step back
794 const nextState = produce(baseState, draft => {
795 draft.x = +0

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 796why, one step back
796 })

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 797–798why, one step back
797 expect(nextState).not.toBe(baseState)
798 expect(nextState).not.toEqual(baseState)

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 799–800why, one step back
799 })
800

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

line 801why, one step back
801 // AKA: recursive produce calls

chore: improve more testsaleclarsonbefore adoption4f59b12

line 802why, one step back
802 describe("nested producers", () => {

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

line 803why, one step back
803 describe("when base state is not a draft", () => {

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 804–806why, one step back
804 // This test ensures the global state used to manage proxies is
805 // never left in a corrupted state by a nested `produce` call.806 it("never affects its parent producer implicitly", () => {

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

lines 807–811why, one step back
807 const base = {obj: {a: 1}}
808 const next = produce(base, draft => {809 // Notice how `base.obj` is passed, not `draft.obj`810 const obj2 = produce(base.obj, draft2 => {811 draft2.a = 0

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 812why, one step back
812 })

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

lines 813–814why, one step back
813 expect(obj2.a).toBe(0)
814 expect(draft.obj.a).toBe(1) // effects should not be visible outside

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 815why, one step back
815 })

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

line 816why, one step back
816 expect(next).toBe(base)

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 817–819why, one step back
817 })
818 })819

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

line 820why, one step back
820 describe("when base state is a draft", () => {

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

lines 821–826why, one step back
821 it("always wraps the draft in a new draft", () => {
822 produce({}, parent => {823 produce(parent, child => {824 expect(child).not.toBe(parent)825 expect(isDraft(child)).toBeTruthy()826 expect(original(child)).toBe(parent)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 827–828why, one step back
827 })
828 })

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

line 829why, one step back
829 })

test: more recursive produce testsAlec Larsonbefore adoption8adfb31

lines 830–855why, one step back
830
831 // Reported by: https://github.com/mweststrate/immer/issues/343832 it("ensures each property is drafted", () => {833 produce({a: {}, b: {}}, parent => {834 parent.a // Access "a" but not "b"835 produce(parent, child => {836 child.c = 1837 expect(isDraft(child.a)).toBeTruthy()838 expect(isDraft(child.b)).toBeTruthy()839 })840 })841 })842843 it("preserves any pending changes", () => {844 produce({a: 1, b: 1, d: 1}, parent => {845 parent.b = 2846 parent.c = 2847 delete parent.d848 produce(parent, child => {849 expect(child.a).toBe(1) // unchanged850 expect(child.b).toBe(2) // changed851 expect(child.c).toBe(2) // added852 expect(child.d).toBeUndefined() // deleted853 })854 })855 })

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

lines 856–863why, one step back
856 })
857858 describe("when base state contains a draft", () => {859 it("wraps unowned draft with its own draft", () => {860 produce({a: {}}, parent => {861 produce({a: parent.a}, child => {862 expect(child.a).not.toBe(parent.a)863 expect(isDraft(child.a)).toBeTruthy()

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

line 864why, one step back
864 expect(original(child.a)).toBe(parent.a)

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

lines 865–895why, one step back
865 })
866 })867 })868869 it("returns unowned draft if no changes were made", () => {870 produce({a: {}}, parent => {871 const result = produce({a: parent.a}, () => {})872 expect(result.a).toBe(parent.a)873 })874 })875876 it("clones the unowned draft when changes are made", () => {877 produce({a: {}}, parent => {878 const result = produce({a: parent.a}, child => {879 child.a.b = 1880 })881 expect(result.a).not.toBe(parent.a)882 expect(result.a.b).toBe(1)883 expect("b" in parent.a).toBeFalsy()884 })885 })886887 // We cannot auto-freeze the result of a nested producer,888 // because it may contain a draft from a parent producer.889 it("never auto-freezes the result", () => {890 produce({a: {}}, parent => {891 const r = produce({a: parent.a}, child => {892 child.b = 1 // Ensure a copy is returned.893 })894 expect(Object.isFrozen(r)).toBeFalsy()895 })

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 896–897why, one step back
896 })
897 })

test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637

lines 898–915why, one step back
898
899 // "Upvalues" are variables from a parent scope.900 it("does not finalize upvalue drafts", () => {901 produce({a: {}, b: {}}, parent => {902 expect(produce({}, () => parent)).toBe(parent)903 parent.x // Ensure proxy not revoked.904905 expect(produce({}, () => [parent])[0]).toBe(parent)906 parent.x // Ensure proxy not revoked.907908 expect(produce({}, () => parent.a)).toBe(parent.a)909 parent.a.x // Ensure proxy not revoked.910911 // Modified parent test912 parent.c = 1913 expect(produce({}, () => [parent.b])[0]).toBe(parent.b)914 parent.b.x // Ensure proxy not revoked.915 })

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 916–919why, one step back
916 })
917918 it("works with interweaved Immer instances", () => {919 const options = {useProxies, autoFreeze}

test: patch the `produce` function for interweaved Immer instances tooaleclarsonbefore adoption31bbe64

lines 920–921why, one step back
920 const one = createPatchedImmer(options)
921 const two = createPatchedImmer(options)

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 922–936why, one step back
922
923 const base = {}924 const result = one.produce(base, s1 =>925 two.produce({s1}, s2 => {926 expect(original(s2.s1)).toBe(s1)927 s2.n = 1928 s2.s1 = one.produce({s2}, s3 => {929 expect(original(s3.s2)).toBe(s2)930 expect(original(s3.s2.s1)).toBe(s2.s1)931 return s3.s2.s1932 })933 })934 )935 expect(result.n).toBe(1)936 expect(result.s1).toBe(base)

test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637

line 937why, one step back
937 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 938–940why, one step back
938 })
939940 if (useProxies)

chore: improve more testsaleclarsonbefore adoption4f59b12

line 941why, one step back
941 it("throws when Object.setPrototypeOf() is used on a draft", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 942why, one step back
942 produce({}, draft => {

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

lines 943–945why, one step back
943 expect(() =>
944 Object.setPrototypeOf(draft, Array)945 ).toThrowErrorMatchingSnapshot()

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 946why, one step back
946 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 947–948why, one step back
947 })
948

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 949–951why, one step back
949 it("supports the 'in' operator", () => {
950 produce(baseState, draft => {951 // Known property

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 952–953why, one step back
952 expect("anArray" in draft).toBe(true)
953 expect(Reflect.has(draft, "anArray")).toBe(true)

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 954why, one step back
954

chore: improve more testsaleclarsonbefore adoption4f59b12

line 955why, one step back
955 // Unknown property

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 956–957why, one step back
956 expect("bla" in draft).toBe(false)
957 expect(Reflect.has(draft, "bla")).toBe(false)

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 958why, one step back
958

chore: improve more testsaleclarsonbefore adoption4f59b12

line 959why, one step back
959 // Known index

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 960–963why, one step back
960 expect(0 in draft.anArray).toBe(true)
961 expect("0" in draft.anArray).toBe(true)962 expect(Reflect.has(draft.anArray, 0)).toBe(true)963 expect(Reflect.has(draft.anArray, "0")).toBe(true)

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 964why, one step back
964

chore: improve more testsaleclarsonbefore adoption4f59b12

line 965why, one step back
965 // Unknown index

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 966–970why, one step back
966 expect(17 in draft.anArray).toBe(false)
967 expect("17" in draft.anArray).toBe(false)968 expect(Reflect.has(draft.anArray, 17)).toBe(false)969 expect(Reflect.has(draft.anArray, "17")).toBe(false)970 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 971–972why, one step back
971 })
972

feat: `this` in recipe is no longer the draft, but unboundMichel Weststratebefore adoption3f40298

line 973why, one step back
973 it("'this' should not be bound anymore - 1", () => {

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 974why, one step back
974 const base = {x: 3}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 975why, one step back
975 const next1 = produce(base, function() {

feat: `this` in recipe is no longer the draft, but unboundMichel Weststratebefore adoption3f40298

line 976why, one step back
976 expect(this).toBe(undefined)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 977why, one step back
977 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 978–979why, one step back
978 })
979

feat: `this` in recipe is no longer the draft, but unboundMichel Weststratebefore adoption3f40298

line 980why, one step back
980 it("'this' should not be bound anymore - 2", () => {

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 981why, one step back
981 const incrementor = produce(function() {

feat: `this` in recipe is no longer the draft, but unboundMichel Weststratebefore adoption3f40298

line 982why, one step back
982 expect(this).toBe(undefined)

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 983why, one step back
983 })

feat: `this` in recipe is no longer the draft, but unboundMichel Weststratebefore adoption3f40298

lines 984–986why, one step back
984 incrementor()
985 })986

refactor(ts): simplify IProduce type testsAlec Larsonbefore adoptionbce282d

line 987why, one step back
987 it("should be possible to use dynamic bound this", () => {

feat: `this` in recipe is no longer the draft, but unboundMichel Weststratebefore adoption3f40298

lines 988–996why, one step back
988 const world = {
989 counter: {count: 1},990 inc: produce(function(draft) {991 expect(this).toBe(world)992 draft.counter.count = this.counter.count + 1993 })994 }995996 expect(world.inc(world).counter.count).toBe(2)

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 997–998why, one step back
997 })
998

test: rename and expand a testaleclarsonbefore adoption733e16f

line 999why, one step back
999 // See here: https://github.com/mweststrate/immer/issues/89

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1000why, one step back
1000 it("supports the spread operator", () => {

test: rename and expand a testaleclarsonbefore adoption733e16f

lines 1001–1004why, one step back
1001 const base = {foo: {x: 0, y: 0}, bar: [0, 0]}
1002 const result = produce(base, draft => {1003 draft.foo = {x: 1, ...draft.foo, y: 1}1004 draft.bar = [1, ...draft.bar, 1]

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 1005why, one step back
1005 })

test: rename and expand a testaleclarsonbefore adoption733e16f

lines 1006–1008why, one step back
1006 expect(result).toEqual({
1007 foo: {x: 0, y: 1},1008 bar: [1, 0, 0, 1]

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1009–1013why, one step back
1009 })
1010 })10111012 it("processes with lodash.set", () => {1013 const base = [{id: 1, a: 1}]

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1014–1016why, one step back
1014 const result = produce(base, draft => {
1015 lodash.set(draft, "[0].a", 2)1016 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1017–1022why, one step back
1017 expect(base[0].a).toEqual(1)
1018 expect(result[0].a).toEqual(2)1019 })10201021 it("processes with lodash.find", () => {1022 const base = [{id: 1, a: 1}]

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1023–1026why, one step back
1023 const result = produce(base, draft => {
1024 const obj1 = lodash.find(draft, {id: 1})1025 lodash.set(obj1, "a", 2)1026 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1027–1030why, one step back
1027 expect(base[0].a).toEqual(1)
1028 expect(result[0].a).toEqual(2)1029 })1030

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1031–1033why, one step back
1031 describe("recipe functions", () => {
1032 it("can return a new object", () => {1033 const base = {x: 3}

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 1034–1035why, one step back
1034 const res = produce(base, d => {
1035 return {x: d.x + 1}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 1036why, one step back
1036 })

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1037–1038why, one step back
1037 expect(res).not.toBe(base)
1038 expect(res).toEqual({x: 4})

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 1039why, one step back
1039 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1040why, one step back
1040

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1041–1042why, one step back
1041 it("can return the draft", () => {
1042 const base = {x: 3}

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 1043–1045why, one step back
1043 const res = produce(base, d => {
1044 d.x = 41045 return d

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1046–1048why, one step back
1046 })
1047 expect(res).not.toBe(base)1048 expect(res).toEqual({x: 4})

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 1049–1050why, one step back
1049 })
1050

fix(es5): throw if a modified child draft is returnedaleclarsonbefore adoptioneabe9db

line 1051why, one step back
1051 it("can return an unmodified child draft", () => {

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 1052–1056why, one step back
1052 const base = {a: {}}
1053 const res = produce(base, d => {1054 return d.a1055 })1056 expect(res).toBe(base.a)

fix(es5): throw if a modified child draft is returnedaleclarsonbefore adoptioneabe9db

lines 1057–1066why, one step back
1057 })
10581059 // TODO: Avoid throwing if only the child draft was modified.1060 it("cannot return a modified child draft", () => {1061 const base = {a: {}}1062 expect(() => {1063 produce(base, d => {1064 d.a.b = 11065 return d.a1066 })

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

line 1067why, one step back
1067 }).toThrowErrorMatchingSnapshot()

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 1068–1082why, one step back
1068 })
10691070 it("can return a frozen object", () => {1071 const res = deepFreeze([{x: 3}])1072 expect(produce({}, () => res)).toBe(res)1073 })10741075 it("can return an object with two references to another object", () => {1076 const next = produce({}, d => {1077 const obj = {}1078 return {obj, arr: [obj]}1079 })1080 expect(next.obj).toBe(next.arr[0])1081 })1082

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 1083why, one step back
1083 it("can return an object with two references to an unmodified draft", () => {

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 1084–1096why, one step back
1084 const base = {a: {}}
1085 const next = produce(base, d => {1086 return [d.a, d.a]1087 })1088 expect(next[0]).toBe(base.a)1089 expect(next[0]).toBe(next[1])1090 })10911092 it("cannot return an object that references itself", () => {1093 const res = {}1094 res.self = res1095 expect(() => {1096 produce(res, () => res.self)

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

line 1097why, one step back
1097 }).toThrowErrorMatchingSnapshot()

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 1098–1103why, one step back
1098 })
1099 })11001101 describe("async recipe function", () => {1102 it("can modify the draft", () => {1103 const base = {a: 0, b: 0}

feat: Dropped node 6 supportMichel Weststratebefore adoptioneecc773

line 1104why, one step back
1104 return produce(base, async d => {

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 1105why, one step back
1105 d.a = 1

feat: Dropped node 6 supportMichel Weststratebefore adoptioneecc773

lines 1106–1107why, one step back
1106 await Promise.resolve()
1107 d.b = 1

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 1108–1127why, one step back
1108 }).then(res => {
1109 expect(res).not.toBe(base)1110 expect(res).toEqual({a: 1, b: 1})1111 })1112 })11131114 it("works with rejected promises", () => {1115 let draft1116 const base = {a: 0, b: 0}1117 const err = new Error("passed")1118 return produce(base, d => {1119 draft = d1120 draft.b = 11121 return Promise.reject(err)1122 }).then(1123 () => {1124 throw "failed"1125 },1126 e => {1127 expect(e).toBe(err)

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

line 1128why, one step back
1128 expect(() => draft.a).toThrowErrorMatchingSnapshot()

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 1129–1154why, one step back
1129 }
1130 )1131 })11321133 it("supports recursive produce calls after await", () => {1134 const base = {obj: {k: 1}}1135 return produce(base, d => {1136 const obj = d.obj1137 delete d.obj1138 return Promise.resolve().then(() => {1139 d.a = produce({}, d => {1140 d.b = obj // Assign a draft owned by the parent scope.1141 })11421143 // Auto-freezing is prevented when an unowned draft exists.1144 expect(Object.isFrozen(d.a)).toBeFalsy()11451146 // Ensure `obj` is not revoked.1147 obj.c = 11148 })1149 }).then(res => {1150 expect(res).not.toBe(base)1151 expect(res).toEqual({1152 a: {b: {k: 1, c: 1}}1153 })1154 })

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 1155why, one step back
1155 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1156–1157why, one step back
1156 })
1157

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1158why, one step back
1158 it("throws when the draft is modified and another object is returned", () => {

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1159–1160why, one step back
1159 const base = {x: 3}
1160 expect(() => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1161–1164why, one step back
1161 produce(base, draft => {
1162 draft.x = 41163 return {x: 5}1164 })

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

line 1165why, one step back
1165 }).toThrowErrorMatchingSnapshot()

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1166–1167why, one step back
1166 })
1167

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1168why, one step back
1168 it("should fix #117 - 1", () => {

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1169why, one step back
1169 const reducer = (state, action) =>

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1170–1177why, one step back
1170 produce(state, draft => {
1171 switch (action.type) {1172 case "SET_STARTING_DOTS":1173 return draft.availableStartingDots.map(a => a)1174 default:1175 break1176 }1177 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1178–1192why, one step back
1178 const base = {
1179 availableStartingDots: [1180 {dots: 4, count: 1},1181 {dots: 3, count: 2},1182 {dots: 2, count: 3},1183 {dots: 1, count: 4}1184 ]1185 }1186 const next = reducer(base, {type: "SET_STARTING_DOTS"})1187 expect(next).toEqual(base.availableStartingDots)1188 expect(next).not.toBe(base.availableStartingDots)1189 })11901191 it("should fix #117 - 2", () => {1192 const reducer = (state, action) =>

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1193–1202why, one step back
1193 produce(state, draft => {
1194 switch (action.type) {1195 case "SET_STARTING_DOTS":1196 return {1197 dots: draft.availableStartingDots.map(a => a)1198 }1199 default:1200 break1201 }1202 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1203–1214why, one step back
1203 const base = {
1204 availableStartingDots: [1205 {dots: 4, count: 1},1206 {dots: 3, count: 2},1207 {dots: 2, count: 3},1208 {dots: 1, count: 4}1209 ]1210 }1211 const next = reducer(base, {type: "SET_STARTING_DOTS"})1212 expect(next).toEqual({dots: base.availableStartingDots})1213 })1214

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1215why, one step back
1215 it("cannot always detect noop assignments - 0", () => {

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1216why, one step back
1216 const baseState = {x: {y: 3}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1217–1220why, one step back
1217 const nextState = produce(baseState, d => {
1218 const a = d.x1219 d.x = a1220 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1221–1223why, one step back
1221 expect(nextState).toBe(baseState)
1222 })1223

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1224why, one step back
1224 it("cannot always detect noop assignments - 1", () => {

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1225why, one step back
1225 const baseState = {x: {y: 3}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1226–1230why, one step back
1226 const nextState = produce(baseState, d => {
1227 const a = d.x1228 d.x = 41229 d.x = a1230 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1231–1236why, one step back
1231 // Ideally, this should actually be the same instances
1232 // but this would be pretty expensive to detect,1233 // so we don't atm1234 expect(nextState).not.toBe(baseState)1235 })1236

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1237why, one step back
1237 it("cannot always detect noop assignments - 2", () => {

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1238why, one step back
1238 const baseState = {x: {y: 3}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1239–1244why, one step back
1239 const nextState = produce(baseState, d => {
1240 const a = d.x1241 const stuff = a.y + 31242 d.x = 41243 d.x = a1244 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1245–1250why, one step back
1245 // Ideally, this should actually be the same instances
1246 // but this would be pretty expensive to detect,1247 // so we don't atm1248 expect(nextState).not.toBe(baseState)1249 })1250

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1251why, one step back
1251 it("cannot always detect noop assignments - 3", () => {

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1252why, one step back
1252 const baseState = {x: 3}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1253–1255why, one step back
1253 const nextState = produce(baseState, d => {
1254 d.x = 31255 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1256–1258why, one step back
1256 expect(nextState).toBe(baseState)
1257 })1258

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1259why, one step back
1259 it("cannot always detect noop assignments - 4", () => {

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1260why, one step back
1260 const baseState = {x: 3}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1261–1264why, one step back
1261 const nextState = produce(baseState, d => {
1262 d.x = 41263 d.x = 31264 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1265–1270why, one step back
1265 // Ideally, this should actually be the same instances
1266 // but this would be pretty expensive to detect,1267 // so we don't atm1268 expect(nextState).not.toBe(baseState)1269 })1270

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1271why, one step back
1271 it("cannot produce undefined by returning undefined", () => {

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1272–1287why, one step back
1272 const base = 3
1273 expect(produce(base, () => 4)).toBe(4)1274 expect(produce(base, () => null)).toBe(null)1275 expect(produce(base, () => undefined)).toBe(3)1276 expect(produce(base, () => {})).toBe(3)1277 expect(produce(base, () => nothing)).toBe(undefined)12781279 expect(produce({}, () => undefined)).toEqual({})1280 expect(produce({}, () => nothing)).toBe(undefined)1281 expect(produce(3, () => nothing)).toBe(undefined)12821283 expect(produce(() => undefined)({})).toEqual({})1284 expect(produce(() => nothing)({})).toBe(undefined)1285 expect(produce(() => nothing)(3)).toBe(undefined)1286 })1287

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1288–1291why, one step back
1288 describe("base state type", () => {
1289 testObjectTypes(produce)1290 testLiteralTypes(produce)1291 })

Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417

line 1292why, one step back
1292

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1293–1297why, one step back
1293 afterEach(() => {
1294 expect(baseState).toBe(origBaseState)1295 expect(baseState).toEqual(createBaseState())1296 })1297

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1298why, one step back
1298 class Foo {}

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1299–1300why, one step back
1299 function createBaseState() {
1300 const data = {

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1301why, one step back
1301 anInstance: new Foo(),

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1302why, one step back
1302 anArray: [3, 2, {c: 3}, 1],

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 1303–1307why, one step back
1303 aMap: new Map([
1304 ["jedi", {name: "Luke", skill: 10}],1305 ["jediTotal", 42],1306 ["force", "these aren't the droids you're looking for"]1307 ]),

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1308–1315why, one step back
1308 aProp: "hi",
1309 anObject: {1310 nested: {1311 yummie: true1312 },1313 coffee: false1314 }1315 }

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 1316why, one step back
1316 return autoFreeze ? deepFreeze(data) : data

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1317why, one step back
1317 }

Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417

lines 1318–1358why, one step back
1318 })
13191320 describe(`isDraft - ${name}`, () => {1321 it("returns true for object drafts", () => {1322 produce({}, state => {1323 expect(isDraft(state)).toBeTruthy()1324 })1325 })1326 it("returns true for array drafts", () => {1327 produce([], state => {1328 expect(isDraft(state)).toBeTruthy()1329 })1330 })1331 it("returns true for objects nested in object drafts", () => {1332 produce({a: {b: {}}}, state => {1333 expect(isDraft(state.a)).toBeTruthy()1334 expect(isDraft(state.a.b)).toBeTruthy()1335 })1336 })1337 it("returns false for new objects added to a draft", () => {1338 produce({}, state => {1339 state.a = {}1340 expect(isDraft(state.a)).toBeFalsy()1341 })1342 })1343 it("returns false for objects returned by the producer", () => {1344 const object = produce(null, Object.create)1345 expect(isDraft(object)).toBeFalsy()1346 })1347 it("returns false for arrays returned by the producer", () => {1348 const array = produce(null, _ => [])1349 expect(isDraft(array)).toBeFalsy()1350 })1351 it("returns false for object drafts returned by the producer", () => {1352 const object = produce({}, state => state)1353 expect(isDraft(object)).toBeFalsy()1354 })1355 it("returns false for array drafts returned by the producer", () => {1356 const array = produce([], state => state)1357 expect(isDraft(array)).toBeFalsy()1358 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1359why, one step back
1359 })

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1360–1361why, one step back
1360}
1361

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1362why, one step back
1362function testObjectTypes(produce) {

feat: limited class supportaleclarsonbefore adoption9877d64

lines 1363–1368why, one step back
1363 class Foo {
1364 constructor(foo) {1365 this.foo = foo1366 this[immerable] = true1367 }1368 }

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1369–1373why, one step back
1369 const values = {
1370 "empty object": {},1371 "plain object": {a: 1, b: 2},1372 "object (no prototype)": Object.create(null),1373 "empty array": [],

feat: limited class supportaleclarsonbefore adoption9877d64

lines 1374–1375why, one step back
1374 "plain array": [1, 2],
1375 "class instance (draftable)": new Foo(1)

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1376–1416why, one step back
1376 }
1377 for (const name in values) {1378 const value = values[name]1379 const copy = shallowCopy(value)1380 testObjectType(name, value)1381 testObjectType(name + " (frozen)", Object.freeze(copy))1382 }1383 function testObjectType(name, base) {1384 describe(name, () => {1385 it("creates a draft", () => {1386 produce(base, draft => {1387 expect(draft).not.toBe(base)1388 expect(shallowCopy(draft, true)).toEqual(base)1389 })1390 })13911392 it("preserves the prototype", () => {1393 const proto = Object.getPrototypeOf(base)1394 produce(base, draft => {1395 expect(Object.getPrototypeOf(draft)).toBe(proto)1396 })1397 })13981399 it("returns the base state when no changes are made", () => {1400 expect(produce(base, () => {})).toBe(base)1401 })14021403 it("returns a copy when changes are made", () => {1404 const random = Math.random()1405 const result = produce(base, draft => {1406 draft[0] = random1407 })1408 expect(result).not.toBe(base)1409 expect(result.constructor).toBe(base.constructor)1410 expect(result[0]).toBe(random)1411 })1412 })1413 }1414}14151416function testLiteralTypes(produce) {

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 1417why, one step back
1417 class Foo {}

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1418why, one step back
1418 const values = {

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1419–1421why, one step back
1419 "falsy number": 0,
1420 "truthy number": 1,1421 "negative number": -1,

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1422why, one step back
1422 NaN: NaN,

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1423–1431why, one step back
1423 infinity: 1 / 0,
1424 true: true,1425 false: false,1426 "empty string": "",1427 "truthy string": "1",1428 null: null,1429 undefined: undefined,14301431 /**

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1432–1433why, one step back
1432 * These objects are treated as literals because Immer
1433 * does not know how to draft them.

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 1434why, one step back
1434 */

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1435why, one step back
1435 function: () => {},

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1436–1440why, one step back
1436 "regexp object": /.+/g,
1437 "boxed number": new Number(0),1438 "boxed string": new String(""),1439 "boxed boolean": new Boolean(),1440 "date object": new Date(),

feat: limited class supportaleclarsonbefore adoption9877d64

line 1441why, one step back
1441 "class instance (not draftable)": new Foo()

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 1442why, one step back
1442 }

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1443–1446why, one step back
1443 for (const name in values) {
1444 describe(name, () => {1445 const value = values[name]1446

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1447–1451why, one step back
1447 it("does not create a draft", () => {
1448 produce(value, draft => {1449 expect(draft).toBe(value)1450 })1451 })

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1452–1453why, one step back
1452
1453 it("returns the base state when no changes are made", () => {

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1454–1455why, one step back
1454 expect(produce(value, () => {})).toBe(value)
1455 })

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1456why, one step back
1456

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 1457why, one step back
1457 if (value && typeof value == "object") {

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1458why, one step back
1458 it("does not return a copy when changes are made", () => {

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1459–1467why, one step back
1459 expect(
1460 produce(value, draft => {1461 draft.foo = true1462 })1463 ).toBe(value)1464 })1465 }1466 })1467 }

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1468–1470why, one step back
1468}
14691470function enumerableOnly(x) {

feat: limited class supportaleclarsonbefore adoption9877d64

lines 1471–1472why, one step back
1471 const copy = Array.isArray(x) ? x.slice() : Object.assign({}, x)
1472 each(copy, (prop, value) => {

test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44

line 1473why, one step back
1473 if (value && typeof value === "object") {

feat: limited class supportaleclarsonbefore adoption9877d64

line 1474why, one step back
1474 copy[prop] = enumerableOnly(value)

test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44

line 1475why, one step back
1475 }

feat: limited class supportaleclarsonbefore adoption9877d64

line 1476why, one step back
1476 })

test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44

line 1477why, one step back
1477 return copy

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1478why, one step back
1478}