base.js
This file
Path
__tests__/base.js
363 lines · 12.7 KiB · JavaScript
Last changed by
Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
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 #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
lines 2–4why, one step back2import * as immerProxy from ".."3import * as immerEs5 from "../es5"4import deepFreeze from "deep-freeze"More repo setupMichel Weststratebefore adoption1637393
line 5why, one step back5Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
lines 6–9why, one step back6runBaseTest("proxy (no freeze)", immerProxy, false)7runBaseTest("proxy (autofreeze)", immerProxy, true)8runBaseTest("es5 (no freeze)", immerEs5, false)9runBaseTest("es5 (autofreeze)", immerEs5, true)Make debugging workMichel Weststratebefore adoptiond0ef4ed
line 10why, one step back10Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 11why, one step back11function runBaseTest(name, lib, freeze) {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 12why, one step back12 describe(`base functionality - ${name}`, () => {Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 13why, one step back13 const immer = lib.defaultMerge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 14–15why, one step back14 let baseState15 let origBaseStateMake debugging workMichel Weststratebefore adoptiond0ef4ed
line 16why, one step back16Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 17why, one step back17 beforeEach(() => {Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 18why, one step back18 lib.setAutoFreeze(freeze)Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 19–20why, one step back19 origBaseState = baseState = createBaseState()20 })Make debugging workMichel Weststratebefore adoptiond0ef4ed
line 21why, one step back21Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 22–24why, one step back22 it("should return the original without modifications", () => {23 const nextState = immer(baseState, () => {})24 expect(nextState).toBe(baseState)Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 25–26why, one step back25 })26Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 27–33why, one step back27 it("should return the original without modifications when reading stuff", () => {28 const nextState = immer(baseState, s => {29 expect(s.aProp).toBe("hi")30 expect(s.anObject.nested).toMatchObject({yummie: true})31 })32 expect(nextState).toBe(baseState)33 })Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
line 34why, one step back34Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 35–40why, one step back35 it("should not return any value: thunk", () => {36 const warning = jest.spyOn(console, "warn")37 immer(baseState, () => ({bad: "don't do this"}))38 immer(baseState, () => [1, 2, 3])39 immer(baseState, () => false)40 immer(baseState, () => "")Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed
line 41why, one step back41Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 42–43why, one step back42 expect(warning).toHaveBeenCalledTimes(4)43 warning.mockClear()Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 44–45why, one step back44 })45Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 46–54why, one step back46 it("should return a copy when modifying stuff", () => {47 const nextState = immer(baseState, s => {48 s.aProp = "hello world"49 })50 expect(nextState).not.toBe(baseState)51 expect(baseState.aProp).toBe("hi")52 expect(nextState.aProp).toBe("hello world")53 // structural sharing?54 expect(nextState.nested).toBe(baseState.nested)Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 55–56why, one step back55 })56Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 57–69why, one step back57 if (58 ("should preserve type",59 () => {60 const nextState = immer(baseState, s => {61 expect(Array.isArray(s)).toBe(true)62 expect(s.protoType).toBe(Object)63 s.anArray.push(3)64 s.aProp = "hello world"65 expect(Array.isArray(s)).toBe(true)66 expect(s.protoType).toBe(Object)67 })68 expect(Array.isArray(nextState)).toBe(true)69 expect(nextState.protoType).toBe(Object)Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 70why, one step back70 })Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 71–81why, one step back71 )72 it("deep change bubbles up", () => {73 const nextState = immer(baseState, s => {74 s.anObject.nested.yummie = false75 })76 expect(nextState).not.toBe(baseState)77 expect(nextState.anObject).not.toBe(baseState.anObject)78 expect(baseState.anObject.nested.yummie).toBe(true)79 expect(nextState.anObject.nested.yummie).toBe(false)80 expect(nextState.anArray).toBe(baseState.anArray)81 })Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
line 82why, one step back82Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 83–90why, one step back83 it("can add props", () => {84 const nextState = immer(baseState, s => {85 s.anObject.cookie = {tasty: true}86 })87 expect(nextState).not.toBe(baseState)88 expect(nextState.anObject).not.toBe(baseState.anObject)89 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)90 expect(nextState.anObject.cookie).toEqual({tasty: true})Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 91–92why, one step back91 })92Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 93–99why, one step back93 it("can delete props", () => {94 const nextState = immer(baseState, s => {95 delete s.anObject.nested96 })97 expect(nextState).not.toBe(baseState)98 expect(nextState.anObject).not.toBe(baseState.anObject)99 expect(nextState.anObject.nested).toBe(undefined)Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 100–101why, one step back100 })101Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 102–106why, one step back102 it("ignores single non-modification", () => {103 const nextState = immer(baseState, s => {104 s.aProp = "hi"105 })106 expect(nextState).toBe(baseState)Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 107–108why, one step back107 })108Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 109–115why, one step back109 it("processes single modification", () => {110 const nextState = immer(baseState, s => {111 s.aProp = "hello"112 s.aProp = "hi"113 })114 expect(nextState).not.toBe(baseState)115 expect(nextState).toEqual(baseState)Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 116–117why, one step back116 })117Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 118–123why, one step back118 it("should support reading arrays", () => {119 const nextState = immer(baseState, s => {120 s.anArray.slice()121 })122 expect(nextState.anArray).toBe(baseState.anArray)123 expect(nextState).toBe(baseState)Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 124–125why, one step back124 })125Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 126–132why, one step back126 it("should support changing arrays", () => {127 const nextState = immer(baseState, s => {128 s.anArray[3] = true129 })130 expect(nextState).not.toBe(baseState)131 expect(nextState.anArray).not.toBe(baseState.anArray)132 expect(nextState.anArray[3]).toEqual(true)Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 133–134why, one step back133 })134Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 135–140why, one step back135 it("should support changing arrays - 2", () => {136 const nextState = immer(baseState, s => {137 s.anArray.splice(1, 1, "a", "b")138 })139 expect(nextState).not.toBe(baseState)140 expect(nextState.anArray).not.toBe(baseState.anArray)Add support for arraysMichel Weststratebefore adoptiond3696cb
line 141why, one step back141Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
lines 142–148why, one step back142 expect(enumerableOnly(nextState.anArray)).toEqual([143 3,144 "a",145 "b",146 {c: 3},147 1148 ])Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 149–150why, one step back149 })150Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 151–160why, one step back151 it("can delete array items", () => {152 const nextState = immer(baseState, s => {153 s.anArray.length = 3154 })155 expect(nextState).not.toBe(baseState)156 expect(nextState.anObject).toBe(baseState.anObject)157 expect(nextState.anArray).not.toBe(baseState.anArray)158 expect(nextState.anArray).toEqual([3, 2, {c: 3}])159 })160Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 161–168why, one step back161 it("should support sorting arrays", () => {162 const nextState = immer(baseState, s => {163 s.anArray[2].c = 4164 s.anArray.sort()165 s.anArray[3].c = 5166 })167 expect(nextState).not.toBe(baseState)168 expect(nextState.anArray).not.toBe(baseState.anArray)Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 169why, one step back169 expect(enumerableOnly(nextState.anArray)).toEqual([1, 2, 3, {c: 5}])Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 170–171why, one step back170 })171Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
lines 172–213why, one step back172 it("should expose property descriptors", () => {173 const nextState = immer([], s => {174 expect(Object.getOwnPropertyDescriptor(s, 0)).toBe(undefined)175 s.unshift("x")176 expect(Object.getOwnPropertyDescriptor(s, 0)).toEqual({177 configurable: true,178 enumerable: true,179 value: "x",180 writable: true181 })182 expect(s.length).toBe(1)183 expect(s[0] === "x").toBe(true)184 })185 expect(nextState).toEqual(["x"])186 expect(Object.getOwnPropertyDescriptor(nextState, 0)).toEqual({187 configurable: !freeze,188 enumerable: true,189 value: "x",190 writable: !freeze191 })192 })193194 it("should support sorting arrays - 2", () => {195 const nextState = immer(baseState, s => {196 s.anArray.unshift("x")197 s.anArray[3].c = 4198 s.anArray.sort()199 s.anArray[3].c = 5200 s.anArray.unshift("y")201 })202 expect(nextState).not.toBe(baseState)203 expect(nextState.anArray).not.toBe(baseState.anArray)204 expect(enumerableOnly(nextState.anArray)).toEqual([205 "y",206 1,207 2,208 3,209 {c: 5},210 "x"211 ])212 })213Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 214–220why, one step back214 it("should updating inside arrays", () => {215 const nextState = immer(baseState, s => {216 s.anArray[2].test = true217 })218 expect(nextState).not.toBe(baseState)219 expect(nextState.anArray).not.toBe(baseState.anArray)220 expect(nextState.anArray).toEqual([3, 2, {c: 3, test: true}, 1])Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 221–222why, one step back221 })222Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 223–230why, one step back223 it("reusing object should work", () => {224 const nextState = immer(baseState, s => {225 const obj = s.anObject226 delete s.anObject227 s.messy = obj228 })229 expect(nextState).not.toBe(baseState)230 expect(nextState.anArray).toBe(baseState.anArray)Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 231why, one step back231 expect(enumerableOnly(nextState)).toEqual({Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 232–241why, one step back232 anArray: [3, 2, {c: 3}, 1],233 aProp: "hi",234 messy: {235 nested: {236 yummie: true237 },238 coffee: false239 }240 })241 expect(nextState.messy.nested).toBe(baseState.anObject.nested)Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 242–243why, one step back242 })243Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 244–250why, one step back244 it("refs should be transparent", () => {245 const nextState = immer(baseState, s => {246 const obj = s.anObject247 s.aProp = "hello"248 delete s.anObject249 obj.coffee = true250 s.messy = objMerge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
line 251why, one step back251 debuggerMerge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 252–254why, one step back252 })253 expect(nextState).not.toBe(baseState)254 expect(nextState.anArray).toBe(baseState.anArray)Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 255why, one step back255 expect(enumerableOnly(nextState)).toEqual({Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 256–265why, one step back256 anArray: [3, 2, {c: 3}, 1],257 aProp: "hello",258 messy: {259 nested: {260 yummie: true261 },262 coffee: true263 }264 })265 expect(nextState.messy.nested).toBe(baseState.anObject.nested)Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e
lines 266–267why, one step back266 })267Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 268–274why, one step back268 it("should allow setting to undefined a defined draft property", () => {269 const nextState = immer(baseState, s => {270 s.aProp = undefined271 })272 expect(nextState).not.toBe(baseState)273 expect(baseState.aProp).toBe("hi")274 expect(nextState.aProp).toBe(undefined)Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
lines 275–276why, one step back275 })276Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 277–287why, one step back277 // ES implementation does't protect against all outside modifications, just some..278 if (name === "proxy") {279 it("should revoke the proxy of the baseState after immer function is executed", () => {280 let proxy281 const nextState = immer(baseState, s => {282 proxy = s283 s.aProp = "hello"284 })285 expect(nextState).not.toBe(baseState)286 expect(baseState.aProp).toBe("hi")287 expect(nextState.aProp).toBe("hello")Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
line 288why, one step back288Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 289–294why, one step back289 expect(() => {290 proxy.aProp = "Hallo"291 }).toThrowError(/revoked/)292 expect(() => {293 const aProp = proxy.aProp294 }).toThrowError(/revoked/)Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 295why, one step back295Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 296–314why, one step back296 expect(nextState).not.toBe(baseState)297 expect(baseState.aProp).toBe("hi")298 expect(nextState.aProp).toBe("hello")299 })300 }301302 it("should revoke the proxy of the baseState after immer function is executed - 2", () => {303 let proxy304 const nextState = immer(baseState, s => {305 proxy = s.anObject306 })307 expect(nextState).toBe(baseState)308 expect(() => {309 // In ES5 implemenation only protects existing props, but alas..310 proxy.coffee = "Hallo"311 }).toThrowError(/revoked/)312 expect(() => {313 const test = proxy.coffee314 }).toThrowError(/revoked/)Updated changelogMichel Weststratebefore adoption8f44348
lines 315–316why, one step back315 })316Added a testcaseMichel Weststratebefore adoption1e468fd
lines 317–333why, one step back317 it("should reflect all changes made in the draft immediately", () => {318 immer(baseState, draft => {319 draft.anArray[0] = 5320 draft.anArray.unshift("test")321 // sliced here; jest will also compare non-enumerable keys, which would include the immer Symbols322 expect(draft.anArray.slice()).toMatchObject([323 "test",324 5,325 2,326 {c: 3},327 1328 ])329 draft.stuffz = "coffee"330 expect(draft.stuffz).toBe("coffee")331 })332 })333Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 334–337why, one step back334 afterEach(() => {335 expect(baseState).toBe(origBaseState)336 expect(baseState).toEqual(createBaseState())337 })Make debugging workMichel Weststratebefore adoptiond0ef4ed
line 338why, one step back338Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 339why, one step back339 function createBaseState() {Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 340why, one step back340 const data = {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 341–348why, one step back341 anArray: [3, 2, {c: 3}, 1],342 aProp: "hi",343 anObject: {344 nested: {345 yummie: true346 },347 coffee: false348 }No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31
line 349why, one step back349 }Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
line 350why, one step back350 return freeze ? deepFreeze(data) : dataMake debugging workMichel Weststratebefore adoptiond0ef4ed
line 351why, one step back351 }Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 352why, one step back352 })Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957
lines 353–357why, one step back353}354355function enumerableOnly(x) {356 // this can be done better...357 return JSON.parse(JSON.stringify(x))Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 358why, one step back358}Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff
lines 359–363why, one step back359360// TODO: test problem scenarios361// nesting immmer362// non-trees363// complex objects / functions