test: improve tests with Immer class
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Using global state in unit tests is usually a bad idea, which the Immer class lets us avoid. Also, we can monkey-patch the `produce` function in order to transparently append the patch listener to the argument list of all `produce` calls. This makes the tests easier to read, since we can avoid an extra level of indentation added by Prettier.
authored by aleclarson · committed by aleclarson · parent a4a1e44 · Browse files · 1 file·+377 added−627 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+377 added−627 removed
- tests:+377 added−627 removed
Provenance
- Commit
ddc9b40- Tree
c312080- Author
aleclarson <alec.stanford.larson@gmail.com>- Committer
aleclarson <alec.stanford.larson@gmail.com>- Parent
a4a1e44
1 file changed+377 added−627 removed
modified:__tests__/base.js+377 added-627 removed
1 "use strict"2-import produce, {3- setAutoFreeze,4- setUseProxies,5- nothing,6- isDraft7-} from "../src/index"2+import {Immer, nothing, isDraft} from "../src/index"3 import {shallowCopy} from "../src/common"4 import deepFreeze from "deep-freeze"5 import cloneDeep from "lodash.clonedeep"6 import * as lodash from "lodash"6 unmodified lines13 runBaseTest("es5 (no freeze)", false, false)14 runBaseTest("es5 (autofreeze)", false, true)15 runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)16 22-function runBaseTest(name, useProxies, freeze, useListener) {17+function runBaseTest(name, useProxies, autoFreeze, useListener) {18+ const immer = new Immer({19+ useProxies,20+ autoFreeze21+ })22+23+ // make sure logic doesn't break when listener is attached24+ const listener = useListener ? function() {} : undefined25+ const produce = (...args) =>26+ typeof args[1] === "function" && args.length < 327+ ? immer.produce(...args, listener)28+ : immer.produce(...args)29+30 describe(`base functionality - ${name}`, () => {31 let baseState32 let origBaseState26- // make sure logic doesn't break when listener is attached27- let listener = useListener ? function() {} : undefined33 34 beforeEach(() => {30- setAutoFreeze(freeze)31- setUseProxies(useProxies)35 origBaseState = baseState = createBaseState()36 })37 38 it("should return the original without modifications", () => {36- const nextState = produce(baseState, () => {}, listener)39+ const nextState = produce(baseState, () => {})40 expect(nextState).toBe(baseState)41 })42 43 it("should return the original without modifications when reading stuff", () => {41- const nextState = produce(42- baseState,43- s => {44- expect(s.aProp).toBe("hi")45- expect(s.anObject.nested).toMatchObject({yummie: true})46- },47- listener48- )44+ const nextState = produce(baseState, s => {45+ expect(s.aProp).toBe("hi")46+ expect(s.anObject.nested).toMatchObject({yummie: true})47+ })48 expect(nextState).toBe(baseState)49 })50 51 it("should return a copy when modifying stuff", () => {53- const nextState = produce(54- baseState,55- s => {56- s.aProp = "hello world"57- },58- listener59- )52+ const nextState = produce(baseState, s => {53+ s.aProp = "hello world"54+ })55 expect(nextState).not.toBe(baseState)56 expect(baseState.aProp).toBe("hi")57 expect(nextState.aProp).toBe("hello world")58 // structural sharing?59 expect(nextState.nested).toBe(baseState.nested)60 })61 62 it("should preserve type", () => {68- const nextState = produce(69- baseState,70- s => {71- expect(Object.getPrototypeOf(s)).toBe(Object.prototype)72- expect(Array.isArray(s.anArray)).toBe(true)73- s.anArray.push(3)74- s.aProp = "hello world"75- expect(Object.getPrototypeOf(s)).toBe(Object.prototype)76- expect(Array.isArray(s.anArray)).toBe(true)77- },63+ const nextState = produce(baseState, s => {64+ expect(Object.getPrototypeOf(s)).toBe(Object.prototype)65+ expect(Array.isArray(s.anArray)).toBe(true)66+ s.anArray.push(3)67+ s.aProp = "hello world"68+ expect(Object.getPrototypeOf(s)).toBe(Object.prototype)69+ expect(Array.isArray(s.anArray)).toBe(true)70+ })78- listener79- )71 expect(Object.getPrototypeOf(nextState)).toBe(Object.prototype)72 expect(Array.isArray(nextState.anArray)).toBe(true)73 })74 75 it("deep change bubbles up", () => {85- const nextState = produce(86- baseState,87- s => {88- s.anObject.nested.yummie = false89- },90- listener91- )76+ const nextState = produce(baseState, s => {77+ s.anObject.nested.yummie = false78+ })79 expect(nextState).not.toBe(baseState)80 expect(nextState.anObject).not.toBe(baseState.anObject)81 expect(baseState.anObject.nested.yummie).toBe(true)82 expect(nextState.anObject.nested.yummie).toBe(false)83 expect(nextState.anArray).toBe(baseState.anArray)84 })85 86 it("can add props", () => {100- const nextState = produce(101- baseState,102- s => {103- s.anObject.cookie = {tasty: true}104- },105- listener106- )87+ const nextState = produce(baseState, s => {88+ s.anObject.cookie = {tasty: true}89+ })90 expect(nextState).not.toBe(baseState)91 expect(nextState.anObject).not.toBe(baseState.anObject)92 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)93 expect(nextState.anObject.cookie).toEqual({tasty: true})94 })95 96 it("can delete props", () => {114- const nextState = produce(115- baseState,116- s => {117- delete s.anObject.nested118- },119- listener120- )97+ const nextState = produce(baseState, s => {98+ delete s.anObject.nested99+ })100 expect(nextState).not.toBe(baseState)101 expect(nextState.anObject).not.toBe(baseState.anObject)102 expect(nextState.anObject.nested).toBe(undefined)103 })104 105 // Found by: https://github.com/mweststrate/immer/pull/267106 it("can delete props added in the producer", () => {128- const nextState = produce(129- baseState,130- s => {131- s.anObject.test = true132- delete s.anObject.test133- },134- listener135- )107+ const nextState = produce(baseState, s => {108+ s.anObject.test = true109+ delete s.anObject.test110+ })111 if (useProxies) {112 expect(nextState).not.toBe(baseState)113 expect(nextState).toEqual(baseState)114 } else {2 unmodified lines117 }118 })119 120 it("ignores single non-modification", () => {146- const nextState = produce(147- baseState,148- s => {149- s.aProp = "hi"150- },151- listener152- )121+ const nextState = produce(baseState, s => {122+ s.aProp = "hi"123+ })124 expect(nextState).toBe(baseState)125 })126 127 it("processes single modification", () => {157- const nextState = produce(158- baseState,159- s => {160- s.aProp = "hello"161- s.aProp = "hi"162- },163- listener164- )128+ const nextState = produce(baseState, s => {129+ s.aProp = "hello"130+ s.aProp = "hi"131+ })132 expect(nextState).not.toBe(baseState)133 expect(nextState).toEqual(baseState)134 })135 20 unmodified lines156 base.x = 1157 base.y = Object.create(null)158 base.y.y = 2159 expect(base.__proto__).toBe(undefined)193- const next = produce(194- base,195- draft => {196- draft.y.z = 3197- draft.y.y++198- draft.x++199- },200- listener201- )160+ const next = produce(base, draft => {161+ draft.y.z = 3162+ draft.y.y++163+ draft.x++164+ })165 expect(next).toEqual({166 x: 2,167 y: {y: 3, z: 3}168 })169 expect(next.__proto__).toBe(undefined)170 })171 172 it("should support reading arrays", () => {210- const nextState = produce(211- baseState,212- s => {213- s.anArray.slice()214- },215- listener216- )173+ const nextState = produce(baseState, s => {174+ s.anArray.slice()175+ })176 expect(nextState.anArray).toBe(baseState.anArray)177 expect(nextState).toBe(baseState)178 })179 180 it("should support changing arrays", () => {222- const nextState = produce(223- baseState,224- s => {225- s.anArray[3] = true226- },227- listener228- )181+ const nextState = produce(baseState, s => {182+ s.anArray[3] = true183+ })184 expect(nextState).not.toBe(baseState)185 expect(nextState.anArray).not.toBe(baseState.anArray)186 expect(nextState.anArray[3]).toEqual(true)187 })188 189 it("should support changing arrays - 2", () => {235- const nextState = produce(236- baseState,237- s => {238- s.anArray.splice(1, 1, "a", "b")239- },240- listener241- )190+ const nextState = produce(baseState, s => {191+ s.anArray.splice(1, 1, "a", "b")192+ })193 expect(nextState).not.toBe(baseState)194 expect(nextState.anArray).not.toBe(baseState.anArray)195 196 expect(enumerableOnly(nextState.anArray)).toEqual([5 unmodified lines202 ])203 })204 205 it("can delete array items", () => {255- const nextState = produce(256- baseState,257- s => {258- s.anArray.length = 3259- },260- listener261- )206+ const nextState = produce(baseState, s => {207+ s.anArray.length = 3208+ })209 expect(nextState).not.toBe(baseState)210 expect(nextState.anObject).toBe(baseState.anObject)211 expect(nextState.anArray).not.toBe(baseState.anArray)212 expect(nextState.anArray).toEqual([3, 2, {c: 3}])213 })214 215 it("should support sorting arrays", () => {269- const nextState = produce(270- baseState,271- s => {272- s.anArray[2].c = 4273- s.anArray.sort()274- s.anArray[3].c = 5275- },276- listener277- )216+ const nextState = produce(baseState, s => {217+ s.anArray[2].c = 4218+ s.anArray.sort()219+ s.anArray[3].c = 5220+ })221 expect(nextState).not.toBe(baseState)222 expect(nextState.anArray).not.toBe(baseState.anArray)223 expect(enumerableOnly(nextState.anArray)).toEqual([1, 2, 3, {c: 5}])224 })225 226 it("should expose property descriptors", () => {284- const nextState = produce(285- [],286- s => {287- expect(Object.getOwnPropertyDescriptor(s, 0)).toBe(288- undefined289- )290- s.unshift("x")291- expect(Object.getOwnPropertyDescriptor(s, 0)).toEqual({292- configurable: true,293- enumerable: true,294- value: "x",295- writable: true296- })297- expect(s.length).toBe(1)298- expect(s[0] === "x").toBe(true)299- },227+ const nextState = produce([], s => {228+ expect(Object.getOwnPropertyDescriptor(s, 0)).toBe(undefined)229+ s.unshift("x")230+ expect(Object.getOwnPropertyDescriptor(s, 0)).toEqual({231+ configurable: true,232+ enumerable: true,233+ value: "x",234+ writable: true235+ })236+ expect(s.length).toBe(1)237+ expect(s[0] === "x").toBe(true)238+ })300- listener301- )239 expect(nextState).toEqual(["x"])240 expect(Object.getOwnPropertyDescriptor(nextState, 0)).toEqual({304- configurable: !freeze,241+ configurable: !autoFreeze,242 enumerable: true,243 value: "x",307- writable: !freeze244+ writable: !autoFreeze245 })246 })247 248 it("should support sorting arrays - 2", () => {312- const nextState = produce(313- baseState,314- s => {315- s.anArray.unshift("x")316- s.anArray[3].c = 4317- s.anArray.sort()318- s.anArray[3].c = 5319- s.anArray.unshift("y")320- },321- listener322- )249+ const nextState = produce(baseState, s => {250+ s.anArray.unshift("x")251+ s.anArray[3].c = 4252+ s.anArray.sort()253+ s.anArray[3].c = 5254+ s.anArray.unshift("y")255+ })256 expect(nextState).not.toBe(baseState)257 expect(nextState.anArray).not.toBe(baseState.anArray)258 expect(enumerableOnly(nextState.anArray)).toEqual([259 "y",5 unmodified lines265 ])266 })267 268 it("should updating inside arrays", () => {336- const nextState = produce(337- baseState,338- s => {339- s.anArray[2].test = true340- },341- listener342- )269+ const nextState = produce(baseState, s => {270+ s.anArray[2].test = true271+ })272 expect(nextState).not.toBe(baseState)273 expect(nextState.anArray).not.toBe(baseState.anArray)274 expect(nextState.anArray).toEqual([3, 2, {c: 3, test: true}, 1])275 })276 277 it("can rename nested objects (no changes)", () => {349- const nextState = produce(350- baseState,351- s => {352- const obj = s.anObject353- delete s.anObject354- s.renamed = obj355- },278+ const nextState = produce(baseState, s => {279+ const obj = s.anObject280+ delete s.anObject281+ s.renamed = obj282+ })356- listener357- )283 expect(nextState).not.toBe(baseState)284 expect(nextState.anArray).toBe(baseState.anArray)285 expect(nextState.renamed.nested).toBe(baseState.anObject.nested)286 expect(enumerableOnly(nextState)).toEqual({10 unmodified lines297 298 // 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)", () => {376- const nextState = produce(377- baseState,378- s => {379- const obj = s.anObject380- delete s.anObject301+ const nextState = produce(baseState, s => {302+ const obj = s.anObject303+ delete s.anObject304 382- obj.coffee = true // change383- obj.nested.yummy = true // add384- delete obj.nested.yummie // delete305+ obj.coffee = true // change306+ obj.nested.yummy = true // add307+ delete obj.nested.yummie // delete308 386- s.renamed = obj387- },388- listener389- )309+ s.renamed = obj310+ })311 expect(nextState).not.toBe(baseState)312 expect(nextState.anArray).toBe(baseState.anArray)313 expect(nextState.renamed.nested).not.toBe(baseState.anObject.nested)314 expect(enumerableOnly(nextState)).toEqual({8 unmodified lines323 })324 })325 326 it("can nest a draft in a new object (no changes)", () => {406- const nextState = produce(407- baseState,408- s => {409- s.foo = {bar: s.anObject}410- delete s.anObject411- },412- listener413- )327+ const nextState = produce(baseState, s => {328+ s.foo = {bar: s.anObject}329+ delete s.anObject330+ })331 expect(nextState).not.toBe(baseState)332 expect(nextState.foo.bar).toBe(baseState.anObject)333 })334 335 it("can nest a draft in a new object (with changes)", () => {419- const nextState = produce(420- baseState,421- s => {422- const obj = s.anObject423- delete s.anObject336+ const nextState = produce(baseState, s => {337+ const obj = s.anObject338+ delete s.anObject339 425- obj.coffee = true // change426- obj.nested.yummy = true // add427- delete obj.nested.yummie // delete340+ obj.coffee = true // change341+ obj.nested.yummy = true // add342+ delete obj.nested.yummie // delete343 429- s.foo = {bar: obj}430- },431- listener432- )344+ s.foo = {bar: obj}345+ })346 expect(nextState).not.toBe(baseState)347 expect(nextState.foo.bar).not.toBe(baseState.anObject)348 expect(nextState.foo).toEqual({349 bar: {3 unmodified lines353 })354 })355 356 it("should allow setting to undefined a defined draft property", () => {444- const nextState = produce(445- baseState,446- s => {447- s.aProp = undefined448- },449- listener450- )357+ const nextState = produce(baseState, s => {358+ s.aProp = undefined359+ })360 expect(nextState).not.toBe(baseState)361 expect(baseState.aProp).toBe("hi")362 expect(nextState.aProp).toBe(undefined)363 })1 unmodified line365 // 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 proxy460- const nextState = produce(461- baseState,462- s => {463- proxy = s464- s.aProp = "hello"465- },466- listener467- )369+ const nextState = produce(baseState, s => {370+ proxy = s371+ s.aProp = "hello"372+ })373 expect(nextState).not.toBe(baseState)374 expect(baseState.aProp).toBe("hi")375 expect(nextState.aProp).toBe("hello")376 11 unmodified lines388 }389 390 it("should revoke the proxy of the baseState after immer function is executed - 2", () => {391 let proxy487- const nextState = produce(488- baseState,489- s => {490- proxy = s.anObject491- },492- listener493- )392+ const nextState = produce(baseState, s => {393+ proxy = s.anObject394+ })395 expect(nextState).toBe(baseState)396 expect(() => {397 // In ES5 implemenation only protects existing props, but alas..398 proxy.coffee = "Hallo"3 unmodified lines402 }).toThrowError(/revoked/)403 })404 405 it("should reflect all changes made in the draft immediately", () => {505- produce(506- baseState,507- draft => {508- draft.anArray[0] = 5509- draft.anArray.unshift("test")510- // sliced here; jest will also compare non-enumerable keys, which would include the immer Symbols511- expect(draft.anArray.slice()).toMatchObject([512- "test",513- 5,514- 2,515- {c: 3},516- 1517- ])518- draft.stuffz = "coffee"519- expect(draft.stuffz).toBe("coffee")520- },406+ produce(baseState, draft => {407+ draft.anArray[0] = 5408+ draft.anArray.unshift("test")409+ // sliced here; jest will also compare non-enumerable keys, which would include the immer Symbols410+ expect(draft.anArray.slice()).toMatchObject([411+ "test",412+ 5,413+ 2,414+ {c: 3},415+ 1416+ ])417+ draft.stuffz = "coffee"418+ expect(draft.stuffz).toBe("coffee")419+ })521- listener522- )420 })421 422 it("should be able to get property descriptors from objects", () => {526- produce(527- {a: 1},528- draft => {529- expect("a" in draft).toBe(true)530- expect("b" in draft).toBe(false)531- expect(532- Reflect.ownKeys(draft).filter(533- x => typeof x === "string"423+ produce({a: 1}, draft => {424+ expect("a" in draft).toBe(true)425+ expect("b" in draft).toBe(false)426+ expect(427+ Reflect.ownKeys(draft).filter(x => typeof x === "string")428+ ).toEqual(["a"])534- )535- ).toEqual(["a"])429 537- expect(538- Object.getOwnPropertyDescriptor(draft, "a")539- ).toMatchObject({540- configurable: true,541- enumerable: true542- })543- draft.a = 2544- expect(545- Object.getOwnPropertyDescriptor(draft, "a")546- ).toMatchObject({547- configurable: true,548- enumerable: true549- })550- expect(551- Object.getOwnPropertyDescriptor(draft, "b")552- ).toBeUndefined()553- draft.b = 2554- expect(555- Object.getOwnPropertyDescriptor(draft, "b")556- ).toMatchObject({557- configurable: true,558- enumerable: true559- })560- expect("a" in draft).toBe(true)561- expect("b" in draft).toBe(true)562- expect(563- Reflect.ownKeys(draft).filter(564- x => typeof x === "string"565- )566- ).toEqual(["a", "b"])567- },568- listener569- )430+ expect(431+ Object.getOwnPropertyDescriptor(draft, "a")432+ ).toMatchObject({433+ configurable: true,434+ enumerable: true435+ })436+ draft.a = 2437+ expect(438+ Object.getOwnPropertyDescriptor(draft, "a")439+ ).toMatchObject({440+ configurable: true,441+ enumerable: true442+ })443+ expect(444+ Object.getOwnPropertyDescriptor(draft, "b")445+ ).toBeUndefined()446+ draft.b = 2447+ expect(448+ Object.getOwnPropertyDescriptor(draft, "b")449+ ).toMatchObject({450+ configurable: true,451+ enumerable: true452+ })453+ expect("a" in draft).toBe(true)454+ expect("b" in draft).toBe(true)455+ expect(456+ Reflect.ownKeys(draft).filter(x => typeof x === "string")457+ ).toEqual(["a", "b"])458+ })459 })460 461 it("should be able to get property descriptors from arrays", () => {573- produce(574- [1],575- draft => {576- expect(0 in draft).toBe(true)577- expect(1 in draft).toBe(false)578- expect("0" in draft).toBe(true)579- expect("1" in draft).toBe(false)580- expect(length in draft).toBe(true)581- expect(582- Reflect.ownKeys(draft).filter(583- x => typeof x === "string"584- )585- ).toEqual(["0", "length"])462+ produce([1], draft => {463+ expect(0 in draft).toBe(true)464+ expect(1 in draft).toBe(false)465+ expect("0" in draft).toBe(true)466+ expect("1" in draft).toBe(false)467+ expect(length in draft).toBe(true)468+ expect(469+ Reflect.ownKeys(draft).filter(x => typeof x === "string")470+ ).toEqual(["0", "length"])471 587- expect(588- Object.getOwnPropertyDescriptor(draft, "length")589- ).toMatchObject({590- configurable: false,591- enumerable: false592- })593- draft[0] = 2594- expect(595- Object.getOwnPropertyDescriptor(draft, 0)596- ).toMatchObject({472+ expect(473+ Object.getOwnPropertyDescriptor(draft, "length")474+ ).toMatchObject({475+ configurable: false,476+ enumerable: false477+ })478+ draft[0] = 2479+ expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(480+ {481 configurable: true,482 enumerable: true599- })600- expect(601- Object.getOwnPropertyDescriptor(draft, 0)602- ).toMatchObject({483+ }484+ )485+ expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(486+ {487 configurable: true,488 enumerable: true605- })606- expect(607- Object.getOwnPropertyDescriptor(draft, 1)608- ).toBeUndefined()609- draft[1] = 2610- expect(611- Object.getOwnPropertyDescriptor(draft, 1)612- ).toMatchObject({489+ }490+ )491+ expect(492+ Object.getOwnPropertyDescriptor(draft, 1)493+ ).toBeUndefined()494+ draft[1] = 2495+ expect(Object.getOwnPropertyDescriptor(draft, 1)).toMatchObject(496+ {497 configurable: true,498 enumerable: true615- })616- expect(617- Reflect.ownKeys(draft).filter(618- x => typeof x === "string"619- )620- ).toEqual(["0", "1", "length"])621- },622- listener623- )499+ }500+ )501+ expect(502+ Reflect.ownKeys(draft).filter(x => typeof x === "string")503+ ).toEqual(["0", "1", "length"])504+ })505 })506 507 if (useProxies === true) {508 it("should not be possible to set property descriptors", () => {509 expect(() => {629- produce(630- {},631- draft => {632- Object.defineProperty(draft, "xx", {633- enumerable: true,634- writeable: true,635- value: 2636- })637- },510+ produce({}, draft => {511+ Object.defineProperty(draft, "xx", {512+ enumerable: true,513+ writeable: true,514+ value: 2515+ })516+ })638- listener639- )517 }).toThrowError(/not support/)518 })519 520 it("should not be possible to add properties to arrays", () => {29 unmodified lines550 })551 552 it("should not throw error, see #53 - 1", () => {553 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}677- const result = produce(678- base,679- draft => {680- draft.arr = draft.arr.filter(item => item.count > 2)681- },682- listener683- )554+ const result = produce(base, draft => {555+ draft.arr = draft.arr.filter(item => item.count > 2)556+ })557 expect(result.arr[0].count).toEqual(3)558 expect(result).toEqual({559 arr: [{count: 3}]560 })1 unmodified line562 })563 564 it("should not throw error, see #53 - 2", () => {565 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}693- const result = produce(694- base,695- draft => {696- draft.newArr = draft.arr.filter(item => item.count > 2)697- },698- listener699- )566+ const result = produce(base, draft => {567+ draft.newArr = draft.arr.filter(item => item.count > 2)568+ })569 expect(result.newArr[0].count).toEqual(3)570 expect(result.arr).toBe(base.arr)571 expect(result).toEqual({572 arr: [18 unmodified lines591 })592 593 it("should not throw error, see #53 - 3", () => {594 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}726- const result = produce(727- base,728- draft => {729- draft.newArr = draft.arr.filter(item => item.count > 2)730- delete draft.arr731- },595+ const result = produce(base, draft => {596+ draft.newArr = draft.arr.filter(item => item.count > 2)597+ delete draft.arr598+ })732- listener733- )599 expect(result.newArr[0].count).toEqual(3)600 expect(result).toEqual({601 newArr: [{count: 3}]602 })1 unmodified line604 })605 606 it("should not throw error, see #53 - 4", () => {607 const base = {bear: {age: 10}}743- const result = produce(744- base,745- draft => {746- draft.bear.legs = 4747- draft.room = {elephant: {kiddo: draft.bear}}748- },608+ const result = produce(base, draft => {609+ draft.bear.legs = 4610+ draft.room = {elephant: {kiddo: draft.bear}}611+ })749- listener750- )612 expect(result).toEqual({613 bear: {age: 10, legs: 4},614 room: {elephant: {kiddo: {age: 10, legs: 4}}}615 })117 unmodified lines733 // 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}}876- const next = produce(877- bear,878- draft => {879- const paw2 = produce(bear.paw, draft => {880- draft.honey = false881- })882- expect(paw2.honey).toBe(false)883- expect(draft.paw.honey).toBe(true) // effects should not be visible outside884- },737+ 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+ })885- listener886- )744 expect(next.paw.honey).toBe(true)745 expect(next).toBe(bear)746 })747 748 it("returns a normal object", () => {749 const bear = {paw: {honey: true}}893- const next = produce(894- bear,895- draft => {896- const paw2 = produce(bear.paw, draft => {897- draft.honey = false898- })899- expect(paw2.honey).toBe(false)900- expect(draft.paw.honey).toBe(true)901- draft.paw = paw2902- expect(draft.paw.honey).toBe(false)903- },750+ 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+ })904- listener905- )759 expect(next.paw.honey).toBe(false)760 expect(next).not.toBe(bear)761 })762 })763 764 describe("with a draft object", () => {765 it("always reuses the draft", () => {766 const bear = {paw: {honey: true}}914- const next = produce(915- bear,916- bear => {917- const paw2 = produce(bear.paw, paw => {918- expect(paw).toBe(bear.paw)919- paw.honey = false920- })921- expect(paw2).toBe(bear.paw)922- expect(paw2.honey).toBe(false)923- expect(bear.paw.honey).toBe(false)924- },767+ 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+ })925- listener926- )776 expect(next.paw.honey).toBe(false)777 expect(next).not.toBe(bear)778 })779 })2 unmodified lines782 it("should not try to change immutable data, see #66", () => {783 const user = require("./test-data")784 785 const base = {}937- const next = produce(938- base,939- draft => {940- draft.user = user941- },942- listener943- )786+ const next = produce(base, draft => {787+ draft.user = user788+ })789 expect(next.user).toBe(user)790 expect(next).not.toBe(base)791 expect(next.user).toEqual(user)792 })1 unmodified line794 it("should not try to change immutable data, see #66 - 2", () => {795 const user = deepFreeze(cloneDeep(require("./test-data")))796 797 const base = {}953- const next = produce(954- base,955- draft => {956- draft.user = user957- },958- listener959- )798+ const next = produce(base, draft => {799+ draft.user = user800+ })801 expect(next.user).toBe(user)802 expect(next).not.toBe(base)803 expect(next.user).toEqual(user)804 })805 806 it("should structurally share identical objects in the tree", () => {807 const base = {bear: {legs: 4}, eagle: {legs: 3}}967- const next = produce(968- base,969- draft => {970- const animal = draft.bear971- animal.legs = animal.legs + 1972- draft.bear = animal973- draft.eagle = animal974- draft.cow = animal975- draft.kiddo = animal976- },808+ 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+ })977- listener978- )816 expect(next).toEqual({817 bear: {legs: 5},818 eagle: {legs: 5},819 cow: {legs: 5},4 unmodified lines824 })825 826 if (useProxies)827 it("should not allow changing prototype", () => {991- produce(992- {},993- draft => {994- expect(() =>995- Object.setPrototypeOf(draft, Array)996- ).toThrow(/does not support `setPrototype/)997- },998- listener999- )828+ produce({}, draft => {829+ expect(() => Object.setPrototypeOf(draft, Array)).toThrow(830+ /does not support `setPrototype/831+ )832+ })833 })834 835 it("'in' should work", () => {1003- produce(1004- createBaseState(),1005- draft => {1006- expect("anArray" in draft).toBe(true)1007- expect(Reflect.has(draft, "anArray")).toBe(true)836+ produce(createBaseState(), draft => {837+ expect("anArray" in draft).toBe(true)838+ expect(Reflect.has(draft, "anArray")).toBe(true)839 1009- expect("bla" in draft).toBe(false)1010- expect(Reflect.has(draft, "bla")).toBe(false)840+ expect("bla" in draft).toBe(false)841+ expect(Reflect.has(draft, "bla")).toBe(false)842 1012- expect(0 in draft.anArray).toBe(true)1013- expect("0" in draft.anArray).toBe(true)1014- expect(Reflect.has(draft.anArray, 0)).toBe(true)1015- expect(Reflect.has(draft.anArray, "0")).toBe(true)843+ 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)847 1017- expect(17 in draft.anArray).toBe(false)1018- expect("17" in draft.anArray).toBe(false)1019- expect(Reflect.has(draft.anArray, 17)).toBe(false)1020- expect(Reflect.has(draft.anArray, "17")).toBe(false)1021- },1022- listener1023- )848+ 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+ })853 })854 855 it("'this' should work - 1", () => {856 const base = {x: 3}1028- const next1 = produce(1029- base,1030- function() {1031- this.x = 41032- },1033- listener1034- )857+ const next1 = produce(base, function() {858+ this.x = 4859+ })860 expect(next1).not.toBe(base)861 expect(next1.x).toBe(4)862 })863 15 unmodified lines879 value: "val"880 }881 }882 }1058- const nextState = produce(1059- state,1060- draft => {1061- draft.users.existingID = {1062- ...draft.users.existingID,1063- loading: true1064- }1065- },883+ const nextState = produce(state, draft => {884+ draft.users.existingID = {885+ ...draft.users.existingID,886+ loading: true887+ }888+ })1066- listener1067- )889 expect(nextState).toEqual({890 users: {891 existingID: {892 loading: true,4 unmodified lines897 })898 899 it("processes with lodash.set", () => {900 const base = [{id: 1, a: 1}]1080- const result = produce(1081- base,1082- draft => {1083- lodash.set(draft, "[0].a", 2)1084- },1085- listener1086- )901+ const result = produce(base, draft => {902+ lodash.set(draft, "[0].a", 2)903+ })904 expect(base[0].a).toEqual(1)905 expect(result[0].a).toEqual(2)906 })907 908 it("processes with lodash.find", () => {909 const base = [{id: 1, a: 1}]1093- const result = produce(1094- base,1095- draft => {1096- const obj1 = lodash.find(draft, {id: 1})1097- lodash.set(obj1, "a", 2)1098- },910+ const result = produce(base, draft => {911+ const obj1 = lodash.find(draft, {id: 1})912+ lodash.set(obj1, "a", 2)913+ })1099- listener1100- )914 expect(base[0].a).toEqual(1)915 expect(result[0].a).toEqual(2)916 })917 918 it("can produce from no state", () => {919 expect(1107- produce(1108- 3,1109- draft => {1110- expect(draft).toBe(3)1111- return 51112- },920+ produce(3, draft => {921+ expect(draft).toBe(3)922+ return 5923+ })1113- listener1114- )924 ).toBe(5)925 })926 927 it("can return something new ", () => {928 const base = {x: 3}1120- const res = produce(1121- base,1122- draft => {1123- return {x: draft.x + 1}1124- },1125- listener1126- )929+ const res = produce(base, draft => {930+ return {x: draft.x + 1}931+ })932 expect(res).not.toBe(base)933 expect(res).toEqual({x: 4})934 })935 936 it("can return the draft new ", () => {937 const base = {x: 3}1133- const res = produce(1134- base,1135- draft => {1136- draft.x = 41137- return draft1138- },938+ const res = produce(base, draft => {939+ draft.x = 4940+ return draft941+ })1139- listener1140- )942 expect(res).not.toBe(base)943 expect(res).toEqual({x: 4})944 })945 946 it("should throw if modifying the draft and returning something new", () => {947 const base = {x: 3}948 expect(() => {1148- produce(1149- base,1150- draft => {1151- draft.x = 41152- return {x: 5}1153- },949+ produce(base, draft => {950+ draft.x = 4951+ return {x: 5}952+ })1154- listener1155- )953 }).toThrow(/An immer producer returned a new value/)954 })955 956 it("should fix #116", () => {1160- const nextState = produce(1161- [1, 2, 3],1162- s => {1163- s.pop()1164- s.push(100)1165- },1166- listener1167- )957+ const nextState = produce([1, 2, 3], s => {958+ s.pop()959+ s.push(100)960+ })961 expect(nextState).toEqual([1, 2, 100])962 })963 964 it("should fix #117", () => {965 const reducer = (state, action) =>1173- produce(1174- state,1175- draft => {1176- switch (action.type) {1177- case "SET_STARTING_DOTS":1178- return draft.availableStartingDots.map(a => a)1179- default:1180- break1181- }1182- },966+ produce(state, draft => {967+ switch (action.type) {968+ case "SET_STARTING_DOTS":969+ return draft.availableStartingDots.map(a => a)970+ default:971+ break972+ }973+ })1183- listener1184- )974 const base = {975 availableStartingDots: [976 {dots: 4, count: 1},977 {dots: 3, count: 2},7 unmodified lines985 })986 987 it("should fix #117 - 2", () => {988 const reducer = (state, action) =>1200- produce(1201- state,1202- draft => {1203- switch (action.type) {1204- case "SET_STARTING_DOTS":1205- return {1206- dots: draft.availableStartingDots.map(1207- a => a1208- )1209- }1210- default:1211- break1212- }1213- },1214- listener1215- )989+ 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+ })999 const base = {1000 availableStartingDots: [1001 {dots: 4, count: 1},1002 {dots: 3, count: 2},6 unmodified lines1009 })1010 1011 it("should return an unmodified primitive baseState (#148)", () => {1012 const baseState = "some string"1230- const nextState = produce(1231- baseState,1232- () => {1233- /* no modification */1234- },1235- listener1236- )1013+ const nextState = produce(baseState, () => {1014+ /* no modification */1015+ })1016 expect(nextState).toBe(baseState)1017 })1018 1019 it("should return an unmodified null baseState (#148)", () => {1020 const baseState = null1242- const nextState = produce(1243- baseState,1244- () => {1245- /* no modification */1246- },1247- listener1248- )1021+ const nextState = produce(baseState, () => {1022+ /* no modification */1023+ })1024 expect(nextState).toBe(baseState)1025 })1026 1027 it("should not detect noop assignments - 0", () => {1028 const baseState = {x: {y: 3}}1254- const nextState = produce(1255- baseState,1256- d => {1257- const a = d.x1258- d.x = a1259- },1260- listener1261- )1029+ const nextState = produce(baseState, d => {1030+ const a = d.x1031+ d.x = a1032+ })1033 expect(nextState).toBe(baseState)1034 })1035 1036 it("should not detect noop assignments - 1", () => {1037 const baseState = {x: {y: 3}}1267- const nextState = produce(1268- baseState,1269- d => {1270- const a = d.x1271- d.x = 41272- d.x = a1273- },1038+ const nextState = produce(baseState, d => {1039+ const a = d.x1040+ d.x = 41041+ d.x = a1042+ })1274- listener1275- )1043 // 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 })1048 1049 it("should not detect noop assignments - 2", () => {1050 const baseState = {x: {y: 3}}1284- const nextState = produce(1285- baseState,1286- d => {1287- const a = d.x1288- const stuff = a.y + 31289- d.x = 41290- d.x = a1291- },1051+ const nextState = produce(baseState, d => {1052+ const a = d.x1053+ const stuff = a.y + 31054+ d.x = 41055+ d.x = a1056+ })1292- listener1293- )1057 // 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 })1062 1063 it("should not detect noop assignments - 3", () => {1064 const baseState = {x: 3}1302- const nextState = produce(1303- baseState,1304- d => {1305- d.x = 31306- },1307- listener1308- )1065+ const nextState = produce(baseState, d => {1066+ d.x = 31067+ })1068 expect(nextState).toBe(baseState)1069 })1070 1071 it("should not detect noop assignments - 4", () => {1072 const baseState = {x: 3}1314- const nextState = produce(1315- baseState,1316- d => {1317- d.x = 41318- d.x = 31319- },1320- listener1321- )1073+ const nextState = produce(baseState, d => {1074+ d.x = 41075+ d.x = 31076+ })1077 // 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)154 unmodified lines1235 },1236 coffee: false1237 }1238 }1484- return freeze ? deepFreeze(data) : data1239+ return autoFreeze ? deepFreeze(data) : data1240 }1241 })1242 1243 describe(`isDraft - ${name}`, () => {1489- beforeAll(() => {1490- setAutoFreeze(freeze)1491- setUseProxies(useProxies)1492- })1493-1244 it("returns true for object drafts", () => {1245 produce({}, state => {1246 expect(isDraft(state)).toBeTruthy()1247 })47 unmodified lines