base.js
This file
Path
__tests__/base.js
688 lines · 24.4 KiB · JavaScript
Last changed by
Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
History of this fileTake it
Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 1why, one step back1"use strict"Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 2why, one step back2import produce, {setAutoFreeze, setUseProxies} from "../src"Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 3why, one step back3import deepFreeze from "deep-freeze"Reproduced #66Michel Weststratebefore adoptiond5de885
line 4why, one step back4import cloneDeep from "lodash.clonedeep"More repo setupMichel Weststratebefore adoption1637393
line 5why, one step back5Making several improvements at onceMichel Weststratebefore adoption1116ccf
lines 6–7why, one step back6jest.setTimeout(1000)7Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 8–11why, one step back8runBaseTest("proxy (no freeze)", true, false)9runBaseTest("proxy (autofreeze)", true, true)10runBaseTest("es5 (no freeze)", false, false)11runBaseTest("es5 (autofreeze)", false, true)Make debugging workMichel Weststratebefore adoptiond0ef4ed
line 12why, one step back12Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 13why, one step back13function runBaseTest(name, useProxies, freeze) {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 14–16why, one step back14 describe(`base functionality - ${name}`, () => {15 let baseState16 let origBaseStateMake debugging workMichel Weststratebefore adoptiond0ef4ed
line 17why, one step back17Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 18why, one step back18 beforeEach(() => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 19–20why, one step back19 setAutoFreeze(freeze)20 setUseProxies(useProxies)Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 21–22why, one step back21 origBaseState = baseState = createBaseState()22 })Make debugging workMichel Weststratebefore adoptiond0ef4ed
line 23why, one step back23Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 24why, one step back24 it("should return the original without modifications", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 25why, one step back25 const nextState = produce(baseState, () => {})Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 26why, one step back26 expect(nextState).toBe(baseState)Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 27–28why, one step back27 })28Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 29why, one step back29 it("should return the original without modifications when reading stuff", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 30why, one step back30 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 31–35why, one step back31 expect(s.aProp).toBe("hi")32 expect(s.anObject.nested).toMatchObject({yummie: true})33 })34 expect(nextState).toBe(baseState)35 })Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
line 36why, one step back36Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 37–38why, one step back37 it("should not return any value: thunk", () => {38 const warning = jest.spyOn(console, "warn")`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
lines 39–42why, one step back39 produce(baseState, () => ({bad: "don't do this"}))40 produce(baseState, () => [1, 2, 3])41 produce(baseState, () => false)42 produce(baseState, () => "")Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
line 43why, one step back43Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 44–45why, one step back44 expect(warning).toHaveBeenCalledTimes(4)45 warning.mockClear()Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 46–47why, one step back46 })47Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 48why, one step back48 it("should return a copy when modifying stuff", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 49why, one step back49 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 50–56why, one step back50 s.aProp = "hello world"51 })52 expect(nextState).not.toBe(baseState)53 expect(baseState.aProp).toBe("hi")54 expect(nextState.aProp).toBe("hello world")55 // structural sharing?56 expect(nextState.nested).toBe(baseState.nested)Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 57–58why, one step back57 })58Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 59–61why, one step back59 if (60 ("should preserve type",61 () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 62why, one step back62 const nextState = produce(baseState, s => {Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 63–71why, one step back63 expect(Array.isArray(s)).toBe(true)64 expect(s.protoType).toBe(Object)65 s.anArray.push(3)66 s.aProp = "hello world"67 expect(Array.isArray(s)).toBe(true)68 expect(s.protoType).toBe(Object)69 })70 expect(Array.isArray(nextState)).toBe(true)71 expect(nextState.protoType).toBe(Object)Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 72why, one step back72 })Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 73–74why, one step back73 )74 it("deep change bubbles up", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 75why, one step back75 const nextState = produce(baseState, s => {Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 76–83why, one step back76 s.anObject.nested.yummie = false77 })78 expect(nextState).not.toBe(baseState)79 expect(nextState.anObject).not.toBe(baseState.anObject)80 expect(baseState.anObject.nested.yummie).toBe(true)81 expect(nextState.anObject.nested.yummie).toBe(false)82 expect(nextState.anArray).toBe(baseState.anArray)83 })Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
line 84why, one step back84Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 85why, one step back85 it("can add props", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 86why, one step back86 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 87–92why, one step back87 s.anObject.cookie = {tasty: true}88 })89 expect(nextState).not.toBe(baseState)90 expect(nextState.anObject).not.toBe(baseState.anObject)91 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)92 expect(nextState.anObject.cookie).toEqual({tasty: true})Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 93–94why, one step back93 })94Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 95why, one step back95 it("can delete props", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 96why, one step back96 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 97–101why, one step back97 delete s.anObject.nested98 })99 expect(nextState).not.toBe(baseState)100 expect(nextState.anObject).not.toBe(baseState.anObject)101 expect(nextState.anObject.nested).toBe(undefined)Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 102–103why, one step back102 })103Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 104why, one step back104 it("ignores single non-modification", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 105why, one step back105 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 106–108why, one step back106 s.aProp = "hi"107 })108 expect(nextState).toBe(baseState)Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 109–110why, one step back109 })110Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 111why, one step back111 it("processes single modification", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 112why, one step back112 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 113–117why, one step back113 s.aProp = "hello"114 s.aProp = "hi"115 })116 expect(nextState).not.toBe(baseState)117 expect(nextState).toEqual(baseState)Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 118–119why, one step back118 })119Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 120why, one step back120 it("should support reading arrays", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 121why, one step back121 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 122–125why, one step back122 s.anArray.slice()123 })124 expect(nextState.anArray).toBe(baseState.anArray)125 expect(nextState).toBe(baseState)Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 126–127why, one step back126 })127Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 128why, one step back128 it("should support changing arrays", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 129why, one step back129 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 130–134why, one step back130 s.anArray[3] = true131 })132 expect(nextState).not.toBe(baseState)133 expect(nextState.anArray).not.toBe(baseState.anArray)134 expect(nextState.anArray[3]).toEqual(true)Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 135–136why, one step back135 })136Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 137why, one step back137 it("should support changing arrays - 2", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 138why, one step back138 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 139–142why, one step back139 s.anArray.splice(1, 1, "a", "b")140 })141 expect(nextState).not.toBe(baseState)142 expect(nextState.anArray).not.toBe(baseState.anArray)Add support for arraysMichel Weststratebefore adoptiond3696cb
line 143why, one step back143Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
lines 144–150why, one step back144 expect(enumerableOnly(nextState.anArray)).toEqual([145 3,146 "a",147 "b",148 {c: 3},149 1150 ])Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 151–152why, one step back151 })152Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 153why, one step back153 it("can delete array items", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 154why, one step back154 const nextState = produce(baseState, s => {Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 155–162why, one step back155 s.anArray.length = 3156 })157 expect(nextState).not.toBe(baseState)158 expect(nextState.anObject).toBe(baseState.anObject)159 expect(nextState.anArray).not.toBe(baseState.anArray)160 expect(nextState.anArray).toEqual([3, 2, {c: 3}])161 })162Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 163why, one step back163 it("should support sorting arrays", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 164why, one step back164 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 165–170why, one step back165 s.anArray[2].c = 4166 s.anArray.sort()167 s.anArray[3].c = 5168 })169 expect(nextState).not.toBe(baseState)170 expect(nextState.anArray).not.toBe(baseState.anArray)Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 171why, one step back171 expect(enumerableOnly(nextState.anArray)).toEqual([1, 2, 3, {c: 5}])Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 172–173why, one step back172 })173Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 174why, one step back174 it("should expose property descriptors", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 175why, one step back175 const nextState = produce([], s => {Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
lines 176–196why, one step back176 expect(Object.getOwnPropertyDescriptor(s, 0)).toBe(undefined)177 s.unshift("x")178 expect(Object.getOwnPropertyDescriptor(s, 0)).toEqual({179 configurable: true,180 enumerable: true,181 value: "x",182 writable: true183 })184 expect(s.length).toBe(1)185 expect(s[0] === "x").toBe(true)186 })187 expect(nextState).toEqual(["x"])188 expect(Object.getOwnPropertyDescriptor(nextState, 0)).toEqual({189 configurable: !freeze,190 enumerable: true,191 value: "x",192 writable: !freeze193 })194 })195196 it("should support sorting arrays - 2", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 197why, one step back197 const nextState = produce(baseState, s => {Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
lines 198–215why, one step back198 s.anArray.unshift("x")199 s.anArray[3].c = 4200 s.anArray.sort()201 s.anArray[3].c = 5202 s.anArray.unshift("y")203 })204 expect(nextState).not.toBe(baseState)205 expect(nextState.anArray).not.toBe(baseState.anArray)206 expect(enumerableOnly(nextState.anArray)).toEqual([207 "y",208 1,209 2,210 3,211 {c: 5},212 "x"213 ])214 })215Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 216why, one step back216 it("should updating inside arrays", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 217why, one step back217 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 218–222why, one step back218 s.anArray[2].test = true219 })220 expect(nextState).not.toBe(baseState)221 expect(nextState.anArray).not.toBe(baseState.anArray)222 expect(nextState.anArray).toEqual([3, 2, {c: 3, test: true}, 1])Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 223–224why, one step back223 })224Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 225why, one step back225 it("reusing object should work", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 226why, one step back226 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 227–232why, one step back227 const obj = s.anObject228 delete s.anObject229 s.messy = obj230 })231 expect(nextState).not.toBe(baseState)232 expect(nextState.anArray).toBe(baseState.anArray)Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 233why, one step back233 expect(enumerableOnly(nextState)).toEqual({Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 234–243why, one step back234 anArray: [3, 2, {c: 3}, 1],235 aProp: "hi",236 messy: {237 nested: {238 yummie: true239 },240 coffee: false241 }242 })243 expect(nextState.messy.nested).toBe(baseState.anObject.nested)Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 244–245why, one step back244 })245Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 246why, one step back246 it("refs should be transparent", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 247why, one step back247 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 248–255why, one step back248 const obj = s.anObject249 s.aProp = "hello"250 delete s.anObject251 obj.coffee = true252 s.messy = obj253 })254 expect(nextState).not.toBe(baseState)255 expect(nextState.anArray).toBe(baseState.anArray)Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 256why, one step back256 expect(enumerableOnly(nextState)).toEqual({Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 257–266why, one step back257 anArray: [3, 2, {c: 3}, 1],258 aProp: "hello",259 messy: {260 nested: {261 yummie: true262 },263 coffee: true264 }265 })266 expect(nextState.messy.nested).toBe(baseState.anObject.nested)Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e
lines 267–268why, one step back267 })268Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 269why, one step back269 it("should allow setting to undefined a defined draft property", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 270why, one step back270 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 271–275why, one step back271 s.aProp = undefined272 })273 expect(nextState).not.toBe(baseState)274 expect(baseState.aProp).toBe("hi")275 expect(nextState.aProp).toBe(undefined)Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
lines 276–277why, one step back276 })277Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 278why, one step back278 // ES implementation does't protect against all outside modifications, just some..Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 279why, one step back279 if (useProxies) {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 280–281why, one step back280 it("should revoke the proxy of the baseState after immer function is executed", () => {281 let proxy`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 282why, one step back282 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 283–288why, one step back283 proxy = s284 s.aProp = "hello"285 })286 expect(nextState).not.toBe(baseState)287 expect(baseState.aProp).toBe("hi")288 expect(nextState.aProp).toBe("hello")Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
line 289why, one step back289Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 290–295why, one step back290 expect(() => {291 proxy.aProp = "Hallo"292 }).toThrowError(/revoked/)293 expect(() => {294 const aProp = proxy.aProp295 }).toThrowError(/revoked/)Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 296why, one step back296Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 297–304why, one step back297 expect(nextState).not.toBe(baseState)298 expect(baseState.aProp).toBe("hi")299 expect(nextState.aProp).toBe("hello")300 })301 }302303 it("should revoke the proxy of the baseState after immer function is executed - 2", () => {304 let proxy`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 305why, one step back305 const nextState = produce(baseState, s => {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 306–315why, one step back306 proxy = s.anObject307 })308 expect(nextState).toBe(baseState)309 expect(() => {310 // In ES5 implemenation only protects existing props, but alas..311 proxy.coffee = "Hallo"312 }).toThrowError(/revoked/)313 expect(() => {314 const test = proxy.coffee315 }).toThrowError(/revoked/)Updated changelogMichel Weststratebefore adoption8f44348
lines 316–317why, one step back316 })317Added a testcaseMichel Weststratebefore adoption1e468fd
line 318why, one step back318 it("should reflect all changes made in the draft immediately", () => {`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea
line 319why, one step back319 produce(baseState, draft => {Added a testcaseMichel Weststratebefore adoption1e468fd
lines 320–333why, one step back320 draft.anArray[0] = 5321 draft.anArray.unshift("test")322 // sliced here; jest will also compare non-enumerable keys, which would include the immer Symbols323 expect(draft.anArray.slice()).toMatchObject([324 "test",325 5,326 2,327 {c: 3},328 1329 ])330 draft.stuffz = "coffee"331 expect(draft.stuffz).toBe("coffee")332 })333 })Fixed configurability of array.length on proxies. Fixes #50Michel Weststratebefore adoption1c7df26
lines 334–419why, one step back334335 it("should be able to get property descriptors from objects", () => {336 produce({a: 1}, draft => {337 expect("a" in draft).toBe(true)338 expect("b" in draft).toBe(false)339 expect(340 Reflect.ownKeys(draft).filter(x => typeof x === "string")341 ).toEqual(["a"])342343 expect(344 Object.getOwnPropertyDescriptor(draft, "a")345 ).toMatchObject({346 configurable: true,347 enumerable: true348 })349 draft.a = 2350 expect(351 Object.getOwnPropertyDescriptor(draft, "a")352 ).toMatchObject({353 configurable: true,354 enumerable: true355 })356 expect(357 Object.getOwnPropertyDescriptor(draft, "b")358 ).toBeUndefined()359 draft.b = 2360 expect(361 Object.getOwnPropertyDescriptor(draft, "b")362 ).toMatchObject({363 configurable: true,364 enumerable: true365 })366 expect("a" in draft).toBe(true)367 expect("b" in draft).toBe(true)368 expect(369 Reflect.ownKeys(draft).filter(x => typeof x === "string")370 ).toEqual(["a", "b"])371 })372 })373374 it("should be able to get property descriptors from arrays", () => {375 produce([1], draft => {376 expect(0 in draft).toBe(true)377 expect(1 in draft).toBe(false)378 expect("0" in draft).toBe(true)379 expect("1" in draft).toBe(false)380 expect(length in draft).toBe(true)381 expect(382 Reflect.ownKeys(draft).filter(x => typeof x === "string")383 ).toEqual(["0", "length"])384385 expect(386 Object.getOwnPropertyDescriptor(draft, "length")387 ).toMatchObject({388 configurable: false,389 enumerable: false390 })391 draft[0] = 2392 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(393 {394 configurable: true,395 enumerable: true396 }397 )398 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(399 {400 configurable: true,401 enumerable: true402 }403 )404 expect(405 Object.getOwnPropertyDescriptor(draft, 1)406 ).toBeUndefined()407 draft[1] = 2408 expect(Object.getOwnPropertyDescriptor(draft, 1)).toMatchObject(409 {410 configurable: true,411 enumerable: true412 }413 )414 expect(415 Reflect.ownKeys(draft).filter(x => typeof x === "string")416 ).toEqual(["0", "1", "length"])417 })418 })419Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 420why, one step back420 if (useProxies === true) {Fixed configurability of array.length on proxies. Fixes #50Michel Weststratebefore adoption1c7df26
lines 421–431why, one step back421 it("should not be possible to set property descriptors", () => {422 expect(() => {423 produce({}, draft => {424 Object.defineProperty(draft, "xx", {425 enumerable: true,426 writeable: true,427 value: 2428 })429 })430 }).toThrowError(/not support/)431 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 432why, one step back432 }initial solutionMichel Weststratebefore adoptiond618d28
line 433why, one step back433Faster, simpler implementationMichel Weststratebefore adoptionc420f0d
lines 434–438why, one step back434 it("should not throw error, see #53 - 1", () => {435 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}436 const result = produce(base, draft => {437 draft.arr = draft.arr.filter(item => item.count > 2)438 })initial solutionMichel Weststratebefore adoptiond618d28
line 439why, one step back439 expect(result.arr[0].count).toEqual(3)Faster, simpler implementationMichel Weststratebefore adoptionc420f0d
lines 440–470why, one step back440 expect(result).toEqual({441 arr: [{count: 3}]442 })443 expect(result.arr[0]).toBe(base.arr[2])444 })445446 it("should not throw error, see #53 - 2", () => {447 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}448 const result = produce(base, draft => {449 draft.newArr = draft.arr.filter(item => item.count > 2)450 })451 expect(result.newArr[0].count).toEqual(3)452 expect(result.arr).toBe(base.arr)453 expect(result).toEqual({454 arr: [455 {456 count: 1457 },458 {459 count: 2460 },461 {462 count: 3463 }464 ],465 newArr: [466 {467 count: 3468 }469 ]470 })Making several improvements at onceMichel Weststratebefore adoption1116ccf
lines 471–472why, one step back471 expect(result.newArr[0]).toBe(base.arr[2])472 expect(result.arr[2]).toBe(base.arr[2])Faster, simpler implementationMichel Weststratebefore adoptionc420f0d
lines 473–485why, one step back473 })474475 it("should not throw error, see #53 - 3", () => {476 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}477 const result = produce(base, draft => {478 draft.newArr = draft.arr.filter(item => item.count > 2)479 delete draft.arr480 })481 expect(result.newArr[0].count).toEqual(3)482 expect(result).toEqual({483 newArr: [{count: 3}]484 })485 expect(result.newArr[0]).toBe(base.arr[2])initial solutionMichel Weststratebefore adoptiond618d28
line 486why, one step back486 })Added a testcaseMichel Weststratebefore adoption1e468fd
line 487why, one step back487Making several improvements at onceMichel Weststratebefore adoption1116ccf
lines 488–576why, one step back488 it("should not throw error, see #53 - 4", () => {489 const base = {bear: {age: 10}}490 const result = produce(base, draft => {491 draft.bear.legs = 4492 draft.room = {elephant: {kiddo: draft.bear}}493 })494 expect(result).toEqual({495 bear: {age: 10, legs: 4},496 room: {elephant: {kiddo: {age: 10, legs: 4}}}497 })498499 const result2 = produce(result, draft => {500 draft.bear.age = 11501 draft.room.elephant.kiddo.legs = 5502 })503 expect(result2).toEqual({504 bear: {age: 11, legs: 4},505 room: {elephant: {kiddo: {age: 10, legs: 5}}}506 })507 })508509 it("should handle dates correctly", () => {510 const data = {date: new Date()}511 const next = produce(data, draft => {512 draft.x = true513 })514 expect(next).toEqual({x: true, date: data.date})515 expect(next.date).toBe(data.date)516 const next2 = produce(next, draft => {517 draft.date.setYear(2015)518 })519 // This still holds; because produce won't proxy Date objects520 // and the original is actually modified!521 expect(next2).toEqual({x: true, date: data.date})522 expect(next2.date).toBe(next.date)523 expect(next2.date).toBe(data.date)524 expect(next2).toBe(next)525 })526527 it("should handle equality correctly - 1", () => {528 const base = {529 y: 3 / 0,530 z: NaN531 }532 const next = produce(base, draft => {533 draft.y = 4 / 0534 draft.z = NaN535 })536 expect(next).toEqual(base)537 expect(next).toBe(base)538 })539540 it("should handle equality correctly - 2", () => {541 const base = {542 x: -0543 }544 const next = produce(base, draft => {545 draft.x = +1546 })547 expect(next).not.toEqual(base)548 expect(next).not.toBe(base)549 })550551 it("should handle nested immer calls correctly - 1", () => {552 const bear = {paw: {honey: true}}553 const next = produce(bear, draft => {554 const paw2 = produce(bear.paw, draft => {555 draft.honey = false556 })557 expect(paw2.honey).toBe(false)558 expect(draft.paw.honey).toBe(true) // effects should not be visible outside559 })560 expect(next.paw.honey).toBe(true)561 expect(next).toBe(bear)562 })563564 it("should handle nested immer calls correctly - 2", () => {565 const bear = {paw: {honey: true}}566 const next = produce(bear, draft => {567 const paw2 = produce(bear.paw, draft => {568 draft.honey = false569 })570 expect(paw2.honey).toBe(false)571 expect(draft.paw.honey).toBe(true)572 draft.paw = paw2573 expect(draft.paw.honey).toBe(false)574 })575 expect(next.paw.honey).toBe(false)576 expect(next).not.toBe(bear)Reproduced #66Michel Weststratebefore adoptiond5de885
lines 577–600why, one step back577 })578579 it("should not try to change immutable data, see #66", () => {580 const user = require("./test-data")581582 const base = {}583 const next = produce(base, draft => {584 draft.user = user585 })586 expect(next.user).toBe(user)587 expect(next).not.toBe(base)588 expect(next.user).toEqual(user)589 })590591 it("should not try to change immutable data, see #66 - 2", () => {592 const user = deepFreeze(cloneDeep(require("./test-data")))593594 const base = {}595 const next = produce(base, draft => {596 draft.user = user597 })598 expect(next.user).toBe(user)599 expect(next).not.toBe(base)600 expect(next.user).toEqual(user)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 601–661why, one step back601 })602603 if (freeze)604 it("should freeze new data well", () => {605 const base = {}606 const next = produce(base, draft => {607 draft.x = {y: [{z: true}]}608 })609 expect(Object.isFrozen(next)).toBe(true)610 expect(Object.isFrozen(next.x)).toBe(true)611 expect(Object.isFrozen(next.x.y)).toBe(true)612 expect(Object.isFrozen(next.x.y[0].z)).toBe(true)613 })614615 it("should structurally share identical objects in the tree", () => {616 const base = {bear: {legs: 4}, eagle: {legs: 3}}617 const next = produce(base, draft => {618 const animal = draft.bear619 animal.legs = animal.legs + 1620 draft.bear = animal621 draft.eagle = animal622 draft.cow = animal623 draft.kiddo = animal624 })625 expect(next).toEqual({626 bear: {legs: 5},627 eagle: {legs: 5},628 cow: {legs: 5},629 kiddo: {legs: 5}630 })631 expect(next.bear).toBe(next.cow)632 expect(next.kiddo).toBe(next.cow)633 })634635 if (useProxies)636 it("should not allow changing prototype", () => {637 produce({}, draft => {638 expect(() => Object.setPrototypeOf(draft, Array)).toThrow(639 /try this/640 )641 })642 })643644 it("in should work", () => {645 produce(createBaseState(), draft => {646 expect("anArray" in draft).toBe(true)647 expect(Reflect.has(draft, "anArray")).toBe(true)648649 expect("bla" in draft).toBe(false)650 expect(Reflect.has(draft, "bla")).toBe(false)651652 expect(0 in draft.anArray).toBe(true)653 expect("0" in draft.anArray).toBe(true)654 expect(Reflect.has(draft.anArray, 0)).toBe(true)655 expect(Reflect.has(draft.anArray, "0")).toBe(true)656657 expect(17 in draft.anArray).toBe(false)658 expect("17" in draft.anArray).toBe(false)659 expect(Reflect.has(draft.anArray, 17)).toBe(false)660 expect(Reflect.has(draft.anArray, "17")).toBe(false)661 })Making several improvements at onceMichel Weststratebefore adoption1116ccf
lines 662–663why, one step back662 })663Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 664–667why, one step back664 afterEach(() => {665 expect(baseState).toBe(origBaseState)666 expect(baseState).toEqual(createBaseState())667 })Make debugging workMichel Weststratebefore adoptiond0ef4ed
line 668why, one step back668Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 669why, one step back669 function createBaseState() {Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 670why, one step back670 const data = {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 671–678why, one step back671 anArray: [3, 2, {c: 3}, 1],672 aProp: "hi",673 anObject: {674 nested: {675 yummie: true676 },677 coffee: false678 }No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31
line 679why, one step back679 }Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 680why, one step back680 return freeze ? deepFreeze(data) : dataMake debugging workMichel Weststratebefore adoptiond0ef4ed
line 681why, one step back681 }Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 682why, one step back682 })Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
lines 683–687why, one step back683}684685function enumerableOnly(x) {686 // this can be done better...687 return JSON.parse(JSON.stringify(x))Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 688why, one step back688}