Skip to content
Myr

4 spaces

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Michel Weststrate · committed by Michel Weststrate · parent 1115489 · Browse files · 3 files+208 added−200 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+208 added−200 removed

  • tests:+194 added−194 removed
  • other:+14 added−6 removed

3 files+208 added−200 removed

modified:__tests__/base.js+194 added-194 removed
1import immer from ".."23describe("base", () => {4 let baseState5 let origBaseState4 let baseState5 let origBaseState67 beforeEach(() => {8 origBaseState = baseState = createBaseState()9 })7 beforeEach(() => {8 origBaseState = baseState = createBaseState()9 })1011 it("should return the original without modifications", () => {12 const nextState = immer(baseState, () => {})13 expect(nextState).toBe(baseState)14 })11 it("should return the original without modifications", () => {12 const nextState = immer(baseState, () => {})13 expect(nextState).toBe(baseState)14 })1516 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})16 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 })21 expect(nextState).toBe(baseState)22 })2324 it("should not return any value: thunk", () => {25 const warning = jest.spyOn(console, "warn")26 immer(baseState, () => ({bad: "don't do this"}))27 immer(baseState, () => [1, 2, 3])28 immer(baseState, () => false)29 immer(baseState, () => "")24 it("should not return any value: thunk", () => {25 const warning = jest.spyOn(console, "warn")26 immer(baseState, () => ({bad: "don't do this"}))27 immer(baseState, () => [1, 2, 3])28 immer(baseState, () => false)29 immer(baseState, () => "")3031 expect(warning).toHaveBeenCalledTimes(4)32 })3334 it("should return a copy when modifying stuff", () => {35 const nextState = immer(baseState, s => {36 s.aProp = "hello world"31 expect(warning).toHaveBeenCalledTimes(4)32 })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 })3345 it("deep change bubbles up", () => {46 const nextState = immer(baseState, s => {47 s.anObject.nested.yummie = false34 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 })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 })4456 it("can add props", () => {57 const nextState = immer(baseState, s => {58 s.anObject.cookie = {tasty: true}45 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 })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 })5566 it("can delete props", () => {67 const nextState = immer(baseState, s => {68 delete s.anObject.nested56 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 })70 expect(nextState).not.toBe(baseState)71 expect(nextState.anObject).not.toBe(baseState.anObject)72 expect(nextState.anObject.nested).toBe(undefined)73 })6575 it("ignores single non-modification", () => {76 const nextState = immer(baseState, s => {77 s.aProp = "hi"66 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 })79 expect(nextState).toBe(baseState)80 })7482 it("processes single modification", () => {83 const nextState = immer(baseState, s => {84 s.aProp = "hello"85 s.aProp = "hi"75 it("ignores single non-modification", () => {76 const nextState = immer(baseState, s => {77 s.aProp = "hi"78 })79 expect(nextState).toBe(baseState)80 })87 expect(nextState).not.toBe(baseState)88 expect(nextState).toEqual(baseState)89 })8191 it("should support reading arrays", () => {92 const nextState = immer(baseState, s => {93 s.anArray.slice()82 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 })95 expect(nextState.anArray).toBe(baseState.anArray)96 expect(nextState).toBe(baseState)97 })9099 it("should support changing arrays", () => {100 const nextState = immer(baseState, s => {101 s.anArray[3] = true91 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 })103 expect(nextState).not.toBe(baseState)104 expect(nextState.anArray).not.toBe(baseState.anArray)105 expect(nextState.anArray[3]).toEqual(true)106 })98108 it("should support changing arrays - 2", () => {109 const nextState = immer(baseState, s => {110 s.anArray.splice(1, 1, "a", "b")99 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 })112 expect(nextState).not.toBe(baseState)113 expect(nextState.anArray).not.toBe(baseState.anArray)107108 it("should support changing arrays - 2", () => {109 const nextState = immer(baseState, s => {115 expect(nextState.anArray).toEqual([3, "a", "b", {c: 3}, 1])116 })110 s.anArray.splice(1, 1, "a", "b")111 })112 expect(nextState).not.toBe(baseState)113 expect(nextState.anArray).not.toBe(baseState.anArray)114118 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 = 5115 expect(nextState.anArray).toEqual([3, "a", "b", {c: 3}, 1])116 })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 })117129 it("should updating inside arrays", () => {130 const nextState = immer(baseState, s => {131 s.anArray[2].test = true118 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 })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 })128138 it("reusing object should work", () => {139 const nextState = immer(baseState, s => {140 debugger141 const obj = s.anObject142 delete s.anObject143 s.messy = obj129 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 })137138 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: true,153 },154 coffee: false,155 },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: true,153 },154 coffee: false,155 },156 })157 expect(nextState.messy.nested).toBe(baseState.anObject.nested)158 })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 = obj160 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: true,176 },177 coffee: true,178 },179 })180 expect(nextState.messy.nested).toBe(baseState.anObject.nested)181 })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: true,176 },177 coffee: true,178 },182183 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 })180 expect(nextState.messy.nested).toBe(baseState.anObject.nested)181 })191183 it("should allow setting to undefined a defined draft property", () => {184 const nextState = immer(baseState, s => {185 s.aProp = undefined192 afterEach(() => {193 expect(baseState).toBe(origBaseState)194 expect(baseState).toEqual(createBaseState())195 })187 expect(nextState).not.toBe(baseState)188 expect(baseState.aProp).toBe("hi")189 expect(nextState.aProp).toBe(undefined)190 })196192 afterEach(() => {193 expect(baseState).toBe(origBaseState)194 expect(baseState).toEqual(createBaseState())195 })196197 function createBaseState() {198 return {199 anArray: [3, 2, {c: 3}, 1],200 aProp: "hi",201 anObject: {202 nested: {203 yummie: true,204 },205 coffee: false,206 },197 function createBaseState() {198 return {199 anArray: [3, 2, {c: 3}, 1],200 aProp: "hi",201 anObject: {202 nested: {203 yummie: true,204 },205 coffee: false,206 },207 }208 }208 }209})210211describe("readme example", () => {212 it("works", () => {213 const baseState = [214 {215 todo: "Learn typescript",216 done: true,217 },218 {219 todo: "Try immer",220 done: false,221 },222 ]212 it("works", () => {213 const baseState = [214 {215 todo: "Learn typescript",216 done: true,217 },218 {219 todo: "Try immer",220 done: false,221 },222 ]223224 const nextState = immer(baseState, state => {225 state.push({todo: "Tweet about it"})226 state[1].done = true227 })224 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)229 // 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)234 // 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 })238 // 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})
modified:.prettierrc+1 added-1 removed
3 unmodified lines
4 "proseWrap": "never",5 "requirePragma": false,6 "semi": false,7 "singleQuote": false,8 "tabWidth": 2,8 "tabWidth": 4,9 "trailingComma": "all",10 "useTabs": false,11 "overrides": [12 {
7 unmodified lines
modified:immer.js+13 added-5 removed