base.js
This file
Path
__tests__/base.js
1,355 lines · 44.6 KiB · JavaScript
Last changed by
Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
History of this fileTake it
The walk stopped after 200 commits: lines older than a1f5c6a point at it.
Readme improvementsMichel Weststratebefore adoptiona1f5c6a
line 1why, one step back1"use strict"Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
line 2why, one step back2import produce, {setAutoFreeze, setUseProxies, nothing} from "../src/immer"Readme improvementsMichel Weststratebefore adoptiona1f5c6a
line 3why, one step back3import deepFreeze from "deep-freeze"Reproduced #66Michel Weststratebefore adoptiond5de885
line 4why, one step back4import cloneDeep from "lodash.clonedeep"added some additional tests to verify solution correctness of #86Michel Weststratebefore adoption681b162
line 5why, one step back5import * as lodash from "lodash"Readme improvementsMichel Weststratebefore adoptiona1f5c6a
line 6why, one step back6Making several improvements at onceMichel Weststratebefore adoption1116ccf
lines 7–8why, one step back7jest.setTimeout(1000)8Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 9–10why, one step back9runBaseTest("proxy (no freeze)", true, false)10runBaseTest("proxy (autofreeze)", true, true)Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 11why, one step back11runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 12–13why, one step back12runBaseTest("es5 (no freeze)", false, false)13runBaseTest("es5 (autofreeze)", false, true)Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 14why, one step back14runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)Readme improvementsMichel Weststratebefore adoptiona1f5c6a
line 15why, one step back15Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 16why, one step back16function runBaseTest(name, useProxies, freeze, useListener) {Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 17–19why, one step back17 describe(`base functionality - ${name}`, () => {18 let baseState19 let origBaseStateMerge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 20–21why, one step back20 // make sure logic doesn't break when listener is attached21 let listener = useListener ? function() {} : undefinedReadme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 22–23why, one step back2223 beforeEach(() => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 24–25why, one step back24 setAutoFreeze(freeze)25 setUseProxies(useProxies)Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 26–29why, one step back26 origBaseState = baseState = createBaseState()27 })2829 it("should return the original without modifications", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 30why, one step back30 const nextState = produce(baseState, () => {}, listener)Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 31–34why, one step back31 expect(nextState).toBe(baseState)32 })3334 it("should return the original without modifications when reading stuff", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 35–42why, one step back35 const nextState = produce(36 baseState,37 s => {38 expect(s.aProp).toBe("hi")39 expect(s.anObject.nested).toMatchObject({yummie: true})40 },41 listener42 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 43–46why, one step back43 expect(nextState).toBe(baseState)44 })4546 it("should return a copy when modifying stuff", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 47–53why, one step back47 const nextState = produce(48 baseState,49 s => {50 s.aProp = "hello world"51 },52 listener53 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 54–60why, one step back54 expect(nextState).not.toBe(baseState)55 expect(baseState.aProp).toBe("hi")56 expect(nextState.aProp).toBe("hello world")57 // structural sharing?58 expect(nextState.nested).toBe(baseState.nested)59 })60Merge pull request #158 from stefanwille/base-test-uses-if-instead-of-itbenbraoubefore adoption0d04d62
line 61why, one step back61 it("should preserve type", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 62–73why, one step back62 const nextState = produce(63 baseState,64 s => {65 expect(Object.getPrototypeOf(s)).toBe(Object.prototype)66 expect(Array.isArray(s.anArray)).toBe(true)67 s.anArray.push(3)68 s.aProp = "hello world"69 expect(Object.getPrototypeOf(s)).toBe(Object.prototype)70 expect(Array.isArray(s.anArray)).toBe(true)71 },72 listener73 )Merge pull request #158 from stefanwille/base-test-uses-if-instead-of-itbenbraoubefore adoption0d04d62
lines 74–78why, one step back74 expect(Object.getPrototypeOf(nextState)).toBe(Object.prototype)75 expect(Array.isArray(nextState.anArray)).toBe(true)76 })7778 it("deep change bubbles up", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 79–85why, one step back79 const nextState = produce(80 baseState,81 s => {82 s.anObject.nested.yummie = false83 },84 listener85 )Merge pull request #158 from stefanwille/base-test-uses-if-instead-of-itbenbraoubefore adoption0d04d62
lines 86–91why, one step back86 expect(nextState).not.toBe(baseState)87 expect(nextState.anObject).not.toBe(baseState.anObject)88 expect(baseState.anObject.nested.yummie).toBe(true)89 expect(nextState.anObject.nested.yummie).toBe(false)90 expect(nextState.anArray).toBe(baseState.anArray)91 })Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 92–93why, one step back9293 it("can add props", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 94–100why, one step back94 const nextState = produce(95 baseState,96 s => {97 s.anObject.cookie = {tasty: true}98 },99 listener100 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 101–107why, one step back101 expect(nextState).not.toBe(baseState)102 expect(nextState.anObject).not.toBe(baseState.anObject)103 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)104 expect(nextState.anObject.cookie).toEqual({tasty: true})105 })106107 it("can delete props", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 108–114why, one step back108 const nextState = produce(109 baseState,110 s => {111 delete s.anObject.nested112 },113 listener114 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 115–120why, one step back115 expect(nextState).not.toBe(baseState)116 expect(nextState.anObject).not.toBe(baseState.anObject)117 expect(nextState.anObject.nested).toBe(undefined)118 })119120 it("ignores single non-modification", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 121–127why, one step back121 const nextState = produce(122 baseState,123 s => {124 s.aProp = "hi"125 },126 listener127 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 128–131why, one step back128 expect(nextState).toBe(baseState)129 })130131 it("processes single modification", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 132–139why, one step back132 const nextState = produce(133 baseState,134 s => {135 s.aProp = "hello"136 s.aProp = "hi"137 },138 listener139 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 140–143why, one step back140 expect(nextState).not.toBe(baseState)141 expect(nextState).toEqual(baseState)142 })143Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85
lines 144–167why, one step back144 it("processes with for loop", () => {145 const base = [{id: 1, a: 1}, {id: 2, a: 1}]146 const findById = (collection, id) => {147 for (const item of collection) {148 if (item.id === id) return item149 }150 return null151 }152 const result = produce(base, draft => {153 const obj1 = findById(draft, 1)154 const obj2 = findById(draft, 2)155 obj1.a = 2156 obj2.a = 2157 })158 expect(result[0].a).toEqual(2)159 expect(result[1].a).toEqual(2)160 })161162 it("works with objects without proto", () => {163 const base = Object.create(null)164 base.x = 1165 base.y = Object.create(null)166 base.y.y = 2167 expect(base.__proto__).toBe(undefined)Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 168–176why, one step back168 const next = produce(169 base,170 draft => {171 draft.y.z = 3172 draft.y.y++173 draft.x++174 },175 listener176 )Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85
lines 177–183why, one step back177 expect(next).toEqual({178 x: 2,179 y: {y: 3, z: 3}180 })181 expect(next.__proto__).toBe(undefined)182 })183Readme improvementsMichel Weststratebefore adoptiona1f5c6a
line 184why, one step back184 it("should support reading arrays", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 185–191why, one step back185 const nextState = produce(186 baseState,187 s => {188 s.anArray.slice()189 },190 listener191 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 192–196why, one step back192 expect(nextState.anArray).toBe(baseState.anArray)193 expect(nextState).toBe(baseState)194 })195196 it("should support changing arrays", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 197–203why, one step back197 const nextState = produce(198 baseState,199 s => {200 s.anArray[3] = true201 },202 listener203 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 204–209why, one step back204 expect(nextState).not.toBe(baseState)205 expect(nextState.anArray).not.toBe(baseState.anArray)206 expect(nextState.anArray[3]).toEqual(true)207 })208209 it("should support changing arrays - 2", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 210–216why, one step back210 const nextState = produce(211 baseState,212 s => {213 s.anArray.splice(1, 1, "a", "b")214 },215 listener216 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 217–229why, one step back217 expect(nextState).not.toBe(baseState)218 expect(nextState.anArray).not.toBe(baseState.anArray)219220 expect(enumerableOnly(nextState.anArray)).toEqual([221 3,222 "a",223 "b",224 {c: 3},225 1226 ])227 })228229 it("can delete array items", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 230–236why, one step back230 const nextState = produce(231 baseState,232 s => {233 s.anArray.length = 3234 },235 listener236 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 237–243why, one step back237 expect(nextState).not.toBe(baseState)238 expect(nextState.anObject).toBe(baseState.anObject)239 expect(nextState.anArray).not.toBe(baseState.anArray)240 expect(nextState.anArray).toEqual([3, 2, {c: 3}])241 })242243 it("should support sorting arrays", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 244–252why, one step back244 const nextState = produce(245 baseState,246 s => {247 s.anArray[2].c = 4248 s.anArray.sort()249 s.anArray[3].c = 5250 },251 listener252 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 253–258why, one step back253 expect(nextState).not.toBe(baseState)254 expect(nextState.anArray).not.toBe(baseState.anArray)255 expect(enumerableOnly(nextState.anArray)).toEqual([1, 2, 3, {c: 5}])256 })257258 it("should expose property descriptors", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 259–276why, one step back259 const nextState = produce(260 [],261 s => {262 expect(Object.getOwnPropertyDescriptor(s, 0)).toBe(263 undefined264 )265 s.unshift("x")266 expect(Object.getOwnPropertyDescriptor(s, 0)).toEqual({267 configurable: true,268 enumerable: true,269 value: "x",270 writable: true271 })272 expect(s.length).toBe(1)273 expect(s[0] === "x").toBe(true)274 },275 listener276 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 277–286why, one step back277 expect(nextState).toEqual(["x"])278 expect(Object.getOwnPropertyDescriptor(nextState, 0)).toEqual({279 configurable: !freeze,280 enumerable: true,281 value: "x",282 writable: !freeze283 })284 })285286 it("should support sorting arrays - 2", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 287–297why, one step back287 const nextState = produce(288 baseState,289 s => {290 s.anArray.unshift("x")291 s.anArray[3].c = 4292 s.anArray.sort()293 s.anArray[3].c = 5294 s.anArray.unshift("y")295 },296 listener297 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 298–310why, one step back298 expect(nextState).not.toBe(baseState)299 expect(nextState.anArray).not.toBe(baseState.anArray)300 expect(enumerableOnly(nextState.anArray)).toEqual([301 "y",302 1,303 2,304 3,305 {c: 5},306 "x"307 ])308 })309310 it("should updating inside arrays", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 311–317why, one step back311 const nextState = produce(312 baseState,313 s => {314 s.anArray[2].test = true315 },316 listener317 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 318–323why, one step back318 expect(nextState).not.toBe(baseState)319 expect(nextState.anArray).not.toBe(baseState.anArray)320 expect(nextState.anArray).toEqual([3, 2, {c: 3, test: true}, 1])321 })322323 it("reusing object should work", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 324–332why, one step back324 const nextState = produce(325 baseState,326 s => {327 const obj = s.anObject328 delete s.anObject329 s.messy = obj330 },331 listener332 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 333–348why, one step back333 expect(nextState).not.toBe(baseState)334 expect(nextState.anArray).toBe(baseState.anArray)335 expect(enumerableOnly(nextState)).toEqual({336 anArray: [3, 2, {c: 3}, 1],337 aProp: "hi",338 messy: {339 nested: {340 yummie: true341 },342 coffee: false343 }344 })345 expect(nextState.messy.nested).toBe(baseState.anObject.nested)346 })347348 it("refs should be transparent", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 349–359why, one step back349 const nextState = produce(350 baseState,351 s => {352 const obj = s.anObject353 s.aProp = "hello"354 delete s.anObject355 obj.coffee = true356 s.messy = obj357 },358 listener359 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 360–375why, one step back360 expect(nextState).not.toBe(baseState)361 expect(nextState.anArray).toBe(baseState.anArray)362 expect(enumerableOnly(nextState)).toEqual({363 anArray: [3, 2, {c: 3}, 1],364 aProp: "hello",365 messy: {366 nested: {367 yummie: true368 },369 coffee: true370 }371 })372 expect(nextState.messy.nested).toBe(baseState.anObject.nested)373 })374375 it("should allow setting to undefined a defined draft property", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 376–382why, one step back376 const nextState = produce(377 baseState,378 s => {379 s.aProp = undefined380 },381 listener382 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 383–388why, one step back383 expect(nextState).not.toBe(baseState)384 expect(baseState.aProp).toBe("hi")385 expect(nextState.aProp).toBe(undefined)386 })387388 // ES implementation does't protect against all outside modifications, just some..Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 389why, one step back389 if (useProxies) {Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 390–391why, one step back390 it("should revoke the proxy of the baseState after immer function is executed", () => {391 let proxyMerge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 392–399why, one step back392 const nextState = produce(393 baseState,394 s => {395 proxy = s396 s.aProp = "hello"397 },398 listener399 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 400–418why, one step back400 expect(nextState).not.toBe(baseState)401 expect(baseState.aProp).toBe("hi")402 expect(nextState.aProp).toBe("hello")403404 expect(() => {405 proxy.aProp = "Hallo"406 }).toThrowError(/revoked/)407 expect(() => {408 const aProp = proxy.aProp409 }).toThrowError(/revoked/)410411 expect(nextState).not.toBe(baseState)412 expect(baseState.aProp).toBe("hi")413 expect(nextState.aProp).toBe("hello")414 })415 }416417 it("should revoke the proxy of the baseState after immer function is executed - 2", () => {418 let proxyMerge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 419–425why, one step back419 const nextState = produce(420 baseState,421 s => {422 proxy = s.anObject423 },424 listener425 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 426–436why, one step back426 expect(nextState).toBe(baseState)427 expect(() => {428 // In ES5 implemenation only protects existing props, but alas..429 proxy.coffee = "Hallo"430 }).toThrowError(/revoked/)431 expect(() => {432 const test = proxy.coffee433 }).toThrowError(/revoked/)434 })435436 it("should reflect all changes made in the draft immediately", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 437–454why, one step back437 produce(438 baseState,439 draft => {440 draft.anArray[0] = 5441 draft.anArray.unshift("test")442 // sliced here; jest will also compare non-enumerable keys, which would include the immer Symbols443 expect(draft.anArray.slice()).toMatchObject([444 "test",445 5,446 2,447 {c: 3},448 1449 ])450 draft.stuffz = "coffee"451 expect(draft.stuffz).toBe("coffee")452 },453 listener454 )Readme improvementsMichel Weststratebefore adoptiona1f5c6a
line 455why, one step back455 })Fixed configurability of array.length on proxies. Fixes #50Michel Weststratebefore adoption1c7df26
lines 456–457why, one step back456457 it("should be able to get property descriptors from objects", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 458–467why, one step back458 produce(459 {a: 1},460 draft => {461 expect("a" in draft).toBe(true)462 expect("b" in draft).toBe(false)463 expect(464 Reflect.ownKeys(draft).filter(465 x => typeof x === "string"466 )467 ).toEqual(["a"])Fixed configurability of array.length on proxies. Fixes #50Michel Weststratebefore adoption1c7df26
line 468why, one step back468Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 469–501why, one step back469 expect(470 Object.getOwnPropertyDescriptor(draft, "a")471 ).toMatchObject({472 configurable: true,473 enumerable: true474 })475 draft.a = 2476 expect(477 Object.getOwnPropertyDescriptor(draft, "a")478 ).toMatchObject({479 configurable: true,480 enumerable: true481 })482 expect(483 Object.getOwnPropertyDescriptor(draft, "b")484 ).toBeUndefined()485 draft.b = 2486 expect(487 Object.getOwnPropertyDescriptor(draft, "b")488 ).toMatchObject({489 configurable: true,490 enumerable: true491 })492 expect("a" in draft).toBe(true)493 expect("b" in draft).toBe(true)494 expect(495 Reflect.ownKeys(draft).filter(496 x => typeof x === "string"497 )498 ).toEqual(["a", "b"])499 },500 listener501 )Fixed configurability of array.length on proxies. Fixes #50Michel Weststratebefore adoption1c7df26
lines 502–504why, one step back502 })503504 it("should be able to get property descriptors from arrays", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 505–517why, one step back505 produce(506 [1],507 draft => {508 expect(0 in draft).toBe(true)509 expect(1 in draft).toBe(false)510 expect("0" in draft).toBe(true)511 expect("1" in draft).toBe(false)512 expect(length in draft).toBe(true)513 expect(514 Reflect.ownKeys(draft).filter(515 x => typeof x === "string"516 )517 ).toEqual(["0", "length"])Fixed configurability of array.length on proxies. Fixes #50Michel Weststratebefore adoption1c7df26
line 518why, one step back518Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 519–528why, one step back519 expect(520 Object.getOwnPropertyDescriptor(draft, "length")521 ).toMatchObject({522 configurable: false,523 enumerable: false524 })525 draft[0] = 2526 expect(527 Object.getOwnPropertyDescriptor(draft, 0)528 ).toMatchObject({Fixed configurability of array.length on proxies. Fixes #50Michel Weststratebefore adoption1c7df26
lines 529–530why, one step back529 configurable: true,530 enumerable: trueMerge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 531–534why, one step back531 })532 expect(533 Object.getOwnPropertyDescriptor(draft, 0)534 ).toMatchObject({Fixed configurability of array.length on proxies. Fixes #50Michel Weststratebefore adoption1c7df26
lines 535–536why, one step back535 configurable: true,536 enumerable: trueMerge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 537–544why, one step back537 })538 expect(539 Object.getOwnPropertyDescriptor(draft, 1)540 ).toBeUndefined()541 draft[1] = 2542 expect(543 Object.getOwnPropertyDescriptor(draft, 1)544 ).toMatchObject({Fixed configurability of array.length on proxies. Fixes #50Michel Weststratebefore adoption1c7df26
lines 545–546why, one step back545 configurable: true,546 enumerable: trueMerge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 547–555why, one step back547 })548 expect(549 Reflect.ownKeys(draft).filter(550 x => typeof x === "string"551 )552 ).toEqual(["0", "1", "length"])553 },554 listener555 )Fixed configurability of array.length on proxies. Fixes #50Michel Weststratebefore adoption1c7df26
lines 556–557why, one step back556 })557Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 558why, one step back558 if (useProxies === true) {Fixed configurability of array.length on proxies. Fixes #50Michel Weststratebefore adoption1c7df26
lines 559–560why, one step back559 it("should not be possible to set property descriptors", () => {560 expect(() => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 561–571why, one step back561 produce(562 {},563 draft => {564 Object.defineProperty(draft, "xx", {565 enumerable: true,566 writeable: true,567 value: 2568 })569 },570 listener571 )Fixed configurability of array.length on proxies. Fixes #50Michel Weststratebefore adoption1c7df26
lines 572–573why, one step back572 }).toThrowError(/not support/)573 })Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c
lines 574–595why, one step back574575 it("should not be possible to add properties to arrays", () => {576 expect(() => {577 produce([], d => {578 d.x = 3579 })580 }).toThrow(581 "Immer does not support setting non-numeric properties on arrays"582 )583 })584585 it("should not be possible to remove properties from arrays", () => {586 expect(() => {587 const base = []588 base.x = 7589 produce(base, d => {590 delete d.x591 })592 }).toThrow(593 "Immer does not support deleting properties from arrays"594 )595 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 596why, one step back596 }Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c
lines 597–605why, one step back597598 it("non-numeric array properties will be lost", () => {599 const base = []600 base.x = 7601 const next = produce(base, d => {602 d.push(3)603 })604 expect(next.x).toBe(undefined)605 })initial solutionMichel Weststratebefore adoptiond618d28
line 606why, one step back606Faster, simpler implementationMichel Weststratebefore adoptionc420f0d
lines 607–608why, one step back607 it("should not throw error, see #53 - 1", () => {608 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 609–615why, one step back609 const result = produce(610 base,611 draft => {612 draft.arr = draft.arr.filter(item => item.count > 2)613 },614 listener615 )initial solutionMichel Weststratebefore adoptiond618d28
line 616why, one step back616 expect(result.arr[0].count).toEqual(3)Faster, simpler implementationMichel Weststratebefore adoptionc420f0d
lines 617–624why, one step back617 expect(result).toEqual({618 arr: [{count: 3}]619 })620 expect(result.arr[0]).toBe(base.arr[2])621 })622623 it("should not throw error, see #53 - 2", () => {624 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 625–631why, one step back625 const result = produce(626 base,627 draft => {628 draft.newArr = draft.arr.filter(item => item.count > 2)629 },630 listener631 )Faster, simpler implementationMichel Weststratebefore adoptionc420f0d
lines 632–651why, one step back632 expect(result.newArr[0].count).toEqual(3)633 expect(result.arr).toBe(base.arr)634 expect(result).toEqual({635 arr: [636 {637 count: 1638 },639 {640 count: 2641 },642 {643 count: 3644 }645 ],646 newArr: [647 {648 count: 3649 }650 ]651 })Making several improvements at onceMichel Weststratebefore adoption1116ccf
lines 652–653why, one step back652 expect(result.newArr[0]).toBe(base.arr[2])653 expect(result.arr[2]).toBe(base.arr[2])Faster, simpler implementationMichel Weststratebefore adoptionc420f0d
lines 654–657why, one step back654 })655656 it("should not throw error, see #53 - 3", () => {657 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 658–665why, one step back658 const result = produce(659 base,660 draft => {661 draft.newArr = draft.arr.filter(item => item.count > 2)662 delete draft.arr663 },664 listener665 )Faster, simpler implementationMichel Weststratebefore adoptionc420f0d
lines 666–670why, one step back666 expect(result.newArr[0].count).toEqual(3)667 expect(result).toEqual({668 newArr: [{count: 3}]669 })670 expect(result.newArr[0]).toBe(base.arr[2])initial solutionMichel Weststratebefore adoptiond618d28
line 671why, one step back671 })Readme improvementsMichel Weststratebefore adoptiona1f5c6a
line 672why, one step back672Making several improvements at onceMichel Weststratebefore adoption1116ccf
lines 673–674why, one step back673 it("should not throw error, see #53 - 4", () => {674 const base = {bear: {age: 10}}Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 675–682why, one step back675 const result = produce(676 base,677 draft => {678 draft.bear.legs = 4679 draft.room = {elephant: {kiddo: draft.bear}}680 },681 listener682 )Making several improvements at onceMichel Weststratebefore adoption1116ccf
lines 683–697why, one step back683 expect(result).toEqual({684 bear: {age: 10, legs: 4},685 room: {elephant: {kiddo: {age: 10, legs: 4}}}686 })687688 const result2 = produce(result, draft => {689 draft.bear.age = 11690 draft.room.elephant.kiddo.legs = 5691 })692 expect(result2).toEqual({693 bear: {age: 11, legs: 4},694 room: {elephant: {kiddo: {age: 10, legs: 5}}}695 })696 })697Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 698–746why, one step back698 it("should not throw error #78", () => {699 const base = {700 arr: [701 {702 id: "100",703 arr: [{id: "1", no: 1}, {id: "2", no: 2}]704 },705 {id: "3", no: 3}706 ]707 }708 const result = produce(base, draft => {709 const base = draft.arr[0]710 draft.arr = draft.arr.slice(0, 1)711 const newArr = {712 id: "101",713 arr: [base, {id: "3"}]714 }715 draft.arr.splice(0, 1, newArr)716 draft.arr[0].arr[1].no = 3717 })718 expect(result).toEqual({719 arr: [720 {721 id: "101",722 arr: [723 {724 id: "100",725 arr: [{id: "1", no: 1}, {id: "2", no: 2}]726 },727 {id: "3", no: 3}728 ]729 }730 ]731 })732 })733734 it("should not throw error #78 - 2", () => {735 const base = {736 arr: [{count: 1}, {count: 2}]737 }738 const result = produce(base, draft => {739 draft.arr = [draft.arr[1]]740 draft.arr[0].count = 1741 })742 expect(result).toEqual({743 arr: [{count: 1}]744 })745 })746Merge pull request #81 from iruca3/fix/treating_constructor_on_proxyMichel Weststratebefore adoptionad5a86a
lines 747–759why, one step back747 it("should handle constructor correctly", () => {748 const base = {749 arr: new Array(),750 obj: new Object()751 }752 const result = produce(base, draft => {753 draft.arrConstructed = draft.arr.constructor(1)754 draft.objConstructed = draft.obj.constructor(1)755 })756 expect(result.arrConstructed).toEqual(new Array().constructor(1))757 expect(result.objConstructed).toEqual(new Object().constructor(1))758 })759Making several improvements at onceMichel Weststratebefore adoption1116ccf
lines 760–801why, one step back760 it("should handle dates correctly", () => {761 const data = {date: new Date()}762 const next = produce(data, draft => {763 draft.x = true764 })765 expect(next).toEqual({x: true, date: data.date})766 expect(next.date).toBe(data.date)767 const next2 = produce(next, draft => {768 draft.date.setYear(2015)769 })770 // This still holds; because produce won't proxy Date objects771 // and the original is actually modified!772 expect(next2).toEqual({x: true, date: data.date})773 expect(next2.date).toBe(next.date)774 expect(next2.date).toBe(data.date)775 expect(next2).toBe(next)776 })777778 it("should handle equality correctly - 1", () => {779 const base = {780 y: 3 / 0,781 z: NaN782 }783 const next = produce(base, draft => {784 draft.y = 4 / 0785 draft.z = NaN786 })787 expect(next).toEqual(base)788 expect(next).toBe(base)789 })790791 it("should handle equality correctly - 2", () => {792 const base = {793 x: -0794 }795 const next = produce(base, draft => {796 draft.x = +1797 })798 expect(next).not.toEqual(base)799 expect(next).not.toBe(base)800 })801Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 802–821why, one step back802 describe("nested immer.produce() call", () => {803 describe("with non-draft object", () => {804 // This test ensures the global state used to manage proxies is805 // never left in a corrupted state by a nested `produce` call.806 it("never affects its parent producer implicitly", () => {807 const bear = {paw: {honey: true}}808 const next = produce(809 bear,810 draft => {811 const paw2 = produce(bear.paw, draft => {812 draft.honey = false813 })814 expect(paw2.honey).toBe(false)815 expect(draft.paw.honey).toBe(true) // effects should not be visible outside816 },817 listener818 )819 expect(next.paw.honey).toBe(true)820 expect(next).toBe(bear)821 })Making several improvements at onceMichel Weststratebefore adoption1116ccf
line 822why, one step back822Merge pull request #250 from aleclarson/patch2Michel Weststratebefore adoption8e7b371
lines 823–862why, one step back823 it("returns a normal object", () => {824 const bear = {paw: {honey: true}}825 const next = produce(826 bear,827 draft => {828 const paw2 = produce(bear.paw, draft => {829 draft.honey = false830 })831 expect(paw2.honey).toBe(false)832 expect(draft.paw.honey).toBe(true)833 draft.paw = paw2834 expect(draft.paw.honey).toBe(false)835 },836 listener837 )838 expect(next.paw.honey).toBe(false)839 expect(next).not.toBe(bear)840 })841 })842843 describe("with a draft object", () => {844 it("always reuses the draft", () => {845 const bear = {paw: {honey: true}}846 const next = produce(847 bear,848 bear => {849 const paw2 = produce(bear.paw, paw => {850 expect(paw).toBe(bear.paw)851 paw.honey = false852 })853 expect(paw2).toBe(bear.paw)854 expect(paw2.honey).toBe(false)855 expect(bear.paw.honey).toBe(false)856 },857 listener858 )859 expect(next.paw.honey).toBe(false)860 expect(next).not.toBe(bear)861 })862 })Reproduced #66Michel Weststratebefore adoptiond5de885
lines 863–868why, one step back863 })864865 it("should not try to change immutable data, see #66", () => {866 const user = require("./test-data")867868 const base = {}Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 869–875why, one step back869 const next = produce(870 base,871 draft => {872 draft.user = user873 },874 listener875 )Reproduced #66Michel Weststratebefore adoptiond5de885
lines 876–884why, one step back876 expect(next.user).toBe(user)877 expect(next).not.toBe(base)878 expect(next.user).toEqual(user)879 })880881 it("should not try to change immutable data, see #66 - 2", () => {882 const user = deepFreeze(cloneDeep(require("./test-data")))883884 const base = {}Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 885–891why, one step back885 const next = produce(886 base,887 draft => {888 draft.user = user889 },890 listener891 )Reproduced #66Michel Weststratebefore adoptiond5de885
lines 892–894why, one step back892 expect(next.user).toBe(user)893 expect(next).not.toBe(base)894 expect(next.user).toEqual(user)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 895–899why, one step back895 })896897 if (freeze)898 it("should freeze new data well", () => {899 const base = {}Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 900–906why, one step back900 const next = produce(901 base,902 draft => {903 draft.x = {y: [{z: true}]}904 },905 listener906 )Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 907–914why, one step back907 expect(Object.isFrozen(next)).toBe(true)908 expect(Object.isFrozen(next.x)).toBe(true)909 expect(Object.isFrozen(next.x.y)).toBe(true)910 expect(Object.isFrozen(next.x.y[0].z)).toBe(true)911 })912913 it("should structurally share identical objects in the tree", () => {914 const base = {bear: {legs: 4}, eagle: {legs: 3}}Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 915–926why, one step back915 const next = produce(916 base,917 draft => {918 const animal = draft.bear919 animal.legs = animal.legs + 1920 draft.bear = animal921 draft.eagle = animal922 draft.cow = animal923 draft.kiddo = animal924 },925 listener926 )Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 927–938why, one step back927 expect(next).toEqual({928 bear: {legs: 5},929 eagle: {legs: 5},930 cow: {legs: 5},931 kiddo: {legs: 5}932 })933 expect(next.bear).toBe(next.cow)934 expect(next.kiddo).toBe(next.cow)935 })936937 if (useProxies)938 it("should not allow changing prototype", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 939–947why, one step back939 produce(940 {},941 draft => {942 expect(() =>943 Object.setPrototypeOf(draft, Array)944 ).toThrow(/does not support `setPrototype/)945 },946 listener947 )Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 948–949why, one step back948 })949Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
line 950why, one step back950 it("'in' should work", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 951–955why, one step back951 produce(952 createBaseState(),953 draft => {954 expect("anArray" in draft).toBe(true)955 expect(Reflect.has(draft, "anArray")).toBe(true)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 956why, one step back956Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 957–958why, one step back957 expect("bla" in draft).toBe(false)958 expect(Reflect.has(draft, "bla")).toBe(false)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 959why, one step back959Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 960–963why, one step back960 expect(0 in draft.anArray).toBe(true)961 expect("0" in draft.anArray).toBe(true)962 expect(Reflect.has(draft.anArray, 0)).toBe(true)963 expect(Reflect.has(draft.anArray, "0")).toBe(true)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 964why, one step back964Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 965–971why, one step back965 expect(17 in draft.anArray).toBe(false)966 expect("17" in draft.anArray).toBe(false)967 expect(Reflect.has(draft.anArray, 17)).toBe(false)968 expect(Reflect.has(draft.anArray, "17")).toBe(false)969 },970 listener971 )Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
lines 972–975why, one step back972 })973974 it("'this' should work - 1", () => {975 const base = {x: 3}Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 976–982why, one step back976 const next1 = produce(977 base,978 function() {979 this.x = 4980 },981 listener982 )Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
lines 983–994why, one step back983 expect(next1).not.toBe(base)984 expect(next1.x).toBe(4)985 })986987 it("'this' should work - 1", () => {988 const base = {x: 3}989 const incrementor = produce(function() {990 this.x = 4991 })992 const next1 = incrementor(base)993 expect(next1).not.toBe(base)994 expect(next1.x).toBe(4)Making several improvements at onceMichel Weststratebefore adoption1116ccf
lines 995–996why, one step back995 })996added test for #89Michel Weststratebefore adoptionf68a31a
lines 997–1005why, one step back997 it("issue #89", () => {998 const state = {999 users: {1000 existingID: {1001 loading: false,1002 value: "val"1003 }1004 }1005 }Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1006–1015why, one step back1006 const nextState = produce(1007 state,1008 draft => {1009 draft.users.existingID = {1010 ...draft.users.existingID,1011 loading: true1012 }1013 },1014 listener1015 )added test for #89Michel Weststratebefore adoptionf68a31a
lines 1016–1023why, one step back1016 expect(nextState).toEqual({1017 users: {1018 existingID: {1019 loading: true,1020 value: "val"1021 }1022 }1023 })added some additional tests to verify solution correctness of #86Michel Weststratebefore adoption681b162
lines 1024–1027why, one step back1024 })10251026 it("processes with lodash.set", () => {1027 const base = [{id: 1, a: 1}]Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1028–1034why, one step back1028 const result = produce(1029 base,1030 draft => {1031 lodash.set(draft, "[0].a", 2)1032 },1033 listener1034 )added some additional tests to verify solution correctness of #86Michel Weststratebefore adoption681b162
lines 1035–1040why, one step back1035 expect(base[0].a).toEqual(1)1036 expect(result[0].a).toEqual(2)1037 })10381039 it("processes with lodash.find", () => {1040 const base = [{id: 1, a: 1}]Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1041–1048why, one step back1041 const result = produce(1042 base,1043 draft => {1044 const obj1 = lodash.find(draft, {id: 1})1045 lodash.set(obj1, "a", 2)1046 },1047 listener1048 )added some additional tests to verify solution correctness of #86Michel Weststratebefore adoption681b162
lines 1049–1050why, one step back1049 expect(base[0].a).toEqual(1)1050 expect(result[0].a).toEqual(2)Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 1051–1054why, one step back1051 })10521053 it("can produce from no state", () => {1054 expect(Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1055–1062why, one step back1055 produce(1056 3,1057 draft => {1058 expect(draft).toBe(3)1059 return 51060 },1061 listener1062 )Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 1063–1067why, one step back1063 ).toBe(5)1064 })10651066 it("can return something new ", () => {1067 const base = {x: 3}Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1068–1074why, one step back1068 const res = produce(1069 base,1070 draft => {1071 return {x: draft.x + 1}1072 },1073 listener1074 )Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 1075–1080why, one step back1075 expect(res).not.toBe(base)1076 expect(res).toEqual({x: 4})1077 })10781079 it("can return the draft new ", () => {1080 const base = {x: 3}Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1081–1088why, one step back1081 const res = produce(1082 base,1083 draft => {1084 draft.x = 41085 return draft1086 },1087 listener1088 )Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 1089–1095why, one step back1089 expect(res).not.toBe(base)1090 expect(res).toEqual({x: 4})1091 })10921093 it("should throw if modifying the draft and returning something new", () => {1094 const base = {x: 3}1095 expect(() => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1096–1103why, one step back1096 produce(1097 base,1098 draft => {1099 draft.x = 41100 return {x: 5}1101 },1102 listener1103 )Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 1104why, one step back1104 }).toThrow(/An immer producer returned a new value/)added test for #89Michel Weststratebefore adoptionf68a31a
lines 1105–1106why, one step back1105 })1106Fixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df
line 1107why, one step back1107 it("should fix #116", () => {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1108–1115why, one step back1108 const nextState = produce(1109 [1, 2, 3],1110 s => {1111 s.pop()1112 s.push(100)1113 },1114 listener1115 )Fixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df
lines 1116–1118why, one step back1116 expect(nextState).toEqual([1, 2, 100])1117 })1118Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
lines 1119–1120why, one step back1119 it("should fix #117", () => {1120 const reducer = (state, action) =>Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1121–1132why, one step back1121 produce(1122 state,1123 draft => {1124 switch (action.type) {1125 case "SET_STARTING_DOTS":1126 return draft.availableStartingDots.map(a => a)1127 default:1128 break1129 }1130 },1131 listener1132 )Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
lines 1133–1147why, one step back1133 const base = {1134 availableStartingDots: [1135 {dots: 4, count: 1},1136 {dots: 3, count: 2},1137 {dots: 2, count: 3},1138 {dots: 1, count: 4}1139 ]1140 }1141 const next = reducer(base, {type: "SET_STARTING_DOTS"})1142 expect(next).toEqual(base.availableStartingDots)1143 expect(next).not.toBe(base.availableStartingDots)1144 })11451146 it("should fix #117 - 2", () => {1147 const reducer = (state, action) =>Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1148–1163why, one step back1148 produce(1149 state,1150 draft => {1151 switch (action.type) {1152 case "SET_STARTING_DOTS":1153 return {1154 dots: draft.availableStartingDots.map(1155 a => a1156 )1157 }1158 default:1159 break1160 }1161 },1162 listener1163 )Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
lines 1164–1175why, one step back1164 const base = {1165 availableStartingDots: [1166 {dots: 4, count: 1},1167 {dots: 3, count: 2},1168 {dots: 2, count: 3},1169 {dots: 1, count: 4}1170 ]1171 }1172 const next = reducer(base, {type: "SET_STARTING_DOTS"})1173 expect(next).toEqual({dots: base.availableStartingDots})1174 })1175Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
lines 1176–1177why, one step back1176 it("should return an unmodified primitive baseState (#148)", () => {1177 const baseState = "some string"Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1178–1184why, one step back1178 const nextState = produce(1179 baseState,1180 () => {1181 /* no modification */1182 },1183 listener1184 )Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
lines 1185–1189why, one step back1185 expect(nextState).toBe(baseState)1186 })11871188 it("should return an unmodified null baseState (#148)", () => {1189 const baseState = nullMerge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1190–1196why, one step back1190 const nextState = produce(1191 baseState,1192 () => {1193 /* no modification */1194 },1195 listener1196 )Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
line 1197why, one step back1197 expect(nextState).toBe(baseState)Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1198–1273why, one step back1198 })11991200 it("should not detect noop assignments - 0", () => {1201 const baseState = {x: {y: 3}}1202 const nextState = produce(1203 baseState,1204 d => {1205 const a = d.x1206 d.x = a1207 },1208 listener1209 )1210 expect(nextState).toBe(baseState)1211 })12121213 it("should not detect noop assignments - 1", () => {1214 const baseState = {x: {y: 3}}1215 const nextState = produce(1216 baseState,1217 d => {1218 const a = d.x1219 d.x = 41220 d.x = a1221 },1222 listener1223 )1224 // Ideally, this should actually be the same instances1225 // but this would be pretty expensive to detect,1226 // so we don't atm1227 expect(nextState).not.toBe(baseState)1228 })12291230 it("should not detect noop assignments - 2", () => {1231 const baseState = {x: {y: 3}}1232 const nextState = produce(1233 baseState,1234 d => {1235 const a = d.x1236 const stuff = a.y + 31237 d.x = 41238 d.x = a1239 },1240 listener1241 )1242 // Ideally, this should actually be the same instances1243 // but this would be pretty expensive to detect,1244 // so we don't atm1245 expect(nextState).not.toBe(baseState)1246 })12471248 it("should not detect noop assignments - 3", () => {1249 const baseState = {x: 3}1250 const nextState = produce(1251 baseState,1252 d => {1253 d.x = 31254 },1255 listener1256 )1257 expect(nextState).toBe(baseState)1258 })12591260 it("should not detect noop assignments - 4", () => {1261 const baseState = {x: 3}1262 const nextState = produce(1263 baseState,1264 d => {1265 d.x = 41266 d.x = 31267 },1268 listener1269 )1270 // Ideally, this should actually be the same instances1271 // but this would be pretty expensive to detect,1272 // so we don't atm1273 expect(nextState).not.toBe(baseState)Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
lines 1274–1275why, one step back1274 })1275added `np` for easier releasingMichel Weststratebefore adoption23bfaa2
lines 1276–1279why, one step back1276 it("immer should have no dependencies", () => {1277 expect(require("../package.json").dependencies).toEqual(undefined)1278 })1279Emphasized in readme that state should be a treeMichel Weststratebefore adoption211f5cf
lines 1280–1287why, one step back1280 it("#174", () => {1281 const nextState = produce([1, 2, 3], s => {1282 s.pop()1283 s.push(100)1284 })1285 expect(nextState).toEqual([1, 2, 100])1286 })1287Tried to reproduce #195Michel Weststratebefore adoption4f27a93
lines 1288–1302why, one step back1288 it("#195 should be able to find items", () => {1289 const state = {1290 items: [1291 {1292 id: 0,1293 task: "drink milk"1294 },1295 {id: 1, task: "eat cookie"}1296 ]1297 }1298 produce(state, draft => {1299 expect(draft.items.find(({id}) => id === 1).task).toBe(1300 "eat cookie"1301 )1302 })Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 1303–1304why, one step back1303 })1304add tests for function as base statealeclarsonbefore adoption4613b06
lines 1305–1313why, one step back1305 it("allows a function as the base state", () => {1306 let fn = () => {}1307 expect(1308 produce(fn, draft => {1309 expect(fn).toBe(draft)1310 })1311 ).toBe(fn)1312 })1313Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 1314–1328why, one step back1314 it("cannot return and produce undefined!", () => {1315 const base = 31316 expect(produce(base, () => 4)).toBe(4)1317 expect(produce(base, () => null)).toBe(null)1318 expect(produce(base, () => undefined)).toBe(3)1319 expect(produce(base, () => {})).toBe(3)1320 expect(produce(base, () => nothing)).toBe(undefined)13211322 expect(produce({}, () => undefined)).toEqual({})1323 expect(produce({}, () => nothing)).toBe(undefined)1324 expect(produce(3, () => nothing)).toBe(undefined)13251326 expect(produce(() => undefined)({})).toEqual({})1327 expect(produce(() => nothing)({})).toBe(undefined)1328 expect(produce(() => nothing)(3)).toBe(undefined)Tried to reproduce #195Michel Weststratebefore adoption4f27a93
lines 1329–1330why, one step back1329 })1330Readme improvementsMichel Weststratebefore adoptiona1f5c6a
lines 1331–1355why, one step back1331 afterEach(() => {1332 expect(baseState).toBe(origBaseState)1333 expect(baseState).toEqual(createBaseState())1334 })13351336 function createBaseState() {1337 const data = {1338 anArray: [3, 2, {c: 3}, 1],1339 aProp: "hi",1340 anObject: {1341 nested: {1342 yummie: true1343 },1344 coffee: false1345 }1346 }1347 return freeze ? deepFreeze(data) : data1348 }1349 })1350}13511352function enumerableOnly(x) {1353 // this can be done better...1354 return JSON.parse(JSON.stringify(x))1355}