Skip to content
Myr

base.js

This file

Path

__tests__/base.js

1,694 lines · 62.9 KiB · JavaScript

Last changed by

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

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: Map.prototype.set should return a draftAndrey Goncharovbefore adoption7001201

line 3why, one step back
3import {each, shallowCopy, isEnumerable, DRAFT_STATE} 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–431why, 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 => {

fix: Map.prototype.set should return a draftAndrey Goncharovbefore adoption7001201

lines 432–434why, one step back
432 // Map.prototype.set should return the Map itself
433 const res = s.aMap.set("force", true)434 expect(res).toBe(s.aMap[DRAFT_STATE].draft)

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 435–440why, one step back
435 })
436 expect(nextState).not.toBe(baseState)437 expect(nextState.aMap).not.toBe(baseState.aMap)438 expect(nextState.aMap.get("force")).toEqual(true)439 })440

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 441–448why, one step back
441 it("state stays the same if the the same item is assigned by key", () => {
442 const nextState = produce(baseState, s => {443 s.aMap.set("jediTotal", 42)444 })445 expect(nextState).toBe(baseState)446 expect(nextState.aMap).toBe(baseState.aMap)447 })448

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 449–478why, one step back
449 it("returns 'size'", () => {
450 const nextState = produce(baseState, s => {451 s.aMap.set("newKey", true)452 expect(s.aMap.size).toBe(baseState.aMap.size + 1)453 })454 expect(nextState).not.toBe(baseState)455 expect(nextState.aMap).not.toBe(baseState.aMap)456 expect(nextState.aMap.get("newKey")).toEqual(true)457 expect(nextState.aMap.size).toEqual(baseState.aMap.size + 1)458 })459460 it("can use 'delete' to remove items", () => {461 const nextState = produce(baseState, s => {462 expect(s.aMap.has("jedi")).toBe(true)463 s.aMap.delete("jedi")464 expect(s.aMap.has("jedi")).toBe(false)465 })466 expect(nextState.aMap).not.toBe(baseState.aMap)467 expect(nextState.aMap.size).toBe(baseState.aMap.size - 1)468 expect(baseState.aMap.has("jedi")).toBe(true)469 expect(nextState.aMap.has("jedi")).toBe(false)470 })471472 it("can use 'clear' to remove items", () => {473 const nextState = produce(baseState, s => {474 expect(s.aMap.size).not.toBe(0)475 s.aMap.clear()476 expect(s.aMap.size).toBe(0)477 })478 expect(nextState.aMap).not.toBe(baseState.aMap)

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

line 479why, one step back
479 expect(baseState.aMap.size).not.toBe(0)

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 480–490why, one step back
480 expect(nextState.aMap.size).toBe(0)
481 })482483 it("support 'has'", () => {484 const nextState = produce(baseState, s => {485 expect(s.aMap.has("newKey")).toBe(false)486 s.aMap.set("newKey", true)487 expect(s.aMap.has("newKey")).toBe(true)488 })489 expect(nextState).not.toBe(baseState)490 expect(nextState.aMap).not.toBe(baseState.aMap)

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 491–492why, one step back
491 expect(baseState.aMap.has("newKey")).toBe(false)
492 expect(nextState.aMap.has("newKey")).toBe(true)

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 493–510why, one step back
493 })
494495 it("supports nested maps", () => {496 const base = new Map([497 ["first", new Map([["second", {prop: "test"}]])]498 ])499 const result = produce(base, draft => {500 draft.get("first").get("second").prop = "test1"501 })502 expect(result).not.toBe(base)503 expect(result.get("first")).not.toBe(base.get("first"))504 expect(result.get("first").get("second")).not.toBe(505 base.get("first").get("second")506 )507 expect(base.get("first").get("second").prop).toBe("test")508 expect(result.get("first").get("second").prop).toBe("test1")509 })510 })

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 511–707why, one step back
511
512 describe("set drafts", () => {513 it("supports iteration", () => {514 const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])515 const findById = (set, id) => {516 for (const item of set) {517 if (item.id === id) return item518 }519 return null520 }521 const result = produce(base, draft => {522 const obj1 = findById(draft, 1)523 const obj2 = findById(draft, 2)524 obj1.a = 2525 obj2.a = 2526 })527 expect(result).not.toBe(base)528 expect(base).toEqual(529 new Set([{id: 1, a: 1}, {id: 2, a: 1}])530 )531 expect(result).toEqual(532 new Set([{id: 1, a: 2}, {id: 2, a: 2}])533 )534 })535536 it("supports 'entries'", () => {537 const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])538 const findById = (set, id) => {539 for (const [item1, item2] of set.entries()) {540 expect(item1).toBe(item2)541 if (item1.id === id) return item1542 }543 return null544 }545 const result = produce(base, draft => {546 const obj1 = findById(draft, 1)547 const obj2 = findById(draft, 2)548 obj1.a = 2549 obj2.a = 2550 })551 expect(result).not.toBe(base)552 expect(base).toEqual(553 new Set([{id: 1, a: 1}, {id: 2, a: 1}])554 )555 expect(result).toEqual(556 new Set([{id: 1, a: 2}, {id: 2, a: 2}])557 )558 })559560 it("supports 'values'", () => {561 const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])562 const findById = (set, id) => {563 for (const item of set.values()) {564 if (item.id === id) return item565 }566 return null567 }568 const result = produce(base, draft => {569 const obj1 = findById(draft, 1)570 const obj2 = findById(draft, 2)571 obj1.a = 2572 obj2.a = 2573 })574 expect(result).not.toBe(base)575 expect(base).toEqual(576 new Set([{id: 1, a: 1}, {id: 2, a: 1}])577 )578 expect(result).toEqual(579 new Set([{id: 1, a: 2}, {id: 2, a: 2}])580 )581 })582583 it("supports 'keys'", () => {584 const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])585 const findById = (set, id) => {586 for (const item of set.keys()) {587 if (item.id === id) return item588 }589 return null590 }591 const result = produce(base, draft => {592 const obj1 = findById(draft, 1)593 const obj2 = findById(draft, 2)594 obj1.a = 2595 obj2.a = 2596 })597 expect(result).not.toBe(base)598 expect(base).toEqual(599 new Set([{id: 1, a: 1}, {id: 2, a: 1}])600 )601 expect(result).toEqual(602 new Set([{id: 1, a: 2}, {id: 2, a: 2}])603 )604 })605606 it("supports forEach with mutation after reads", () => {607 const base = new Set([{id: 1, a: 1}, {id: 2, a: 2}])608 const result = produce(base, draft => {609 let sum1 = 0610 draft.forEach(({a}) => {611 sum1 += a612 })613 expect(sum1).toBe(3)614 let sum2 = 0615 draft.forEach(item => {616 item.a += 10617 sum2 += item.a618 })619 expect(sum2).toBe(23)620 })621 expect(result).not.toBe(base)622 expect(base).toEqual(623 new Set([{id: 1, a: 1}, {id: 2, a: 2}])624 )625 expect(result).toEqual(626 new Set([{id: 1, a: 11}, {id: 2, a: 12}])627 )628 })629630 it("state stays the same if the same item is added", () => {631 const nextState = produce(baseState, s => {632 s.aSet.add("Luke")633 })634 expect(nextState).toBe(baseState)635 expect(nextState.aSet).toBe(baseState.aSet)636 })637638 it("can add new items", () => {639 const nextState = produce(baseState, s => {640 // Set.prototype.set should return the Set itself641 const res = s.aSet.add("force")642 expect(res).toBe(s.aSet[DRAFT_STATE].draft)643 })644 expect(nextState).not.toBe(baseState)645 expect(nextState.aSet).not.toBe(baseState.aSet)646 expect(baseState.aSet.has("force")).toBe(false)647 expect(nextState.aSet.has("force")).toBe(true)648 })649650 it("returns 'size'", () => {651 const nextState = produce(baseState, s => {652 s.aSet.add("newKey")653 expect(s.aSet.size).toBe(baseState.aSet.size + 1)654 })655 expect(nextState).not.toBe(baseState)656 expect(nextState.aSet).not.toBe(baseState.aSet)657 expect(nextState.aSet.has("newKey")).toBe(true)658 expect(nextState.aSet.size).toEqual(baseState.aSet.size + 1)659 })660661 it("can use 'delete' to remove items", () => {662 const nextState = produce(baseState, s => {663 expect(s.aSet.has("Luke")).toBe(true)664 s.aSet.delete("Luke")665 expect(s.aSet.has("Luke")).toBe(false)666 })667 expect(nextState.aSet).not.toBe(baseState.aSet)668 expect(nextState.aSet.size).toBe(baseState.aSet.size - 1)669 expect(baseState.aSet.has("Luke")).toBe(true)670 expect(nextState.aSet.has("Luke")).toBe(false)671 })672673 it("can use 'clear' to remove items", () => {674 const nextState = produce(baseState, s => {675 expect(s.aSet.size).not.toBe(0)676 s.aSet.clear()677 expect(s.aSet.size).toBe(0)678 })679 expect(nextState.aSet).not.toBe(baseState.aSet)680 expect(baseState.aSet.size).not.toBe(0)681 expect(nextState.aSet.size).toBe(0)682 })683684 it("supports 'has'", () => {685 const nextState = produce(baseState, s => {686 expect(s.aSet.has("newKey")).toBe(false)687 s.aSet.add("newKey")688 expect(s.aSet.has("newKey")).toBe(true)689 })690 expect(nextState).not.toBe(baseState)691 expect(nextState.aSet).not.toBe(baseState.aSet)692 expect(baseState.aSet.has("newKey")).toBe(false)693 expect(nextState.aSet.has("newKey")).toBe(true)694 })695696 it("supports nested sets", () => {697 const base = new Set([new Set(["Serenity"])])698 const result = produce(base, draft => {699 draft.forEach(nestedItem => nestedItem.add("Firefly"))700 })701 expect(result).not.toBe(base)702 expect(base).toEqual(new Set([new Set(["Serenity"])]))703 expect(result).toEqual(704 new Set([new Set(["Serenity", "Firefly"])])705 )706 })707 })

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 708–709why, one step back
708 }
709

feat: limited class supportaleclarsonbefore adoption9877d64

lines 710–736why, one step back
710 it("supports `immerable` symbol on constructor", () => {
711 class One {}712 One[immerable] = true713 const baseState = new One()714 const nextState = produce(baseState, draft => {715 expect(draft).not.toBe(baseState)716 draft.foo = true717 })718 expect(nextState).not.toBe(baseState)719 expect(nextState.foo).toBeTruthy()720 })721722 it("preserves symbol properties", () => {723 const test = Symbol("test")724 const baseState = {[test]: true}725 const nextState = produce(baseState, s => {726 expect(s[test]).toBeTruthy()727 s.foo = true728 })729 expect(nextState).toEqual({730 [test]: true,731 foo: true732 })733 })734735 it("preserves non-enumerable properties", () => {736 const baseState = {}

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 740–741why, one step back
740 enumerable: false
741 })

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

lines 742–746why, one step back
742 // Non-enumerable primitive property
743 Object.defineProperty(baseState, "bar", {744 value: 1,745 enumerable: false746 })

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

lines 750–753why, one step back
750 s.bar++
751 expect(isEnumerable(s, "foo")).toBeFalsy()752 s.foo.a++753 expect(isEnumerable(s, "foo")).toBeFalsy()

feat: limited class supportaleclarsonbefore adoption9877d64

lines 754–771why, one step back
754 })
755 expect(nextState.foo).toBeTruthy()756 expect(isEnumerable(nextState, "foo")).toBeFalsy()757 })758759 it("throws on computed properties", () => {760 const baseState = {}761 Object.defineProperty(baseState, "foo", {762 get: () => {},763 enumerable: true764 })765 expect(() => {766 produce(baseState, s => {767 // Proxies only throw once a change is made.768 if (useProxies) {769 s.modified = true770 }771 })

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 773–793why, one step back
773 })
774775 it("allows inherited computed properties", () => {776 const proto = {}777 Object.defineProperty(proto, "foo", {778 get() {779 return this.bar780 },781 set(val) {782 this.bar = val783 }784 })785 const baseState = Object.create(proto)786 produce(baseState, s => {787 expect(s.bar).toBeUndefined()788 s.foo = {}789 expect(s.bar).toBeDefined()790 expect(s.foo).toBe(s.bar)791 })792 })793

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 794–815why, one step back
794 it("supports a base state with multiple references to an object", () => {
795 const obj = {}796 const res = produce({a: obj, b: obj}, d => {797 // Two drafts are created for each occurrence of an object in the base state.798 expect(d.a).not.toBe(d.b)799 d.a.z = true800 expect(d.b.z).toBeUndefined()801 })802 expect(res.b).toBe(obj)803 expect(res.a).not.toBe(res.b)804 expect(res.a.z).toBeTruthy()805 })806807 // NOTE: Except the root draft.808 it("supports multiple references to any modified draft", () => {809 const next = produce({a: {b: 1}}, d => {810 d.a.b++811 d.b = d.a812 })813 expect(next.a).toBe(next.b)814 })815

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

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

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 817–819why, one step back
817 const nextState = produce({obj: {}}, s => {
818 s.foo = s.obj819 delete s.obj

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 820why, one step back
820 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 822–823why, one step back
822 })
823

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

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

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 827–830why, one step back
827 const nextState = produce({obj: {a: 1, b: 1}}, s => {
828 s.obj.a = true // change829 delete s.obj.b // delete830 s.obj.c = true // add

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 831why, one step back
831

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 832–833why, one step back
832 s.foo = s.obj
833 delete s.obj

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 834why, one step back
834 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 836–837why, one step back
836 })
837

test: draft nested in new objectaleclarsonbefore adoptionf46659b

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 843why, one step back
843 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: draft nested in new objectaleclarsonbefore adoptionf46659b

lines 845–846why, one step back
845 })
846

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 847–851why, one step back
847 it("can nest a modified draft in a new object", () => {
848 const nextState = produce({obj: {a: 1, b: 1}}, s => {849 s.obj.a = true // change850 delete s.obj.b // delete851 s.obj.c = true // add

test: draft nested in new objectaleclarsonbefore adoptionf46659b

line 852why, one step back
852

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 855why, one step back
855 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: draft nested in new objectaleclarsonbefore adoptionf46659b

lines 857–858why, one step back
857 })
858

chore: improve some testsaleclarsonbefore adoptionefd47f6

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 860–862why, one step back
860 const nextState = produce(baseState, s => {
861 s.aProp = undefined862 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

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

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 864–872why, one step back
864 expect(nextState.aProp).toBe(undefined)
865 })866867 it("supports assigning undefined to a new property", () => {868 const baseState = {}869 const nextState = produce(baseState, s => {870 s.aProp = undefined871 })872 expect(nextState).not.toBe(baseState)

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 873–875why, one step back
873 expect(nextState.aProp).toBe(undefined)
874 })875

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

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

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

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

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 882why, one step back
882

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

lines 883–888why, one step back
883 // Test object drafts:
884 let draft885 produce({a: 1, b: 1}, s => {886 draft = s887 delete s.b888 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 889why, one step back
889

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

lines 890–892why, one step back
890 // Access known property on object draft.
891 expectRevoked(() => {892 draft.a

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 893–894why, one step back
893 })
894

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

lines 895–897why, one step back
895 // Assign known property on object draft.
896 expectRevoked(() => {897 draft.a = true

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 898why, one step back
898 })

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

lines 899–934why, one step back
899
900 // Access unknown property on object draft.901 expectRevoked(() => {902 draft.z903 }, useProxies)904905 // Assign unknown property on object draft.906 expectRevoked(() => {907 draft.z = true908 }, useProxies)909910 // Test array drafts:911 produce([1, 2], s => {912 draft = s913 s.pop()914 })915916 // Access known index of an array draft.917 expectRevoked(() => {918 draft[0]919 })920921 // Assign known index of an array draft.922 expectRevoked(() => {923 draft[0] = true924 })925926 // Access unknown index of an array draft.927 expectRevoked(() => {928 draft[1]929 }, useProxies)930931 // Assign unknown index of an array draft.932 expectRevoked(() => {933 draft[1] = true934 }, useProxies)

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 935–936why, one step back
935 })
936

test: add missing edge casealeclarsonbefore adoptionc46af36

lines 937–944why, one step back
937 it("can access a child draft that was created before the draft was modified", () => {
938 produce({a: {}}, s => {939 const before = s.a940 s.b = 1941 expect(s.a).toBe(before)942 })943 })944

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 946–948why, one step back
946 produce(baseState, draft => {
947 draft.anArray[0] = 5948 draft.anArray.unshift("test")

test: tiny improvementaleclarsonbefore adoptionee49da3

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 950–958why, one step back
950 "test",
951 5,952 2,953 {c: 3},954 1955 ])956 draft.stuffz = "coffee"957 expect(draft.stuffz).toBe("coffee")958 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 959–960why, one step back
959 })
960

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 964–970why, one step back
964 produce({}, draft => {
965 Object.defineProperty(draft, "xx", {966 enumerable: true,967 writeable: true,968 value: 2969 })970 })

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 972–974why, one step back
972 })
973974 it("should handle constructor correctly", () => {

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 976–978why, one step back
976 arr: new Array(),
977 obj: new Object()978 }

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 980–987why, one step back
980 draft.arrConstructed = draft.arr.constructor(1)
981 draft.objConstructed = draft.obj.constructor(1)982 })983 expect(result.arrConstructed).toEqual(new Array().constructor(1))984 expect(result.objConstructed).toEqual(new Object().constructor(1))985 })986987 it("should handle equality correctly - 1", () => {

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 989–991why, one step back
989 y: 3 / 0,
990 z: NaN991 }

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 993–995why, one step back
993 draft.y = 4 / 0
994 draft.z = NaN995 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 997–999why, one step back
997 })
998999 it("should handle equality correctly - 2", () => {

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1001–1002why, one step back
1001 x: -0
1002 }

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1005why, one step back
1005 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1008–1009why, one step back
1008 })
1009

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

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

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

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

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

lines 1016–1020why, one step back
1016 const base = {obj: {a: 1}}
1017 const next = produce(base, draft => {1018 // Notice how `base.obj` is passed, not `draft.obj`1019 const obj2 = produce(base.obj, draft2 => {1020 draft2.a = 0

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 1021why, one step back
1021 })

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 1024why, one step back
1024 })

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

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

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 1026–1028why, one step back
1026 })
1027 })1028

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

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

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

lines 1030–1035why, one step back
1030 it("always wraps the draft in a new draft", () => {
1031 produce({}, parent => {1032 produce(parent, child => {1033 expect(child).not.toBe(parent)1034 expect(isDraft(child)).toBeTruthy()1035 expect(original(child)).toBe(parent)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1036–1037why, one step back
1036 })
1037 })

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

line 1038why, one step back
1038 })

test: more recursive produce testsAlec Larsonbefore adoption8adfb31

lines 1039–1064why, one step back
1039
1040 // Reported by: https://github.com/mweststrate/immer/issues/3431041 it("ensures each property is drafted", () => {1042 produce({a: {}, b: {}}, parent => {1043 parent.a // Access "a" but not "b"1044 produce(parent, child => {1045 child.c = 11046 expect(isDraft(child.a)).toBeTruthy()1047 expect(isDraft(child.b)).toBeTruthy()1048 })1049 })1050 })10511052 it("preserves any pending changes", () => {1053 produce({a: 1, b: 1, d: 1}, parent => {1054 parent.b = 21055 parent.c = 21056 delete parent.d1057 produce(parent, child => {1058 expect(child.a).toBe(1) // unchanged1059 expect(child.b).toBe(2) // changed1060 expect(child.c).toBe(2) // added1061 expect(child.d).toBeUndefined() // deleted1062 })1063 })1064 })

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

lines 1065–1072why, one step back
1065 })
10661067 describe("when base state contains a draft", () => {1068 it("wraps unowned draft with its own draft", () => {1069 produce({a: {}}, parent => {1070 produce({a: parent.a}, child => {1071 expect(child.a).not.toBe(parent.a)1072 expect(isDraft(child.a)).toBeTruthy()

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

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

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

lines 1074–1104why, one step back
1074 })
1075 })1076 })10771078 it("returns unowned draft if no changes were made", () => {1079 produce({a: {}}, parent => {1080 const result = produce({a: parent.a}, () => {})1081 expect(result.a).toBe(parent.a)1082 })1083 })10841085 it("clones the unowned draft when changes are made", () => {1086 produce({a: {}}, parent => {1087 const result = produce({a: parent.a}, child => {1088 child.a.b = 11089 })1090 expect(result.a).not.toBe(parent.a)1091 expect(result.a.b).toBe(1)1092 expect("b" in parent.a).toBeFalsy()1093 })1094 })10951096 // We cannot auto-freeze the result of a nested producer,1097 // because it may contain a draft from a parent producer.1098 it("never auto-freezes the result", () => {1099 produce({a: {}}, parent => {1100 const r = produce({a: parent.a}, child => {1101 child.b = 1 // Ensure a copy is returned.1102 })1103 expect(Object.isFrozen(r)).toBeFalsy()1104 })

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 1105–1106why, one step back
1105 })
1106 })

test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637

lines 1107–1124why, one step back
1107
1108 // "Upvalues" are variables from a parent scope.1109 it("does not finalize upvalue drafts", () => {1110 produce({a: {}, b: {}}, parent => {1111 expect(produce({}, () => parent)).toBe(parent)1112 parent.x // Ensure proxy not revoked.11131114 expect(produce({}, () => [parent])[0]).toBe(parent)1115 parent.x // Ensure proxy not revoked.11161117 expect(produce({}, () => parent.a)).toBe(parent.a)1118 parent.a.x // Ensure proxy not revoked.11191120 // Modified parent test1121 parent.c = 11122 expect(produce({}, () => [parent.b])[0]).toBe(parent.b)1123 parent.b.x // Ensure proxy not revoked.1124 })

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

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

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

lines 1129–1130why, one step back
1129 const one = createPatchedImmer(options)
1130 const two = createPatchedImmer(options)

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 1131–1145why, one step back
1131
1132 const base = {}1133 const result = one.produce(base, s1 =>1134 two.produce({s1}, s2 => {1135 expect(original(s2.s1)).toBe(s1)1136 s2.n = 11137 s2.s1 = one.produce({s2}, s3 => {1138 expect(original(s3.s2)).toBe(s2)1139 expect(original(s3.s2.s1)).toBe(s2.s1)1140 return s3.s2.s11141 })1142 })1143 )1144 expect(result.n).toBe(1)1145 expect(result.s1).toBe(base)

test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637

line 1146why, one step back
1146 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1147–1149why, one step back
1147 })
11481149 if (useProxies)

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

lines 1152–1154why, one step back
1152 expect(() =>
1153 Object.setPrototypeOf(draft, Array)1154 ).toThrowErrorMatchingSnapshot()

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

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1163why, one step back
1163

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1167why, one step back
1167

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1169–1172why, one step back
1169 expect(0 in draft.anArray).toBe(true)
1170 expect("0" in draft.anArray).toBe(true)1171 expect(Reflect.has(draft.anArray, 0)).toBe(true)1172 expect(Reflect.has(draft.anArray, "0")).toBe(true)

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1173why, one step back
1173

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1175–1179why, one step back
1175 expect(17 in draft.anArray).toBe(false)
1176 expect("17" in draft.anArray).toBe(false)1177 expect(Reflect.has(draft.anArray, 17)).toBe(false)1178 expect(Reflect.has(draft.anArray, "17")).toBe(false)1179 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1180–1181why, one step back
1180 })
1181

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

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

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

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 1186why, one step back
1186 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1187–1188why, one step back
1187 })
1188

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

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

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

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

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1192why, one step back
1192 })

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

lines 1193–1195why, one step back
1193 incrementor()
1194 })1195

refactor(ts): simplify IProduce type testsAlec Larsonbefore adoptionbce282d

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

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

lines 1197–1205why, one step back
1197 const world = {
1198 counter: {count: 1},1199 inc: produce(function(draft) {1200 expect(this).toBe(world)1201 draft.counter.count = this.counter.count + 11202 })1203 }12041205 expect(world.inc(world).counter.count).toBe(2)

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1206–1207why, one step back
1206 })
1207

test: rename and expand a testaleclarsonbefore adoption733e16f

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: rename and expand a testaleclarsonbefore adoption733e16f

lines 1210–1213why, one step back
1210 const base = {foo: {x: 0, y: 0}, bar: [0, 0]}
1211 const result = produce(base, draft => {1212 draft.foo = {x: 1, ...draft.foo, y: 1}1213 draft.bar = [1, ...draft.bar, 1]

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 1214why, one step back
1214 })

test: rename and expand a testaleclarsonbefore adoption733e16f

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1218–1222why, one step back
1218 })
1219 })12201221 it("processes with lodash.set", () => {1222 const base = [{id: 1, a: 1}]

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1226–1231why, one step back
1226 expect(base[0].a).toEqual(1)
1227 expect(result[0].a).toEqual(2)1228 })12291230 it("processes with lodash.find", () => {1231 const base = [{id: 1, a: 1}]

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1232–1235why, one step back
1232 const result = produce(base, draft => {
1233 const obj1 = lodash.find(draft, {id: 1})1234 lodash.set(obj1, "a", 2)1235 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1236–1239why, one step back
1236 expect(base[0].a).toEqual(1)
1237 expect(result[0].a).toEqual(2)1238 })1239

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve coveragealeclarsonbefore adoption14d7e3c

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 1245why, one step back
1245 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 1248why, one step back
1248 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1249why, one step back
1249

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 1252–1254why, one step back
1252 const res = produce(base, d => {
1253 d.x = 41254 return d

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 1258–1259why, one step back
1258 })
1259

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

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

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 1261–1265why, one step back
1261 const base = {a: {}}
1262 const res = produce(base, d => {1263 return d.a1264 })1265 expect(res).toBe(base.a)

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

lines 1266–1275why, one step back
1266 })
12671268 // TODO: Avoid throwing if only the child draft was modified.1269 it("cannot return a modified child draft", () => {1270 const base = {a: {}}1271 expect(() => {1272 produce(base, d => {1273 d.a.b = 11274 return d.a1275 })

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

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

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 1277–1291why, one step back
1277 })
12781279 it("can return a frozen object", () => {1280 const res = deepFreeze([{x: 3}])1281 expect(produce({}, () => res)).toBe(res)1282 })12831284 it("can return an object with two references to another object", () => {1285 const next = produce({}, d => {1286 const obj = {}1287 return {obj, arr: [obj]}1288 })1289 expect(next.obj).toBe(next.arr[0])1290 })1291

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 1293–1305why, one step back
1293 const base = {a: {}}
1294 const next = produce(base, d => {1295 return [d.a, d.a]1296 })1297 expect(next[0]).toBe(base.a)1298 expect(next[0]).toBe(next[1])1299 })13001301 it("cannot return an object that references itself", () => {1302 const res = {}1303 res.self = res1304 expect(() => {1305 produce(res, () => res.self)

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 1307–1312why, one step back
1307 })
1308 })13091310 describe("async recipe function", () => {1311 it("can modify the draft", () => {1312 const base = {a: 0, b: 0}

feat: Dropped node 6 supportMichel Weststratebefore adoptioneecc773

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

feat: Dropped node 6 supportMichel Weststratebefore adoptioneecc773

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 1317–1336why, one step back
1317 }).then(res => {
1318 expect(res).not.toBe(base)1319 expect(res).toEqual({a: 1, b: 1})1320 })1321 })13221323 it("works with rejected promises", () => {1324 let draft1325 const base = {a: 0, b: 0}1326 const err = new Error("passed")1327 return produce(base, d => {1328 draft = d1329 draft.b = 11330 return Promise.reject(err)1331 }).then(1332 () => {1333 throw "failed"1334 },1335 e => {1336 expect(e).toBe(err)

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 1338–1363why, one step back
1338 }
1339 )1340 })13411342 it("supports recursive produce calls after await", () => {1343 const base = {obj: {k: 1}}1344 return produce(base, d => {1345 const obj = d.obj1346 delete d.obj1347 return Promise.resolve().then(() => {1348 d.a = produce({}, d => {1349 d.b = obj // Assign a draft owned by the parent scope.1350 })13511352 // Auto-freezing is prevented when an unowned draft exists.1353 expect(Object.isFrozen(d.a)).toBeFalsy()13541355 // Ensure `obj` is not revoked.1356 obj.c = 11357 })1358 }).then(res => {1359 expect(res).not.toBe(base)1360 expect(res).toEqual({1361 a: {b: {k: 1, c: 1}}1362 })1363 })

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 1364why, one step back
1364 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1365–1366why, one step back
1365 })
1366

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1370–1373why, one step back
1370 produce(base, draft => {
1371 draft.x = 41372 return {x: 5}1373 })

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1375–1376why, one step back
1375 })
1376

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1379–1386why, one step back
1379 produce(state, draft => {
1380 switch (action.type) {1381 case "SET_STARTING_DOTS":1382 return draft.availableStartingDots.map(a => a)1383 default:1384 break1385 }1386 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1387–1401why, one step back
1387 const base = {
1388 availableStartingDots: [1389 {dots: 4, count: 1},1390 {dots: 3, count: 2},1391 {dots: 2, count: 3},1392 {dots: 1, count: 4}1393 ]1394 }1395 const next = reducer(base, {type: "SET_STARTING_DOTS"})1396 expect(next).toEqual(base.availableStartingDots)1397 expect(next).not.toBe(base.availableStartingDots)1398 })13991400 it("should fix #117 - 2", () => {1401 const reducer = (state, action) =>

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1402–1411why, one step back
1402 produce(state, draft => {
1403 switch (action.type) {1404 case "SET_STARTING_DOTS":1405 return {1406 dots: draft.availableStartingDots.map(a => a)1407 }1408 default:1409 break1410 }1411 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1412–1423why, one step back
1412 const base = {
1413 availableStartingDots: [1414 {dots: 4, count: 1},1415 {dots: 3, count: 2},1416 {dots: 2, count: 3},1417 {dots: 1, count: 4}1418 ]1419 }1420 const next = reducer(base, {type: "SET_STARTING_DOTS"})1421 expect(next).toEqual({dots: base.availableStartingDots})1422 })1423

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1426–1429why, one step back
1426 const nextState = produce(baseState, d => {
1427 const a = d.x1428 d.x = a1429 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1430–1432why, one step back
1430 expect(nextState).toBe(baseState)
1431 })1432

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1435–1439why, one step back
1435 const nextState = produce(baseState, d => {
1436 const a = d.x1437 d.x = 41438 d.x = a1439 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1440–1445why, one step back
1440 // Ideally, this should actually be the same instances
1441 // but this would be pretty expensive to detect,1442 // so we don't atm1443 expect(nextState).not.toBe(baseState)1444 })1445

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1448–1453why, one step back
1448 const nextState = produce(baseState, d => {
1449 const a = d.x1450 const stuff = a.y + 31451 d.x = 41452 d.x = a1453 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1454–1459why, one step back
1454 // Ideally, this should actually be the same instances
1455 // but this would be pretty expensive to detect,1456 // so we don't atm1457 expect(nextState).not.toBe(baseState)1458 })1459

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1462–1464why, one step back
1462 const nextState = produce(baseState, d => {
1463 d.x = 31464 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1465–1467why, one step back
1465 expect(nextState).toBe(baseState)
1466 })1467

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1470–1473why, one step back
1470 const nextState = produce(baseState, d => {
1471 d.x = 41472 d.x = 31473 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1474–1479why, one step back
1474 // Ideally, this should actually be the same instances
1475 // but this would be pretty expensive to detect,1476 // so we don't atm1477 expect(nextState).not.toBe(baseState)1478 })1479

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1481–1496why, one step back
1481 const base = 3
1482 expect(produce(base, () => 4)).toBe(4)1483 expect(produce(base, () => null)).toBe(null)1484 expect(produce(base, () => undefined)).toBe(3)1485 expect(produce(base, () => {})).toBe(3)1486 expect(produce(base, () => nothing)).toBe(undefined)14871488 expect(produce({}, () => undefined)).toEqual({})1489 expect(produce({}, () => nothing)).toBe(undefined)1490 expect(produce(3, () => nothing)).toBe(undefined)14911492 expect(produce(() => undefined)({})).toEqual({})1493 expect(produce(() => nothing)({})).toBe(undefined)1494 expect(produce(() => nothing)(3)).toBe(undefined)1495 })1496

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1497–1500why, one step back
1497 describe("base state type", () => {
1498 testObjectTypes(produce)1499 testLiteralTypes(produce)1500 })

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

line 1501why, one step back
1501

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1502–1506why, one step back
1502 afterEach(() => {
1503 expect(baseState).toBe(origBaseState)1504 expect(baseState).toEqual(createBaseState())1505 })1506

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1508–1509why, one step back
1508 function createBaseState() {
1509 const data = {

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

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

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 1512–1515why, one step back
1512 aMap: new Map([
1513 ["jedi", {name: "Luke", skill: 10}],1514 ["jediTotal", 42],1515 ["force", "these aren't the droids you're looking for"]

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 1516–1522why, one step back
1516 ]),
1517 aSet: new Set([1518 "Luke",1519 42,1520 {1521 jedi: "Yoda"1522 }

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 1523why, one step back
1523 ]),

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1524–1531why, one step back
1524 aProp: "hi",
1525 anObject: {1526 nested: {1527 yummie: true1528 },1529 coffee: false1530 }1531 }

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1533why, one step back
1533 }

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

lines 1534–1574why, one step back
1534 })
15351536 describe(`isDraft - ${name}`, () => {1537 it("returns true for object drafts", () => {1538 produce({}, state => {1539 expect(isDraft(state)).toBeTruthy()1540 })1541 })1542 it("returns true for array drafts", () => {1543 produce([], state => {1544 expect(isDraft(state)).toBeTruthy()1545 })1546 })1547 it("returns true for objects nested in object drafts", () => {1548 produce({a: {b: {}}}, state => {1549 expect(isDraft(state.a)).toBeTruthy()1550 expect(isDraft(state.a.b)).toBeTruthy()1551 })1552 })1553 it("returns false for new objects added to a draft", () => {1554 produce({}, state => {1555 state.a = {}1556 expect(isDraft(state.a)).toBeFalsy()1557 })1558 })1559 it("returns false for objects returned by the producer", () => {1560 const object = produce(null, Object.create)1561 expect(isDraft(object)).toBeFalsy()1562 })1563 it("returns false for arrays returned by the producer", () => {1564 const array = produce(null, _ => [])1565 expect(isDraft(array)).toBeFalsy()1566 })1567 it("returns false for object drafts returned by the producer", () => {1568 const object = produce({}, state => state)1569 expect(isDraft(object)).toBeFalsy()1570 })1571 it("returns false for array drafts returned by the producer", () => {1572 const array = produce([], state => state)1573 expect(isDraft(array)).toBeFalsy()1574 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1575why, one step back
1575 })

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1576–1577why, one step back
1576}
1577

chore: improve more testsaleclarsonbefore adoption4f59b12

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 1579–1584why, one step back
1579 class Foo {
1580 constructor(foo) {1581 this.foo = foo1582 this[immerable] = true1583 }1584 }

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1585–1589why, one step back
1585 const values = {
1586 "empty object": {},1587 "plain object": {a: 1, b: 2},1588 "object (no prototype)": Object.create(null),1589 "empty array": [],

feat: limited class supportaleclarsonbefore adoption9877d64

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

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1592–1632why, one step back
1592 }
1593 for (const name in values) {1594 const value = values[name]1595 const copy = shallowCopy(value)1596 testObjectType(name, value)1597 testObjectType(name + " (frozen)", Object.freeze(copy))1598 }1599 function testObjectType(name, base) {1600 describe(name, () => {1601 it("creates a draft", () => {1602 produce(base, draft => {1603 expect(draft).not.toBe(base)1604 expect(shallowCopy(draft, true)).toEqual(base)1605 })1606 })16071608 it("preserves the prototype", () => {1609 const proto = Object.getPrototypeOf(base)1610 produce(base, draft => {1611 expect(Object.getPrototypeOf(draft)).toBe(proto)1612 })1613 })16141615 it("returns the base state when no changes are made", () => {1616 expect(produce(base, () => {})).toBe(base)1617 })16181619 it("returns a copy when changes are made", () => {1620 const random = Math.random()1621 const result = produce(base, draft => {1622 draft[0] = random1623 })1624 expect(result).not.toBe(base)1625 expect(result.constructor).toBe(base.constructor)1626 expect(result[0]).toBe(random)1627 })1628 })1629 }1630}16311632function testLiteralTypes(produce) {

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1635–1637why, one step back
1635 "falsy number": 0,
1636 "truthy number": 1,1637 "negative number": -1,

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1639–1647why, one step back
1639 infinity: 1 / 0,
1640 true: true,1641 false: false,1642 "empty string": "",1643 "truthy string": "1",1644 null: null,1645 undefined: undefined,16461647 /**

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 1650why, one step back
1650 */

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1652–1656why, one step back
1652 "regexp object": /.+/g,
1653 "boxed number": new Number(0),1654 "boxed string": new String(""),1655 "boxed boolean": new Boolean(),1656 "date object": new Date(),

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 1658why, one step back
1658 }

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1659–1662why, one step back
1659 for (const name in values) {
1660 describe(name, () => {1661 const value = values[name]1662

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1663–1667why, one step back
1663 it("does not create a draft", () => {
1664 produce(value, draft => {1665 expect(draft).toBe(value)1666 })1667 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

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

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1672why, one step back
1672

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1675–1683why, one step back
1675 expect(
1676 produce(value, draft => {1677 draft.foo = true1678 })1679 ).toBe(value)1680 })1681 }1682 })1683 }

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 1684–1686why, one step back
1684}
16851686function enumerableOnly(x) {

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44

line 1691why, one step back
1691 }

feat: limited class supportaleclarsonbefore adoption9877d64

line 1692why, one step back
1692 })

test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44

line 1693why, one step back
1693 return copy

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

line 1694why, one step back
1694}