base.js
This file
Path
__tests__/base.js
326 lines · 11.4 KiB · JavaScript
Last changed by
improved frozen testMichel Weststratebefore adoption1fa3715
History of this fileTake it
Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 1why, one step back1"use strict"improved test setupMichel Weststratebefore adoption6990c33
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 back5improved test setupMichel Weststratebefore adoption6990c33
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 back10improved test setupMichel Weststratebefore adoption6990c33
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}`, () => {improved test setupMichel Weststratebefore adoption6990c33
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(() => {improved test setupMichel Weststratebefore adoption6990c33
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 #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 57–65why, one step back57 it("deep change bubbles up", () => {58 const nextState = immer(baseState, s => {59 s.anObject.nested.yummie = false60 })61 expect(nextState).not.toBe(baseState)62 expect(nextState.anObject).not.toBe(baseState.anObject)63 expect(baseState.anObject.nested.yummie).toBe(true)64 expect(nextState.anObject.nested.yummie).toBe(false)65 expect(nextState.anArray).toBe(baseState.anArray)Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 66–67why, one step back66 })67Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 68–75why, one step back68 it("can add props", () => {69 const nextState = immer(baseState, s => {70 s.anObject.cookie = {tasty: true}71 })72 expect(nextState).not.toBe(baseState)73 expect(nextState.anObject).not.toBe(baseState.anObject)74 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)75 expect(nextState.anObject.cookie).toEqual({tasty: true})Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 76–77why, one step back76 })77Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 78–84why, one step back78 it("can delete props", () => {79 const nextState = immer(baseState, s => {80 delete s.anObject.nested81 })82 expect(nextState).not.toBe(baseState)83 expect(nextState.anObject).not.toBe(baseState.anObject)84 expect(nextState.anObject.nested).toBe(undefined)Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 85–86why, one step back85 })86Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 87–91why, one step back87 it("ignores single non-modification", () => {88 const nextState = immer(baseState, s => {89 s.aProp = "hi"90 })91 expect(nextState).toBe(baseState)Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 92–93why, one step back92 })93Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 94–100why, one step back94 it("processes single modification", () => {95 const nextState = immer(baseState, s => {96 s.aProp = "hello"97 s.aProp = "hi"98 })99 expect(nextState).not.toBe(baseState)100 expect(nextState).toEqual(baseState)Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 101–102why, one step back101 })102Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 103–108why, one step back103 it("should support reading arrays", () => {104 const nextState = immer(baseState, s => {105 s.anArray.slice()106 })107 expect(nextState.anArray).toBe(baseState.anArray)108 expect(nextState).toBe(baseState)Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 109–110why, one step back109 })110Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 111–117why, one step back111 it("should support changing arrays", () => {112 const nextState = immer(baseState, s => {113 s.anArray[3] = true114 })115 expect(nextState).not.toBe(baseState)116 expect(nextState.anArray).not.toBe(baseState.anArray)117 expect(nextState.anArray[3]).toEqual(true)Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 118–119why, one step back118 })119Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 120–125why, one step back120 it("should support changing arrays - 2", () => {121 const nextState = immer(baseState, s => {122 s.anArray.splice(1, 1, "a", "b")123 })124 expect(nextState).not.toBe(baseState)125 expect(nextState.anArray).not.toBe(baseState.anArray)Add support for arraysMichel Weststratebefore adoptiond3696cb
line 126why, one step back126improved test setupMichel Weststratebefore adoption6990c33
lines 127–133why, one step back127 expect(enumerableOnly(nextState.anArray)).toEqual([128 3,129 "a",130 "b",131 {c: 3},132 1133 ])Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 134–135why, one step back134 })135Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 136–143why, one step back136 it("should support sorting arrays", () => {137 const nextState = immer(baseState, s => {138 s.anArray[2].c = 4139 s.anArray.sort()140 s.anArray[3].c = 5141 })142 expect(nextState).not.toBe(baseState)143 expect(nextState.anArray).not.toBe(baseState.anArray)improved test setupMichel Weststratebefore adoption6990c33
line 144why, one step back144 expect(enumerableOnly(nextState.anArray)).toEqual([1, 2, 3, {c: 5}])Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 145–146why, one step back145 })146improved frozen testMichel Weststratebefore adoption1fa3715
line 147why, one step back147 it.skip("should support sorting arrays - 2", () => {improved test setupMichel Weststratebefore adoption6990c33
lines 148–159why, one step back148 const nextState = immer([], s => {149 debugger150 s.unshift("x")151 // console.dir(s)152 // console.dir(Object.getOwnPropertyDescriptor(s, "length"))153 // console.dir(Object.getOwnPropertyDescriptor(s, 0))154 expect(s.length).toBe(1)155 expect(s[0] === "x").toBe(true)156 })157 expect(nextState).toEqual(["x"])158 })159improved frozen testMichel Weststratebefore adoption1fa3715
line 160why, one step back160 it.skip("should support sorting arrays - 2", () => {improved test setupMichel Weststratebefore adoption6990c33
lines 161–182why, one step back161 const nextState = immer(baseState, s => {162 debugger163 s.anArray.unshift("x")164 s.anArray[3].c = 4165 // console.dir(s.anArray)166 s.anArray.sort()167 // console.dir(s.anArray)168 s.anArray[3].c = 5169 s.anArray.unshift("y")170 })171 expect(nextState).not.toBe(baseState)172 expect(nextState.anArray).not.toBe(baseState.anArray)173 expect(enumerableOnly(nextState.anArray)).toEqual([174 "y",175 1,176 2,177 3,178 {c: 5},179 "x"180 ])181 })182Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 183–189why, one step back183 it("should updating inside arrays", () => {184 const nextState = immer(baseState, s => {185 s.anArray[2].test = true186 })187 expect(nextState).not.toBe(baseState)188 expect(nextState.anArray).not.toBe(baseState.anArray)189 expect(nextState.anArray).toEqual([3, 2, {c: 3, test: true}, 1])Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 190–191why, one step back190 })191Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 192–199why, one step back192 it("reusing object should work", () => {193 const nextState = immer(baseState, s => {194 const obj = s.anObject195 delete s.anObject196 s.messy = obj197 })198 expect(nextState).not.toBe(baseState)199 expect(nextState.anArray).toBe(baseState.anArray)improved test setupMichel Weststratebefore adoption6990c33
line 200why, one step back200 expect(enumerableOnly(nextState)).toEqual({Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 201–210why, one step back201 anArray: [3, 2, {c: 3}, 1],202 aProp: "hi",203 messy: {204 nested: {205 yummie: true206 },207 coffee: false208 }209 })210 expect(nextState.messy.nested).toBe(baseState.anObject.nested)Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 211–212why, one step back211 })212Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 213–222why, one step back213 it("refs should be transparent", () => {214 const nextState = immer(baseState, s => {215 const obj = s.anObject216 s.aProp = "hello"217 delete s.anObject218 obj.coffee = true219 s.messy = obj220 })221 expect(nextState).not.toBe(baseState)222 expect(nextState.anArray).toBe(baseState.anArray)improved test setupMichel Weststratebefore adoption6990c33
line 223why, one step back223 expect(enumerableOnly(nextState)).toEqual({Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 224–233why, one step back224 anArray: [3, 2, {c: 3}, 1],225 aProp: "hello",226 messy: {227 nested: {228 yummie: true229 },230 coffee: true231 }232 })233 expect(nextState.messy.nested).toBe(baseState.anObject.nested)Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e
lines 234–235why, one step back234 })235Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 236–242why, one step back236 it("should allow setting to undefined a defined draft property", () => {237 const nextState = immer(baseState, s => {238 s.aProp = undefined239 })240 expect(nextState).not.toBe(baseState)241 expect(baseState.aProp).toBe("hi")242 expect(nextState.aProp).toBe(undefined)Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
lines 243–244why, one step back243 })244Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 245–255why, one step back245 // ES implementation does't protect against all outside modifications, just some..246 if (name === "proxy") {247 it("should revoke the proxy of the baseState after immer function is executed", () => {248 let proxy249 const nextState = immer(baseState, s => {250 proxy = s251 s.aProp = "hello"252 })253 expect(nextState).not.toBe(baseState)254 expect(baseState.aProp).toBe("hi")255 expect(nextState.aProp).toBe("hello")Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca
line 256why, one step back256Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 257–262why, one step back257 expect(() => {258 proxy.aProp = "Hallo"259 }).toThrowError(/revoked/)260 expect(() => {261 const aProp = proxy.aProp262 }).toThrowError(/revoked/)Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494
line 263why, one step back263Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 264–282why, one step back264 expect(nextState).not.toBe(baseState)265 expect(baseState.aProp).toBe("hi")266 expect(nextState.aProp).toBe("hello")267 })268 }269270 it("should revoke the proxy of the baseState after immer function is executed - 2", () => {271 let proxy272 const nextState = immer(baseState, s => {273 proxy = s.anObject274 })275 expect(nextState).toBe(baseState)276 expect(() => {277 // In ES5 implemenation only protects existing props, but alas..278 proxy.coffee = "Hallo"279 }).toThrowError(/revoked/)280 expect(() => {281 const test = proxy.coffee282 }).toThrowError(/revoked/)Updated changelogMichel Weststratebefore adoption8f44348
lines 283–284why, one step back283 })284Added a testcaseMichel Weststratebefore adoption1e468fd
lines 285–301why, one step back285 it("should reflect all changes made in the draft immediately", () => {286 immer(baseState, draft => {287 draft.anArray[0] = 5288 draft.anArray.unshift("test")289 // sliced here; jest will also compare non-enumerable keys, which would include the immer Symbols290 expect(draft.anArray.slice()).toMatchObject([291 "test",292 5,293 2,294 {c: 3},295 1296 ])297 draft.stuffz = "coffee"298 expect(draft.stuffz).toBe("coffee")299 })300 })301Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 302–305why, one step back302 afterEach(() => {303 expect(baseState).toBe(origBaseState)304 expect(baseState).toEqual(createBaseState())305 })Make debugging workMichel Weststratebefore adoptiond0ef4ed
line 306why, one step back306Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 307why, one step back307 function createBaseState() {improved test setupMichel Weststratebefore adoption6990c33
line 308why, one step back308 const data = {Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
lines 309–316why, one step back309 anArray: [3, 2, {c: 3}, 1],310 aProp: "hi",311 anObject: {312 nested: {313 yummie: true314 },315 coffee: false316 }No trailing commas, might break babel, fixes #21Michel Weststratebefore adoption2a1da31
line 317why, one step back317 }improved test setupMichel Weststratebefore adoption6990c33
line 318why, one step back318 return freeze ? deepFreeze(data) : dataMake debugging workMichel Weststratebefore adoptiond0ef4ed
line 319why, one step back319 }Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 320why, one step back320 })improved test setupMichel Weststratebefore adoption6990c33
lines 321–325why, one step back321}322323function enumerableOnly(x) {324 // this can be done better...325 return JSON.parse(JSON.stringify(x))Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb
line 326why, one step back326}