Skip to content
Myr

base.js

This file

Path

__tests__/base.js

1,210 lines · 42.6 KiB · JavaScript

Last changed by

test: avoid async/await for node 6aleclarsonbefore adoptione9f274e

History of this file

Take it

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

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"

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 14–15why, one step back
14runBaseTest("proxy (no freeze)", true, false)
15runBaseTest("proxy (autofreeze)", true, true)

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 16why, one step back
16runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 17–18why, one step back
17runBaseTest("es5 (no freeze)", false, false)
18runBaseTest("es5 (autofreeze)", false, true)

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 19why, one step back
19runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 20why, one step back
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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

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)

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 65–67why, one step back
65 expect(nextState.nested).toBe(baseState.nested)
66 })67

Merge pull request #158 from stefanwille/base-test-uses-if-instead-of-itbenbraoubefore adoption0d04d62

line 68why, one step back
68 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 })

Merge pull request #158 from stefanwille/base-test-uses-if-instead-of-itbenbraoubefore adoption0d04d62

lines 72–77why, 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 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 78–79why, one step back
78
79 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 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

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

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 113–115why, one step back
113 it("can get property descriptors", () => {
114 const getDescriptor = Object.getOwnPropertyDescriptor115 const baseState = deepFreeze([{a: 1}])

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

lines 116–122why, one step back
116 produce(baseState, arr => {
117 const obj = arr[0]118 const desc = {119 configurable: true,120 enumerable: true,121 ...(useProxies && {writable: true})122 }

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 123why, one step back
123

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

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

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 127–129why, one step back
127
128 // Deleted property129 delete obj.a

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

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

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 133–135why, one step back
133
134 // Unknown property135 expect(getDescriptor(obj, "b")).toBeUndefined()

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

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

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 137–139why, one step back
137
138 // Added property139 obj.b = 2

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 143why, one step back
143 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 144–145why, one step back
144 })
145

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 146–152why, one step back
146 describe("array drafts", () => {
147 it("supports Array.isArray()", () => {148 const nextState = produce(baseState, s => {149 expect(Array.isArray(s.anArray)).toBeTruthy()150 s.anArray.push(1)151 })152 expect(Array.isArray(nextState.anArray)).toBeTruthy()

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 153–154why, one step back
153 })
154

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 155–160why, one step back
155 it("supports index access", () => {
156 const value = baseState.anArray[0]157 const nextState = produce(baseState, s => {158 expect(s.anArray[0]).toBe(value)159 })160 expect(nextState).toBe(baseState)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 161why, one step back
161 })

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 162–178why, one step back
162
163 it("supports iteration", () => {164 const base = [{id: 1, a: 1}, {id: 2, a: 1}]165 const findById = (collection, id) => {166 for (const item of collection) {167 if (item.id === id) return item168 }169 return null170 }171 const result = produce(base, draft => {172 const obj1 = findById(draft, 1)173 const obj2 = findById(draft, 2)174 obj1.a = 2175 obj2.a = 2176 })177 expect(result[0].a).toEqual(2)178 expect(result[1].a).toEqual(2)

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

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

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 181–187why, one step back
181 it("can assign an index via bracket notation", () => {
182 const nextState = produce(baseState, s => {183 s.anArray[3] = true184 })185 expect(nextState).not.toBe(baseState)186 expect(nextState.anArray).not.toBe(baseState.anArray)187 expect(nextState.anArray[3]).toEqual(true)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 188why, one step back
188 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 189why, one step back
189

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 190–196why, one step back
190 it("can use splice() to both add and remove items", () => {
191 const nextState = produce(baseState, s => {192 s.anArray.splice(1, 1, "a", "b")193 })194 expect(nextState.anArray).not.toBe(baseState.anArray)195 expect(nextState.anArray[1]).toBe("a")196 expect(nextState.anArray[2]).toBe("b")

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 197why, one step back
197 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 198why, one step back
198

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 199–205why, one step back
199 it("can truncate via the length property", () => {
200 const baseLength = baseState.anArray.length201 const nextState = produce(baseState, s => {202 s.anArray.length = baseLength - 1203 })204 expect(nextState.anArray).not.toBe(baseState.anArray)205 expect(nextState.anArray.length).toBe(baseLength - 1)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 206why, one step back
206 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 207why, one step back
207

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 208–215why, one step back
208 it("can extend via the length property", () => {
209 const baseLength = baseState.anArray.length210 const nextState = produce(baseState, s => {211 s.anArray.length = baseLength + 1212 })213 expect(nextState.anArray).not.toBe(baseState.anArray)214 expect(nextState.anArray.length).toBe(baseLength + 1)215 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 216why, one step back
216

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 217–223why, one step back
217 // Reported here: https://github.com/mweststrate/immer/issues/116
218 it("can pop then push", () => {219 const nextState = produce([1, 2, 3], s => {220 s.pop()221 s.push(100)222 })223 expect(nextState).toEqual([1, 2, 100])

test: rename and move a testaleclarsonbefore adoption8e0325c

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

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 226–232why, one step back
226 it("can be sorted", () => {
227 const baseState = [3, 1, 2]228 const nextState = produce(baseState, s => {229 s.sort()230 })231 expect(nextState).not.toBe(baseState)232 expect(nextState).toEqual([1, 2, 3])

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 233why, one step back
233 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 234why, one step back
234

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 235–243why, one step back
235 it("supports modifying nested objects", () => {
236 const baseState = [{a: 1}, {}]237 const nextState = produce(baseState, s => {238 s[0].a++239 s[1].a = 0240 })241 expect(nextState).not.toBe(baseState)242 expect(nextState[0].a).toBe(2)243 expect(nextState[1].a).toBe(0)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 244why, one step back
244 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 245why, one step back
245

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 246–250why, one step back
246 it("never preserves non-numeric properties", () => {
247 const baseState = []248 baseState.x = 7249 const nextState = produce(baseState, s => {250 s.push(3)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 251why, one step back
251 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 253why, one step back
253 })

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 254–260why, one step back
254
255 if (useProxies) {256 it("throws when a non-numeric property is added", () => {257 expect(() => {258 produce([], d => {259 d.x = 3260 })

feat: limited class supportaleclarsonbefore adoption9877d64

lines 261–263why, one step back
261 }).toThrow(
262 "Immer only supports setting array indices and the 'length' property"263 )

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 264–272why, one step back
264 })
265266 it("throws when a non-numeric property is deleted", () => {267 expect(() => {268 const baseState = []269 baseState.x = 7270 produce(baseState, d => {271 delete d.x272 })

feat: limited class supportaleclarsonbefore adoption9877d64

line 273why, one step back
273 }).toThrow("Immer only supports deleting array indices")

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 278–304why, one step back
278 it("supports `immerable` symbol on constructor", () => {
279 class One {}280 One[immerable] = true281 const baseState = new One()282 const nextState = produce(baseState, draft => {283 expect(draft).not.toBe(baseState)284 draft.foo = true285 })286 expect(nextState).not.toBe(baseState)287 expect(nextState.foo).toBeTruthy()288 })289290 it("preserves symbol properties", () => {291 const test = Symbol("test")292 const baseState = {[test]: true}293 const nextState = produce(baseState, s => {294 expect(s[test]).toBeTruthy()295 s.foo = true296 })297 expect(nextState).toEqual({298 [test]: true,299 foo: true300 })301 })302303 it("preserves non-enumerable properties", () => {304 const baseState = {}

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 308–309why, one step back
308 enumerable: false
309 })

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

lines 310–314why, one step back
310 // Non-enumerable primitive property
311 Object.defineProperty(baseState, "bar", {312 value: 1,313 enumerable: false314 })

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: improve "property descriptor" testsaleclarsonbefore adoptiona84eea5

lines 318–321why, one step back
318 s.bar++
319 expect(isEnumerable(s, "foo")).toBeFalsy()320 s.foo.a++321 expect(isEnumerable(s, "foo")).toBeFalsy()

feat: limited class supportaleclarsonbefore adoption9877d64

lines 322–361why, one step back
322 })
323 expect(nextState.foo).toBeTruthy()324 expect(isEnumerable(nextState, "foo")).toBeFalsy()325 })326327 it("throws on computed properties", () => {328 const baseState = {}329 Object.defineProperty(baseState, "foo", {330 get: () => {},331 enumerable: true332 })333 expect(() => {334 produce(baseState, s => {335 // Proxies only throw once a change is made.336 if (useProxies) {337 s.modified = true338 }339 })340 }).toThrowError("Immer drafts cannot have computed properties")341 })342343 it("allows inherited computed properties", () => {344 const proto = {}345 Object.defineProperty(proto, "foo", {346 get() {347 return this.bar348 },349 set(val) {350 this.bar = val351 }352 })353 const baseState = Object.create(proto)354 produce(baseState, s => {355 expect(s.bar).toBeUndefined()356 s.foo = {}357 expect(s.bar).toBeDefined()358 expect(s.foo).toBe(s.bar)359 })360 })361

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 362–383why, one step back
362 it("supports a base state with multiple references to an object", () => {
363 const obj = {}364 const res = produce({a: obj, b: obj}, d => {365 // Two drafts are created for each occurrence of an object in the base state.366 expect(d.a).not.toBe(d.b)367 d.a.z = true368 expect(d.b.z).toBeUndefined()369 })370 expect(res.b).toBe(obj)371 expect(res.a).not.toBe(res.b)372 expect(res.a.z).toBeTruthy()373 })374375 // NOTE: Except the root draft.376 it("supports multiple references to any modified draft", () => {377 const next = produce({a: {b: 1}}, d => {378 d.a.b++379 d.b = d.a380 })381 expect(next.a).toBe(next.b)382 })383

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

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

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 385–387why, one step back
385 const nextState = produce({obj: {}}, s => {
386 s.foo = s.obj387 delete s.obj

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 388why, one step back
388 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 390–391why, one step back
390 })
391

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

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

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 395–398why, one step back
395 const nextState = produce({obj: {a: 1, b: 1}}, s => {
396 s.obj.a = true // change397 delete s.obj.b // delete398 s.obj.c = true // add

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 399why, one step back
399

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 400–401why, one step back
400 s.foo = s.obj
401 delete s.obj

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 402why, one step back
402 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 404–405why, one step back
404 })
405

test: draft nested in new objectaleclarsonbefore adoptionf46659b

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 411why, one step back
411 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: draft nested in new objectaleclarsonbefore adoptionf46659b

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

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 415–419why, one step back
415 it("can nest a modified draft in a new object", () => {
416 const nextState = produce({obj: {a: 1, b: 1}}, s => {417 s.obj.a = true // change418 delete s.obj.b // delete419 s.obj.c = true // add

test: draft nested in new objectaleclarsonbefore adoptionf46659b

line 420why, one step back
420

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 423why, one step back
423 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: draft nested in new objectaleclarsonbefore adoptionf46659b

lines 425–426why, one step back
425 })
426

chore: improve some testsaleclarsonbefore adoptionefd47f6

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 428–430why, one step back
428 const nextState = produce(baseState, s => {
429 s.aProp = undefined430 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

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

chore: improve some testsaleclarsonbefore adoptionefd47f6

lines 432–440why, one step back
432 expect(nextState.aProp).toBe(undefined)
433 })434435 it("supports assigning undefined to a new property", () => {436 const baseState = {}437 const nextState = produce(baseState, s => {438 s.aProp = undefined439 })440 expect(nextState).not.toBe(baseState)

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 441–443why, one step back
441 expect(nextState.aProp).toBe(undefined)
442 })443

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

lines 444–449why, one step back
444 // NOTE: ES5 drafts only protect existing properties when revoked.
445 it("revokes the draft once produce returns", () => {446 const expectRevoked = (fn, shouldThrow = true) => {447 if (shouldThrow) expect(fn).toThrowError(/revoked/)448 else expect(fn).not.toThrow()449 }

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 450why, one step back
450

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

lines 451–456why, one step back
451 // Test object drafts:
452 let draft453 produce({a: 1, b: 1}, s => {454 draft = s455 delete s.b456 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 457why, one step back
457

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

lines 458–460why, one step back
458 // Access known property on object draft.
459 expectRevoked(() => {460 draft.a

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 461–462why, one step back
461 })
462

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

lines 463–465why, one step back
463 // Assign known property on object draft.
464 expectRevoked(() => {465 draft.a = true

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 466why, one step back
466 })

test: rewrite "revoked proxy" testsaleclarsonbefore adoptionb4964ad

lines 467–502why, one step back
467
468 // Access unknown property on object draft.469 expectRevoked(() => {470 draft.z471 }, useProxies)472473 // Assign unknown property on object draft.474 expectRevoked(() => {475 draft.z = true476 }, useProxies)477478 // Test array drafts:479 produce([1, 2], s => {480 draft = s481 s.pop()482 })483484 // Access known index of an array draft.485 expectRevoked(() => {486 draft[0]487 })488489 // Assign known index of an array draft.490 expectRevoked(() => {491 draft[0] = true492 })493494 // Access unknown index of an array draft.495 expectRevoked(() => {496 draft[1]497 }, useProxies)498499 // Assign unknown index of an array draft.500 expectRevoked(() => {501 draft[1] = true502 }, useProxies)

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 503–504why, one step back
503 })
504

test: add missing edge casealeclarsonbefore adoptionc46af36

lines 505–512why, one step back
505 it("can access a child draft that was created before the draft was modified", () => {
506 produce({a: {}}, s => {507 const before = s.a508 s.b = 1509 expect(s.a).toBe(before)510 })511 })512

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 514–516why, one step back
514 produce(baseState, draft => {
515 draft.anArray[0] = 5516 draft.anArray.unshift("test")

test: tiny improvementaleclarsonbefore adoptionee49da3

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 518–526why, one step back
518 "test",
519 5,520 2,521 {c: 3},522 1523 ])524 draft.stuffz = "coffee"525 expect(draft.stuffz).toBe("coffee")526 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 532–538why, one step back
532 produce({}, draft => {
533 Object.defineProperty(draft, "xx", {534 enumerable: true,535 writeable: true,536 value: 2537 })538 })

feat: limited class supportaleclarsonbefore adoption9877d64

lines 539–541why, one step back
539 }).toThrow(
540 "Object.defineProperty() cannot be used on an Immer draft"541 )

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 542why, one step back
542 })

Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c

line 543why, one step back
543

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 546–548why, one step back
546 arr: new Array(),
547 obj: new Object()548 }

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 550–557why, one step back
550 draft.arrConstructed = draft.arr.constructor(1)
551 draft.objConstructed = draft.obj.constructor(1)552 })553 expect(result.arrConstructed).toEqual(new Array().constructor(1))554 expect(result.objConstructed).toEqual(new Object().constructor(1))555 })556557 it("should handle equality correctly - 1", () => {

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 559–561why, one step back
559 y: 3 / 0,
560 z: NaN561 }

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 563–565why, one step back
563 draft.y = 4 / 0
564 draft.z = NaN565 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 567–569why, one step back
567 })
568569 it("should handle equality correctly - 2", () => {

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 571–572why, one step back
571 x: -0
572 }

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 575why, one step back
575 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 578–579why, one step back
578 })
579

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

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

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

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

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

lines 586–590why, one step back
586 const base = {obj: {a: 1}}
587 const next = produce(base, draft => {588 // Notice how `base.obj` is passed, not `draft.obj`589 const obj2 = produce(base.obj, draft2 => {590 draft2.a = 0

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 591why, one step back
591 })

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 594why, one step back
594 })

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

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

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 596–598why, one step back
596 })
597 })598

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

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

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

lines 600–605why, one step back
600 it("always wraps the draft in a new draft", () => {
601 produce({}, parent => {602 produce(parent, child => {603 expect(child).not.toBe(parent)604 expect(isDraft(child)).toBeTruthy()605 expect(original(child)).toBe(parent)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 606–607why, one step back
606 })
607 })

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

lines 608–616why, one step back
608 })
609 })610611 describe("when base state contains a draft", () => {612 it("wraps unowned draft with its own draft", () => {613 produce({a: {}}, parent => {614 produce({a: parent.a}, child => {615 expect(child.a).not.toBe(parent.a)616 expect(isDraft(child.a)).toBeTruthy()

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

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

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

lines 618–648why, one step back
618 })
619 })620 })621622 it("returns unowned draft if no changes were made", () => {623 produce({a: {}}, parent => {624 const result = produce({a: parent.a}, () => {})625 expect(result.a).toBe(parent.a)626 })627 })628629 it("clones the unowned draft when changes are made", () => {630 produce({a: {}}, parent => {631 const result = produce({a: parent.a}, child => {632 child.a.b = 1633 })634 expect(result.a).not.toBe(parent.a)635 expect(result.a.b).toBe(1)636 expect("b" in parent.a).toBeFalsy()637 })638 })639640 // We cannot auto-freeze the result of a nested producer,641 // because it may contain a draft from a parent producer.642 it("never auto-freezes the result", () => {643 produce({a: {}}, parent => {644 const r = produce({a: parent.a}, child => {645 child.b = 1 // Ensure a copy is returned.646 })647 expect(Object.isFrozen(r)).toBeFalsy()648 })

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

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

test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637

lines 651–668why, one step back
651
652 // "Upvalues" are variables from a parent scope.653 it("does not finalize upvalue drafts", () => {654 produce({a: {}, b: {}}, parent => {655 expect(produce({}, () => parent)).toBe(parent)656 parent.x // Ensure proxy not revoked.657658 expect(produce({}, () => [parent])[0]).toBe(parent)659 parent.x // Ensure proxy not revoked.660661 expect(produce({}, () => parent.a)).toBe(parent.a)662 parent.a.x // Ensure proxy not revoked.663664 // Modified parent test665 parent.c = 1666 expect(produce({}, () => [parent.b])[0]).toBe(parent.b)667 parent.b.x // Ensure proxy not revoked.668 })

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

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

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

lines 673–674why, one step back
673 const one = createPatchedImmer(options)
674 const two = createPatchedImmer(options)

test: interweaved Immer instancesaleclarsonbefore adoptionbe06ef5

lines 675–689why, one step back
675
676 const base = {}677 const result = one.produce(base, s1 =>678 two.produce({s1}, s2 => {679 expect(original(s2.s1)).toBe(s1)680 s2.n = 1681 s2.s1 = one.produce({s2}, s3 => {682 expect(original(s3.s2)).toBe(s2)683 expect(original(s3.s2.s1)).toBe(s2.s1)684 return s3.s2.s1685 })686 })687 )688 expect(result.n).toBe(1)689 expect(result.s1).toBe(base)

test: returning an upvalue draft from a nested produceraleclarsonbefore adoptionb053637

line 690why, one step back
690 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 691–693why, one step back
691 })
692693 if (useProxies)

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 695–696why, one step back
695 produce({}, draft => {
696 expect(() => Object.setPrototypeOf(draft, Array)).toThrow(

feat: limited class supportaleclarsonbefore adoption9877d64

line 697why, one step back
697 "Object.setPrototypeOf() cannot be used on an Immer draft"

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 700–701why, one step back
700 })
701

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 707why, one step back
707

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 711why, one step back
711

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 713–716why, one step back
713 expect(0 in draft.anArray).toBe(true)
714 expect("0" in draft.anArray).toBe(true)715 expect(Reflect.has(draft.anArray, 0)).toBe(true)716 expect(Reflect.has(draft.anArray, "0")).toBe(true)

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 717why, one step back
717

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 719–723why, one step back
719 expect(17 in draft.anArray).toBe(false)
720 expect("17" in draft.anArray).toBe(false)721 expect(Reflect.has(draft.anArray, 17)).toBe(false)722 expect(Reflect.has(draft.anArray, "17")).toBe(false)723 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 728–730why, one step back
728 const next1 = produce(base, function() {
729 this.x = 4730 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 731–734why, one step back
731 expect(next1).not.toBe(base)
732 expect(next1.x).toBe(4)733 })734

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 736–744why, one step back
736 const base = {x: 3}
737 const incrementor = produce(function() {738 this.x = 4739 })740 const next1 = incrementor(base)741 expect(next1).not.toBe(base)742 expect(next1.x).toBe(4)743 })744

test: rename and expand a testaleclarsonbefore adoption733e16f

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: rename and expand a testaleclarsonbefore adoption733e16f

lines 747–750why, one step back
747 const base = {foo: {x: 0, y: 0}, bar: [0, 0]}
748 const result = produce(base, draft => {749 draft.foo = {x: 1, ...draft.foo, y: 1}750 draft.bar = [1, ...draft.bar, 1]

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 751why, one step back
751 })

test: rename and expand a testaleclarsonbefore adoption733e16f

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 755–759why, one step back
755 })
756 })757758 it("processes with lodash.set", () => {759 const base = [{id: 1, a: 1}]

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 763–768why, one step back
763 expect(base[0].a).toEqual(1)
764 expect(result[0].a).toEqual(2)765 })766767 it("processes with lodash.find", () => {768 const base = [{id: 1, a: 1}]

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 769–772why, one step back
769 const result = produce(base, draft => {
770 const obj1 = lodash.find(draft, {id: 1})771 lodash.set(obj1, "a", 2)772 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 773–776why, one step back
773 expect(base[0].a).toEqual(1)
774 expect(result[0].a).toEqual(2)775 })776

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve coveragealeclarsonbefore adoption14d7e3c

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 782why, one step back
782 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 785why, one step back
785 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 786why, one step back
786

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 789–791why, one step back
789 const res = produce(base, d => {
790 d.x = 4791 return d

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 795–817why, one step back
795 })
796797 it("can return a child draft", () => {798 const base = {a: {}}799 const res = produce(base, d => {800 return d.a801 })802 expect(res).toBe(base.a)803 })804805 it("can return a frozen object", () => {806 const res = deepFreeze([{x: 3}])807 expect(produce({}, () => res)).toBe(res)808 })809810 it("can return an object with two references to another object", () => {811 const next = produce({}, d => {812 const obj = {}813 return {obj, arr: [obj]}814 })815 expect(next.obj).toBe(next.arr[0])816 })817

feat: support async recipesaleclarsonbefore adoption5fee518

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

test: improve coveragealeclarsonbefore adoption14d7e3c

lines 819–832why, one step back
819 const base = {a: {}}
820 const next = produce(base, d => {821 return [d.a, d.a]822 })823 expect(next[0]).toBe(base.a)824 expect(next[0]).toBe(next[1])825 })826827 it("cannot return an object that references itself", () => {828 const res = {}829 res.self = res830 expect(() => {831 produce(res, () => res.self)832 }).toThrow("Immer forbids circular references")

feat: support async recipesaleclarsonbefore adoption5fee518

lines 833–835why, one step back
833 })
834 })835

test: avoid async/await for node 6aleclarsonbefore adoptione9f274e

line 836why, one step back
836 // TODO: rewrite tests with async/await once node 6 support is dropped

feat: support async recipesaleclarsonbefore adoption5fee518

line 837why, one step back
837 describe("async recipe function", () => {

test: avoid async/await for node 6aleclarsonbefore adoptione9f274e

line 838why, one step back
838 it("can modify the draft", () => {

feat: support async recipesaleclarsonbefore adoption5fee518

line 839why, one step back
839 const base = {a: 0, b: 0}

test: avoid async/await for node 6aleclarsonbefore adoptione9f274e

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

feat: support async recipesaleclarsonbefore adoption5fee518

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

test: avoid async/await for node 6aleclarsonbefore adoptione9f274e

lines 842–847why, one step back
842 return Promise.resolve().then(() => {
843 d.b = 1844 })845 }).then(res => {846 expect(res).not.toBe(base)847 expect(res).toEqual({a: 1, b: 1})

feat: support async recipesaleclarsonbefore adoption5fee518

lines 848–850why, one step back
848 })
849 })850

test: avoid async/await for node 6aleclarsonbefore adoptione9f274e

line 851why, one step back
851 it("works with rejected promises", () => {

feat: support async recipesaleclarsonbefore adoption5fee518

lines 852–854why, one step back
852 let draft
853 const base = {a: 0, b: 0}854 const err = new Error("passed")

test: avoid async/await for node 6aleclarsonbefore adoptione9f274e

lines 855–867why, one step back
855 return produce(base, d => {
856 draft = d857 draft.b = 1858 return Promise.reject(err)859 }).then(860 () => {861 throw "failed"862 },863 e => {864 expect(e).toBe(err)865 expect(() => draft.a).toThrowError(/revoked/)866 }867 )

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 868why, one step back
868 })

test: recursive produce call in async recipealeclarsonbefore adoptione266f4b

line 869why, one step back
869

test: avoid async/await for node 6aleclarsonbefore adoptione9f274e

line 870why, one step back
870 it("supports recursive produce calls after await", () => {

test: recursive produce call in async recipealeclarsonbefore adoptione266f4b

line 871why, one step back
871 const base = {obj: {k: 1}}

test: avoid async/await for node 6aleclarsonbefore adoptione9f274e

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

test: recursive produce call in async recipealeclarsonbefore adoptione266f4b

lines 873–874why, one step back
873 const obj = d.obj
874 delete d.obj

test: avoid async/await for node 6aleclarsonbefore adoptione9f274e

lines 875–878why, one step back
875 return Promise.resolve().then(() => {
876 d.a = produce({}, d => {877 d.b = obj // Assign a draft owned by the parent scope.878 })

test: recursive produce call in async recipealeclarsonbefore adoptione266f4b

line 879why, one step back
879

test: avoid async/await for node 6aleclarsonbefore adoptione9f274e

lines 880–881why, one step back
880 // Auto-freezing is prevented when an unowned draft exists.
881 expect(Object.isFrozen(d.a)).toBeFalsy()

test: recursive produce call in async recipealeclarsonbefore adoptione266f4b

line 882why, one step back
882

test: avoid async/await for node 6aleclarsonbefore adoptione9f274e

lines 883–884why, one step back
883 // Ensure `obj` is not revoked.
884 obj.c = 1

test: recursive produce call in async recipealeclarsonbefore adoptione266f4b

line 885why, one step back
885 })

test: avoid async/await for node 6aleclarsonbefore adoptione9f274e

lines 886–890why, one step back
886 }).then(res => {
887 expect(res).not.toBe(base)888 expect(res).toEqual({889 a: {b: {k: 1, c: 1}}890 })

test: recursive produce call in async recipealeclarsonbefore adoptione266f4b

lines 891–892why, one step back
891 })
892 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 898–901why, one step back
898 produce(base, draft => {
899 draft.x = 4900 return {x: 5}901 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 902–904why, one step back
902 }).toThrow(/An immer producer returned a new value/)
903 })904

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 907–914why, one step back
907 produce(state, draft => {
908 switch (action.type) {909 case "SET_STARTING_DOTS":910 return draft.availableStartingDots.map(a => a)911 default:912 break913 }914 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 915–929why, one step back
915 const base = {
916 availableStartingDots: [917 {dots: 4, count: 1},918 {dots: 3, count: 2},919 {dots: 2, count: 3},920 {dots: 1, count: 4}921 ]922 }923 const next = reducer(base, {type: "SET_STARTING_DOTS"})924 expect(next).toEqual(base.availableStartingDots)925 expect(next).not.toBe(base.availableStartingDots)926 })927928 it("should fix #117 - 2", () => {929 const reducer = (state, action) =>

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 930–939why, one step back
930 produce(state, draft => {
931 switch (action.type) {932 case "SET_STARTING_DOTS":933 return {934 dots: draft.availableStartingDots.map(a => a)935 }936 default:937 break938 }939 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 940–951why, one step back
940 const base = {
941 availableStartingDots: [942 {dots: 4, count: 1},943 {dots: 3, count: 2},944 {dots: 2, count: 3},945 {dots: 1, count: 4}946 ]947 }948 const next = reducer(base, {type: "SET_STARTING_DOTS"})949 expect(next).toEqual({dots: base.availableStartingDots})950 })951

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 954–957why, one step back
954 const nextState = produce(baseState, d => {
955 const a = d.x956 d.x = a957 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 958–960why, one step back
958 expect(nextState).toBe(baseState)
959 })960

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 963–967why, one step back
963 const nextState = produce(baseState, d => {
964 const a = d.x965 d.x = 4966 d.x = a967 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 968–973why, one step back
968 // Ideally, this should actually be the same instances
969 // but this would be pretty expensive to detect,970 // so we don't atm971 expect(nextState).not.toBe(baseState)972 })973

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 976–981why, one step back
976 const nextState = produce(baseState, d => {
977 const a = d.x978 const stuff = a.y + 3979 d.x = 4980 d.x = a981 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 982–987why, one step back
982 // Ideally, this should actually be the same instances
983 // but this would be pretty expensive to detect,984 // so we don't atm985 expect(nextState).not.toBe(baseState)986 })987

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 990–992why, one step back
990 const nextState = produce(baseState, d => {
991 d.x = 3992 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 993–995why, one step back
993 expect(nextState).toBe(baseState)
994 })995

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 998–1001why, one step back
998 const nextState = produce(baseState, d => {
999 d.x = 41000 d.x = 31001 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 1002–1005why, one step back
1002 // Ideally, this should actually be the same instances
1003 // but this would be pretty expensive to detect,1004 // so we don't atm1005 expect(nextState).not.toBe(baseState)

Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637

lines 1006–1007why, one step back
1006 })
1007

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 1009–1022why, one step back
1009 const base = 3
1010 expect(produce(base, () => 4)).toBe(4)1011 expect(produce(base, () => null)).toBe(null)1012 expect(produce(base, () => undefined)).toBe(3)1013 expect(produce(base, () => {})).toBe(3)1014 expect(produce(base, () => nothing)).toBe(undefined)10151016 expect(produce({}, () => undefined)).toEqual({})1017 expect(produce({}, () => nothing)).toBe(undefined)1018 expect(produce(3, () => nothing)).toBe(undefined)10191020 expect(produce(() => undefined)({})).toEqual({})1021 expect(produce(() => nothing)({})).toBe(undefined)1022 expect(produce(() => nothing)(3)).toBe(undefined)

Tried to reproduce #195Michel Weststratebefore adoption4f27a93

lines 1023–1024why, one step back
1023 })
1024

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1025–1028why, one step back
1025 describe("base state type", () => {
1026 testObjectTypes(produce)1027 testLiteralTypes(produce)1028 })

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

line 1029why, one step back
1029

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 1030–1034why, one step back
1030 afterEach(() => {
1031 expect(baseState).toBe(origBaseState)1032 expect(baseState).toEqual(createBaseState())1033 })1034

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 1036–1037why, one step back
1036 function createBaseState() {
1037 const data = {

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 1039–1047why, one step back
1039 anArray: [3, 2, {c: 3}, 1],
1040 aProp: "hi",1041 anObject: {1042 nested: {1043 yummie: true1044 },1045 coffee: false1046 }1047 }

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 1049why, one step back
1049 }

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

lines 1050–1090why, one step back
1050 })
10511052 describe(`isDraft - ${name}`, () => {1053 it("returns true for object drafts", () => {1054 produce({}, state => {1055 expect(isDraft(state)).toBeTruthy()1056 })1057 })1058 it("returns true for array drafts", () => {1059 produce([], state => {1060 expect(isDraft(state)).toBeTruthy()1061 })1062 })1063 it("returns true for objects nested in object drafts", () => {1064 produce({a: {b: {}}}, state => {1065 expect(isDraft(state.a)).toBeTruthy()1066 expect(isDraft(state.a.b)).toBeTruthy()1067 })1068 })1069 it("returns false for new objects added to a draft", () => {1070 produce({}, state => {1071 state.a = {}1072 expect(isDraft(state.a)).toBeFalsy()1073 })1074 })1075 it("returns false for objects returned by the producer", () => {1076 const object = produce(null, Object.create)1077 expect(isDraft(object)).toBeFalsy()1078 })1079 it("returns false for arrays returned by the producer", () => {1080 const array = produce(null, _ => [])1081 expect(isDraft(array)).toBeFalsy()1082 })1083 it("returns false for object drafts returned by the producer", () => {1084 const object = produce({}, state => state)1085 expect(isDraft(object)).toBeFalsy()1086 })1087 it("returns false for array drafts returned by the producer", () => {1088 const array = produce([], state => state)1089 expect(isDraft(array)).toBeFalsy()1090 })

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 1091why, one step back
1091 })

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1092–1093why, one step back
1092}
1093

chore: improve more testsaleclarsonbefore adoption4f59b12

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 1095–1100why, one step back
1095 class Foo {
1096 constructor(foo) {1097 this.foo = foo1098 this[immerable] = true1099 }1100 }

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1101–1105why, one step back
1101 const values = {
1102 "empty object": {},1103 "plain object": {a: 1, b: 2},1104 "object (no prototype)": Object.create(null),1105 "empty array": [],

feat: limited class supportaleclarsonbefore adoption9877d64

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

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1108–1148why, one step back
1108 }
1109 for (const name in values) {1110 const value = values[name]1111 const copy = shallowCopy(value)1112 testObjectType(name, value)1113 testObjectType(name + " (frozen)", Object.freeze(copy))1114 }1115 function testObjectType(name, base) {1116 describe(name, () => {1117 it("creates a draft", () => {1118 produce(base, draft => {1119 expect(draft).not.toBe(base)1120 expect(shallowCopy(draft, true)).toEqual(base)1121 })1122 })11231124 it("preserves the prototype", () => {1125 const proto = Object.getPrototypeOf(base)1126 produce(base, draft => {1127 expect(Object.getPrototypeOf(draft)).toBe(proto)1128 })1129 })11301131 it("returns the base state when no changes are made", () => {1132 expect(produce(base, () => {})).toBe(base)1133 })11341135 it("returns a copy when changes are made", () => {1136 const random = Math.random()1137 const result = produce(base, draft => {1138 draft[0] = random1139 })1140 expect(result).not.toBe(base)1141 expect(result.constructor).toBe(base.constructor)1142 expect(result[0]).toBe(random)1143 })1144 })1145 }1146}11471148function testLiteralTypes(produce) {

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1151–1153why, one step back
1151 "falsy number": 0,
1152 "truthy number": 1,1153 "negative number": -1,

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1155–1163why, one step back
1155 infinity: 1 / 0,
1156 true: true,1157 false: false,1158 "empty string": "",1159 "truthy string": "1",1160 null: null,1161 undefined: undefined,11621163 /**

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 1166why, one step back
1166 */

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1168–1172why, one step back
1168 "regexp object": /.+/g,
1169 "boxed number": new Number(0),1170 "boxed string": new String(""),1171 "boxed boolean": new Boolean(),1172 "date object": new Date(),

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 1174why, one step back
1174 }

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1175–1178why, one step back
1175 for (const name in values) {
1176 describe(name, () => {1177 const value = values[name]1178

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1179–1183why, one step back
1179 it("does not create a draft", () => {
1180 produce(value, draft => {1181 expect(draft).toBe(value)1182 })1183 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

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

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1188why, one step back
1188

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1191–1199why, one step back
1191 expect(
1192 produce(value, draft => {1193 draft.foo = true1194 })1195 ).toBe(value)1196 })1197 }1198 })1199 }

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

lines 1200–1202why, one step back
1200}
12011202function enumerableOnly(x) {

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44

line 1207why, one step back
1207 }

feat: limited class supportaleclarsonbefore adoption9877d64

line 1208why, one step back
1208 })

test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44

line 1209why, one step back
1209 return copy

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 1210why, one step back
1210}