empty.ts
This file
Path
__tests__/empty.ts
203 lines · 4.8 KiB · TypeScript
Last changed by
fix: #768 `immerable` field being lost during patch value cloning (#771)Michel Weststratebefore adoptione0b7c01
History of this fileTake it
The walk stopped after 200 commits: lines older than fbae585 point at it.
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–4why, one step back1import {2 produce,3 produceWithPatches,4 setUseProxies,fix: #768 `immerable` field being lost during patch value cloning (#771)Michel Weststratebefore adoptione0b7c01
lines 5–7why, one step back5 enableAllPlugins,6 immerable,7 applyPatchesMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 8why, one step back8} from "../src/immer"fix: #768 `immerable` field being lost during patch value cloning (#771)Michel Weststratebefore adoptione0b7c01
line 9why, one step back9import {DRAFT_STATE, Patch} from "../src/internal"docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585
line 10why, one step back10Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 11–12why, one step back11enableAllPlugins()12docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585
lines 13–91why, one step back13test("empty stub test", () => {14 expect(true).toBe(true)15})1617describe("map set - es5", () => {18 test("can assign set value", () => {19 setUseProxies(false)2021 const baseState = new Map([["x", 1]])22 debugger23 const nextState = produce(baseState, s => {24 s.set("x", 2)25 })26 expect(baseState.get("x")).toEqual(1)27 expect(nextState).not.toBe(baseState)28 expect(nextState.get("x")).toEqual(2)29 })3031 test("can assign by key", () => {32 setUseProxies(false)3334 const baseState = new Map([["x", {a: 1}]])35 const nextState = produce(baseState, s => {36 s.get("x")!.a++37 })38 expect(nextState.get("x")!.a).toEqual(2)39 expect(baseState.get("x")!.a).toEqual(1)40 expect(nextState).not.toBe(baseState)41 })42})4344describe("map set - proxy", () => {45 test("can assign set value", () => {46 setUseProxies(true)4748 const baseState = new Map([["x", 1]])49 const nextState = produce(baseState, s => {50 s.set("x", 2)51 })52 expect(baseState.get("x")).toEqual(1)53 expect(nextState).not.toBe(baseState)54 expect(nextState.get("x")).toEqual(2)55 })5657 test("can assign by key", () => {58 setUseProxies(true)5960 const baseState = new Map([["x", {a: 1}]])61 const nextState = produce(baseState, s => {62 s.get("x")!.a++63 })64 expect(nextState.get("x")!.a).toEqual(2)65 expect(baseState.get("x")!.a).toEqual(1)66 expect(nextState).not.toBe(baseState)67 })6869 test("deep change bubbles up", () => {70 setUseProxies(true)7172 const baseState = createBaseState()73 const nextState = produce(baseState, s => {74 s.anObject.nested.yummie = false75 })76 expect(nextState).not.toBe(baseState)77 expect(nextState.anObject).not.toBe(baseState.anObject)78 expect(baseState.anObject.nested.yummie).toBe(true)79 expect(nextState.anObject.nested.yummie).toBe(false)80 expect(nextState.anArray).toBe(baseState.anArray)81 })8283 it("can assign by key", () => {84 setUseProxies(true)85 const baseState = createBaseState()8687 const nextState = produce(baseState, s => {88 // Map.prototype.set should return the Map itself89 const res = s.aMap.set("force", true)90 // @ts-ignore91 if (!global.USES_BUILD)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 92why, one step back92 expect(res).toBe((s.aMap as any)[DRAFT_STATE].draft_)docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585
lines 93–153why, one step back93 })94 expect(nextState).not.toBe(baseState)95 expect(nextState.aMap).not.toBe(baseState.aMap)96 expect(nextState.aMap.get("force")).toEqual(true)97 })9899 it("can use 'delete' to remove items", () => {100 const baseState = createBaseState()101102 const nextState = produce(baseState, s => {103 expect(s.aMap.has("jedi")).toBe(true)104 expect(s.aMap.delete("jedi")).toBe(true)105 expect(s.aMap.has("jedi")).toBe(false)106 })107 expect(nextState.aMap).not.toBe(baseState.aMap)108 expect(nextState.aMap.size).toBe(baseState.aMap.size - 1)109 expect(baseState.aMap.has("jedi")).toBe(true)110 expect(nextState.aMap.has("jedi")).toBe(false)111 })112113 it("support 'has'", () => {114 const baseState = createBaseState()115116 const nextState = produce(baseState, s => {117 expect(s.aMap.has("newKey")).toBe(false)118 s.aMap.set("newKey", true)119 expect(s.aMap.has("newKey")).toBe(true)120 })121 expect(nextState).not.toBe(baseState)122 expect(nextState.aMap).not.toBe(baseState.aMap)123 expect(baseState.aMap.has("newKey")).toBe(false)124 expect(nextState.aMap.has("newKey")).toBe(true)125 })126})127128function createBaseState() {129 const data = {130 anInstance: new (class {})(),131 anArray: [3, 2, {c: 3}, 1],132 aMap: new Map([133 ["jedi", {name: "Luke", skill: 10}],134 ["jediTotal", 42],135 ["force", "these aren't the droids you're looking for"]136 ] as any),137 aSet: new Set([138 "Luke",139 42,140 {141 jedi: "Yoda"142 }143 ]),144 aProp: "hi",145 anObject: {146 nested: {147 yummie: true148 },149 coffee: false150 }151 }152 return data153}fix: #768 `immerable` field being lost during patch value cloning (#771)Michel Weststratebefore adoptione0b7c01
lines 154–203why, one step back154155describe("#768", () => {156 class Stock {157 [immerable] = true158159 constructor(public price: number) {}160161 pushPrice(price: number) {162 this.price = price163 }164 }165166 type State = {167 stock: Stock168 }169170 test("bla", () => {171 // Set up conditions to produce the error172 const errorProducingPatch = [173 {174 op: "replace",175 path: ["stock"],176 value: new Stock(200)177 }178 ] as Patch[]179180 // Start with modified state181 const state = {182 stock: new Stock(100)183 }184185 expect(state.stock.price).toEqual(100)186 expect(state.stock[immerable]).toBeTruthy()187 // Use patch to "replace" stocks188 debugger189 const resetState: State = applyPatches(state, errorProducingPatch)190 expect(state.stock.price).toEqual(100)191 expect(resetState.stock.price).toEqual(200)192 expect(resetState.stock[immerable]).toBeTruthy()193194 // Problems come in when resetState is modified195 const updatedState = produce(resetState, draft => {196 draft.stock.pushPrice(300)197 })198 expect(state.stock.price).toEqual(100)199 expect(updatedState.stock.price).toEqual(300)200 expect(updatedState.stock[immerable]).toBeTruthy()201 expect(resetState.stock.price).toEqual(200)202 })203})