Verified listener doesn't break base impl
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent af95dc8 · Browse files · 2 files·+579 added−339 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+579 added−339 removed
- tests:+578 added−338 removed
- other:+1 added−1 removed
Provenance
- Commit
6504581- Tree
f8aa96d- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
af95dc8
2 files changed+579 added−339 removed
modified:__tests__/base.js+578 added-338 removed
6 unmodified lines7 jest.setTimeout(1000)8 9 runBaseTest("proxy (no freeze)", true, false)10 runBaseTest("proxy (autofreeze)", true, true)11+runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)12 runBaseTest("es5 (no freeze)", false, false)13 runBaseTest("es5 (autofreeze)", false, true)14+runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)15 14-function runBaseTest(name, useProxies, freeze) {16+function runBaseTest(name, useProxies, freeze, useListener) {17 describe(`base functionality - ${name}`, () => {18 let baseState19 let origBaseState20+ // make sure logic doesn't break when listener is attached21+ let listener = useListener ? function() {} : undefined22 23 beforeEach(() => {24 setAutoFreeze(freeze)25 setUseProxies(useProxies)26 origBaseState = baseState = createBaseState()27 })28 29 it("should return the original without modifications", () => {26- const nextState = produce(baseState, () => {})30+ const nextState = produce(baseState, () => {}, listener)31 expect(nextState).toBe(baseState)32 })33 34 it("should return the original without modifications when reading stuff", () => {31- const nextState = produce(baseState, s => {32- expect(s.aProp).toBe("hi")33- expect(s.anObject.nested).toMatchObject({yummie: true})34- })35+ const nextState = produce(36+ baseState,37+ s => {38+ expect(s.aProp).toBe("hi")39+ expect(s.anObject.nested).toMatchObject({yummie: true})40+ },41+ listener42+ )43 expect(nextState).toBe(baseState)44 })45 46 it("should return a copy when modifying stuff", () => {39- const nextState = produce(baseState, s => {40- s.aProp = "hello world"41- })47+ const nextState = produce(48+ baseState,49+ s => {50+ s.aProp = "hello world"51+ },52+ listener53+ )54 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 })60 61 it("should preserve type", () => {62+ const nextState = produce(63+ baseState,50- const nextState = produce(baseState, s => {51- expect(Object.getPrototypeOf(s)).toBe(Object.prototype)52- expect(Array.isArray(s.anArray)).toBe(true)53- s.anArray.push(3)54- s.aProp = "hello world"55- expect(Object.getPrototypeOf(s)).toBe(Object.prototype)56- expect(Array.isArray(s.anArray)).toBe(true)57- })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+ )74 expect(Object.getPrototypeOf(nextState)).toBe(Object.prototype)75 expect(Array.isArray(nextState.anArray)).toBe(true)76 })77 78 it("deep change bubbles up", () => {63- const nextState = produce(baseState, s => {64- s.anObject.nested.yummie = false65- })79+ const nextState = produce(80+ baseState,81+ s => {82+ s.anObject.nested.yummie = false83+ },84+ listener85+ )86 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 })92 93 it("can add props", () => {74- const nextState = produce(baseState, s => {75- s.anObject.cookie = {tasty: true}76- })94+ const nextState = produce(95+ baseState,96+ s => {97+ s.anObject.cookie = {tasty: true}98+ },99+ listener100+ )101 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 })106 107 it("can delete props", () => {84- const nextState = produce(baseState, s => {85- delete s.anObject.nested86- })108+ const nextState = produce(109+ baseState,110+ s => {111+ delete s.anObject.nested112+ },113+ listener114+ )115 expect(nextState).not.toBe(baseState)116 expect(nextState.anObject).not.toBe(baseState.anObject)117 expect(nextState.anObject.nested).toBe(undefined)118 })119 120 it("ignores single non-modification", () => {93- const nextState = produce(baseState, s => {94- s.aProp = "hi"95- })121+ const nextState = produce(122+ baseState,123+ s => {124+ s.aProp = "hi"125+ },126+ listener127+ )128 expect(nextState).toBe(baseState)129 })130 131 it("processes single modification", () => {100- const nextState = produce(baseState, s => {101- s.aProp = "hello"102- s.aProp = "hi"103- })132+ const nextState = produce(133+ baseState,134+ s => {135+ s.aProp = "hello"136+ s.aProp = "hi"137+ },138+ listener139+ )140 expect(nextState).not.toBe(baseState)141 expect(nextState).toEqual(baseState)142 })143 20 unmodified lines164 base.x = 1165 base.y = Object.create(null)166 base.y.y = 2167 expect(base.__proto__).toBe(undefined)132- const next = produce(base, draft => {133- draft.y.z = 3134- draft.y.y++135- draft.x++136- })168+ const next = produce(169+ base,170+ draft => {171+ draft.y.z = 3172+ draft.y.y++173+ draft.x++174+ },175+ listener176+ )177 expect(next).toEqual({178 x: 2,179 y: {y: 3, z: 3}180 })181 expect(next.__proto__).toBe(undefined)182 })183 184 it("should support reading arrays", () => {145- const nextState = produce(baseState, s => {146- s.anArray.slice()147- })185+ const nextState = produce(186+ baseState,187+ s => {188+ s.anArray.slice()189+ },190+ listener191+ )192 expect(nextState.anArray).toBe(baseState.anArray)193 expect(nextState).toBe(baseState)194 })195 196 it("should support changing arrays", () => {153- const nextState = produce(baseState, s => {154- s.anArray[3] = true155- })197+ const nextState = produce(198+ baseState,199+ s => {200+ s.anArray[3] = true201+ },202+ listener203+ )204 expect(nextState).not.toBe(baseState)205 expect(nextState.anArray).not.toBe(baseState.anArray)206 expect(nextState.anArray[3]).toEqual(true)207 })208 209 it("should support changing arrays - 2", () => {162- const nextState = produce(baseState, s => {163- s.anArray.splice(1, 1, "a", "b")164- })210+ const nextState = produce(211+ baseState,212+ s => {213+ s.anArray.splice(1, 1, "a", "b")214+ },215+ listener216+ )217 expect(nextState).not.toBe(baseState)218 expect(nextState.anArray).not.toBe(baseState.anArray)219 220 expect(enumerableOnly(nextState.anArray)).toEqual([5 unmodified lines226 ])227 })228 229 it("can delete array items", () => {178- const nextState = produce(baseState, s => {179- s.anArray.length = 3180- })230+ const nextState = produce(231+ baseState,232+ s => {233+ s.anArray.length = 3234+ },235+ listener236+ )237 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 })242 243 it("should support sorting arrays", () => {188- const nextState = produce(baseState, s => {189- s.anArray[2].c = 4190- s.anArray.sort()191- s.anArray[3].c = 5192- })244+ const nextState = produce(245+ baseState,246+ s => {247+ s.anArray[2].c = 4248+ s.anArray.sort()249+ s.anArray[3].c = 5250+ },251+ listener252+ )253 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 })257 258 it("should expose property descriptors", () => {259+ const nextState = produce(260+ [],261+ s => {262+ expect(Object.getOwnPropertyDescriptor(s, 0)).toBe(199- const nextState = produce([], s => {200- expect(Object.getOwnPropertyDescriptor(s, 0)).toBe(undefined)201- s.unshift("x")202- expect(Object.getOwnPropertyDescriptor(s, 0)).toEqual({203- configurable: true,204- enumerable: true,205- value: "x",206- writable: true207- })208- expect(s.length).toBe(1)209- expect(s[0] === "x").toBe(true)210- })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+ )277 expect(nextState).toEqual(["x"])278 expect(Object.getOwnPropertyDescriptor(nextState, 0)).toEqual({279 configurable: !freeze,280 enumerable: true,2 unmodified lines283 })284 })285 286 it("should support sorting arrays - 2", () => {221- const nextState = produce(baseState, s => {222- s.anArray.unshift("x")223- s.anArray[3].c = 4224- s.anArray.sort()225- s.anArray[3].c = 5226- s.anArray.unshift("y")227- })287+ 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+ )298 expect(nextState).not.toBe(baseState)299 expect(nextState.anArray).not.toBe(baseState.anArray)300 expect(enumerableOnly(nextState.anArray)).toEqual([301 "y",5 unmodified lines307 ])308 })309 310 it("should updating inside arrays", () => {241- const nextState = produce(baseState, s => {242- s.anArray[2].test = true243- })311+ const nextState = produce(312+ baseState,313+ s => {314+ s.anArray[2].test = true315+ },316+ listener317+ )318 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 })322 323 it("reusing object should work", () => {324+ const nextState = produce(325+ baseState,250- const nextState = produce(baseState, s => {251- const obj = s.anObject252- delete s.anObject253- s.messy = obj254- })326+ s => {327+ const obj = s.anObject328+ delete s.anObject329+ s.messy = obj330+ },331+ listener332+ )333 expect(nextState).not.toBe(baseState)334 expect(nextState.anArray).toBe(baseState.anArray)335 expect(enumerableOnly(nextState)).toEqual({336 anArray: [3, 2, {c: 3}, 1],8 unmodified lines345 expect(nextState.messy.nested).toBe(baseState.anObject.nested)346 })347 348 it("refs should be transparent", () => {349+ const nextState = produce(350+ baseState,271- const nextState = produce(baseState, s => {272- const obj = s.anObject273- s.aProp = "hello"274- delete s.anObject275- obj.coffee = true276- s.messy = obj277- })351+ s => {352+ const obj = s.anObject353+ s.aProp = "hello"354+ delete s.anObject355+ obj.coffee = true356+ s.messy = obj357+ },358+ listener359+ )360 expect(nextState).not.toBe(baseState)361 expect(nextState.anArray).toBe(baseState.anArray)362 expect(enumerableOnly(nextState)).toEqual({363 anArray: [3, 2, {c: 3}, 1],8 unmodified lines372 expect(nextState.messy.nested).toBe(baseState.anObject.nested)373 })374 375 it("should allow setting to undefined a defined draft property", () => {294- const nextState = produce(baseState, s => {295- s.aProp = undefined296- })376+ const nextState = produce(377+ baseState,378+ s => {379+ s.aProp = undefined380+ },381+ listener382+ )383 expect(nextState).not.toBe(baseState)384 expect(baseState.aProp).toBe("hi")385 expect(nextState.aProp).toBe(undefined)386 })1 unmodified line388 // ES implementation does't protect against all outside modifications, just some..389 if (useProxies) {390 it("should revoke the proxy of the baseState after immer function is executed", () => {391 let proxy306- const nextState = produce(baseState, s => {307- proxy = s308- s.aProp = "hello"309- })392+ const nextState = produce(393+ baseState,394+ s => {395+ proxy = s396+ s.aProp = "hello"397+ },398+ listener399+ )400 expect(nextState).not.toBe(baseState)401 expect(baseState.aProp).toBe("hi")402 expect(nextState.aProp).toBe("hello")403 11 unmodified lines415 }416 417 it("should revoke the proxy of the baseState after immer function is executed - 2", () => {418 let proxy329- const nextState = produce(baseState, s => {330- proxy = s.anObject331- })419+ const nextState = produce(420+ baseState,421+ s => {422+ proxy = s.anObject423+ },424+ listener425+ )426 expect(nextState).toBe(baseState)427 expect(() => {428 // In ES5 implemenation only protects existing props, but alas..429 proxy.coffee = "Hallo"3 unmodified lines433 }).toThrowError(/revoked/)434 })435 436 it("should reflect all changes made in the draft immediately", () => {437+ produce(438+ baseState,343- produce(baseState, draft => {344- draft.anArray[0] = 5345- draft.anArray.unshift("test")346- // sliced here; jest will also compare non-enumerable keys, which would include the immer Symbols347- expect(draft.anArray.slice()).toMatchObject([348- "test",349- 5,350- 2,351- {c: 3},352- 1353- ])354- draft.stuffz = "coffee"355- expect(draft.stuffz).toBe("coffee")356- })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+ )455 })456 457 it("should be able to get property descriptors from objects", () => {458+ produce(459+ {a: 1},360- produce({a: 1}, draft => {361- expect("a" in draft).toBe(true)362- expect("b" in draft).toBe(false)363- expect(364- Reflect.ownKeys(draft).filter(x => typeof x === "string")365- ).toEqual(["a"])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"])468 367- expect(368- Object.getOwnPropertyDescriptor(draft, "a")369- ).toMatchObject({370- configurable: true,371- enumerable: true372- })373- draft.a = 2374- expect(375- Object.getOwnPropertyDescriptor(draft, "a")376- ).toMatchObject({377- configurable: true,378- enumerable: true379- })380- expect(381- Object.getOwnPropertyDescriptor(draft, "b")382- ).toBeUndefined()383- draft.b = 2384- expect(385- Object.getOwnPropertyDescriptor(draft, "b")386- ).toMatchObject({387- configurable: true,388- enumerable: true389- })390- expect("a" in draft).toBe(true)391- expect("b" in draft).toBe(true)392- expect(393- Reflect.ownKeys(draft).filter(x => typeof x === "string")394- ).toEqual(["a", "b"])395- })469+ 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+ )502 })503 504 it("should be able to get property descriptors from arrays", () => {399- produce([1], draft => {400- expect(0 in draft).toBe(true)401- expect(1 in draft).toBe(false)402- expect("0" in draft).toBe(true)403- expect("1" in draft).toBe(false)404- expect(length in draft).toBe(true)405- expect(406- Reflect.ownKeys(draft).filter(x => typeof x === "string")407- ).toEqual(["0", "length"])505+ 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"])518 409- expect(410- Object.getOwnPropertyDescriptor(draft, "length")411- ).toMatchObject({412- configurable: false,413- enumerable: false414- })415- draft[0] = 2416- expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(417- {519+ 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({529 configurable: true,530 enumerable: true420- }421- )422- expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(423- {531+ })532+ expect(533+ Object.getOwnPropertyDescriptor(draft, 0)534+ ).toMatchObject({535 configurable: true,536 enumerable: true426- }427- )428- expect(429- Object.getOwnPropertyDescriptor(draft, 1)430- ).toBeUndefined()431- draft[1] = 2432- expect(Object.getOwnPropertyDescriptor(draft, 1)).toMatchObject(433- {537+ })538+ expect(539+ Object.getOwnPropertyDescriptor(draft, 1)540+ ).toBeUndefined()541+ draft[1] = 2542+ expect(543+ Object.getOwnPropertyDescriptor(draft, 1)544+ ).toMatchObject({545 configurable: true,546 enumerable: true436- }437- )438- expect(439- Reflect.ownKeys(draft).filter(x => typeof x === "string")440- ).toEqual(["0", "1", "length"])441- })547+ })548+ expect(549+ Reflect.ownKeys(draft).filter(550+ x => typeof x === "string"551+ )552+ ).toEqual(["0", "1", "length"])553+ },554+ listener555+ )556 })557 558 if (useProxies === true) {559 it("should not be possible to set property descriptors", () => {560 expect(() => {561+ produce(562+ {},447- produce({}, draft => {448- Object.defineProperty(draft, "xx", {449- enumerable: true,450- writeable: true,451- value: 2452- })453- })563+ draft => {564+ Object.defineProperty(draft, "xx", {565+ enumerable: true,566+ writeable: true,567+ value: 2568+ })569+ },570+ listener571+ )572 }).toThrowError(/not support/)573 })574 }575 576 it("should not throw error, see #53 - 1", () => {577 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}460- const result = produce(base, draft => {461- draft.arr = draft.arr.filter(item => item.count > 2)462- })578+ const result = produce(579+ base,580+ draft => {581+ draft.arr = draft.arr.filter(item => item.count > 2)582+ },583+ listener584+ )585 expect(result.arr[0].count).toEqual(3)586 expect(result).toEqual({587 arr: [{count: 3}]588 })1 unmodified line590 })591 592 it("should not throw error, see #53 - 2", () => {593 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}472- const result = produce(base, draft => {473- draft.newArr = draft.arr.filter(item => item.count > 2)474- })594+ const result = produce(595+ base,596+ draft => {597+ draft.newArr = draft.arr.filter(item => item.count > 2)598+ },599+ listener600+ )601 expect(result.newArr[0].count).toEqual(3)602 expect(result.arr).toBe(base.arr)603 expect(result).toEqual({604 arr: [18 unmodified lines623 })624 625 it("should not throw error, see #53 - 3", () => {626 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}627+ const result = produce(628+ base,501- const result = produce(base, draft => {502- draft.newArr = draft.arr.filter(item => item.count > 2)503- delete draft.arr504- })629+ draft => {630+ draft.newArr = draft.arr.filter(item => item.count > 2)631+ delete draft.arr632+ },633+ listener634+ )635 expect(result.newArr[0].count).toEqual(3)636 expect(result).toEqual({637 newArr: [{count: 3}]638 })1 unmodified line640 })641 642 it("should not throw error, see #53 - 4", () => {643 const base = {bear: {age: 10}}644+ const result = produce(645+ base,514- const result = produce(base, draft => {515- draft.bear.legs = 4516- draft.room = {elephant: {kiddo: draft.bear}}517- })646+ draft => {647+ draft.bear.legs = 4648+ draft.room = {elephant: {kiddo: draft.bear}}649+ },650+ listener651+ )652 expect(result).toEqual({653 bear: {age: 10, legs: 4},654 room: {elephant: {kiddo: {age: 10, legs: 4}}}655 })113 unmodified lines769 })770 771 it("should handle nested immer calls correctly - 1", () => {772 const bear = {paw: {honey: true}}773+ const next = produce(774+ bear,639- const next = produce(bear, draft => {640- const paw2 = produce(bear.paw, draft => {641- draft.honey = false642- })643- expect(paw2.honey).toBe(false)644- expect(draft.paw.honey).toBe(true) // effects should not be visible outside645- })775+ draft => {776+ const paw2 = produce(bear.paw, draft => {777+ draft.honey = false778+ })779+ expect(paw2.honey).toBe(false)780+ expect(draft.paw.honey).toBe(true) // effects should not be visible outside781+ },782+ listener783+ )784 expect(next.paw.honey).toBe(true)785 expect(next).toBe(bear)786 })787 788 it("should handle nested immer calls correctly - 2", () => {789 const bear = {paw: {honey: true}}790+ const next = produce(791+ bear,652- const next = produce(bear, draft => {653- const paw2 = produce(bear.paw, draft => {654- draft.honey = false655- })656- expect(paw2.honey).toBe(false)657- expect(draft.paw.honey).toBe(true)658- draft.paw = paw2659- expect(draft.paw.honey).toBe(false)660- })792+ draft => {793+ const paw2 = produce(bear.paw, draft => {794+ draft.honey = false795+ })796+ expect(paw2.honey).toBe(false)797+ expect(draft.paw.honey).toBe(true)798+ draft.paw = paw2799+ expect(draft.paw.honey).toBe(false)800+ },801+ listener802+ )803 expect(next.paw.honey).toBe(false)804 expect(next).not.toBe(bear)805 })806 807 it("should not try to change immutable data, see #66", () => {808 const user = require("./test-data")809 810 const base = {}669- const next = produce(base, draft => {670- draft.user = user671- })811+ const next = produce(812+ base,813+ draft => {814+ draft.user = user815+ },816+ listener817+ )818 expect(next.user).toBe(user)819 expect(next).not.toBe(base)820 expect(next.user).toEqual(user)821 })1 unmodified line823 it("should not try to change immutable data, see #66 - 2", () => {824 const user = deepFreeze(cloneDeep(require("./test-data")))825 826 const base = {}681- const next = produce(base, draft => {682- draft.user = user683- })827+ const next = produce(828+ base,829+ draft => {830+ draft.user = user831+ },832+ listener833+ )834 expect(next.user).toBe(user)835 expect(next).not.toBe(base)836 expect(next.user).toEqual(user)837 })838 839 if (freeze)840 it("should freeze new data well", () => {841 const base = {}692- const next = produce(base, draft => {693- draft.x = {y: [{z: true}]}694- })842+ const next = produce(843+ base,844+ draft => {845+ draft.x = {y: [{z: true}]}846+ },847+ listener848+ )849 expect(Object.isFrozen(next)).toBe(true)850 expect(Object.isFrozen(next.x)).toBe(true)851 expect(Object.isFrozen(next.x.y)).toBe(true)852 expect(Object.isFrozen(next.x.y[0].z)).toBe(true)853 })854 855 it("should structurally share identical objects in the tree", () => {856 const base = {bear: {legs: 4}, eagle: {legs: 3}}857+ const next = produce(858+ base,703- const next = produce(base, draft => {704- const animal = draft.bear705- animal.legs = animal.legs + 1706- draft.bear = animal707- draft.eagle = animal708- draft.cow = animal709- draft.kiddo = animal710- })859+ draft => {860+ const animal = draft.bear861+ animal.legs = animal.legs + 1862+ draft.bear = animal863+ draft.eagle = animal864+ draft.cow = animal865+ draft.kiddo = animal866+ },867+ listener868+ )869 expect(next).toEqual({870 bear: {legs: 5},871 eagle: {legs: 5},872 cow: {legs: 5},4 unmodified lines877 })878 879 if (useProxies)880 it("should not allow changing prototype", () => {723- produce({}, draft => {724- expect(() => Object.setPrototypeOf(draft, Array)).toThrow(725- /does not support `setPrototype/726- )727- })881+ produce(882+ {},883+ draft => {884+ expect(() =>885+ Object.setPrototypeOf(draft, Array)886+ ).toThrow(/does not support `setPrototype/)887+ },888+ listener889+ )890 })891 892 it("'in' should work", () => {893+ produce(894+ createBaseState(),731- produce(createBaseState(), draft => {732- expect("anArray" in draft).toBe(true)733- expect(Reflect.has(draft, "anArray")).toBe(true)895+ draft => {896+ expect("anArray" in draft).toBe(true)897+ expect(Reflect.has(draft, "anArray")).toBe(true)898 735- expect("bla" in draft).toBe(false)736- expect(Reflect.has(draft, "bla")).toBe(false)899+ expect("bla" in draft).toBe(false)900+ expect(Reflect.has(draft, "bla")).toBe(false)901 738- expect(0 in draft.anArray).toBe(true)739- expect("0" in draft.anArray).toBe(true)740- expect(Reflect.has(draft.anArray, 0)).toBe(true)741- expect(Reflect.has(draft.anArray, "0")).toBe(true)902+ expect(0 in draft.anArray).toBe(true)903+ expect("0" in draft.anArray).toBe(true)904+ expect(Reflect.has(draft.anArray, 0)).toBe(true)905+ expect(Reflect.has(draft.anArray, "0")).toBe(true)906 743- expect(17 in draft.anArray).toBe(false)744- expect("17" in draft.anArray).toBe(false)745- expect(Reflect.has(draft.anArray, 17)).toBe(false)746- expect(Reflect.has(draft.anArray, "17")).toBe(false)747- })907+ expect(17 in draft.anArray).toBe(false)908+ expect("17" in draft.anArray).toBe(false)909+ expect(Reflect.has(draft.anArray, 17)).toBe(false)910+ expect(Reflect.has(draft.anArray, "17")).toBe(false)911+ },912+ listener913+ )914 })915 916 it("'this' should work - 1", () => {917 const base = {x: 3}752- const next1 = produce(base, function() {753- this.x = 4754- })918+ const next1 = produce(919+ base,920+ function() {921+ this.x = 4922+ },923+ listener924+ )925 expect(next1).not.toBe(base)926 expect(next1.x).toBe(4)927 })928 15 unmodified lines944 value: "val"945 }946 }947 }948+ const nextState = produce(949+ state,778- const nextState = produce(state, draft => {779- draft.users.existingID = {780- ...draft.users.existingID,781- loading: true782- }783- })950+ draft => {951+ draft.users.existingID = {952+ ...draft.users.existingID,953+ loading: true954+ }955+ },956+ listener957+ )958 expect(nextState).toEqual({959 users: {960 existingID: {961 loading: true,4 unmodified lines966 })967 968 it("processes with lodash.set", () => {969 const base = [{id: 1, a: 1}]796- const result = produce(base, draft => {797- lodash.set(draft, "[0].a", 2)798- })970+ const result = produce(971+ base,972+ draft => {973+ lodash.set(draft, "[0].a", 2)974+ },975+ listener976+ )977 expect(base[0].a).toEqual(1)978 expect(result[0].a).toEqual(2)979 })980 981 it("processes with lodash.find", () => {982 const base = [{id: 1, a: 1}]983+ const result = produce(984+ base,805- const result = produce(base, draft => {806- const obj1 = lodash.find(draft, {id: 1})807- lodash.set(obj1, "a", 2)808- })985+ draft => {986+ const obj1 = lodash.find(draft, {id: 1})987+ lodash.set(obj1, "a", 2)988+ },989+ listener990+ )991 expect(base[0].a).toEqual(1)992 expect(result[0].a).toEqual(2)993 })994 995 it("can produce from no state", () => {996 expect(997+ produce(998+ 3,815- produce(3, draft => {816- expect(draft).toBe(3)817- return 5818- })999+ draft => {1000+ expect(draft).toBe(3)1001+ return 51002+ },1003+ listener1004+ )1005 ).toBe(5)1006 })1007 1008 it("can return something new ", () => {1009 const base = {x: 3}824- const res = produce(base, draft => {825- return {x: draft.x + 1}826- })1010+ const res = produce(1011+ base,1012+ draft => {1013+ return {x: draft.x + 1}1014+ },1015+ listener1016+ )1017 expect(res).not.toBe(base)1018 expect(res).toEqual({x: 4})1019 })1020 1021 it("can return the draft new ", () => {1022 const base = {x: 3}1023+ const res = produce(1024+ base,833- const res = produce(base, draft => {834- draft.x = 4835- return draft836- })1025+ draft => {1026+ draft.x = 41027+ return draft1028+ },1029+ listener1030+ )1031 expect(res).not.toBe(base)1032 expect(res).toEqual({x: 4})1033 })1034 1035 it("should throw if modifying the draft and returning something new", () => {1036 const base = {x: 3}1037 expect(() => {1038+ produce(1039+ base,844- produce(base, draft => {845- draft.x = 4846- return {x: 5}847- })1040+ draft => {1041+ draft.x = 41042+ return {x: 5}1043+ },1044+ listener1045+ )1046 }).toThrow(/An immer producer returned a new value/)1047 })1048 1049 it("should fix #116", () => {852- const nextState = produce([1, 2, 3], s => {853- s.pop()854- s.push(100)855- })1050+ const nextState = produce(1051+ [1, 2, 3],1052+ s => {1053+ s.pop()1054+ s.push(100)1055+ },1056+ listener1057+ )1058 expect(nextState).toEqual([1, 2, 100])1059 })1060 1061 it("should fix #117", () => {1062 const reducer = (state, action) =>1063+ produce(1064+ state,861- produce(state, draft => {862- switch (action.type) {863- case "SET_STARTING_DOTS":864- return draft.availableStartingDots.map(a => a)865- default:866- break867- }868- })1065+ draft => {1066+ switch (action.type) {1067+ case "SET_STARTING_DOTS":1068+ return draft.availableStartingDots.map(a => a)1069+ default:1070+ break1071+ }1072+ },1073+ listener1074+ )1075 const base = {1076 availableStartingDots: [1077 {dots: 4, count: 1},1078 {dots: 3, count: 2},7 unmodified lines1086 })1087 1088 it("should fix #117 - 2", () => {1089 const reducer = (state, action) =>884- produce(state, draft => {885- switch (action.type) {886- case "SET_STARTING_DOTS":887- return {888- dots: draft.availableStartingDots.map(a => a)889- }890- default:891- break892- }893- })1090+ produce(1091+ state,1092+ draft => {1093+ switch (action.type) {1094+ case "SET_STARTING_DOTS":1095+ return {1096+ dots: draft.availableStartingDots.map(1097+ a => a1098+ )1099+ }1100+ default:1101+ break1102+ }1103+ },1104+ listener1105+ )1106 const base = {1107 availableStartingDots: [1108 {dots: 4, count: 1},1109 {dots: 3, count: 2},6 unmodified lines1116 })1117 1118 it("should return an unmodified primitive baseState (#148)", () => {1119 const baseState = "some string"908- const nextState = produce(baseState, () => {909- /* no modification */910- })1120+ const nextState = produce(1121+ baseState,1122+ () => {1123+ /* no modification */1124+ },1125+ listener1126+ )1127 expect(nextState).toBe(baseState)1128 })1129 1130 it("should return an unmodified null baseState (#148)", () => {1131 const baseState = null916- const nextState = produce(baseState, () => {917- /* no modification */918- })1132+ const nextState = produce(1133+ baseState,1134+ () => {1135+ /* no modification */1136+ },1137+ listener1138+ )1139 expect(nextState).toBe(baseState)1140 })1141 1142 it("should not detect noop assignments - 0", () => {1143 const baseState = {x: {y: 3}}924- const nextState = produce(baseState, d => {925- const a = d.x926- d.x = a927- })1144+ const nextState = produce(1145+ baseState,1146+ d => {1147+ const a = d.x1148+ d.x = a1149+ },1150+ listener1151+ )1152 expect(nextState).toBe(baseState)1153 })1154 1155 it("should not detect noop assignments - 1", () => {1156 const baseState = {x: {y: 3}}1157+ const nextState = produce(1158+ baseState,933- const nextState = produce(baseState, d => {934- const a = d.x935- d.x = 4936- d.x = a937- })1159+ d => {1160+ const a = d.x1161+ d.x = 41162+ d.x = a1163+ },1164+ listener1165+ )1166 // Ideally, this should actually be the same instances1167 // but this would be pretty expensive to detect,1168 // so we don't atm1169 expect(nextState).not.toBe(baseState)1170 })1171 1172 it("should not detect noop assignments - 2", () => {1173 const baseState = {x: {y: 3}}1174+ const nextState = produce(1175+ baseState,946- const nextState = produce(baseState, d => {947- const a = d.x948- const stuff = a.y + 3949- d.x = 4950- d.x = a951- })1176+ d => {1177+ const a = d.x1178+ const stuff = a.y + 31179+ d.x = 41180+ d.x = a1181+ },1182+ listener1183+ )1184 // Ideally, this should actually be the same instances1185 // but this would be pretty expensive to detect,1186 // so we don't atm1187 expect(nextState).not.toBe(baseState)1188 })1189 1190 it("should not detect noop assignments - 3", () => {1191 const baseState = {x: 3}960- const nextState = produce(baseState, d => {961- d.x = 3962- })1192+ const nextState = produce(1193+ baseState,1194+ d => {1195+ d.x = 31196+ },1197+ listener1198+ )1199 expect(nextState).toBe(baseState)1200 })1201 1202 it("should not detect noop assignments - 4", () => {1203 const baseState = {x: 3}968- const nextState = produce(baseState, d => {969- d.x = 4970- d.x = 3971- })1204+ const nextState = produce(1205+ baseState,1206+ d => {1207+ d.x = 41208+ d.x = 31209+ },1210+ listener1211+ )1212 // Ideally, this should actually be the same instances1213 // but this would be pretty expensive to detect,1214 // so we don't atm1215 expect(nextState).not.toBe(baseState)31 unmodified linesmodified:src/es5.js+1 added-1 removed
220 unmodified lines221 inversePatches.push({op: "replace", path: [], value: baseState})222 }223 } else {224 if (patchListener) markChangesRecursively(rootProxy)225- else markChangesSweep() // this one is more efficient if we don't need to know which attributes have changed225+ markChangesSweep() // this one is more efficient if we don't need to know which attributes have changed226 result = finalize(rootProxy, [], patches, inversePatches)227 }228 // make sure all proxies become unusable229 each(states, (_, state) => {41 unmodified lines