Skip to content
Myr

base.js

This file

Path

__tests__/base.js

1,335 lines · 47.3 KiB · JavaScript

Last changed by

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

History of this file

Take it

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

v1.0.2Michel Weststratebefore adoption551b110

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44

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

v1.0.2Michel Weststratebefore adoption551b110

lines 4–11why, one step back
4import deepFreeze from "deep-freeze"
5import cloneDeep from "lodash.clonedeep"6import * as lodash from "lodash"78jest.setTimeout(1000)910runBaseTest("proxy (no freeze)", true, false)11runBaseTest("proxy (autofreeze)", true, true)

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

v1.0.2Michel Weststratebefore adoption551b110

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

v1.0.2Michel Weststratebefore adoption551b110

line 16why, one step back
16

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 17–29why, one step back
17function runBaseTest(name, useProxies, autoFreeze, useListener) {
18 const immer = new Immer({19 useProxies,20 autoFreeze21 })2223 // make sure logic doesn't break when listener is attached24 const listener = useListener ? function() {} : undefined25 const produce = (...args) =>26 typeof args[1] === "function" && args.length < 327 ? immer.produce(...args, listener)28 : immer.produce(...args)29

v1.0.2Michel Weststratebefore adoption551b110

lines 30–38why, one step back
30 describe(`base functionality - ${name}`, () => {
31 let baseState32 let origBaseState3334 beforeEach(() => {35 origBaseState = baseState = createBaseState()36 })3738 it("should return the original without modifications", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 39why, one step back
39 const nextState = produce(baseState, () => {})

v1.0.2Michel Weststratebefore adoption551b110

lines 40–43why, one step back
40 expect(nextState).toBe(baseState)
41 })4243 it("should return the original without modifications when reading stuff", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 44–47why, one step back
44 const nextState = produce(baseState, s => {
45 expect(s.aProp).toBe("hi")46 expect(s.anObject.nested).toMatchObject({yummie: true})47 })

v1.0.2Michel Weststratebefore adoption551b110

lines 48–51why, one step back
48 expect(nextState).toBe(baseState)
49 })5051 it("should return a copy when modifying stuff", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 52–54why, one step back
52 const nextState = produce(baseState, s => {
53 s.aProp = "hello world"54 })

v1.0.2Michel Weststratebefore adoption551b110

lines 55–61why, one step back
55 expect(nextState).not.toBe(baseState)
56 expect(baseState.aProp).toBe("hi")57 expect(nextState.aProp).toBe("hello world")58 // structural sharing?59 expect(nextState.nested).toBe(baseState.nested)60 })61

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

line 62why, one step back
62 it("should preserve type", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 63–70why, one step back
63 const nextState = produce(baseState, s => {
64 expect(Object.getPrototypeOf(s)).toBe(Object.prototype)65 expect(Array.isArray(s.anArray)).toBe(true)66 s.anArray.push(3)67 s.aProp = "hello world"68 expect(Object.getPrototypeOf(s)).toBe(Object.prototype)69 expect(Array.isArray(s.anArray)).toBe(true)70 })

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

lines 71–75why, one step back
71 expect(Object.getPrototypeOf(nextState)).toBe(Object.prototype)
72 expect(Array.isArray(nextState.anArray)).toBe(true)73 })7475 it("deep change bubbles up", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 76–78why, one step back
76 const nextState = produce(baseState, s => {
77 s.anObject.nested.yummie = false78 })

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

lines 79–84why, one step back
79 expect(nextState).not.toBe(baseState)
80 expect(nextState.anObject).not.toBe(baseState.anObject)81 expect(baseState.anObject.nested.yummie).toBe(true)82 expect(nextState.anObject.nested.yummie).toBe(false)83 expect(nextState.anArray).toBe(baseState.anArray)84 })

v1.0.2Michel Weststratebefore adoption551b110

lines 85–86why, one step back
85
86 it("can add props", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

v1.0.2Michel Weststratebefore adoption551b110

lines 90–96why, one step back
90 expect(nextState).not.toBe(baseState)
91 expect(nextState.anObject).not.toBe(baseState.anObject)92 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)93 expect(nextState.anObject.cookie).toEqual({tasty: true})94 })9596 it("can delete props", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 97–99why, one step back
97 const nextState = produce(baseState, s => {
98 delete s.anObject.nested99 })

v1.0.2Michel Weststratebefore adoption551b110

lines 100–104why, one step back
100 expect(nextState).not.toBe(baseState)
101 expect(nextState.anObject).not.toBe(baseState.anObject)102 expect(nextState.anObject.nested).toBe(undefined)103 })104

test: deleting props added in the produceraleclarsonbefore adoption691e73b

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 107–110why, one step back
107 const nextState = produce(baseState, s => {
108 s.anObject.test = true109 delete s.anObject.test110 })

test: deleting props added in the produceraleclarsonbefore adoption691e73b

lines 111–119why, one step back
111 if (useProxies) {
112 expect(nextState).not.toBe(baseState)113 expect(nextState).toEqual(baseState)114 } else {115 // The copy is avoided in ES5.116 expect(nextState).toBe(baseState)117 }118 })119

v1.0.2Michel Weststratebefore adoption551b110

line 120why, one step back
120 it("ignores single non-modification", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 121–123why, one step back
121 const nextState = produce(baseState, s => {
122 s.aProp = "hi"123 })

v1.0.2Michel Weststratebefore adoption551b110

lines 124–127why, one step back
124 expect(nextState).toBe(baseState)
125 })126127 it("processes single modification", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 128–131why, one step back
128 const nextState = produce(baseState, s => {
129 s.aProp = "hello"130 s.aProp = "hi"131 })

v1.0.2Michel Weststratebefore adoption551b110

lines 132–159why, one step back
132 expect(nextState).not.toBe(baseState)
133 expect(nextState).toEqual(baseState)134 })135136 it("processes with for loop", () => {137 const base = [{id: 1, a: 1}, {id: 2, a: 1}]138 const findById = (collection, id) => {139 for (const item of collection) {140 if (item.id === id) return item141 }142 return null143 }144 const result = produce(base, draft => {145 const obj1 = findById(draft, 1)146 const obj2 = findById(draft, 2)147 obj1.a = 2148 obj2.a = 2149 })150 expect(result[0].a).toEqual(2)151 expect(result[1].a).toEqual(2)152 })153154 it("works with objects without proto", () => {155 const base = Object.create(null)156 base.x = 1157 base.y = Object.create(null)158 base.y.y = 2159 expect(base.__proto__).toBe(undefined)

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 160–164why, one step back
160 const next = produce(base, draft => {
161 draft.y.z = 3162 draft.y.y++163 draft.x++164 })

v1.0.2Michel Weststratebefore adoption551b110

lines 165–172why, one step back
165 expect(next).toEqual({
166 x: 2,167 y: {y: 3, z: 3}168 })169 expect(next.__proto__).toBe(undefined)170 })171172 it("should support reading arrays", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 173–175why, one step back
173 const nextState = produce(baseState, s => {
174 s.anArray.slice()175 })

v1.0.2Michel Weststratebefore adoption551b110

lines 176–180why, one step back
176 expect(nextState.anArray).toBe(baseState.anArray)
177 expect(nextState).toBe(baseState)178 })179180 it("should support changing arrays", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 181–183why, one step back
181 const nextState = produce(baseState, s => {
182 s.anArray[3] = true183 })

v1.0.2Michel Weststratebefore adoption551b110

lines 184–189why, one step back
184 expect(nextState).not.toBe(baseState)
185 expect(nextState.anArray).not.toBe(baseState.anArray)186 expect(nextState.anArray[3]).toEqual(true)187 })188189 it("should support changing arrays - 2", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 190–192why, one step back
190 const nextState = produce(baseState, s => {
191 s.anArray.splice(1, 1, "a", "b")192 })

v1.0.2Michel Weststratebefore adoption551b110

lines 193–205why, one step back
193 expect(nextState).not.toBe(baseState)
194 expect(nextState.anArray).not.toBe(baseState.anArray)195196 expect(enumerableOnly(nextState.anArray)).toEqual([197 3,198 "a",199 "b",200 {c: 3},201 1202 ])203 })204205 it("can delete array items", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 206–208why, one step back
206 const nextState = produce(baseState, s => {
207 s.anArray.length = 3208 })

v1.0.2Michel Weststratebefore adoption551b110

lines 209–215why, one step back
209 expect(nextState).not.toBe(baseState)
210 expect(nextState.anObject).toBe(baseState.anObject)211 expect(nextState.anArray).not.toBe(baseState.anArray)212 expect(nextState.anArray).toEqual([3, 2, {c: 3}])213 })214215 it("should support sorting arrays", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 216–220why, one step back
216 const nextState = produce(baseState, s => {
217 s.anArray[2].c = 4218 s.anArray.sort()219 s.anArray[3].c = 5220 })

v1.0.2Michel Weststratebefore adoption551b110

lines 221–226why, one step back
221 expect(nextState).not.toBe(baseState)
222 expect(nextState.anArray).not.toBe(baseState.anArray)223 expect(enumerableOnly(nextState.anArray)).toEqual([1, 2, 3, {c: 5}])224 })225226 it("should expose property descriptors", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 227–238why, one step back
227 const nextState = produce([], s => {
228 expect(Object.getOwnPropertyDescriptor(s, 0)).toBe(undefined)229 s.unshift("x")230 expect(Object.getOwnPropertyDescriptor(s, 0)).toEqual({231 configurable: true,232 enumerable: true,233 value: "x",234 writable: true235 })236 expect(s.length).toBe(1)237 expect(s[0] === "x").toBe(true)238 })

v1.0.2Michel Weststratebefore adoption551b110

lines 239–240why, one step back
239 expect(nextState).toEqual(["x"])
240 expect(Object.getOwnPropertyDescriptor(nextState, 0)).toEqual({

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 241why, one step back
241 configurable: !autoFreeze,

v1.0.2Michel Weststratebefore adoption551b110

lines 242–243why, one step back
242 enumerable: true,
243 value: "x",

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

line 244why, one step back
244 writable: !autoFreeze

v1.0.2Michel Weststratebefore adoption551b110

lines 245–248why, one step back
245 })
246 })247248 it("should support sorting arrays - 2", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 249–255why, one step back
249 const nextState = produce(baseState, s => {
250 s.anArray.unshift("x")251 s.anArray[3].c = 4252 s.anArray.sort()253 s.anArray[3].c = 5254 s.anArray.unshift("y")255 })

v1.0.2Michel Weststratebefore adoption551b110

lines 256–268why, one step back
256 expect(nextState).not.toBe(baseState)
257 expect(nextState.anArray).not.toBe(baseState.anArray)258 expect(enumerableOnly(nextState.anArray)).toEqual([259 "y",260 1,261 2,262 3,263 {c: 5},264 "x"265 ])266 })267268 it("should updating inside arrays", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 269–271why, one step back
269 const nextState = produce(baseState, s => {
270 s.anArray[2].test = true271 })

v1.0.2Michel Weststratebefore adoption551b110

lines 272–276why, one step back
272 expect(nextState).not.toBe(baseState)
273 expect(nextState.anArray).not.toBe(baseState.anArray)274 expect(nextState.anArray).toEqual([3, 2, {c: 3, test: true}, 1])275 })276

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 278–282why, one step back
278 const nextState = produce(baseState, s => {
279 const obj = s.anObject280 delete s.anObject281 s.renamed = obj282 })

v1.0.2Michel Weststratebefore adoption551b110

lines 283–284why, one step back
283 expect(nextState).not.toBe(baseState)
284 expect(nextState.anArray).toBe(baseState.anArray)

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 285why, one step back
285 expect(nextState.renamed.nested).toBe(baseState.anObject.nested)

v1.0.2Michel Weststratebefore adoption551b110

lines 286–288why, one step back
286 expect(enumerableOnly(nextState)).toEqual({
287 anArray: [3, 2, {c: 3}, 1],288 aProp: "hi",

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 289why, one step back
289 renamed: {

v1.0.2Michel Weststratebefore adoption551b110

lines 290–297why, one step back
290 nested: {
291 yummie: true292 },293 coffee: false294 }295 })296 })297

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 301–303why, one step back
301 const nextState = produce(baseState, s => {
302 const obj = s.anObject303 delete s.anObject

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 304why, one step back
304

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 305–307why, one step back
305 obj.coffee = true // change
306 obj.nested.yummy = true // add307 delete obj.nested.yummie // delete

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 308why, one step back
308

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 309–310why, one step back
309 s.renamed = obj
310 })

v1.0.2Michel Weststratebefore adoption551b110

lines 311–312why, one step back
311 expect(nextState).not.toBe(baseState)
312 expect(nextState.anArray).toBe(baseState.anArray)

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 313why, one step back
313 expect(nextState.renamed.nested).not.toBe(baseState.anObject.nested)

v1.0.2Michel Weststratebefore adoption551b110

lines 314–315why, one step back
314 expect(enumerableOnly(nextState)).toEqual({
315 anArray: [3, 2, {c: 3}, 1],

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

lines 316–317why, one step back
316 aProp: "hi",
317 renamed: {

v1.0.2Michel Weststratebefore adoption551b110

line 318why, one step back
318 nested: {

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 319why, one step back
319 yummy: true

v1.0.2Michel Weststratebefore adoption551b110

lines 320–325why, one step back
320 },
321 coffee: true322 }323 })324 })325

test: draft nested in new objectaleclarsonbefore adoptionf46659b

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 327–330why, one step back
327 const nextState = produce(baseState, s => {
328 s.foo = {bar: s.anObject}329 delete s.anObject330 })

test: draft nested in new objectaleclarsonbefore adoptionf46659b

lines 331–335why, one step back
331 expect(nextState).not.toBe(baseState)
332 expect(nextState.foo.bar).toBe(baseState.anObject)333 })334335 it("can nest a draft in a new object (with changes)", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 336–338why, one step back
336 const nextState = produce(baseState, s => {
337 const obj = s.anObject338 delete s.anObject

test: draft nested in new objectaleclarsonbefore adoptionf46659b

line 339why, one step back
339

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 340–342why, one step back
340 obj.coffee = true // change
341 obj.nested.yummy = true // add342 delete obj.nested.yummie // delete

test: draft nested in new objectaleclarsonbefore adoptionf46659b

line 343why, one step back
343

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 344–345why, one step back
344 s.foo = {bar: obj}
345 })

test: draft nested in new objectaleclarsonbefore adoptionf46659b

lines 346–355why, one step back
346 expect(nextState).not.toBe(baseState)
347 expect(nextState.foo.bar).not.toBe(baseState.anObject)348 expect(nextState.foo).toEqual({349 bar: {350 coffee: true,351 nested: {yummy: true}352 }353 })354 })355

v1.0.2Michel Weststratebefore adoption551b110

line 356why, one step back
356 it("should allow setting to undefined a defined draft property", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 357–359why, one step back
357 const nextState = produce(baseState, s => {
358 s.aProp = undefined359 })

v1.0.2Michel Weststratebefore adoption551b110

lines 360–368why, one step back
360 expect(nextState).not.toBe(baseState)
361 expect(baseState.aProp).toBe("hi")362 expect(nextState.aProp).toBe(undefined)363 })364365 // ES implementation does't protect against all outside modifications, just some..366 if (useProxies) {367 it("should revoke the proxy of the baseState after immer function is executed", () => {368 let proxy

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 369–372why, one step back
369 const nextState = produce(baseState, s => {
370 proxy = s371 s.aProp = "hello"372 })

v1.0.2Michel Weststratebefore adoption551b110

lines 373–391why, one step back
373 expect(nextState).not.toBe(baseState)
374 expect(baseState.aProp).toBe("hi")375 expect(nextState.aProp).toBe("hello")376377 expect(() => {378 proxy.aProp = "Hallo"379 }).toThrowError(/revoked/)380 expect(() => {381 const aProp = proxy.aProp382 }).toThrowError(/revoked/)383384 expect(nextState).not.toBe(baseState)385 expect(baseState.aProp).toBe("hi")386 expect(nextState.aProp).toBe("hello")387 })388 }389390 it("should revoke the proxy of the baseState after immer function is executed - 2", () => {391 let proxy

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 392–394why, one step back
392 const nextState = produce(baseState, s => {
393 proxy = s.anObject394 })

v1.0.2Michel Weststratebefore adoption551b110

lines 395–405why, one step back
395 expect(nextState).toBe(baseState)
396 expect(() => {397 // In ES5 implemenation only protects existing props, but alas..398 proxy.coffee = "Hallo"399 }).toThrowError(/revoked/)400 expect(() => {401 const test = proxy.coffee402 }).toThrowError(/revoked/)403 })404405 it("should reflect all changes made in the draft immediately", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 406–419why, one step back
406 produce(baseState, draft => {
407 draft.anArray[0] = 5408 draft.anArray.unshift("test")409 // sliced here; jest will also compare non-enumerable keys, which would include the immer Symbols410 expect(draft.anArray.slice()).toMatchObject([411 "test",412 5,413 2,414 {c: 3},415 1416 ])417 draft.stuffz = "coffee"418 expect(draft.stuffz).toBe("coffee")419 })

v1.0.2Michel Weststratebefore adoption551b110

lines 420–422why, one step back
420 })
421422 it("should be able to get property descriptors from objects", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 423–428why, one step back
423 produce({a: 1}, draft => {
424 expect("a" in draft).toBe(true)425 expect("b" in draft).toBe(false)426 expect(427 Reflect.ownKeys(draft).filter(x => typeof x === "string")428 ).toEqual(["a"])

v1.0.2Michel Weststratebefore adoption551b110

line 429why, one step back
429

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 430–458why, one step back
430 expect(
431 Object.getOwnPropertyDescriptor(draft, "a")432 ).toMatchObject({433 configurable: true,434 enumerable: true435 })436 draft.a = 2437 expect(438 Object.getOwnPropertyDescriptor(draft, "a")439 ).toMatchObject({440 configurable: true,441 enumerable: true442 })443 expect(444 Object.getOwnPropertyDescriptor(draft, "b")445 ).toBeUndefined()446 draft.b = 2447 expect(448 Object.getOwnPropertyDescriptor(draft, "b")449 ).toMatchObject({450 configurable: true,451 enumerable: true452 })453 expect("a" in draft).toBe(true)454 expect("b" in draft).toBe(true)455 expect(456 Reflect.ownKeys(draft).filter(x => typeof x === "string")457 ).toEqual(["a", "b"])458 })

v1.0.2Michel Weststratebefore adoption551b110

lines 459–461why, one step back
459 })
460461 it("should be able to get property descriptors from arrays", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 462–470why, one step back
462 produce([1], draft => {
463 expect(0 in draft).toBe(true)464 expect(1 in draft).toBe(false)465 expect("0" in draft).toBe(true)466 expect("1" in draft).toBe(false)467 expect(length in draft).toBe(true)468 expect(469 Reflect.ownKeys(draft).filter(x => typeof x === "string")470 ).toEqual(["0", "length"])

v1.0.2Michel Weststratebefore adoption551b110

line 471why, one step back
471

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 472–480why, one step back
472 expect(
473 Object.getOwnPropertyDescriptor(draft, "length")474 ).toMatchObject({475 configurable: false,476 enumerable: false477 })478 draft[0] = 2479 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(480 {

v1.0.2Michel Weststratebefore adoption551b110

lines 481–482why, one step back
481 configurable: true,
482 enumerable: true

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 483–486why, one step back
483 }
484 )485 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(486 {

v1.0.2Michel Weststratebefore adoption551b110

lines 487–488why, one step back
487 configurable: true,
488 enumerable: true

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 489–496why, one step back
489 }
490 )491 expect(492 Object.getOwnPropertyDescriptor(draft, 1)493 ).toBeUndefined()494 draft[1] = 2495 expect(Object.getOwnPropertyDescriptor(draft, 1)).toMatchObject(496 {

v1.0.2Michel Weststratebefore adoption551b110

lines 497–498why, one step back
497 configurable: true,
498 enumerable: true

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 499–504why, one step back
499 }
500 )501 expect(502 Reflect.ownKeys(draft).filter(x => typeof x === "string")503 ).toEqual(["0", "1", "length"])504 })

v1.0.2Michel Weststratebefore adoption551b110

lines 505–509why, one step back
505 })
506507 if (useProxies === true) {508 it("should not be possible to set property descriptors", () => {509 expect(() => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 510–516why, one step back
510 produce({}, draft => {
511 Object.defineProperty(draft, "xx", {512 enumerable: true,513 writeable: true,514 value: 2515 })516 })

v1.0.2Michel Weststratebefore adoption551b110

lines 517–518why, one step back
517 }).toThrowError(/not support/)
518 })

Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c

lines 519–540why, one step back
519
520 it("should not be possible to add properties to arrays", () => {521 expect(() => {522 produce([], d => {523 d.x = 3524 })525 }).toThrow(526 "Immer does not support setting non-numeric properties on arrays"527 )528 })529530 it("should not be possible to remove properties from arrays", () => {531 expect(() => {532 const base = []533 base.x = 7534 produce(base, d => {535 delete d.x536 })537 }).toThrow(538 "Immer does not support deleting properties from arrays"539 )540 })

v1.0.2Michel Weststratebefore adoption551b110

line 541why, one step back
541 }

Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c

lines 542–550why, one step back
542
543 it("non-numeric array properties will be lost", () => {544 const base = []545 base.x = 7546 const next = produce(base, d => {547 d.push(3)548 })549 expect(next.x).toBe(undefined)550 })

v1.0.2Michel Weststratebefore adoption551b110

lines 551–553why, one step back
551
552 it("should not throw error, see #53 - 1", () => {553 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 554–556why, one step back
554 const result = produce(base, draft => {
555 draft.arr = draft.arr.filter(item => item.count > 2)556 })

v1.0.2Michel Weststratebefore adoption551b110

lines 557–565why, one step back
557 expect(result.arr[0].count).toEqual(3)
558 expect(result).toEqual({559 arr: [{count: 3}]560 })561 expect(result.arr[0]).toBe(base.arr[2])562 })563564 it("should not throw error, see #53 - 2", () => {565 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 566–568why, one step back
566 const result = produce(base, draft => {
567 draft.newArr = draft.arr.filter(item => item.count > 2)568 })

v1.0.2Michel Weststratebefore adoption551b110

lines 569–594why, one step back
569 expect(result.newArr[0].count).toEqual(3)
570 expect(result.arr).toBe(base.arr)571 expect(result).toEqual({572 arr: [573 {574 count: 1575 },576 {577 count: 2578 },579 {580 count: 3581 }582 ],583 newArr: [584 {585 count: 3586 }587 ]588 })589 expect(result.newArr[0]).toBe(base.arr[2])590 expect(result.arr[2]).toBe(base.arr[2])591 })592593 it("should not throw error, see #53 - 3", () => {594 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 595–598why, one step back
595 const result = produce(base, draft => {
596 draft.newArr = draft.arr.filter(item => item.count > 2)597 delete draft.arr598 })

v1.0.2Michel Weststratebefore adoption551b110

lines 599–607why, one step back
599 expect(result.newArr[0].count).toEqual(3)
600 expect(result).toEqual({601 newArr: [{count: 3}]602 })603 expect(result.newArr[0]).toBe(base.arr[2])604 })605606 it("should not throw error, see #53 - 4", () => {607 const base = {bear: {age: 10}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 608–611why, one step back
608 const result = produce(base, draft => {
609 draft.bear.legs = 4610 draft.room = {elephant: {kiddo: draft.bear}}611 })

v1.0.2Michel Weststratebefore adoption551b110

lines 612–730why, one step back
612 expect(result).toEqual({
613 bear: {age: 10, legs: 4},614 room: {elephant: {kiddo: {age: 10, legs: 4}}}615 })616617 const result2 = produce(result, draft => {618 draft.bear.age = 11619 draft.room.elephant.kiddo.legs = 5620 })621 expect(result2).toEqual({622 bear: {age: 11, legs: 4},623 room: {elephant: {kiddo: {age: 10, legs: 5}}}624 })625 })626627 it("should not throw error #78", () => {628 const base = {629 arr: [630 {631 id: "100",632 arr: [{id: "1", no: 1}, {id: "2", no: 2}]633 },634 {id: "3", no: 3}635 ]636 }637 const result = produce(base, draft => {638 const base = draft.arr[0]639 draft.arr = draft.arr.slice(0, 1)640 const newArr = {641 id: "101",642 arr: [base, {id: "3"}]643 }644 draft.arr.splice(0, 1, newArr)645 draft.arr[0].arr[1].no = 3646 })647 expect(result).toEqual({648 arr: [649 {650 id: "101",651 arr: [652 {653 id: "100",654 arr: [{id: "1", no: 1}, {id: "2", no: 2}]655 },656 {id: "3", no: 3}657 ]658 }659 ]660 })661 })662663 it("should not throw error #78 - 2", () => {664 const base = {665 arr: [{count: 1}, {count: 2}]666 }667 const result = produce(base, draft => {668 draft.arr = [draft.arr[1]]669 draft.arr[0].count = 1670 })671 expect(result).toEqual({672 arr: [{count: 1}]673 })674 })675676 it("should handle constructor correctly", () => {677 const base = {678 arr: new Array(),679 obj: new Object()680 }681 const result = produce(base, draft => {682 draft.arrConstructed = draft.arr.constructor(1)683 draft.objConstructed = draft.obj.constructor(1)684 })685 expect(result.arrConstructed).toEqual(new Array().constructor(1))686 expect(result.objConstructed).toEqual(new Object().constructor(1))687 })688689 it("should handle dates correctly", () => {690 const data = {date: new Date()}691 const next = produce(data, draft => {692 draft.x = true693 })694 expect(next).toEqual({x: true, date: data.date})695 expect(next.date).toBe(data.date)696 const next2 = produce(next, draft => {697 draft.date.setYear(2015)698 })699 // This still holds; because produce won't proxy Date objects700 // and the original is actually modified!701 expect(next2).toEqual({x: true, date: data.date})702 expect(next2.date).toBe(next.date)703 expect(next2.date).toBe(data.date)704 expect(next2).toBe(next)705 })706707 it("should handle equality correctly - 1", () => {708 const base = {709 y: 3 / 0,710 z: NaN711 }712 const next = produce(base, draft => {713 draft.y = 4 / 0714 draft.z = NaN715 })716 expect(next).toEqual(base)717 expect(next).toBe(base)718 })719720 it("should handle equality correctly - 2", () => {721 const base = {722 x: -0723 }724 const next = produce(base, draft => {725 draft.x = +1726 })727 expect(next).not.toEqual(base)728 expect(next).not.toBe(base)729 })730

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

lines 731–733why, one step back
731 // AKA: recursive produce calls
732 describe("a nested producer", () => {733 describe("when base state is not a draft", () => {

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 734–737why, one step back
734 // This test ensures the global state used to manage proxies is
735 // never left in a corrupted state by a nested `produce` call.736 it("never affects its parent producer implicitly", () => {737 const bear = {paw: {honey: true}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 738–744why, one step back
738 const next = produce(bear, draft => {
739 const paw2 = produce(bear.paw, draft => {740 draft.honey = false741 })742 expect(paw2.honey).toBe(false)743 expect(draft.paw.honey).toBe(true) // effects should not be visible outside744 })

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 745–747why, one step back
745 expect(next.paw.honey).toBe(true)
746 expect(next).toBe(bear)747 })

v1.0.2Michel Weststratebefore adoption551b110

line 748why, one step back
748

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 749–750why, one step back
749 it("returns a normal object", () => {
750 const bear = {paw: {honey: true}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 751–759why, one step back
751 const next = produce(bear, draft => {
752 const paw2 = produce(bear.paw, draft => {753 draft.honey = false754 })755 expect(paw2.honey).toBe(false)756 expect(draft.paw.honey).toBe(true)757 draft.paw = paw2758 expect(draft.paw.honey).toBe(false)759 })

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 760–764why, one step back
760 expect(next.paw.honey).toBe(false)
761 expect(next).not.toBe(bear)762 })763 })764

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

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

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 766–767why, one step back
766 it("always reuses the draft", () => {
767 const bear = {paw: {honey: true}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 768–776why, one step back
768 const next = produce(bear, bear => {
769 const paw2 = produce(bear.paw, paw => {770 expect(paw).toBe(bear.paw)771 paw.honey = false772 })773 expect(paw2).toBe(bear.paw)774 expect(paw2.honey).toBe(false)775 expect(bear.paw.honey).toBe(false)776 })

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 777–778why, one step back
777 expect(next.paw.honey).toBe(false)
778 expect(next).not.toBe(bear)

test: base state that contains a draftaleclarsonbefore adoptiond9b91d5

lines 779–818why, one step back
779 })
780 })781782 describe("when base state contains a draft", () => {783 it("wraps unowned draft with its own draft", () => {784 produce({a: {}}, parent => {785 produce({a: parent.a}, child => {786 expect(child.a).not.toBe(parent.a)787 expect(isDraft(child.a)).toBeTruthy()788 })789 })790 })791792 it("returns unowned draft if no changes were made", () => {793 produce({a: {}}, parent => {794 const result = produce({a: parent.a}, () => {})795 expect(result.a).toBe(parent.a)796 })797 })798799 it("clones the unowned draft when changes are made", () => {800 produce({a: {}}, parent => {801 const result = produce({a: parent.a}, child => {802 child.a.b = 1803 })804 expect(result.a).not.toBe(parent.a)805 expect(result.a.b).toBe(1)806 expect("b" in parent.a).toBeFalsy()807 })808 })809810 // We cannot auto-freeze the result of a nested producer,811 // because it may contain a draft from a parent producer.812 it("never auto-freezes the result", () => {813 produce({a: {}}, parent => {814 const r = produce({a: parent.a}, child => {815 child.b = 1 // Ensure a copy is returned.816 })817 expect(Object.isFrozen(r)).toBeFalsy()818 })

Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371

lines 819–820why, one step back
819 })
820 })

v1.0.2Michel Weststratebefore adoption551b110

lines 821–826why, one step back
821 })
822823 it("should not try to change immutable data, see #66", () => {824 const user = require("./test-data")825826 const base = {}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 827–829why, one step back
827 const next = produce(base, draft => {
828 draft.user = user829 })

v1.0.2Michel Weststratebefore adoption551b110

lines 830–838why, one step back
830 expect(next.user).toBe(user)
831 expect(next).not.toBe(base)832 expect(next.user).toEqual(user)833 })834835 it("should not try to change immutable data, see #66 - 2", () => {836 const user = deepFreeze(cloneDeep(require("./test-data")))837838 const base = {}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 839–841why, one step back
839 const next = produce(base, draft => {
840 draft.user = user841 })

v1.0.2Michel Weststratebefore adoption551b110

lines 842–848why, one step back
842 expect(next.user).toBe(user)
843 expect(next).not.toBe(base)844 expect(next.user).toEqual(user)845 })846847 it("should structurally share identical objects in the tree", () => {848 const base = {bear: {legs: 4}, eagle: {legs: 3}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 849–856why, one step back
849 const next = produce(base, draft => {
850 const animal = draft.bear851 animal.legs = animal.legs + 1852 draft.bear = animal853 draft.eagle = animal854 draft.cow = animal855 draft.kiddo = animal856 })

v1.0.2Michel Weststratebefore adoption551b110

lines 857–868why, one step back
857 expect(next).toEqual({
858 bear: {legs: 5},859 eagle: {legs: 5},860 cow: {legs: 5},861 kiddo: {legs: 5}862 })863 expect(next.bear).toBe(next.cow)864 expect(next.kiddo).toBe(next.cow)865 })866867 if (useProxies)868 it("should not allow changing prototype", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 869–873why, one step back
869 produce({}, draft => {
870 expect(() => Object.setPrototypeOf(draft, Array)).toThrow(871 /does not support `setPrototype/872 )873 })

v1.0.2Michel Weststratebefore adoption551b110

lines 874–876why, one step back
874 })
875876 it("'in' should work", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 877–879why, one step back
877 produce(createBaseState(), draft => {
878 expect("anArray" in draft).toBe(true)879 expect(Reflect.has(draft, "anArray")).toBe(true)

v1.0.2Michel Weststratebefore adoption551b110

line 880why, one step back
880

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

v1.0.2Michel Weststratebefore adoption551b110

line 883why, one step back
883

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 884–887why, one step back
884 expect(0 in draft.anArray).toBe(true)
885 expect("0" in draft.anArray).toBe(true)886 expect(Reflect.has(draft.anArray, 0)).toBe(true)887 expect(Reflect.has(draft.anArray, "0")).toBe(true)

v1.0.2Michel Weststratebefore adoption551b110

line 888why, one step back
888

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 889–893why, one step back
889 expect(17 in draft.anArray).toBe(false)
890 expect("17" in draft.anArray).toBe(false)891 expect(Reflect.has(draft.anArray, 17)).toBe(false)892 expect(Reflect.has(draft.anArray, "17")).toBe(false)893 })

v1.0.2Michel Weststratebefore adoption551b110

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

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 898–900why, one step back
898 const next1 = produce(base, function() {
899 this.x = 4900 })

v1.0.2Michel Weststratebefore adoption551b110

lines 901–923why, one step back
901 expect(next1).not.toBe(base)
902 expect(next1.x).toBe(4)903 })904905 it("'this' should work - 1", () => {906 const base = {x: 3}907 const incrementor = produce(function() {908 this.x = 4909 })910 const next1 = incrementor(base)911 expect(next1).not.toBe(base)912 expect(next1.x).toBe(4)913 })914915 it("issue #89", () => {916 const state = {917 users: {918 existingID: {919 loading: false,920 value: "val"921 }922 }923 }

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 924–929why, one step back
924 const nextState = produce(state, draft => {
925 draft.users.existingID = {926 ...draft.users.existingID,927 loading: true928 }929 })

v1.0.2Michel Weststratebefore adoption551b110

lines 930–941why, one step back
930 expect(nextState).toEqual({
931 users: {932 existingID: {933 loading: true,934 value: "val"935 }936 }937 })938 })939940 it("processes with lodash.set", () => {941 const base = [{id: 1, a: 1}]

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

v1.0.2Michel Weststratebefore adoption551b110

lines 945–950why, one step back
945 expect(base[0].a).toEqual(1)
946 expect(result[0].a).toEqual(2)947 })948949 it("processes with lodash.find", () => {950 const base = [{id: 1, a: 1}]

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 951–954why, one step back
951 const result = produce(base, draft => {
952 const obj1 = lodash.find(draft, {id: 1})953 lodash.set(obj1, "a", 2)954 })

v1.0.2Michel Weststratebefore adoption551b110

lines 955–956why, one step back
955 expect(base[0].a).toEqual(1)
956 expect(result[0].a).toEqual(2)

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

lines 957–960why, one step back
957 })
958959 it("can produce from no state", () => {960 expect(

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 961–964why, one step back
961 produce(3, draft => {
962 expect(draft).toBe(3)963 return 5964 })

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

lines 965–969why, one step back
965 ).toBe(5)
966 })967968 it("can return something new ", () => {969 const base = {x: 3}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 970–972why, one step back
970 const res = produce(base, draft => {
971 return {x: draft.x + 1}972 })

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

lines 973–978why, one step back
973 expect(res).not.toBe(base)
974 expect(res).toEqual({x: 4})975 })976977 it("can return the draft new ", () => {978 const base = {x: 3}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 979–982why, one step back
979 const res = produce(base, draft => {
980 draft.x = 4981 return draft982 })

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

lines 983–989why, one step back
983 expect(res).not.toBe(base)
984 expect(res).toEqual({x: 4})985 })986987 it("should throw if modifying the draft and returning something new", () => {988 const base = {x: 3}989 expect(() => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 990–993why, one step back
990 produce(base, draft => {
991 draft.x = 4992 return {x: 5}993 })

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 994why, one step back
994 }).toThrow(/An immer producer returned a new value/)

v1.0.2Michel Weststratebefore adoption551b110

lines 995–996why, one step back
995 })
996

Fixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df

line 997why, one step back
997 it("should fix #116", () => {

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 998–1001why, one step back
998 const nextState = produce([1, 2, 3], s => {
999 s.pop()1000 s.push(100)1001 })

Fixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df

lines 1002–1004why, one step back
1002 expect(nextState).toEqual([1, 2, 100])
1003 })1004

Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145

lines 1005–1006why, one step back
1005 it("should fix #117", () => {
1006 const reducer = (state, action) =>

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1007–1014why, one step back
1007 produce(state, draft => {
1008 switch (action.type) {1009 case "SET_STARTING_DOTS":1010 return draft.availableStartingDots.map(a => a)1011 default:1012 break1013 }1014 })

Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145

lines 1015–1029why, one step back
1015 const base = {
1016 availableStartingDots: [1017 {dots: 4, count: 1},1018 {dots: 3, count: 2},1019 {dots: 2, count: 3},1020 {dots: 1, count: 4}1021 ]1022 }1023 const next = reducer(base, {type: "SET_STARTING_DOTS"})1024 expect(next).toEqual(base.availableStartingDots)1025 expect(next).not.toBe(base.availableStartingDots)1026 })10271028 it("should fix #117 - 2", () => {1029 const reducer = (state, action) =>

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1030–1039why, one step back
1030 produce(state, draft => {
1031 switch (action.type) {1032 case "SET_STARTING_DOTS":1033 return {1034 dots: draft.availableStartingDots.map(a => a)1035 }1036 default:1037 break1038 }1039 })

Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145

lines 1040–1051why, one step back
1040 const base = {
1041 availableStartingDots: [1042 {dots: 4, count: 1},1043 {dots: 3, count: 2},1044 {dots: 2, count: 3},1045 {dots: 1, count: 4}1046 ]1047 }1048 const next = reducer(base, {type: "SET_STARTING_DOTS"})1049 expect(next).toEqual({dots: base.availableStartingDots})1050 })1051

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

lines 1052–1053why, one step back
1052 it("should return an unmodified primitive baseState (#148)", () => {
1053 const baseState = "some string"

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1054–1056why, one step back
1054 const nextState = produce(baseState, () => {
1055 /* no modification */1056 })

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

lines 1057–1061why, one step back
1057 expect(nextState).toBe(baseState)
1058 })10591060 it("should return an unmodified null baseState (#148)", () => {1061 const baseState = null

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1062–1064why, one step back
1062 const nextState = produce(baseState, () => {
1063 /* no modification */1064 })

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

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 1066–1069why, one step back
1066 })
10671068 it("should not detect noop assignments - 0", () => {1069 const baseState = {x: {y: 3}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1070–1073why, one step back
1070 const nextState = produce(baseState, d => {
1071 const a = d.x1072 d.x = a1073 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 1074–1078why, one step back
1074 expect(nextState).toBe(baseState)
1075 })10761077 it("should not detect noop assignments - 1", () => {1078 const baseState = {x: {y: 3}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1079–1083why, one step back
1079 const nextState = produce(baseState, d => {
1080 const a = d.x1081 d.x = 41082 d.x = a1083 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 1084–1091why, one step back
1084 // Ideally, this should actually be the same instances
1085 // but this would be pretty expensive to detect,1086 // so we don't atm1087 expect(nextState).not.toBe(baseState)1088 })10891090 it("should not detect noop assignments - 2", () => {1091 const baseState = {x: {y: 3}}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1092–1097why, one step back
1092 const nextState = produce(baseState, d => {
1093 const a = d.x1094 const stuff = a.y + 31095 d.x = 41096 d.x = a1097 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 1098–1105why, one step back
1098 // Ideally, this should actually be the same instances
1099 // but this would be pretty expensive to detect,1100 // so we don't atm1101 expect(nextState).not.toBe(baseState)1102 })11031104 it("should not detect noop assignments - 3", () => {1105 const baseState = {x: 3}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1106–1108why, one step back
1106 const nextState = produce(baseState, d => {
1107 d.x = 31108 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 1109–1113why, one step back
1109 expect(nextState).toBe(baseState)
1110 })11111112 it("should not detect noop assignments - 4", () => {1113 const baseState = {x: 3}

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

lines 1114–1117why, one step back
1114 const nextState = produce(baseState, d => {
1115 d.x = 41116 d.x = 31117 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 1118–1121why, one step back
1118 // Ideally, this should actually be the same instances
1119 // but this would be pretty expensive to detect,1120 // so we don't atm1121 expect(nextState).not.toBe(baseState)

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

lines 1122–1123why, one step back
1122 })
1123

added `np` for easier releasingMichel Weststratebefore adoption23bfaa2

lines 1124–1127why, one step back
1124 it("immer should have no dependencies", () => {
1125 expect(require("../package.json").dependencies).toEqual(undefined)1126 })1127

Emphasized in readme that state should be a treeMichel Weststratebefore adoption211f5cf

lines 1128–1135why, one step back
1128 it("#174", () => {
1129 const nextState = produce([1, 2, 3], s => {1130 s.pop()1131 s.push(100)1132 })1133 expect(nextState).toEqual([1, 2, 100])1134 })1135

Tried to reproduce #195Michel Weststratebefore adoption4f27a93

lines 1136–1150why, one step back
1136 it("#195 should be able to find items", () => {
1137 const state = {1138 items: [1139 {1140 id: 0,1141 task: "drink milk"1142 },1143 {id: 1, task: "eat cookie"}1144 ]1145 }1146 produce(state, draft => {1147 expect(draft.items.find(({id}) => id === 1).task).toBe(1148 "eat cookie"1149 )1150 })

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

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

add tests for function as base statealeclarsonbefore adoption4613b06

lines 1153–1161why, one step back
1153 it("allows a function as the base state", () => {
1154 let fn = () => {}1155 expect(1156 produce(fn, draft => {1157 expect(fn).toBe(draft)1158 })1159 ).toBe(fn)1160 })1161

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 1162–1176why, one step back
1162 it("cannot return and produce undefined!", () => {
1163 const base = 31164 expect(produce(base, () => 4)).toBe(4)1165 expect(produce(base, () => null)).toBe(null)1166 expect(produce(base, () => undefined)).toBe(3)1167 expect(produce(base, () => {})).toBe(3)1168 expect(produce(base, () => nothing)).toBe(undefined)11691170 expect(produce({}, () => undefined)).toEqual({})1171 expect(produce({}, () => nothing)).toBe(undefined)1172 expect(produce(3, () => nothing)).toBe(undefined)11731174 expect(produce(() => undefined)({})).toEqual({})1175 expect(produce(() => nothing)({})).toBe(undefined)1176 expect(produce(() => nothing)(3)).toBe(undefined)

Tried to reproduce #195Michel Weststratebefore adoption4f27a93

lines 1177–1178why, one step back
1177 })
1178

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

lines 1179–1231why, one step back
1179 // TODO: use fuzz testing
1180 {1181 class Foo {}1182 const primitives = {1183 "falsy number": 0,1184 "truthy number": 1,1185 "negative number": -1,1186 infinity: 1 / 0,1187 true: true,1188 false: false,1189 "empty string": "",1190 "truthy string": "1",1191 null: null,1192 undefined: undefined,11931194 /**1195 * These objects are treated as primitives because Immer1196 * chooses not to make drafts for them.1197 */1198 "regexp object": /.+/g,1199 "boxed number": new Number(0),1200 "boxed string": new String(""),1201 "boxed boolean": new Boolean(),1202 "date object": new Date(),1203 "class instance": new Foo()1204 }1205 for (const name in primitives) {1206 describe("base state type - " + name, () => {1207 const value = primitives[name]1208 it("does not create a draft", () => {1209 produce(value, draft => {1210 expect(draft).toBe(value)1211 })1212 })1213 it("returns the same value when the producer returns undefined", () => {1214 expect(produce(value, () => {})).toBe(value)1215 })1216 if (value && typeof value == "object") {1217 it("does not return a copy when the producer makes changes", () => {1218 expect(1219 produce(value, draft => {1220 draft.foo = true1221 })1222 ).toBe(value)1223 })1224 }1225 })1226 }1227 const objects = {1228 "empty object": {},1229 "plain object": {a: 1, b: {c: 1}},1230 "frozen object": Object.freeze({}),1231 "null-prototype object": Object.create(null),

fix(tests): base state typealeclarsonbefore adoptiond157d7a

lines 1232–1234why, one step back
1232 "frozen null-prototype object": Object.freeze(
1233 Object.create(null)1234 ),

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

lines 1235–1241why, one step back
1235 "empty array": [],
1236 "plain array": [1, [2, [3, []]]],1237 "frozen array": Object.freeze([])1238 }1239 for (const name in objects) {1240 describe("base state type - " + name, () => {1241 const value = objects[name]

fix(tests): base state typealeclarsonbefore adoptiond157d7a

lines 1242–1250why, one step back
1242 it("creates a draft", () => {
1243 produce(value, draft => {1244 expect(draft).not.toBe(value)1245 expect(enumerableOnly(draft)).toEqual(value)1246 })1247 })1248 it("returns the same value when the producer does nothing", () => {1249 expect(produce(value, () => {})).toBe(value)1250 })

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

lines 1251–1253why, one step back
1251 it("returns a copy when changes are made", () => {
1252 const random = Math.random()1253 const result = produce(value, draft => {

fix(tests): base state typealeclarsonbefore adoptiond157d7a

line 1254why, one step back
1254 draft[0] = random

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

lines 1255–1257why, one step back
1255 })
1256 expect(result).not.toBe(value)1257 expect(result.constructor).toBe(value.constructor)

fix(tests): base state typealeclarsonbefore adoptiond157d7a

line 1258why, one step back
1258 expect(result[0]).toBe(random)

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

lines 1259–1263why, one step back
1259 })
1260 })1261 }1262 }1263

v1.0.2Michel Weststratebefore adoption551b110

lines 1264–1279why, one step back
1264 afterEach(() => {
1265 expect(baseState).toBe(origBaseState)1266 expect(baseState).toEqual(createBaseState())1267 })12681269 function createBaseState() {1270 const data = {1271 anArray: [3, 2, {c: 3}, 1],1272 aProp: "hi",1273 anObject: {1274 nested: {1275 yummie: true1276 },1277 coffee: false1278 }1279 }

test: improve tests with Immer classaleclarsonbefore adoptionddc9b40

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

v1.0.2Michel Weststratebefore adoption551b110

line 1281why, one step back
1281 }

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

lines 1282–1322why, one step back
1282 })
12831284 describe(`isDraft - ${name}`, () => {1285 it("returns true for object drafts", () => {1286 produce({}, state => {1287 expect(isDraft(state)).toBeTruthy()1288 })1289 })1290 it("returns true for array drafts", () => {1291 produce([], state => {1292 expect(isDraft(state)).toBeTruthy()1293 })1294 })1295 it("returns true for objects nested in object drafts", () => {1296 produce({a: {b: {}}}, state => {1297 expect(isDraft(state.a)).toBeTruthy()1298 expect(isDraft(state.a.b)).toBeTruthy()1299 })1300 })1301 it("returns false for new objects added to a draft", () => {1302 produce({}, state => {1303 state.a = {}1304 expect(isDraft(state.a)).toBeFalsy()1305 })1306 })1307 it("returns false for objects returned by the producer", () => {1308 const object = produce(null, Object.create)1309 expect(isDraft(object)).toBeFalsy()1310 })1311 it("returns false for arrays returned by the producer", () => {1312 const array = produce(null, _ => [])1313 expect(isDraft(array)).toBeFalsy()1314 })1315 it("returns false for object drafts returned by the producer", () => {1316 const object = produce({}, state => state)1317 expect(isDraft(object)).toBeFalsy()1318 })1319 it("returns false for array drafts returned by the producer", () => {1320 const array = produce([], state => state)1321 expect(isDraft(array)).toBeFalsy()1322 })

v1.0.2Michel Weststratebefore adoption551b110

lines 1323–1326why, one step back
1323 })
1324}13251326function enumerableOnly(x) {

test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44

lines 1327–1334why, one step back
1327 const copy = shallowCopy(x)
1328 for (const key in copy) {1329 const value = copy[key]1330 if (value && typeof value === "object") {1331 copy[key] = enumerableOnly(value)1332 }1333 }1334 return copy

v1.0.2Michel Weststratebefore adoption551b110

line 1335why, one step back
1335}