base.js
This file
Path
__tests__/base.js
1,294 lines · 45.6 KiB · JavaScript
Last changed by
test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
History of this fileTake it
The walk stopped after 200 commits: lines older than 551b110 point at it.
v1.0.2Michel Weststratebefore adoption551b110
line 1why, one step back1"use strict"test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 2why, one step back2import {Immer, nothing, isDraft} from "../src/index"test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44
line 3why, one step back3import {shallowCopy} from "../src/common"v1.0.2Michel Weststratebefore adoption551b110
lines 4–11why, one step back4import 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 back12runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)v1.0.2Michel Weststratebefore adoption551b110
lines 13–14why, one step back13runBaseTest("es5 (no freeze)", false, false)14runBaseTest("es5 (autofreeze)", false, true)Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 15why, one step back15runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)v1.0.2Michel Weststratebefore adoption551b110
line 16why, one step back16test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 17–29why, one step back17function 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)29v1.0.2Michel Weststratebefore adoption551b110
lines 30–38why, one step back30 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 back39 const nextState = produce(baseState, () => {})v1.0.2Michel Weststratebefore adoption551b110
lines 40–43why, one step back40 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 back44 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 back48 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 back52 const nextState = produce(baseState, s => {53 s.aProp = "hello world"54 })v1.0.2Michel Weststratebefore adoption551b110
lines 55–61why, one step back55 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 })61Merge pull request #158 from stefanwille/base-test-uses-if-instead-of-itbenbraoubefore adoption0d04d62
line 62why, one step back62 it("should preserve type", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 63–70why, one step back63 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 back71 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 back76 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 back79 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 back8586 it("can add props", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 87–89why, one step back87 const nextState = produce(baseState, s => {88 s.anObject.cookie = {tasty: true}89 })v1.0.2Michel Weststratebefore adoption551b110
lines 90–96why, one step back90 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 back97 const nextState = produce(baseState, s => {98 delete s.anObject.nested99 })v1.0.2Michel Weststratebefore adoption551b110
lines 100–104why, one step back100 expect(nextState).not.toBe(baseState)101 expect(nextState.anObject).not.toBe(baseState.anObject)102 expect(nextState.anObject.nested).toBe(undefined)103 })104test: deleting props added in the produceraleclarsonbefore adoption691e73b
lines 105–106why, one step back105 // Found by: https://github.com/mweststrate/immer/pull/267106 it("can delete props added in the producer", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 107–110why, one step back107 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 back111 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 })119v1.0.2Michel Weststratebefore adoption551b110
line 120why, one step back120 it("ignores single non-modification", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 121–123why, one step back121 const nextState = produce(baseState, s => {122 s.aProp = "hi"123 })v1.0.2Michel Weststratebefore adoption551b110
lines 124–127why, one step back124 expect(nextState).toBe(baseState)125 })126127 it("processes single modification", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 128–131why, one step back128 const nextState = produce(baseState, s => {129 s.aProp = "hello"130 s.aProp = "hi"131 })v1.0.2Michel Weststratebefore adoption551b110
lines 132–159why, one step back132 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 back160 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 back165 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 back173 const nextState = produce(baseState, s => {174 s.anArray.slice()175 })v1.0.2Michel Weststratebefore adoption551b110
lines 176–180why, one step back176 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 back181 const nextState = produce(baseState, s => {182 s.anArray[3] = true183 })v1.0.2Michel Weststratebefore adoption551b110
lines 184–189why, one step back184 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 back190 const nextState = produce(baseState, s => {191 s.anArray.splice(1, 1, "a", "b")192 })v1.0.2Michel Weststratebefore adoption551b110
lines 193–205why, one step back193 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 back206 const nextState = produce(baseState, s => {207 s.anArray.length = 3208 })v1.0.2Michel Weststratebefore adoption551b110
lines 209–215why, one step back209 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 back216 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 back221 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 back227 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 back239 expect(nextState).toEqual(["x"])240 expect(Object.getOwnPropertyDescriptor(nextState, 0)).toEqual({test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 241why, one step back241 configurable: !autoFreeze,v1.0.2Michel Weststratebefore adoption551b110
lines 242–243why, one step back242 enumerable: true,243 value: "x",test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 244why, one step back244 writable: !autoFreezev1.0.2Michel Weststratebefore adoption551b110
lines 245–248why, one step back245 })246 })247248 it("should support sorting arrays - 2", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 249–255why, one step back249 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 back256 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 back269 const nextState = produce(baseState, s => {270 s.anArray[2].test = true271 })v1.0.2Michel Weststratebefore adoption551b110
lines 272–276why, one step back272 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 })276test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 277why, one step back277 it("can rename nested objects (no changes)", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 278–282why, one step back278 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 back283 expect(nextState).not.toBe(baseState)284 expect(nextState.anArray).toBe(baseState.anArray)test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 285why, one step back285 expect(nextState.renamed.nested).toBe(baseState.anObject.nested)v1.0.2Michel Weststratebefore adoption551b110
lines 286–288why, one step back286 expect(enumerableOnly(nextState)).toEqual({287 anArray: [3, 2, {c: 3}, 1],288 aProp: "hi",test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 289why, one step back289 renamed: {v1.0.2Michel Weststratebefore adoption551b110
lines 290–297why, one step back290 nested: {291 yummie: true292 },293 coffee: false294 }295 })296 })297test: renaming propertiesaleclarsonbefore adoptiond83f4b5
lines 298–300why, one step back298 // Very similar to the test before, but the reused object has one299 // 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 back301 const nextState = produce(baseState, s => {302 const obj = s.anObject303 delete s.anObjecttest: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 304why, one step back304test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 305–307why, one step back305 obj.coffee = true // change306 obj.nested.yummy = true // add307 delete obj.nested.yummie // deletetest: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 308why, one step back308test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 309–310why, one step back309 s.renamed = obj310 })v1.0.2Michel Weststratebefore adoption551b110
lines 311–312why, one step back311 expect(nextState).not.toBe(baseState)312 expect(nextState.anArray).toBe(baseState.anArray)test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 313why, one step back313 expect(nextState.renamed.nested).not.toBe(baseState.anObject.nested)v1.0.2Michel Weststratebefore adoption551b110
lines 314–315why, one step back314 expect(enumerableOnly(nextState)).toEqual({315 anArray: [3, 2, {c: 3}, 1],test: renaming propertiesaleclarsonbefore adoptiond83f4b5
lines 316–317why, one step back316 aProp: "hi",317 renamed: {v1.0.2Michel Weststratebefore adoption551b110
line 318why, one step back318 nested: {test: renaming propertiesaleclarsonbefore adoptiond83f4b5
line 319why, one step back319 yummy: truev1.0.2Michel Weststratebefore adoption551b110
lines 320–325why, one step back320 },321 coffee: true322 }323 })324 })325test: draft nested in new objectaleclarsonbefore adoptionf46659b
line 326why, one step back326 it("can nest a draft in a new object (no changes)", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 327–330why, one step back327 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 back331 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 back336 const nextState = produce(baseState, s => {337 const obj = s.anObject338 delete s.anObjecttest: draft nested in new objectaleclarsonbefore adoptionf46659b
line 339why, one step back339test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 340–342why, one step back340 obj.coffee = true // change341 obj.nested.yummy = true // add342 delete obj.nested.yummie // deletetest: draft nested in new objectaleclarsonbefore adoptionf46659b
line 343why, one step back343test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 344–345why, one step back344 s.foo = {bar: obj}345 })test: draft nested in new objectaleclarsonbefore adoptionf46659b
lines 346–355why, one step back346 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 })355v1.0.2Michel Weststratebefore adoption551b110
line 356why, one step back356 it("should allow setting to undefined a defined draft property", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 357–359why, one step back357 const nextState = produce(baseState, s => {358 s.aProp = undefined359 })v1.0.2Michel Weststratebefore adoption551b110
lines 360–368why, one step back360 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 proxytest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 369–372why, one step back369 const nextState = produce(baseState, s => {370 proxy = s371 s.aProp = "hello"372 })v1.0.2Michel Weststratebefore adoption551b110
lines 373–391why, one step back373 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 proxytest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 392–394why, one step back392 const nextState = produce(baseState, s => {393 proxy = s.anObject394 })v1.0.2Michel Weststratebefore adoption551b110
lines 395–405why, one step back395 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 back406 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 back420 })421422 it("should be able to get property descriptors from objects", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 423–428why, one step back423 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 back429test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 430–458why, one step back430 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 back459 })460461 it("should be able to get property descriptors from arrays", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 462–470why, one step back462 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 back471test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 472–480why, one step back472 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 back481 configurable: true,482 enumerable: truetest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 483–486why, one step back483 }484 )485 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(486 {v1.0.2Michel Weststratebefore adoption551b110
lines 487–488why, one step back487 configurable: true,488 enumerable: truetest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 489–496why, one step back489 }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 back497 configurable: true,498 enumerable: truetest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 499–504why, one step back499 }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 back505 })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 back510 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 back517 }).toThrowError(/not support/)518 })Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c
lines 519–540why, one step back519520 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 back541 }Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c
lines 542–550why, one step back542543 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 back551552 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 back554 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 back557 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 back566 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 back569 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 back595 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 back599 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 back608 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 back612 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 })730Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 731–736why, one step back731 describe("nested immer.produce() call", () => {732 describe("with non-draft object", () => {733 // This test ensures the global state used to manage proxies is734 // never left in a corrupted state by a nested `produce` call.735 it("never affects its parent producer implicitly", () => {736 const bear = {paw: {honey: true}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 737–743why, one step back737 const next = produce(bear, draft => {738 const paw2 = produce(bear.paw, draft => {739 draft.honey = false740 })741 expect(paw2.honey).toBe(false)742 expect(draft.paw.honey).toBe(true) // effects should not be visible outside743 })Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 744–746why, one step back744 expect(next.paw.honey).toBe(true)745 expect(next).toBe(bear)746 })v1.0.2Michel Weststratebefore adoption551b110
line 747why, one step back747Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 748–749why, one step back748 it("returns a normal object", () => {749 const bear = {paw: {honey: true}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 750–758why, one step back750 const next = produce(bear, draft => {751 const paw2 = produce(bear.paw, draft => {752 draft.honey = false753 })754 expect(paw2.honey).toBe(false)755 expect(draft.paw.honey).toBe(true)756 draft.paw = paw2757 expect(draft.paw.honey).toBe(false)758 })Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 759–766why, one step back759 expect(next.paw.honey).toBe(false)760 expect(next).not.toBe(bear)761 })762 })763764 describe("with a draft object", () => {765 it("always reuses the draft", () => {766 const bear = {paw: {honey: true}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 767–775why, one step back767 const next = produce(bear, bear => {768 const paw2 = produce(bear.paw, paw => {769 expect(paw).toBe(bear.paw)770 paw.honey = false771 })772 expect(paw2).toBe(bear.paw)773 expect(paw2.honey).toBe(false)774 expect(bear.paw.honey).toBe(false)775 })Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 776–779why, one step back776 expect(next.paw.honey).toBe(false)777 expect(next).not.toBe(bear)778 })779 })v1.0.2Michel Weststratebefore adoption551b110
lines 780–785why, one step back780 })781782 it("should not try to change immutable data, see #66", () => {783 const user = require("./test-data")784785 const base = {}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 786–788why, one step back786 const next = produce(base, draft => {787 draft.user = user788 })v1.0.2Michel Weststratebefore adoption551b110
lines 789–797why, one step back789 expect(next.user).toBe(user)790 expect(next).not.toBe(base)791 expect(next.user).toEqual(user)792 })793794 it("should not try to change immutable data, see #66 - 2", () => {795 const user = deepFreeze(cloneDeep(require("./test-data")))796797 const base = {}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 798–800why, one step back798 const next = produce(base, draft => {799 draft.user = user800 })v1.0.2Michel Weststratebefore adoption551b110
lines 801–807why, one step back801 expect(next.user).toBe(user)802 expect(next).not.toBe(base)803 expect(next.user).toEqual(user)804 })805806 it("should structurally share identical objects in the tree", () => {807 const base = {bear: {legs: 4}, eagle: {legs: 3}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 808–815why, one step back808 const next = produce(base, draft => {809 const animal = draft.bear810 animal.legs = animal.legs + 1811 draft.bear = animal812 draft.eagle = animal813 draft.cow = animal814 draft.kiddo = animal815 })v1.0.2Michel Weststratebefore adoption551b110
lines 816–827why, one step back816 expect(next).toEqual({817 bear: {legs: 5},818 eagle: {legs: 5},819 cow: {legs: 5},820 kiddo: {legs: 5}821 })822 expect(next.bear).toBe(next.cow)823 expect(next.kiddo).toBe(next.cow)824 })825826 if (useProxies)827 it("should not allow changing prototype", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 828–832why, one step back828 produce({}, draft => {829 expect(() => Object.setPrototypeOf(draft, Array)).toThrow(830 /does not support `setPrototype/831 )832 })v1.0.2Michel Weststratebefore adoption551b110
lines 833–835why, one step back833 })834835 it("'in' should work", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 836–838why, one step back836 produce(createBaseState(), draft => {837 expect("anArray" in draft).toBe(true)838 expect(Reflect.has(draft, "anArray")).toBe(true)v1.0.2Michel Weststratebefore adoption551b110
line 839why, one step back839test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 840–841why, one step back840 expect("bla" in draft).toBe(false)841 expect(Reflect.has(draft, "bla")).toBe(false)v1.0.2Michel Weststratebefore adoption551b110
line 842why, one step back842test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 843–846why, one step back843 expect(0 in draft.anArray).toBe(true)844 expect("0" in draft.anArray).toBe(true)845 expect(Reflect.has(draft.anArray, 0)).toBe(true)846 expect(Reflect.has(draft.anArray, "0")).toBe(true)v1.0.2Michel Weststratebefore adoption551b110
line 847why, one step back847test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 848–852why, one step back848 expect(17 in draft.anArray).toBe(false)849 expect("17" in draft.anArray).toBe(false)850 expect(Reflect.has(draft.anArray, 17)).toBe(false)851 expect(Reflect.has(draft.anArray, "17")).toBe(false)852 })v1.0.2Michel Weststratebefore adoption551b110
lines 853–856why, one step back853 })854855 it("'this' should work - 1", () => {856 const base = {x: 3}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 857–859why, one step back857 const next1 = produce(base, function() {858 this.x = 4859 })v1.0.2Michel Weststratebefore adoption551b110
lines 860–882why, one step back860 expect(next1).not.toBe(base)861 expect(next1.x).toBe(4)862 })863864 it("'this' should work - 1", () => {865 const base = {x: 3}866 const incrementor = produce(function() {867 this.x = 4868 })869 const next1 = incrementor(base)870 expect(next1).not.toBe(base)871 expect(next1.x).toBe(4)872 })873874 it("issue #89", () => {875 const state = {876 users: {877 existingID: {878 loading: false,879 value: "val"880 }881 }882 }test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 883–888why, one step back883 const nextState = produce(state, draft => {884 draft.users.existingID = {885 ...draft.users.existingID,886 loading: true887 }888 })v1.0.2Michel Weststratebefore adoption551b110
lines 889–900why, one step back889 expect(nextState).toEqual({890 users: {891 existingID: {892 loading: true,893 value: "val"894 }895 }896 })897 })898899 it("processes with lodash.set", () => {900 const base = [{id: 1, a: 1}]test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 901–903why, one step back901 const result = produce(base, draft => {902 lodash.set(draft, "[0].a", 2)903 })v1.0.2Michel Weststratebefore adoption551b110
lines 904–909why, one step back904 expect(base[0].a).toEqual(1)905 expect(result[0].a).toEqual(2)906 })907908 it("processes with lodash.find", () => {909 const base = [{id: 1, a: 1}]test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 910–913why, one step back910 const result = produce(base, draft => {911 const obj1 = lodash.find(draft, {id: 1})912 lodash.set(obj1, "a", 2)913 })v1.0.2Michel Weststratebefore adoption551b110
lines 914–915why, one step back914 expect(base[0].a).toEqual(1)915 expect(result[0].a).toEqual(2)Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 916–919why, one step back916 })917918 it("can produce from no state", () => {919 expect(test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 920–923why, one step back920 produce(3, draft => {921 expect(draft).toBe(3)922 return 5923 })Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 924–928why, one step back924 ).toBe(5)925 })926927 it("can return something new ", () => {928 const base = {x: 3}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 929–931why, one step back929 const res = produce(base, draft => {930 return {x: draft.x + 1}931 })Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 932–937why, one step back932 expect(res).not.toBe(base)933 expect(res).toEqual({x: 4})934 })935936 it("can return the draft new ", () => {937 const base = {x: 3}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 938–941why, one step back938 const res = produce(base, draft => {939 draft.x = 4940 return draft941 })Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 942–948why, one step back942 expect(res).not.toBe(base)943 expect(res).toEqual({x: 4})944 })945946 it("should throw if modifying the draft and returning something new", () => {947 const base = {x: 3}948 expect(() => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 949–952why, one step back949 produce(base, draft => {950 draft.x = 4951 return {x: 5}952 })Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 953why, one step back953 }).toThrow(/An immer producer returned a new value/)v1.0.2Michel Weststratebefore adoption551b110
lines 954–955why, one step back954 })955Fixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df
line 956why, one step back956 it("should fix #116", () => {test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 957–960why, one step back957 const nextState = produce([1, 2, 3], s => {958 s.pop()959 s.push(100)960 })Fixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df
lines 961–963why, one step back961 expect(nextState).toEqual([1, 2, 100])962 })963Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
lines 964–965why, one step back964 it("should fix #117", () => {965 const reducer = (state, action) =>test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 966–973why, one step back966 produce(state, draft => {967 switch (action.type) {968 case "SET_STARTING_DOTS":969 return draft.availableStartingDots.map(a => a)970 default:971 break972 }973 })Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
lines 974–988why, one step back974 const base = {975 availableStartingDots: [976 {dots: 4, count: 1},977 {dots: 3, count: 2},978 {dots: 2, count: 3},979 {dots: 1, count: 4}980 ]981 }982 const next = reducer(base, {type: "SET_STARTING_DOTS"})983 expect(next).toEqual(base.availableStartingDots)984 expect(next).not.toBe(base.availableStartingDots)985 })986987 it("should fix #117 - 2", () => {988 const reducer = (state, action) =>test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 989–998why, one step back989 produce(state, draft => {990 switch (action.type) {991 case "SET_STARTING_DOTS":992 return {993 dots: draft.availableStartingDots.map(a => a)994 }995 default:996 break997 }998 })Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
lines 999–1010why, one step back999 const base = {1000 availableStartingDots: [1001 {dots: 4, count: 1},1002 {dots: 3, count: 2},1003 {dots: 2, count: 3},1004 {dots: 1, count: 4}1005 ]1006 }1007 const next = reducer(base, {type: "SET_STARTING_DOTS"})1008 expect(next).toEqual({dots: base.availableStartingDots})1009 })1010Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
lines 1011–1012why, one step back1011 it("should return an unmodified primitive baseState (#148)", () => {1012 const baseState = "some string"test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1013–1015why, one step back1013 const nextState = produce(baseState, () => {1014 /* no modification */1015 })Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
lines 1016–1020why, one step back1016 expect(nextState).toBe(baseState)1017 })10181019 it("should return an unmodified null baseState (#148)", () => {1020 const baseState = nulltest: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1021–1023why, one step back1021 const nextState = produce(baseState, () => {1022 /* no modification */1023 })Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
line 1024why, one step back1024 expect(nextState).toBe(baseState)Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1025–1028why, one step back1025 })10261027 it("should not detect noop assignments - 0", () => {1028 const baseState = {x: {y: 3}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1029–1032why, one step back1029 const nextState = produce(baseState, d => {1030 const a = d.x1031 d.x = a1032 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1033–1037why, one step back1033 expect(nextState).toBe(baseState)1034 })10351036 it("should not detect noop assignments - 1", () => {1037 const baseState = {x: {y: 3}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1038–1042why, one step back1038 const nextState = produce(baseState, d => {1039 const a = d.x1040 d.x = 41041 d.x = a1042 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1043–1050why, one step back1043 // Ideally, this should actually be the same instances1044 // but this would be pretty expensive to detect,1045 // so we don't atm1046 expect(nextState).not.toBe(baseState)1047 })10481049 it("should not detect noop assignments - 2", () => {1050 const baseState = {x: {y: 3}}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1051–1056why, one step back1051 const nextState = produce(baseState, d => {1052 const a = d.x1053 const stuff = a.y + 31054 d.x = 41055 d.x = a1056 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1057–1064why, one step back1057 // Ideally, this should actually be the same instances1058 // but this would be pretty expensive to detect,1059 // so we don't atm1060 expect(nextState).not.toBe(baseState)1061 })10621063 it("should not detect noop assignments - 3", () => {1064 const baseState = {x: 3}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1065–1067why, one step back1065 const nextState = produce(baseState, d => {1066 d.x = 31067 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1068–1072why, one step back1068 expect(nextState).toBe(baseState)1069 })10701071 it("should not detect noop assignments - 4", () => {1072 const baseState = {x: 3}test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
lines 1073–1076why, one step back1073 const nextState = produce(baseState, d => {1074 d.x = 41075 d.x = 31076 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1077–1080why, one step back1077 // Ideally, this should actually be the same instances1078 // but this would be pretty expensive to detect,1079 // so we don't atm1080 expect(nextState).not.toBe(baseState)Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
lines 1081–1082why, one step back1081 })1082added `np` for easier releasingMichel Weststratebefore adoption23bfaa2
lines 1083–1086why, one step back1083 it("immer should have no dependencies", () => {1084 expect(require("../package.json").dependencies).toEqual(undefined)1085 })1086Emphasized in readme that state should be a treeMichel Weststratebefore adoption211f5cf
lines 1087–1094why, one step back1087 it("#174", () => {1088 const nextState = produce([1, 2, 3], s => {1089 s.pop()1090 s.push(100)1091 })1092 expect(nextState).toEqual([1, 2, 100])1093 })1094Tried to reproduce #195Michel Weststratebefore adoption4f27a93
lines 1095–1109why, one step back1095 it("#195 should be able to find items", () => {1096 const state = {1097 items: [1098 {1099 id: 0,1100 task: "drink milk"1101 },1102 {id: 1, task: "eat cookie"}1103 ]1104 }1105 produce(state, draft => {1106 expect(draft.items.find(({id}) => id === 1).task).toBe(1107 "eat cookie"1108 )1109 })Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 1110–1111why, one step back1110 })1111add tests for function as base statealeclarsonbefore adoption4613b06
lines 1112–1120why, one step back1112 it("allows a function as the base state", () => {1113 let fn = () => {}1114 expect(1115 produce(fn, draft => {1116 expect(fn).toBe(draft)1117 })1118 ).toBe(fn)1119 })1120Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 1121–1135why, one step back1121 it("cannot return and produce undefined!", () => {1122 const base = 31123 expect(produce(base, () => 4)).toBe(4)1124 expect(produce(base, () => null)).toBe(null)1125 expect(produce(base, () => undefined)).toBe(3)1126 expect(produce(base, () => {})).toBe(3)1127 expect(produce(base, () => nothing)).toBe(undefined)11281129 expect(produce({}, () => undefined)).toEqual({})1130 expect(produce({}, () => nothing)).toBe(undefined)1131 expect(produce(3, () => nothing)).toBe(undefined)11321133 expect(produce(() => undefined)({})).toEqual({})1134 expect(produce(() => nothing)({})).toBe(undefined)1135 expect(produce(() => nothing)(3)).toBe(undefined)Tried to reproduce #195Michel Weststratebefore adoption4f27a93
lines 1136–1137why, one step back1136 })1137Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
lines 1138–1190why, one step back1138 // TODO: use fuzz testing1139 {1140 class Foo {}1141 const primitives = {1142 "falsy number": 0,1143 "truthy number": 1,1144 "negative number": -1,1145 infinity: 1 / 0,1146 true: true,1147 false: false,1148 "empty string": "",1149 "truthy string": "1",1150 null: null,1151 undefined: undefined,11521153 /**1154 * These objects are treated as primitives because Immer1155 * chooses not to make drafts for them.1156 */1157 "regexp object": /.+/g,1158 "boxed number": new Number(0),1159 "boxed string": new String(""),1160 "boxed boolean": new Boolean(),1161 "date object": new Date(),1162 "class instance": new Foo()1163 }1164 for (const name in primitives) {1165 describe("base state type - " + name, () => {1166 const value = primitives[name]1167 it("does not create a draft", () => {1168 produce(value, draft => {1169 expect(draft).toBe(value)1170 })1171 })1172 it("returns the same value when the producer returns undefined", () => {1173 expect(produce(value, () => {})).toBe(value)1174 })1175 if (value && typeof value == "object") {1176 it("does not return a copy when the producer makes changes", () => {1177 expect(1178 produce(value, draft => {1179 draft.foo = true1180 })1181 ).toBe(value)1182 })1183 }1184 })1185 }1186 const objects = {1187 "empty object": {},1188 "plain object": {a: 1, b: {c: 1}},1189 "frozen object": Object.freeze({}),1190 "null-prototype object": Object.create(null),fix(tests): base state typealeclarsonbefore adoptiond157d7a
lines 1191–1193why, one step back1191 "frozen null-prototype object": Object.freeze(1192 Object.create(null)1193 ),Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
lines 1194–1200why, one step back1194 "empty array": [],1195 "plain array": [1, [2, [3, []]]],1196 "frozen array": Object.freeze([])1197 }1198 for (const name in objects) {1199 describe("base state type - " + name, () => {1200 const value = objects[name]fix(tests): base state typealeclarsonbefore adoptiond157d7a
lines 1201–1209why, one step back1201 it("creates a draft", () => {1202 produce(value, draft => {1203 expect(draft).not.toBe(value)1204 expect(enumerableOnly(draft)).toEqual(value)1205 })1206 })1207 it("returns the same value when the producer does nothing", () => {1208 expect(produce(value, () => {})).toBe(value)1209 })Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
lines 1210–1212why, one step back1210 it("returns a copy when changes are made", () => {1211 const random = Math.random()1212 const result = produce(value, draft => {fix(tests): base state typealeclarsonbefore adoptiond157d7a
line 1213why, one step back1213 draft[0] = randomMerge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
lines 1214–1216why, one step back1214 })1215 expect(result).not.toBe(value)1216 expect(result.constructor).toBe(value.constructor)fix(tests): base state typealeclarsonbefore adoptiond157d7a
line 1217why, one step back1217 expect(result[0]).toBe(random)Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
lines 1218–1222why, one step back1218 })1219 })1220 }1221 }1222v1.0.2Michel Weststratebefore adoption551b110
lines 1223–1238why, one step back1223 afterEach(() => {1224 expect(baseState).toBe(origBaseState)1225 expect(baseState).toEqual(createBaseState())1226 })12271228 function createBaseState() {1229 const data = {1230 anArray: [3, 2, {c: 3}, 1],1231 aProp: "hi",1232 anObject: {1233 nested: {1234 yummie: true1235 },1236 coffee: false1237 }1238 }test: improve tests with Immer classaleclarsonbefore adoptionddc9b40
line 1239why, one step back1239 return autoFreeze ? deepFreeze(data) : datav1.0.2Michel Weststratebefore adoption551b110
line 1240why, one step back1240 }Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
lines 1241–1281why, one step back1241 })12421243 describe(`isDraft - ${name}`, () => {1244 it("returns true for object drafts", () => {1245 produce({}, state => {1246 expect(isDraft(state)).toBeTruthy()1247 })1248 })1249 it("returns true for array drafts", () => {1250 produce([], state => {1251 expect(isDraft(state)).toBeTruthy()1252 })1253 })1254 it("returns true for objects nested in object drafts", () => {1255 produce({a: {b: {}}}, state => {1256 expect(isDraft(state.a)).toBeTruthy()1257 expect(isDraft(state.a.b)).toBeTruthy()1258 })1259 })1260 it("returns false for new objects added to a draft", () => {1261 produce({}, state => {1262 state.a = {}1263 expect(isDraft(state.a)).toBeFalsy()1264 })1265 })1266 it("returns false for objects returned by the producer", () => {1267 const object = produce(null, Object.create)1268 expect(isDraft(object)).toBeFalsy()1269 })1270 it("returns false for arrays returned by the producer", () => {1271 const array = produce(null, _ => [])1272 expect(isDraft(array)).toBeFalsy()1273 })1274 it("returns false for object drafts returned by the producer", () => {1275 const object = produce({}, state => state)1276 expect(isDraft(object)).toBeFalsy()1277 })1278 it("returns false for array drafts returned by the producer", () => {1279 const array = produce([], state => state)1280 expect(isDraft(array)).toBeFalsy()1281 })v1.0.2Michel Weststratebefore adoption551b110
lines 1282–1285why, one step back1282 })1283}12841285function enumerableOnly(x) {test: improve `enumerableOnly` functionaleclarsonbefore adoptiona4a1e44
lines 1286–1293why, one step back1286 const copy = shallowCopy(x)1287 for (const key in copy) {1288 const value = copy[key]1289 if (value && typeof value === "object") {1290 copy[key] = enumerableOnly(value)1291 }1292 }1293 return copyv1.0.2Michel Weststratebefore adoption551b110
line 1294why, one step back1294}