base.js
This file
Path
__tests__/base.js
243 lines · 7.4 KiB · JavaScript
Last changed by
mendGregory Assasiebefore adoption12c4cae
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–23why, 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 })23Give warning if something is returned from thunk #3Gregory Assasiebefore adoption8bde915
lines 24–25why, one step back24 it("should not return any value: thunk", () => {25 const warning = jest.spyOn(console, "warn");mendGregory Assasiebefore adoption12c4cae
lines 26–29why, one step back26 immer(baseState, () => ({bad: "don't do this"}))27 immer(baseState, () => [1,2,3])28 immer(baseState, () => false)29 immer(baseState, () => "")Give warning if something is returned from thunk #3Gregory Assasiebefore adoption8bde915
lines 30–33why, one step back3031 expect(warning).toHaveBeenCalledTimes(4);32 })33Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
lines 34–90why, one step back34 it("should return a copy when modifying stuff", () => {35 const nextState = immer(baseState, s => {36 s.aProp = "hello world"37 })38 expect(nextState).not.toBe(baseState)39 expect(baseState.aProp).toBe("hi")40 expect(nextState.aProp).toBe("hello world")41 // structural sharing?42 expect(nextState.nested).toBe(baseState.nested)43 })4445 it("deep change bubbles up", () => {46 const nextState = immer(baseState, s => {47 s.anObject.nested.yummie = false48 })49 expect(nextState).not.toBe(baseState)50 expect(nextState.anObject).not.toBe(baseState.anObject)51 expect(baseState.anObject.nested.yummie).toBe(true)52 expect(nextState.anObject.nested.yummie).toBe(false)53 expect(nextState.anArray).toBe(baseState.anArray)54 })5556 it("can add props", () => {57 const nextState = immer(baseState, s => {58 s.anObject.cookie = { tasty: true }59 })60 expect(nextState).not.toBe(baseState)61 expect(nextState.anObject).not.toBe(baseState.anObject)62 expect(nextState.anObject.nested).toBe(baseState.anObject.nested)63 expect(nextState.anObject.cookie).toEqual({ tasty: true })64 })6566 it("can delete props", () => {67 const nextState = immer(baseState, s => {68 delete s.anObject.nested69 })70 expect(nextState).not.toBe(baseState)71 expect(nextState.anObject).not.toBe(baseState.anObject)72 expect(nextState.anObject.nested).toBe(undefined)73 })7475 it("ignores single non-modification", () => {76 const nextState = immer(baseState, s => {77 s.aProp = "hi"78 })79 expect(nextState).toBe(baseState)80 })8182 it("processes single modification", () => {83 const nextState = immer(baseState, s => {84 s.aProp = "hello"85 s.aProp = "hi"86 })87 expect(nextState).not.toBe(baseState)88 expect(nextState).toEqual(baseState)89 })90Add support for arraysMichel Weststratebefore adoptiond3696cb
lines 91–128why, one step back91 it("should support reading arrays", () => {92 const nextState = immer(baseState, s => {93 s.anArray.slice()94 })95 expect(nextState.anArray).toBe(baseState.anArray)96 expect(nextState).toBe(baseState)97 })9899 it("should support changing arrays", () => {100 const nextState = immer(baseState, s => {101 s.anArray[3] = true102 })103 expect(nextState).not.toBe(baseState)104 expect(nextState.anArray).not.toBe(baseState.anArray)105 expect(nextState.anArray[3]).toEqual(true)106 })107108 it("should support changing arrays - 2", () => {109 const nextState = immer(baseState, s => {110 s.anArray.splice(1, 1, "a", "b")111 })112 expect(nextState).not.toBe(baseState)113 expect(nextState.anArray).not.toBe(baseState.anArray)114115 expect(nextState.anArray).toEqual([3, "a", "b", { c: 3 }, 1])116 })117118 it("should support sorting arrays", () => {119 const nextState = immer(baseState, s => {120 s.anArray[2].c = 4121 s.anArray.sort()122 s.anArray[3].c = 5123 })124 expect(nextState).not.toBe(baseState)125 expect(nextState.anArray).not.toBe(baseState.anArray)126 expect(nextState.anArray).toEqual([1, 2, 3, { c: 5 }])127 })128Code cleanupMichel Weststratebefore adoption2b2d0cb
lines 129–137why, one step back129 it("should updating inside arrays", () => {130 const nextState = immer(baseState, s => {131 s.anArray[2].test = true132 })133 expect(nextState).not.toBe(baseState)134 expect(nextState.anArray).not.toBe(baseState.anArray)135 expect(nextState.anArray).toEqual([3, 2, { c: 3, test: true }, 1])136 })137Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 138–182why, one step back138 it("reusing object should work", () => {139 const nextState = immer(baseState, s => {140 debugger141 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({148 anArray: [3, 2, { c: 3 }, 1],149 aProp: "hi",150 messy: {151 nested: {152 yummie: true153 },154 coffee: false155 }156 })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({171 anArray: [3, 2, { c: 3 }, 1],172 aProp: "hello",173 messy: {174 nested: {175 yummie: true176 },177 coffee: true178 }179 })180 expect(nextState.messy.nested).toBe(baseState.anObject.nested)181 })182Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e
lines 183–191why, 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)188 expect(baseState.aProp).toBe('hi')189 expect(nextState.aProp).toBe(undefined)190 })191Make debugging workMichel Weststratebefore adoptiond0ef4ed
line 192why, one step back192 afterEach(() => {Wrote tests, fixed a bugMichel Weststratebefore adoption46e1d5d
line 193why, one step back193 expect(baseState).toBe(origBaseState)Make debugging workMichel Weststratebefore adoptiond0ef4ed
lines 194–196why, one step back194 expect(baseState).toEqual(createBaseState())195 })196Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 197–206why, one step back197 function createBaseState() {198 return {199 anArray: [3, 2, { c: 3 }, 1],200 aProp: "hi",201 anObject: {202 nested: {203 yummie: true204 },205 coffee: false206 }Make debugging workMichel Weststratebefore adoptiond0ef4ed
lines 207–208why, one step back207 }208 }Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694
lines 209–243why, one step back209})210211describe("readme example", () => {212 it("works", () => {213 const baseState = [214 {215 todo: "Learn typescript",216 done: true217 },218 {219 todo: "Try immer",220 done: false221 }222 ]223224 const nextState = immer(baseState, state => {225 state.push({ todo: "Tweet about it" })226 state[1].done = true227 })228229 // the new item is only added to the next state,230 // base state is unmodified231 expect(baseState.length).toBe(2)232 expect(nextState.length).toBe(3)233234 // same for the changed 'done' prop235 expect(baseState[1].done).toBe(false)236 expect(nextState[1].done).toBe(true)237238 // unchanged data is structurally shared239 expect(nextState[0]).toBe(baseState[0])240 // changed data not (dûh)241 expect(nextState[1]).not.toBe(baseState[1])242 })243})