Skip to content
Myr

base.js

This file

Path

__tests__/base.js

1,151 lines · 40.5 KiB · JavaScript

Last changed by

test: improve "nested producer" testsaleclarsonbefore adoptionf9cd736

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–832why, 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 })817818 it("can return an object with two references to any pristine draft", () => {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")

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 833why, one step back
833 })

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

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

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

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 839–842why, one step back
839 produce(base, draft => {
840 draft.x = 4841 return {x: 5}842 })

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

lines 843–845why, one step back
843 }).toThrow(/An immer producer returned a new value/)
844 })845

chore: improve more testsaleclarsonbefore adoption4f59b12

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

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

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 848–855why, one step back
848 produce(state, draft => {
849 switch (action.type) {850 case "SET_STARTING_DOTS":851 return draft.availableStartingDots.map(a => a)852 default:853 break854 }855 })

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

lines 856–870why, one step back
856 const base = {
857 availableStartingDots: [858 {dots: 4, count: 1},859 {dots: 3, count: 2},860 {dots: 2, count: 3},861 {dots: 1, count: 4}862 ]863 }864 const next = reducer(base, {type: "SET_STARTING_DOTS"})865 expect(next).toEqual(base.availableStartingDots)866 expect(next).not.toBe(base.availableStartingDots)867 })868869 it("should fix #117 - 2", () => {870 const reducer = (state, action) =>

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 871–880why, one step back
871 produce(state, draft => {
872 switch (action.type) {873 case "SET_STARTING_DOTS":874 return {875 dots: draft.availableStartingDots.map(a => a)876 }877 default:878 break879 }880 })

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

lines 881–892why, one step back
881 const base = {
882 availableStartingDots: [883 {dots: 4, count: 1},884 {dots: 3, count: 2},885 {dots: 2, count: 3},886 {dots: 1, count: 4}887 ]888 }889 const next = reducer(base, {type: "SET_STARTING_DOTS"})890 expect(next).toEqual({dots: base.availableStartingDots})891 })892

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 895–898why, one step back
895 const nextState = produce(baseState, d => {
896 const a = d.x897 d.x = a898 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 899–901why, one step back
899 expect(nextState).toBe(baseState)
900 })901

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 904–908why, one step back
904 const nextState = produce(baseState, d => {
905 const a = d.x906 d.x = 4907 d.x = a908 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 909–914why, one step back
909 // Ideally, this should actually be the same instances
910 // but this would be pretty expensive to detect,911 // so we don't atm912 expect(nextState).not.toBe(baseState)913 })914

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 917–922why, one step back
917 const nextState = produce(baseState, d => {
918 const a = d.x919 const stuff = a.y + 3920 d.x = 4921 d.x = a922 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 923–928why, one step back
923 // Ideally, this should actually be the same instances
924 // but this would be pretty expensive to detect,925 // so we don't atm926 expect(nextState).not.toBe(baseState)927 })928

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 931–933why, one step back
931 const nextState = produce(baseState, d => {
932 d.x = 3933 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 934–936why, one step back
934 expect(nextState).toBe(baseState)
935 })936

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 939–942why, one step back
939 const nextState = produce(baseState, d => {
940 d.x = 4941 d.x = 3942 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 943–946why, one step back
943 // Ideally, this should actually be the same instances
944 // but this would be pretty expensive to detect,945 // so we don't atm946 expect(nextState).not.toBe(baseState)

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

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 950–963why, one step back
950 const base = 3
951 expect(produce(base, () => 4)).toBe(4)952 expect(produce(base, () => null)).toBe(null)953 expect(produce(base, () => undefined)).toBe(3)954 expect(produce(base, () => {})).toBe(3)955 expect(produce(base, () => nothing)).toBe(undefined)956957 expect(produce({}, () => undefined)).toEqual({})958 expect(produce({}, () => nothing)).toBe(undefined)959 expect(produce(3, () => nothing)).toBe(undefined)960961 expect(produce(() => undefined)({})).toEqual({})962 expect(produce(() => nothing)({})).toBe(undefined)963 expect(produce(() => nothing)(3)).toBe(undefined)

Tried to reproduce #195Michel Weststratebefore adoption4f27a93

lines 964–965why, one step back
964 })
965

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 966–969why, one step back
966 describe("base state type", () => {
967 testObjectTypes(produce)968 testLiteralTypes(produce)969 })

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

line 970why, one step back
970

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

lines 971–975why, one step back
971 afterEach(() => {
972 expect(baseState).toBe(origBaseState)973 expect(baseState).toEqual(createBaseState())974 })975

chore: improve more testsaleclarsonbefore adoption4f59b12

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

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

lines 977–978why, one step back
977 function createBaseState() {
978 const data = {

chore: improve more testsaleclarsonbefore adoption4f59b12

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

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

lines 980–988why, one step back
980 anArray: [3, 2, {c: 3}, 1],
981 aProp: "hi",982 anObject: {983 nested: {984 yummie: true985 },986 coffee: false987 }988 }

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

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

line 990why, one step back
990 }

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

lines 991–1031why, one step back
991 })
992993 describe(`isDraft - ${name}`, () => {994 it("returns true for object drafts", () => {995 produce({}, state => {996 expect(isDraft(state)).toBeTruthy()997 })998 })999 it("returns true for array drafts", () => {1000 produce([], state => {1001 expect(isDraft(state)).toBeTruthy()1002 })1003 })1004 it("returns true for objects nested in object drafts", () => {1005 produce({a: {b: {}}}, state => {1006 expect(isDraft(state.a)).toBeTruthy()1007 expect(isDraft(state.a.b)).toBeTruthy()1008 })1009 })1010 it("returns false for new objects added to a draft", () => {1011 produce({}, state => {1012 state.a = {}1013 expect(isDraft(state.a)).toBeFalsy()1014 })1015 })1016 it("returns false for objects returned by the producer", () => {1017 const object = produce(null, Object.create)1018 expect(isDraft(object)).toBeFalsy()1019 })1020 it("returns false for arrays returned by the producer", () => {1021 const array = produce(null, _ => [])1022 expect(isDraft(array)).toBeFalsy()1023 })1024 it("returns false for object drafts returned by the producer", () => {1025 const object = produce({}, state => state)1026 expect(isDraft(object)).toBeFalsy()1027 })1028 it("returns false for array drafts returned by the producer", () => {1029 const array = produce([], state => state)1030 expect(isDraft(array)).toBeFalsy()1031 })

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

line 1032why, one step back
1032 })

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1033–1034why, one step back
1033}
1034

chore: improve more testsaleclarsonbefore adoption4f59b12

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 1036–1041why, one step back
1036 class Foo {
1037 constructor(foo) {1038 this.foo = foo1039 this[immerable] = true1040 }1041 }

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1042–1046why, one step back
1042 const values = {
1043 "empty object": {},1044 "plain object": {a: 1, b: 2},1045 "object (no prototype)": Object.create(null),1046 "empty array": [],

feat: limited class supportaleclarsonbefore adoption9877d64

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

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1049–1089why, one step back
1049 }
1050 for (const name in values) {1051 const value = values[name]1052 const copy = shallowCopy(value)1053 testObjectType(name, value)1054 testObjectType(name + " (frozen)", Object.freeze(copy))1055 }1056 function testObjectType(name, base) {1057 describe(name, () => {1058 it("creates a draft", () => {1059 produce(base, draft => {1060 expect(draft).not.toBe(base)1061 expect(shallowCopy(draft, true)).toEqual(base)1062 })1063 })10641065 it("preserves the prototype", () => {1066 const proto = Object.getPrototypeOf(base)1067 produce(base, draft => {1068 expect(Object.getPrototypeOf(draft)).toBe(proto)1069 })1070 })10711072 it("returns the base state when no changes are made", () => {1073 expect(produce(base, () => {})).toBe(base)1074 })10751076 it("returns a copy when changes are made", () => {1077 const random = Math.random()1078 const result = produce(base, draft => {1079 draft[0] = random1080 })1081 expect(result).not.toBe(base)1082 expect(result.constructor).toBe(base.constructor)1083 expect(result[0]).toBe(random)1084 })1085 })1086 }1087}10881089function testLiteralTypes(produce) {

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1092–1094why, one step back
1092 "falsy number": 0,
1093 "truthy number": 1,1094 "negative number": -1,

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1096–1104why, one step back
1096 infinity: 1 / 0,
1097 true: true,1098 false: false,1099 "empty string": "",1100 "truthy string": "1",1101 null: null,1102 undefined: undefined,11031104 /**

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 1107why, one step back
1107 */

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1109–1113why, one step back
1109 "regexp object": /.+/g,
1110 "boxed number": new Number(0),1111 "boxed string": new String(""),1112 "boxed boolean": new Boolean(),1113 "date object": new Date(),

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

line 1115why, one step back
1115 }

chore: improve more testsaleclarsonbefore adoption4f59b12

lines 1116–1119why, one step back
1116 for (const name in values) {
1117 describe(name, () => {1118 const value = values[name]1119

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1120–1124why, one step back
1120 it("does not create a draft", () => {
1121 produce(value, draft => {1122 expect(draft).toBe(value)1123 })1124 })

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

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

chore: improve more testsaleclarsonbefore adoption4f59b12

line 1129why, one step back
1129

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

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

chore: improve more testsaleclarsonbefore adoption4f59b12

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

test: extract some tests into their own functionaleclarsonbefore adoption21846ab

lines 1132–1140why, one step back
1132 expect(
1133 produce(value, draft => {1134 draft.foo = true1135 })1136 ).toBe(value)1137 })1138 }1139 })1140 }

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

lines 1141–1143why, one step back
1141}
11421143function enumerableOnly(x) {

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44

line 1148why, one step back
1148 }

feat: limited class supportaleclarsonbefore adoption9877d64

line 1149why, one step back
1149 })

test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44

line 1150why, one step back
1150 return copy

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

line 1151why, one step back
1151}