base.js
This file
Path
__tests__/base.js
231 lines · 7.3 KiB · JavaScript
Last changed by
Merge branch 'master' into feature/freezeMichel Weststratebefore adoptionf08403a
History of this fileTake it
Added support for frozen trees, and automatically freeze generated treesMichel Weststratebefore adoptionab8987f
line 1why, one step back1"use strict"More repo setupMichel Weststratebefore adoption1637393
lines 2–3why, one step back2import immer from ".."3Make debugging workMichel Weststratebefore adoptiond0ef4ed
lines 4–5why, one step back4describe("base", () => {5 let baseStateWrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
line 6why, one step back6 let origBaseStateMake debugging workMichel Weststratebefore adoptiond0ef4ed
lines 7–8why, one step back78 beforeEach(() => {Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
line 9why, one step back9 origBaseState = baseState = createBaseState()Make debugging workMichel Weststratebefore adoptiond0ef4ed
lines 10–16why, one step back10 })1112 it("should return the original without modifications", () => {13 const nextState = immer(baseState, () => {})14 expect(nextState).toBe(baseState)15 })16Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 17–19why, one step back17 it("should return the original without modifications when reading stuff", () => {18 const nextState = immer(baseState, s => {19 expect(s.aProp).toBe("hi")Merge branch 'master' into feature/freezeMichel Weststratebefore adoptionf08403a
line 20why, one step back20 expect(s.anObject.nested).toEqual({yummie: true})Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 21–24why, one step back21 })22 expect(nextState).toBe(baseState)23 })24Merge branch 'master' into feature/freezeMichel Weststratebefore adoptionf08403a
lines 25–34why, one step back25 it("should not return any value: thunk", () => {26 const warning = jest.spyOn(console, "warn")27 immer(baseState, () => ({bad: "don't do this"}))28 immer(baseState, () => [1, 2, 3])29 immer(baseState, () => false)30 immer(baseState, () => "")3132 expect(warning).toHaveBeenCalledTimes(4)33 })34Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 35–58why, one step back35 it("should return a copy when modifying stuff", () => {36 const nextState = immer(baseState, s => {37 s.aProp = "hello world"38 })39 expect(nextState).not.toBe(baseState)40 expect(baseState.aProp).toBe("hi")41 expect(nextState.aProp).toBe("hello world")42 // structural sharing?43 expect(nextState.nested).toBe(baseState.nested)44 })4546 it("deep change bubbles up", () => {47 const nextState = immer(baseState, s => {48 s.anObject.nested.yummie = false49 })50 expect(nextState).not.toBe(baseState)51 expect(nextState.anObject).not.toBe(baseState.anObject)52 expect(baseState.anObject.nested.yummie).toBe(true)53 expect(nextState.anObject.nested.yummie).toBe(false)54 expect(nextState.anArray).toBe(baseState.anArray)55 })5657 it("can add props", () => {58 const nextState = immer(baseState, s => {Merge branch 'master' into feature/freezeMichel Weststratebefore adoptionf08403a
line 59why, one step back59 s.anObject.cookie = {tasty: true}Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 60–63why, one step back60 })61 expect(nextState).not.toBe(baseState)62 expect(nextState.anObject).not.toBe(baseState.anObject)63 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)Merge branch 'master' into feature/freezeMichel Weststratebefore adoptionf08403a
line 64why, one step back64 expect(nextState.anObject.cookie).toEqual({tasty: true})Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 65–91why, one step back65 })6667 it("can delete props", () => {68 const nextState = immer(baseState, s => {69 delete s.anObject.nested70 })71 expect(nextState).not.toBe(baseState)72 expect(nextState.anObject).not.toBe(baseState.anObject)73 expect(nextState.anObject.nested).toBe(undefined)74 })7576 it("ignores single non-modification", () => {77 const nextState = immer(baseState, s => {78 s.aProp = "hi"79 })80 expect(nextState).toBe(baseState)81 })8283 it("processes single modification", () => {84 const nextState = immer(baseState, s => {85 s.aProp = "hello"86 s.aProp = "hi"87 })88 expect(nextState).not.toBe(baseState)89 expect(nextState).toEqual(baseState)90 })91Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 92–115why, one step back92 it("should support reading arrays", () => {93 const nextState = immer(baseState, s => {94 s.anArray.slice()95 })96 expect(nextState.anArray).toBe(baseState.anArray)97 expect(nextState).toBe(baseState)98 })99100 it("should support changing arrays", () => {101 const nextState = immer(baseState, s => {102 s.anArray[3] = true103 })104 expect(nextState).not.toBe(baseState)105 expect(nextState.anArray).not.toBe(baseState.anArray)106 expect(nextState.anArray[3]).toEqual(true)107 })108109 it("should support changing arrays - 2", () => {110 const nextState = immer(baseState, s => {111 s.anArray.splice(1, 1, "a", "b")112 })113 expect(nextState).not.toBe(baseState)114 expect(nextState.anArray).not.toBe(baseState.anArray)115Merge branch 'master' into feature/freezeMichel Weststratebefore adoptionf08403a
line 116why, one step back116 expect(nextState.anArray).toEqual([3, "a", "b", {c: 3}, 1])Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 117–126why, one step back117 })118119 it("should support sorting arrays", () => {120 const nextState = immer(baseState, s => {121 s.anArray[2].c = 4122 s.anArray.sort()123 s.anArray[3].c = 5124 })125 expect(nextState).not.toBe(baseState)126 expect(nextState.anArray).not.toBe(baseState.anArray)Merge branch 'master' into feature/freezeMichel Weststratebefore adoptionf08403a
line 127why, one step back127 expect(nextState.anArray).toEqual([1, 2, 3, {c: 5}])Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 128–129why, one step back128 })129Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 130–135why, one step back130 it("should updating inside arrays", () => {131 const nextState = immer(baseState, s => {132 s.anArray[2].test = true133 })134 expect(nextState).not.toBe(baseState)135 expect(nextState.anArray).not.toBe(baseState.anArray)Merge branch 'master' into feature/freezeMichel Weststratebefore adoptionf08403a
line 136why, one step back136 expect(nextState.anArray).toEqual([3, 2, {c: 3, test: true}, 1])Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 137–138why, one step back137 })138Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 139–147why, one step back139 it("reusing object should work", () => {140 const nextState = immer(baseState, s => {141 const obj = s.anObject142 delete s.anObject143 s.messy = obj144 })145 expect(nextState).not.toBe(baseState)146 expect(nextState.anArray).toBe(baseState.anArray)147 expect(nextState).toEqual({Merge branch 'master' into feature/freezeMichel Weststratebefore adoptionf08403a
line 148why, one step back148 anArray: [3, 2, {c: 3}, 1],Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 149–151why, one step back149 aProp: "hi",150 messy: {151 nested: {Merge branch 'master' into feature/freezeMichel Weststratebefore adoptionf08403a
line 152why, one step back152 yummie: true,Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
line 153why, one step back153 },Merge branch 'master' into feature/freezeMichel Weststratebefore adoptionf08403a
lines 154–155why, one step back154 coffee: false,155 },Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 156–170why, one step back156 })157 expect(nextState.messy.nested).toBe(baseState.anObject.nested)158 })159160 it("refs should be transparent", () => {161 const nextState = immer(baseState, s => {162 const obj = s.anObject163 s.aProp = "hello"164 delete s.anObject165 obj.coffee = true166 s.messy = obj167 })168 expect(nextState).not.toBe(baseState)169 expect(nextState.anArray).toBe(baseState.anArray)170 expect(nextState).toEqual({Merge branch 'master' into feature/freezeMichel Weststratebefore adoptionf08403a
line 171why, one step back171 anArray: [3, 2, {c: 3}, 1],Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 172–174why, one step back172 aProp: "hello",173 messy: {174 nested: {Merge branch 'master' into feature/freezeMichel Weststratebefore adoptionf08403a
line 175why, one step back175 yummie: true,Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
line 176why, one step back176 },Merge branch 'master' into feature/freezeMichel Weststratebefore adoptionf08403a
lines 177–178why, one step back177 coffee: true,178 },Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 179–182why, one step back179 })180 expect(nextState.messy.nested).toBe(baseState.anObject.nested)181 })182Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e
lines 183–187why, one step back183 it("should allow setting to undefined a defined draft property", () => {184 const nextState = immer(baseState, s => {185 s.aProp = undefined186 })187 expect(nextState).not.toBe(baseState)Added support for frozen trees, and automatically freeze generated treesMichel Weststratebefore adoptionab8987f
line 188why, one step back188 expect(baseState.aProp).toBe("hi")Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e
lines 189–191why, one step back189 expect(nextState.aProp).toBe(undefined)190 })191Merge branch 'master' into feature/freezeMichel Weststratebefore adoptionf08403a
lines 192–213why, one step back192 it("should revoke the proxy of the baseState after immer function is executed", () => {193 let proxy194 const nextState = immer(baseState, s => {195 proxy = s196 s.aProp = "hello"197 })198 expect(nextState).not.toBe(baseState)199 expect(baseState.aProp).toBe("hi")200 expect(nextState.aProp).toBe("hello")201202 expect(() => {203 proxy.aProp = "Hallo"204 }).toThrowError(/^Cannot perform.*on a proxy that has been revoked/)205 expect(() => {206 const aProp = proxy.aProp207 }).toThrowError(/^Cannot perform.*on a proxy that has been revoked/)208209 expect(nextState).not.toBe(baseState)210 expect(baseState.aProp).toBe("hi")211 expect(nextState.aProp).toBe("hello")212 })213Make debugging workMichel Weststratebefore adoptiond0ef4ed
line 214why, one step back214 afterEach(() => {Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
line 215why, one step back215 expect(baseState).toBe(origBaseState)Make debugging workMichel Weststratebefore adoptiond0ef4ed
lines 216–218why, one step back216 expect(baseState).toEqual(createBaseState())217 })218Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 219–220why, one step back219 function createBaseState() {220 return {Merge branch 'master' into feature/freezeMichel Weststratebefore adoptionf08403a
line 221why, one step back221 anArray: [3, 2, {c: 3}, 1],Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 222–224why, one step back222 aProp: "hi",223 anObject: {224 nested: {Merge branch 'master' into feature/freezeMichel Weststratebefore adoptionf08403a
line 225why, one step back225 yummie: true,Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
line 226why, one step back226 },Merge branch 'master' into feature/freezeMichel Weststratebefore adoptionf08403a
lines 227–228why, one step back227 coffee: false,228 },Make debugging workMichel Weststratebefore adoptiond0ef4ed
lines 229–230why, one step back229 }230 }Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
line 231why, one step back231})