empty.ts
This file
Path
__tests__/empty.ts
169 lines · 4.1 KiB · TypeScript
Last changed by
Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
History of this fileTake it
The walk stopped after 200 commits: lines older than 6555173 point at it.
fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 1–4why, one step back1import {2 produce,3 produceWithPatches,4 immerable,Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 5–6why, one step back5 enableMapSet,6 enablePatches,fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 7–10why, one step back7 applyPatches8} from "../src/immer"9import {DRAFT_STATE, Patch} from "../src/internal"10Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 11–12why, one step back11enableMapSet()12enablePatches()fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 13–169why, one step back1314test("empty stub test", () => {15 expect(true).toBe(true)16})1718describe("map set - proxy", () => {19 test("can assign set value", () => {20 const baseState = new Map([["x", 1]])21 const nextState = produce(baseState, s => {22 s.set("x", 2)23 })24 expect(baseState.get("x")).toEqual(1)25 expect(nextState).not.toBe(baseState)26 expect(nextState.get("x")).toEqual(2)27 })2829 test("can assign by key", () => {30 const baseState = new Map([["x", {a: 1}]])31 const nextState = produce(baseState, s => {32 s.get("x")!.a++33 })34 expect(nextState.get("x")!.a).toEqual(2)35 expect(baseState.get("x")!.a).toEqual(1)36 expect(nextState).not.toBe(baseState)37 })3839 test("deep change bubbles up", () => {40 const baseState = createBaseState()41 const nextState = produce(baseState, s => {42 s.anObject.nested.yummie = false43 })44 expect(nextState).not.toBe(baseState)45 expect(nextState.anObject).not.toBe(baseState.anObject)46 expect(baseState.anObject.nested.yummie).toBe(true)47 expect(nextState.anObject.nested.yummie).toBe(false)48 expect(nextState.anArray).toBe(baseState.anArray)49 })5051 it("can assign by key", () => {52 const baseState = createBaseState()5354 const nextState = produce(baseState, s => {55 // Map.prototype.set should return the Map itself56 const res = s.aMap.set("force", true)57 // @ts-ignore58 if (!global.USES_BUILD)59 expect(res).toBe((s.aMap as any)[DRAFT_STATE].draft_)60 })61 expect(nextState).not.toBe(baseState)62 expect(nextState.aMap).not.toBe(baseState.aMap)63 expect(nextState.aMap.get("force")).toEqual(true)64 })6566 it("can use 'delete' to remove items", () => {67 const baseState = createBaseState()6869 const nextState = produce(baseState, s => {70 expect(s.aMap.has("jedi")).toBe(true)71 expect(s.aMap.delete("jedi")).toBe(true)72 expect(s.aMap.has("jedi")).toBe(false)73 })74 expect(nextState.aMap).not.toBe(baseState.aMap)75 expect(nextState.aMap.size).toBe(baseState.aMap.size - 1)76 expect(baseState.aMap.has("jedi")).toBe(true)77 expect(nextState.aMap.has("jedi")).toBe(false)78 })7980 it("support 'has'", () => {81 const baseState = createBaseState()8283 const nextState = produce(baseState, s => {84 expect(s.aMap.has("newKey")).toBe(false)85 s.aMap.set("newKey", true)86 expect(s.aMap.has("newKey")).toBe(true)87 })88 expect(nextState).not.toBe(baseState)89 expect(nextState.aMap).not.toBe(baseState.aMap)90 expect(baseState.aMap.has("newKey")).toBe(false)91 expect(nextState.aMap.has("newKey")).toBe(true)92 })93})9495function createBaseState() {96 const data = {97 anInstance: new (class {})(),98 anArray: [3, 2, {c: 3}, 1],99 aMap: new Map([100 ["jedi", {name: "Luke", skill: 10}],101 ["jediTotal", 42],102 ["force", "these aren't the droids you're looking for"]103 ] as any),104 aSet: new Set([105 "Luke",106 42,107 {108 jedi: "Yoda"109 }110 ]),111 aProp: "hi",112 anObject: {113 nested: {114 yummie: true115 },116 coffee: false117 }118 }119 return data120}121122describe("#768", () => {123 class Stock {124 [immerable] = true125126 constructor(public price: number) {}127128 pushPrice(price: number) {129 this.price = price130 }131 }132133 type State = {134 stock: Stock135 }136137 test("bla", () => {138 // Set up conditions to produce the error139 const errorProducingPatch = [140 {141 op: "replace",142 path: ["stock"],143 value: new Stock(200)144 }145 ] as Patch[]146147 // Start with modified state148 const state = {149 stock: new Stock(100)150 }151152 expect(state.stock.price).toEqual(100)153 expect(state.stock[immerable]).toBeTruthy()154 // Use patch to "replace" stocks155 const resetState: State = applyPatches(state, errorProducingPatch)156 expect(state.stock.price).toEqual(100)157 expect(resetState.stock.price).toEqual(200)158 expect(resetState.stock[immerable]).toBeTruthy()159160 // Problems come in when resetState is modified161 const updatedState = produce(resetState, draft => {162 draft.stock.pushPrice(300)163 })164 expect(state.stock.price).toEqual(100)165 expect(updatedState.stock.price).toEqual(300)166 expect(updatedState.stock[immerable]).toBeTruthy()167 expect(resetState.stock.price).toEqual(200)168 })169})