base.js
This file
Path
__tests__/base.js
233 lines · 7.1 KiB · JavaScript
Last changed by
Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e
History of this fileTake it
More repo setupMichel Weststratebefore adoption1637393
lines 1–2why, one step back1import immer from ".."2Make debugging workMichel Weststratebefore adoptiond0ef4ed
lines 3–4why, one step back3describe("base", () => {4 let baseStateWrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
line 5why, one step back5 let origBaseStateMake debugging workMichel Weststratebefore adoptiond0ef4ed
lines 6–7why, one step back67 beforeEach(() => {Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
line 8why, one step back8 origBaseState = baseState = createBaseState()Make debugging workMichel Weststratebefore adoptiond0ef4ed
lines 9–15why, one step back9 })1011 it("should return the original without modifications", () => {12 const nextState = immer(baseState, () => {})13 expect(nextState).toBe(baseState)14 })15Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 16–80why, one step back16 it("should return the original without modifications when reading stuff", () => {17 const nextState = immer(baseState, s => {18 expect(s.aProp).toBe("hi")19 expect(s.anObject.nested).toEqual({ yummie: true })20 })21 expect(nextState).toBe(baseState)22 })2324 it("should return a copy when modifying stuff", () => {25 const nextState = immer(baseState, s => {26 s.aProp = "hello world"27 })28 expect(nextState).not.toBe(baseState)29 expect(baseState.aProp).toBe("hi")30 expect(nextState.aProp).toBe("hello world")31 // structural sharing?32 expect(nextState.nested).toBe(baseState.nested)33 })3435 it("deep change bubbles up", () => {36 const nextState = immer(baseState, s => {37 s.anObject.nested.yummie = false38 })39 expect(nextState).not.toBe(baseState)40 expect(nextState.anObject).not.toBe(baseState.anObject)41 expect(baseState.anObject.nested.yummie).toBe(true)42 expect(nextState.anObject.nested.yummie).toBe(false)43 expect(nextState.anArray).toBe(baseState.anArray)44 })4546 it("can add props", () => {47 const nextState = immer(baseState, s => {48 s.anObject.cookie = { tasty: true }49 })50 expect(nextState).not.toBe(baseState)51 expect(nextState.anObject).not.toBe(baseState.anObject)52 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)53 expect(nextState.anObject.cookie).toEqual({ tasty: true })54 })5556 it("can delete props", () => {57 const nextState = immer(baseState, s => {58 delete s.anObject.nested59 })60 expect(nextState).not.toBe(baseState)61 expect(nextState.anObject).not.toBe(baseState.anObject)62 expect(nextState.anObject.nested).toBe(undefined)63 })6465 it("ignores single non-modification", () => {66 const nextState = immer(baseState, s => {67 s.aProp = "hi"68 })69 expect(nextState).toBe(baseState)70 })7172 it("processes single modification", () => {73 const nextState = immer(baseState, s => {74 s.aProp = "hello"75 s.aProp = "hi"76 })77 expect(nextState).not.toBe(baseState)78 expect(nextState).toEqual(baseState)79 })80Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 81–118why, one step back81 it("should support reading arrays", () => {82 const nextState = immer(baseState, s => {83 s.anArray.slice()84 })85 expect(nextState.anArray).toBe(baseState.anArray)86 expect(nextState).toBe(baseState)87 })8889 it("should support changing arrays", () => {90 const nextState = immer(baseState, s => {91 s.anArray[3] = true92 })93 expect(nextState).not.toBe(baseState)94 expect(nextState.anArray).not.toBe(baseState.anArray)95 expect(nextState.anArray[3]).toEqual(true)96 })9798 it("should support changing arrays - 2", () => {99 const nextState = immer(baseState, s => {100 s.anArray.splice(1, 1, "a", "b")101 })102 expect(nextState).not.toBe(baseState)103 expect(nextState.anArray).not.toBe(baseState.anArray)104105 expect(nextState.anArray).toEqual([3, "a", "b", { c: 3 }, 1])106 })107108 it("should support sorting arrays", () => {109 const nextState = immer(baseState, s => {110 s.anArray[2].c = 4111 s.anArray.sort()112 s.anArray[3].c = 5113 })114 expect(nextState).not.toBe(baseState)115 expect(nextState.anArray).not.toBe(baseState.anArray)116 expect(nextState.anArray).toEqual([1, 2, 3, { c: 5 }])117 })118Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 119–127why, one step back119 it("should updating inside arrays", () => {120 const nextState = immer(baseState, s => {121 s.anArray[2].test = true122 })123 expect(nextState).not.toBe(baseState)124 expect(nextState.anArray).not.toBe(baseState.anArray)125 expect(nextState.anArray).toEqual([3, 2, { c: 3, test: true }, 1])126 })127Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 128–172why, one step back128 it("reusing object should work", () => {129 const nextState = immer(baseState, s => {130 debugger131 const obj = s.anObject132 delete s.anObject133 s.messy = obj134 })135 expect(nextState).not.toBe(baseState)136 expect(nextState.anArray).toBe(baseState.anArray)137 expect(nextState).toEqual({138 anArray: [3, 2, { c: 3 }, 1],139 aProp: "hi",140 messy: {141 nested: {142 yummie: true143 },144 coffee: false145 }146 })147 expect(nextState.messy.nested).toBe(baseState.anObject.nested)148 })149150 it("refs should be transparent", () => {151 const nextState = immer(baseState, s => {152 const obj = s.anObject153 s.aProp = "hello"154 delete s.anObject155 obj.coffee = true156 s.messy = obj157 })158 expect(nextState).not.toBe(baseState)159 expect(nextState.anArray).toBe(baseState.anArray)160 expect(nextState).toEqual({161 anArray: [3, 2, { c: 3 }, 1],162 aProp: "hello",163 messy: {164 nested: {165 yummie: true166 },167 coffee: true168 }169 })170 expect(nextState.messy.nested).toBe(baseState.anObject.nested)171 })172Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e
lines 173–181why, one step back173 it("should allow setting to undefined a defined draft property", () => {174 const nextState = immer(baseState, s => {175 s.aProp = undefined176 })177 expect(nextState).not.toBe(baseState)178 expect(baseState.aProp).toBe('hi')179 expect(nextState.aProp).toBe(undefined)180 })181Make debugging workMichel Weststratebefore adoptiond0ef4ed
line 182why, one step back182 afterEach(() => {Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
line 183why, one step back183 expect(baseState).toBe(origBaseState)Make debugging workMichel Weststratebefore adoptiond0ef4ed
lines 184–186why, one step back184 expect(baseState).toEqual(createBaseState())185 })186Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 187–196why, one step back187 function createBaseState() {188 return {189 anArray: [3, 2, { c: 3 }, 1],190 aProp: "hi",191 anObject: {192 nested: {193 yummie: true194 },195 coffee: false196 }Make debugging workMichel Weststratebefore adoptiond0ef4ed
lines 197–198why, one step back197 }198 }Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 199–233why, one step back199})200201describe("readme example", () => {202 it("works", () => {203 const baseState = [204 {205 todo: "Learn typescript",206 done: true207 },208 {209 todo: "Try immer",210 done: false211 }212 ]213214 const nextState = immer(baseState, state => {215 state.push({ todo: "Tweet about it" })216 state[1].done = true217 })218219 // the new item is only added to the next state,220 // base state is unmodified221 expect(baseState.length).toBe(2)222 expect(nextState.length).toBe(3)223224 // same for the changed 'done' prop225 expect(baseState[1].done).toBe(false)226 expect(nextState[1].done).toBe(true)227228 // unchanged data is structurally shared229 expect(nextState[0]).toBe(baseState[0])230 // changed data not (dûh)231 expect(nextState[1]).not.toBe(baseState[1])232 })233})