empty.ts
This file
Path
__tests__/empty.ts
144 lines · 3.6 KiB · TypeScript
Last changed by
chore: verify build during testMichel Weststratebefore adoptionef8990b
History of this fileTake it
feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 1why, one step back1import {produce, produceWithPatches, setUseProxies} from "../src"fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 2why, one step back2import {DRAFT_STATE} from "../src/internal"feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 3–7why, one step back34test("empty stub test", () => {5 expect(true).toBe(true)6})7feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 8–12why, one step back8describe("map set - es5", () => {9 test("can assign set value", () => {10 setUseProxies(false)1112 const baseState = new Map([["x", 1]])fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 13why, one step back13 debuggerfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 14–19why, one step back14 const nextState = produce(baseState, s => {15 s.set("x", 2)16 })17 expect(baseState.get("x")).toEqual(1)18 expect(nextState).not.toBe(baseState)19 expect(nextState.get("x")).toEqual(2)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 20why, one step back20 })feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 21–32why, one step back2122 test("can assign by key", () => {23 setUseProxies(false)2425 const baseState = new Map([["x", {a: 1}]])26 const nextState = produce(baseState, s => {27 s.get("x")!.a++28 })29 expect(nextState.get("x")!.a).toEqual(2)30 expect(baseState.get("x")!.a).toEqual(1)31 expect(nextState).not.toBe(baseState)32 })feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 33why, one step back33})feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 34–80why, one step back3435describe("map set - proxy", () => {36 test("can assign set value", () => {37 setUseProxies(true)3839 const baseState = new Map([["x", 1]])40 const nextState = produce(baseState, s => {41 s.set("x", 2)42 })43 expect(baseState.get("x")).toEqual(1)44 expect(nextState).not.toBe(baseState)45 expect(nextState.get("x")).toEqual(2)46 })4748 test("can assign by key", () => {49 setUseProxies(true)5051 const baseState = new Map([["x", {a: 1}]])52 const nextState = produce(baseState, s => {53 s.get("x")!.a++54 })55 expect(nextState.get("x")!.a).toEqual(2)56 expect(baseState.get("x")!.a).toEqual(1)57 expect(nextState).not.toBe(baseState)58 })5960 test("deep change bubbles up", () => {61 setUseProxies(true)6263 const baseState = createBaseState()64 const nextState = produce(baseState, s => {65 s.anObject.nested.yummie = false66 })67 expect(nextState).not.toBe(baseState)68 expect(nextState.anObject).not.toBe(baseState.anObject)69 expect(baseState.anObject.nested.yummie).toBe(true)70 expect(nextState.anObject.nested.yummie).toBe(false)71 expect(nextState.anArray).toBe(baseState.anArray)72 })7374 it("can assign by key", () => {75 setUseProxies(true)76 const baseState = createBaseState()7778 const nextState = produce(baseState, s => {79 // Map.prototype.set should return the Map itself80 const res = s.aMap.set("force", true)chore: verify build during testMichel Weststratebefore adoptionef8990b
lines 81–83why, one step back81 // @ts-ignore82 if (!global.USES_BUILD)83 expect(res).toBe((s.aMap as any)[DRAFT_STATE].draft)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 84–144why, one step back84 })85 expect(nextState).not.toBe(baseState)86 expect(nextState.aMap).not.toBe(baseState.aMap)87 expect(nextState.aMap.get("force")).toEqual(true)88 })8990 it("can use 'delete' to remove items", () => {91 const baseState = createBaseState()9293 const nextState = produce(baseState, s => {94 expect(s.aMap.has("jedi")).toBe(true)95 expect(s.aMap.delete("jedi")).toBe(true)96 expect(s.aMap.has("jedi")).toBe(false)97 })98 expect(nextState.aMap).not.toBe(baseState.aMap)99 expect(nextState.aMap.size).toBe(baseState.aMap.size - 1)100 expect(baseState.aMap.has("jedi")).toBe(true)101 expect(nextState.aMap.has("jedi")).toBe(false)102 })103104 it("support 'has'", () => {105 const baseState = createBaseState()106107 const nextState = produce(baseState, s => {108 expect(s.aMap.has("newKey")).toBe(false)109 s.aMap.set("newKey", true)110 expect(s.aMap.has("newKey")).toBe(true)111 })112 expect(nextState).not.toBe(baseState)113 expect(nextState.aMap).not.toBe(baseState.aMap)114 expect(baseState.aMap.has("newKey")).toBe(false)115 expect(nextState.aMap.has("newKey")).toBe(true)116 })117})118119function createBaseState() {120 const data = {121 anInstance: new (class {})(),122 anArray: [3, 2, {c: 3}, 1],123 aMap: new Map([124 ["jedi", {name: "Luke", skill: 10}],125 ["jediTotal", 42],126 ["force", "these aren't the droids you're looking for"]127 ] as any),128 aSet: new Set([129 "Luke",130 42,131 {132 jedi: "Yoda"133 }134 ]),135 aProp: "hi",136 anObject: {137 nested: {138 yummie: true139 },140 coffee: false141 }142 }143 return data144}