Skip to content
Myr

base.js

This file

Path

__tests__/base.js

1,269 lines · 44.7 KiB · JavaScript

Last changed by

test: more recursive produce testsAlec Larsonbefore adoption8adfb31

History of this file

Take it

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 2–3why, one step back
2import {Immer, nothing, original, isDraft, immerable} from "../src/index"
3import {each, shallowCopy, isEnumerable} from "../src/common"

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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)

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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 }

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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()

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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)

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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 }

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 302–328why, one step back
302 it("supports `immerable` symbol on constructor", () => {
303 class One {}304 One[immerable] = true305 const baseState = new One()306 const nextState = produce(baseState, draft => {307 expect(draft).not.toBe(baseState)308 draft.foo = true309 })310 expect(nextState).not.toBe(baseState)311 expect(nextState.foo).toBeTruthy()312 })313314 it("preserves symbol properties", () => {315 const test = Symbol("test")316 const baseState = {[test]: true}317 const nextState = produce(baseState, s => {318 expect(s[test]).toBeTruthy()319 s.foo = true320 })321 expect(nextState).toEqual({322 [test]: true,323 foo: true324 })325 })326327 it("preserves non-enumerable properties", () => {328 const baseState = {}

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 332–333why, one step back
332 enumerable: false
333 })

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

lines 334–338why, one step back
334 // Non-enumerable primitive property
335 Object.defineProperty(baseState, "bar", {336 value: 1,337 enumerable: false338 })

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

lines 342–345why, one step back
342 s.bar++
343 expect(isEnumerable(s, "foo")).toBeFalsy()344 s.foo.a++345 expect(isEnumerable(s, "foo")).toBeFalsy()

feat: limited class supportaleclarsonbefore adoption9877d64

lines 346–363why, one step back
346 })
347 expect(nextState.foo).toBeTruthy()348 expect(isEnumerable(nextState, "foo")).toBeFalsy()349 })350351 it("throws on computed properties", () => {352 const baseState = {}353 Object.defineProperty(baseState, "foo", {354 get: () => {},355 enumerable: true356 })357 expect(() => {358 produce(baseState, s => {359 // Proxies only throw once a change is made.360 if (useProxies) {361 s.modified = true362 }363 })

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 365–385why, one step back
365 })
366367 it("allows inherited computed properties", () => {368 const proto = {}369 Object.defineProperty(proto, "foo", {370 get() {371 return this.bar372 },373 set(val) {374 this.bar = val375 }376 })377 const baseState = Object.create(proto)378 produce(baseState, s => {379 expect(s.bar).toBeUndefined()380 s.foo = {}381 expect(s.bar).toBeDefined()382 expect(s.foo).toBe(s.bar)383 })384 })385

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 386–407why, one step back
386 it("supports a base state with multiple references to an object", () => {
387 const obj = {}388 const res = produce({a: obj, b: obj}, d => {389 // Two drafts are created for each occurrence of an object in the base state.390 expect(d.a).not.toBe(d.b)391 d.a.z = true392 expect(d.b.z).toBeUndefined()393 })394 expect(res.b).toBe(obj)395 expect(res.a).not.toBe(res.b)396 expect(res.a.z).toBeTruthy()397 })398399 // NOTE: Except the root draft.400 it("supports multiple references to any modified draft", () => {401 const next = produce({a: {b: 1}}, d => {402 d.a.b++403 d.b = d.a404 })405 expect(next.a).toBe(next.b)406 })407

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

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

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 409–411why, one step back
409 const nextState = produce({obj: {}}, s => {
410 s.foo = s.obj411 delete s.obj

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 412why, one step back
412 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 414–415why, one step back
414 })
415

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

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

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 419–422why, one step back
419 const nextState = produce({obj: {a: 1, b: 1}}, s => {
420 s.obj.a = true // change421 delete s.obj.b // delete422 s.obj.c = true // add

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 423why, one step back
423

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 424–425why, one step back
424 s.foo = s.obj
425 delete s.obj

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 426why, one step back
426 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 428–429why, one step back
428 })
429

test: draft nested in new objectaleclarsonbefore adoptionf46659b

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 435why, one step back
435 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: draft nested in new objectaleclarsonbefore adoptionf46659b

lines 437–438why, one step back
437 })
438

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 439–443why, one step back
439 it("can nest a modified draft in a new object", () => {
440 const nextState = produce({obj: {a: 1, b: 1}}, s => {441 s.obj.a = true // change442 delete s.obj.b // delete443 s.obj.c = true // add

test: draft nested in new objectaleclarsonbefore adoptionf46659b

line 444why, one step back
444

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 447why, one step back
447 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: draft nested in new objectaleclarsonbefore adoptionf46659b

lines 449–450why, one step back
449 })
450

chore: improve some testsaleclarsonbefore adoptionefd47f6

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 452–454why, one step back
452 const nextState = produce(baseState, s => {
453 s.aProp = undefined454 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 456–464why, one step back
456 expect(nextState.aProp).toBe(undefined)
457 })458459 it("supports assigning undefined to a new property", () => {460 const baseState = {}461 const nextState = produce(baseState, s => {462 s.aProp = undefined463 })464 expect(nextState).not.toBe(baseState)

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 465–467why, one step back
465 expect(nextState.aProp).toBe(undefined)
466 })467

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

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

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

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

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

line 474why, one step back
474

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

lines 475–480why, one step back
475 // Test object drafts:
476 let draft477 produce({a: 1, b: 1}, s => {478 draft = s479 delete s.b480 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

line 481why, one step back
481

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

lines 482–484why, one step back
482 // Access known property on object draft.
483 expectRevoked(() => {484 draft.a

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 485–486why, one step back
485 })
486

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

lines 487–489why, one step back
487 // Assign known property on object draft.
488 expectRevoked(() => {489 draft.a = true

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 490why, one step back
490 })

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

lines 491–526why, one step back
491
492 // Access unknown property on object draft.493 expectRevoked(() => {494 draft.z495 }, useProxies)496497 // Assign unknown property on object draft.498 expectRevoked(() => {499 draft.z = true500 }, useProxies)501502 // Test array drafts:503 produce([1, 2], s => {504 draft = s505 s.pop()506 })507508 // Access known index of an array draft.509 expectRevoked(() => {510 draft[0]511 })512513 // Assign known index of an array draft.514 expectRevoked(() => {515 draft[0] = true516 })517518 // Access unknown index of an array draft.519 expectRevoked(() => {520 draft[1]521 }, useProxies)522523 // Assign unknown index of an array draft.524 expectRevoked(() => {525 draft[1] = true526 }, useProxies)

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 527–528why, one step back
527 })
528

test: add missing edge casealeclarsonbefore adoptionc46af36

lines 529–536why, one step back
529 it("can access a child draft that was created before the draft was modified", () => {
530 produce({a: {}}, s => {531 const before = s.a532 s.b = 1533 expect(s.a).toBe(before)534 })535 })536

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 538–540why, one step back
538 produce(baseState, draft => {
539 draft.anArray[0] = 5540 draft.anArray.unshift("test")

test: tiny improvementaleclarsonbefore adoptionee49da3

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 542–550why, one step back
542 "test",
543 5,544 2,545 {c: 3},546 1547 ])548 draft.stuffz = "coffee"549 expect(draft.stuffz).toBe("coffee")550 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 551–552why, one step back
551 })
552

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 556–562why, one step back
556 produce({}, draft => {
557 Object.defineProperty(draft, "xx", {558 enumerable: true,559 writeable: true,560 value: 2561 })562 })

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

line 564why, one step back
564 })

Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c

line 565why, one step back
565

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

line 566why, one step back
566 it("should handle constructor correctly", () => {

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 568–570why, one step back
568 arr: new Array(),
569 obj: new Object()570 }

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 572–579why, one step back
572 draft.arrConstructed = draft.arr.constructor(1)
573 draft.objConstructed = draft.obj.constructor(1)574 })575 expect(result.arrConstructed).toEqual(new Array().constructor(1))576 expect(result.objConstructed).toEqual(new Object().constructor(1))577 })578579 it("should handle equality correctly - 1", () => {

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 581–583why, one step back
581 y: 3 / 0,
582 z: NaN583 }

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 585–587why, one step back
585 draft.y = 4 / 0
586 draft.z = NaN587 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 589–591why, one step back
589 })
590591 it("should handle equality correctly - 2", () => {

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 593–594why, one step back
593 x: -0
594 }

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

line 597why, one step back
597 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 600–601why, one step back
600 })
601

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

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

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

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

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

lines 608–612why, one step back
608 const base = {obj: {a: 1}}
609 const next = produce(base, draft => {610 // Notice how `base.obj` is passed, not `draft.obj`611 const obj2 = produce(base.obj, draft2 => {612 draft2.a = 0

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 613why, one step back
613 })

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 616why, one step back
616 })

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

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

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 618–620why, one step back
618 })
619 })620

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

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

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

lines 622–627why, one step back
622 it("always wraps the draft in a new draft", () => {
623 produce({}, parent => {624 produce(parent, child => {625 expect(child).not.toBe(parent)626 expect(isDraft(child)).toBeTruthy()627 expect(original(child)).toBe(parent)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

line 630why, one step back
630 })

test: more recursive produce testsAlec Larsonbefore adoption8adfb31

lines 631–656why, one step back
631
632 // Reported by: https://github.com/mweststrate/immer/issues/343633 it("ensures each property is drafted", () => {634 produce({a: {}, b: {}}, parent => {635 parent.a // Access "a" but not "b"636 produce(parent, child => {637 child.c = 1638 expect(isDraft(child.a)).toBeTruthy()639 expect(isDraft(child.b)).toBeTruthy()640 })641 })642 })643644 it("preserves any pending changes", () => {645 produce({a: 1, b: 1, d: 1}, parent => {646 parent.b = 2647 parent.c = 2648 delete parent.d649 produce(parent, child => {650 expect(child.a).toBe(1) // unchanged651 expect(child.b).toBe(2) // changed652 expect(child.c).toBe(2) // added653 expect(child.d).toBeUndefined() // deleted654 })655 })656 })

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

lines 657–664why, one step back
657 })
658659 describe("when base state contains a draft", () => {660 it("wraps unowned draft with its own draft", () => {661 produce({a: {}}, parent => {662 produce({a: parent.a}, child => {663 expect(child.a).not.toBe(parent.a)664 expect(isDraft(child.a)).toBeTruthy()

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

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

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

lines 666–696why, one step back
666 })
667 })668 })669670 it("returns unowned draft if no changes were made", () => {671 produce({a: {}}, parent => {672 const result = produce({a: parent.a}, () => {})673 expect(result.a).toBe(parent.a)674 })675 })676677 it("clones the unowned draft when changes are made", () => {678 produce({a: {}}, parent => {679 const result = produce({a: parent.a}, child => {680 child.a.b = 1681 })682 expect(result.a).not.toBe(parent.a)683 expect(result.a.b).toBe(1)684 expect("b" in parent.a).toBeFalsy()685 })686 })687688 // We cannot auto-freeze the result of a nested producer,689 // because it may contain a draft from a parent producer.690 it("never auto-freezes the result", () => {691 produce({a: {}}, parent => {692 const r = produce({a: parent.a}, child => {693 child.b = 1 // Ensure a copy is returned.694 })695 expect(Object.isFrozen(r)).toBeFalsy()696 })

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 697–698why, one step back
697 })
698 })

test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637

lines 699–716why, one step back
699
700 // "Upvalues" are variables from a parent scope.701 it("does not finalize upvalue drafts", () => {702 produce({a: {}, b: {}}, parent => {703 expect(produce({}, () => parent)).toBe(parent)704 parent.x // Ensure proxy not revoked.705706 expect(produce({}, () => [parent])[0]).toBe(parent)707 parent.x // Ensure proxy not revoked.708709 expect(produce({}, () => parent.a)).toBe(parent.a)710 parent.a.x // Ensure proxy not revoked.711712 // Modified parent test713 parent.c = 1714 expect(produce({}, () => [parent.b])[0]).toBe(parent.b)715 parent.b.x // Ensure proxy not revoked.716 })

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

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

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

lines 721–722why, one step back
721 const one = createPatchedImmer(options)
722 const two = createPatchedImmer(options)

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 723–737why, one step back
723
724 const base = {}725 const result = one.produce(base, s1 =>726 two.produce({s1}, s2 => {727 expect(original(s2.s1)).toBe(s1)728 s2.n = 1729 s2.s1 = one.produce({s2}, s3 => {730 expect(original(s3.s2)).toBe(s2)731 expect(original(s3.s2.s1)).toBe(s2.s1)732 return s3.s2.s1733 })734 })735 )736 expect(result.n).toBe(1)737 expect(result.s1).toBe(base)

test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637

line 738why, one step back
738 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 739–741why, one step back
739 })
740741 if (useProxies)

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

lines 744–746why, one step back
744 expect(() =>
745 Object.setPrototypeOf(draft, Array)746 ).toThrowErrorMatchingSnapshot()

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 747why, one step back
747 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 748–749why, one step back
748 })
749

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

line 755why, one step back
755

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

line 759why, one step back
759

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 761–764why, one step back
761 expect(0 in draft.anArray).toBe(true)
762 expect("0" in draft.anArray).toBe(true)763 expect(Reflect.has(draft.anArray, 0)).toBe(true)764 expect(Reflect.has(draft.anArray, "0")).toBe(true)

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

line 765why, one step back
765

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 767–771why, one step back
767 expect(17 in draft.anArray).toBe(false)
768 expect("17" in draft.anArray).toBe(false)769 expect(Reflect.has(draft.anArray, 17)).toBe(false)770 expect(Reflect.has(draft.anArray, "17")).toBe(false)771 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 772–775why, one step back
772 })
773774 it("'this' should work - 1", () => {775 const base = {x: 3}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 776–778why, one step back
776 const next1 = produce(base, function() {
777 this.x = 4778 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 779–782why, one step back
779 expect(next1).not.toBe(base)
780 expect(next1.x).toBe(4)781 })782

chore: improve more testsaleclarsonbefore adoption4f59b12

line 783why, one step back
783 it("'this' should work - 2", () => {

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 784–792why, one step back
784 const base = {x: 3}
785 const incrementor = produce(function() {786 this.x = 4787 })788 const next1 = incrementor(base)789 expect(next1).not.toBe(base)790 expect(next1.x).toBe(4)791 })792

test: rename and expand a testaleclarsonbefore adoption733e16f

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: rename and expand a testaleclarsonbefore adoption733e16f

lines 795–798why, one step back
795 const base = {foo: {x: 0, y: 0}, bar: [0, 0]}
796 const result = produce(base, draft => {797 draft.foo = {x: 1, ...draft.foo, y: 1}798 draft.bar = [1, ...draft.bar, 1]

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 799why, one step back
799 })

test: rename and expand a testaleclarsonbefore adoption733e16f

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 803–807why, one step back
803 })
804 })805806 it("processes with lodash.set", () => {807 const base = [{id: 1, a: 1}]

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 811–816why, one step back
811 expect(base[0].a).toEqual(1)
812 expect(result[0].a).toEqual(2)813 })814815 it("processes with lodash.find", () => {816 const base = [{id: 1, a: 1}]

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 817–820why, one step back
817 const result = produce(base, draft => {
818 const obj1 = lodash.find(draft, {id: 1})819 lodash.set(obj1, "a", 2)820 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 821–824why, one step back
821 expect(base[0].a).toEqual(1)
822 expect(result[0].a).toEqual(2)823 })824

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve coveragealeclarsonbefore adoption14d7e3c

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 830why, one step back
830 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 833why, one step back
833 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

line 834why, one step back
834

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 837–839why, one step back
837 const res = produce(base, d => {
838 d.x = 4839 return d

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 843–844why, one step back
843 })
844

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

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

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 846–850why, one step back
846 const base = {a: {}}
847 const res = produce(base, d => {848 return d.a849 })850 expect(res).toBe(base.a)

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

lines 851–860why, one step back
851 })
852853 // TODO: Avoid throwing if only the child draft was modified.854 it("cannot return a modified child draft", () => {855 const base = {a: {}}856 expect(() => {857 produce(base, d => {858 d.a.b = 1859 return d.a860 })

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

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

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 862–876why, one step back
862 })
863864 it("can return a frozen object", () => {865 const res = deepFreeze([{x: 3}])866 expect(produce({}, () => res)).toBe(res)867 })868869 it("can return an object with two references to another object", () => {870 const next = produce({}, d => {871 const obj = {}872 return {obj, arr: [obj]}873 })874 expect(next.obj).toBe(next.arr[0])875 })876

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 878–890why, one step back
878 const base = {a: {}}
879 const next = produce(base, d => {880 return [d.a, d.a]881 })882 expect(next[0]).toBe(base.a)883 expect(next[0]).toBe(next[1])884 })885886 it("cannot return an object that references itself", () => {887 const res = {}888 res.self = res889 expect(() => {890 produce(res, () => res.self)

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 892–923why, one step back
892 })
893 })894895 // TODO: rewrite tests with async/await once node 6 support is dropped896 describe("async recipe function", () => {897 it("can modify the draft", () => {898 const base = {a: 0, b: 0}899 return produce(base, d => {900 d.a = 1901 return Promise.resolve().then(() => {902 d.b = 1903 })904 }).then(res => {905 expect(res).not.toBe(base)906 expect(res).toEqual({a: 1, b: 1})907 })908 })909910 it("works with rejected promises", () => {911 let draft912 const base = {a: 0, b: 0}913 const err = new Error("passed")914 return produce(base, d => {915 draft = d916 draft.b = 1917 return Promise.reject(err)918 }).then(919 () => {920 throw "failed"921 },922 e => {923 expect(e).toBe(err)

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 925–950why, one step back
925 }
926 )927 })928929 it("supports recursive produce calls after await", () => {930 const base = {obj: {k: 1}}931 return produce(base, d => {932 const obj = d.obj933 delete d.obj934 return Promise.resolve().then(() => {935 d.a = produce({}, d => {936 d.b = obj // Assign a draft owned by the parent scope.937 })938939 // Auto-freezing is prevented when an unowned draft exists.940 expect(Object.isFrozen(d.a)).toBeFalsy()941942 // Ensure `obj` is not revoked.943 obj.c = 1944 })945 }).then(res => {946 expect(res).not.toBe(base)947 expect(res).toEqual({948 a: {b: {k: 1, c: 1}}949 })950 })

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 951why, one step back
951 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 952–953why, one step back
952 })
953

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 957–960why, one step back
957 produce(base, draft => {
958 draft.x = 4959 return {x: 5}960 })

test: use Jest snapshots for expected errorsAlec Larsonbefore adoption0ad9a91

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 962–963why, one step back
962 })
963

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 966–973why, one step back
966 produce(state, draft => {
967 switch (action.type) {968 case "SET_STARTING_DOTS":969 return draft.availableStartingDots.map(a => a)970 default:971 break972 }973 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 974–988why, one step back
974 const base = {
975 availableStartingDots: [976 {dots: 4, count: 1},977 {dots: 3, count: 2},978 {dots: 2, count: 3},979 {dots: 1, count: 4}980 ]981 }982 const next = reducer(base, {type: "SET_STARTING_DOTS"})983 expect(next).toEqual(base.availableStartingDots)984 expect(next).not.toBe(base.availableStartingDots)985 })986987 it("should fix #117 - 2", () => {988 const reducer = (state, action) =>

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 989–998why, one step back
989 produce(state, draft => {
990 switch (action.type) {991 case "SET_STARTING_DOTS":992 return {993 dots: draft.availableStartingDots.map(a => a)994 }995 default:996 break997 }998 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 999–1010why, one step back
999 const base = {
1000 availableStartingDots: [1001 {dots: 4, count: 1},1002 {dots: 3, count: 2},1003 {dots: 2, count: 3},1004 {dots: 1, count: 4}1005 ]1006 }1007 const next = reducer(base, {type: "SET_STARTING_DOTS"})1008 expect(next).toEqual({dots: base.availableStartingDots})1009 })1010

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1013–1016why, one step back
1013 const nextState = produce(baseState, d => {
1014 const a = d.x1015 d.x = a1016 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 1017–1019why, one step back
1017 expect(nextState).toBe(baseState)
1018 })1019

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1022–1026why, one step back
1022 const nextState = produce(baseState, d => {
1023 const a = d.x1024 d.x = 41025 d.x = a1026 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 1027–1032why, one step back
1027 // Ideally, this should actually be the same instances
1028 // but this would be pretty expensive to detect,1029 // so we don't atm1030 expect(nextState).not.toBe(baseState)1031 })1032

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1035–1040why, one step back
1035 const nextState = produce(baseState, d => {
1036 const a = d.x1037 const stuff = a.y + 31038 d.x = 41039 d.x = a1040 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 1041–1046why, one step back
1041 // Ideally, this should actually be the same instances
1042 // but this would be pretty expensive to detect,1043 // so we don't atm1044 expect(nextState).not.toBe(baseState)1045 })1046

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1049–1051why, one step back
1049 const nextState = produce(baseState, d => {
1050 d.x = 31051 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 1052–1054why, one step back
1052 expect(nextState).toBe(baseState)
1053 })1054

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1057–1060why, one step back
1057 const nextState = produce(baseState, d => {
1058 d.x = 41059 d.x = 31060 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 1061–1066why, one step back
1061 // Ideally, this should actually be the same instances
1062 // but this would be pretty expensive to detect,1063 // so we don't atm1064 expect(nextState).not.toBe(baseState)1065 })1066

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 1068–1081why, one step back
1068 const base = 3
1069 expect(produce(base, () => 4)).toBe(4)1070 expect(produce(base, () => null)).toBe(null)1071 expect(produce(base, () => undefined)).toBe(3)1072 expect(produce(base, () => {})).toBe(3)1073 expect(produce(base, () => nothing)).toBe(undefined)10741075 expect(produce({}, () => undefined)).toEqual({})1076 expect(produce({}, () => nothing)).toBe(undefined)1077 expect(produce(3, () => nothing)).toBe(undefined)10781079 expect(produce(() => undefined)({})).toEqual({})1080 expect(produce(() => nothing)({})).toBe(undefined)1081 expect(produce(() => nothing)(3)).toBe(undefined)

Tried to reproduce #195Michel Weststratebefore adoption4f27a93

lines 1082–1083why, one step back
1082 })
1083

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1084–1087why, one step back
1084 describe("base state type", () => {
1085 testObjectTypes(produce)1086 testLiteralTypes(produce)1087 })

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

line 1088why, one step back
1088

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 1089–1093why, one step back
1089 afterEach(() => {
1090 expect(baseState).toBe(origBaseState)1091 expect(baseState).toEqual(createBaseState())1092 })1093

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 1095–1096why, one step back
1095 function createBaseState() {
1096 const data = {

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 1098–1106why, one step back
1098 anArray: [3, 2, {c: 3}, 1],
1099 aProp: "hi",1100 anObject: {1101 nested: {1102 yummie: true1103 },1104 coffee: false1105 }1106 }

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

line 1108why, one step back
1108 }

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

lines 1109–1149why, one step back
1109 })
11101111 describe(`isDraft - ${name}`, () => {1112 it("returns true for object drafts", () => {1113 produce({}, state => {1114 expect(isDraft(state)).toBeTruthy()1115 })1116 })1117 it("returns true for array drafts", () => {1118 produce([], state => {1119 expect(isDraft(state)).toBeTruthy()1120 })1121 })1122 it("returns true for objects nested in object drafts", () => {1123 produce({a: {b: {}}}, state => {1124 expect(isDraft(state.a)).toBeTruthy()1125 expect(isDraft(state.a.b)).toBeTruthy()1126 })1127 })1128 it("returns false for new objects added to a draft", () => {1129 produce({}, state => {1130 state.a = {}1131 expect(isDraft(state.a)).toBeFalsy()1132 })1133 })1134 it("returns false for objects returned by the producer", () => {1135 const object = produce(null, Object.create)1136 expect(isDraft(object)).toBeFalsy()1137 })1138 it("returns false for arrays returned by the producer", () => {1139 const array = produce(null, _ => [])1140 expect(isDraft(array)).toBeFalsy()1141 })1142 it("returns false for object drafts returned by the producer", () => {1143 const object = produce({}, state => state)1144 expect(isDraft(object)).toBeFalsy()1145 })1146 it("returns false for array drafts returned by the producer", () => {1147 const array = produce([], state => state)1148 expect(isDraft(array)).toBeFalsy()1149 })

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

line 1150why, one step back
1150 })

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1151–1152why, one step back
1151}
1152

chore: improve more testsaleclarsonbefore adoption4f59b12

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 1154–1159why, one step back
1154 class Foo {
1155 constructor(foo) {1156 this.foo = foo1157 this[immerable] = true1158 }1159 }

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1160–1164why, one step back
1160 const values = {
1161 "empty object": {},1162 "plain object": {a: 1, b: 2},1163 "object (no prototype)": Object.create(null),1164 "empty array": [],

feat: limited class supportaleclarsonbefore adoption9877d64

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

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1167–1207why, one step back
1167 }
1168 for (const name in values) {1169 const value = values[name]1170 const copy = shallowCopy(value)1171 testObjectType(name, value)1172 testObjectType(name + " (frozen)", Object.freeze(copy))1173 }1174 function testObjectType(name, base) {1175 describe(name, () => {1176 it("creates a draft", () => {1177 produce(base, draft => {1178 expect(draft).not.toBe(base)1179 expect(shallowCopy(draft, true)).toEqual(base)1180 })1181 })11821183 it("preserves the prototype", () => {1184 const proto = Object.getPrototypeOf(base)1185 produce(base, draft => {1186 expect(Object.getPrototypeOf(draft)).toBe(proto)1187 })1188 })11891190 it("returns the base state when no changes are made", () => {1191 expect(produce(base, () => {})).toBe(base)1192 })11931194 it("returns a copy when changes are made", () => {1195 const random = Math.random()1196 const result = produce(base, draft => {1197 draft[0] = random1198 })1199 expect(result).not.toBe(base)1200 expect(result.constructor).toBe(base.constructor)1201 expect(result[0]).toBe(random)1202 })1203 })1204 }1205}12061207function testLiteralTypes(produce) {

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1210–1212why, one step back
1210 "falsy number": 0,
1211 "truthy number": 1,1212 "negative number": -1,

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1214–1222why, one step back
1214 infinity: 1 / 0,
1215 true: true,1216 false: false,1217 "empty string": "",1218 "truthy string": "1",1219 null: null,1220 undefined: undefined,12211222 /**

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 1225why, one step back
1225 */

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1227–1231why, one step back
1227 "regexp object": /.+/g,
1228 "boxed number": new Number(0),1229 "boxed string": new String(""),1230 "boxed boolean": new Boolean(),1231 "date object": new Date(),

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 1233why, one step back
1233 }

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1234–1237why, one step back
1234 for (const name in values) {
1235 describe(name, () => {1236 const value = values[name]1237

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1238–1242why, one step back
1238 it("does not create a draft", () => {
1239 produce(value, draft => {1240 expect(draft).toBe(value)1241 })1242 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

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

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1247why, one step back
1247

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1250–1258why, one step back
1250 expect(
1251 produce(value, draft => {1252 draft.foo = true1253 })1254 ).toBe(value)1255 })1256 }1257 })1258 }

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 1259–1261why, one step back
1259}
12601261function enumerableOnly(x) {

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44

line 1266why, one step back
1266 }

feat: limited class supportaleclarsonbefore adoption9877d64

line 1267why, one step back
1267 })

test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44

line 1268why, one step back
1268 return copy

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

line 1269why, one step back
1269}