empty.ts
This file
Path
__tests__/empty.ts
151 lines · 3.7 KiB · TypeScript
Last changed by
Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
History of this fileTake it
New build set. Current statsMichel Weststratebefore adoption9aec0fe
lines 1–6why, one step back1import {2 produce,3 produceWithPatches,4 setUseProxies,5 enableAllPlugins6} from "../src/immer"fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 7why, one step back7import {DRAFT_STATE} from "../src/internal"New build set. Current statsMichel Weststratebefore adoption9aec0fe
lines 8–9why, one step back89enableAllPlugins()feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 10–14why, one step back1011test("empty stub test", () => {12 expect(true).toBe(true)13})14feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 15–19why, one step back15describe("map set - es5", () => {16 test("can assign set value", () => {17 setUseProxies(false)1819 const baseState = new Map([["x", 1]])fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 20why, one step back20 debuggerfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 21–26why, one step back21 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)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 27why, one step back27 })feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 28–39why, one step back2829 test("can assign by key", () => {30 setUseProxies(false)3132 const baseState = new Map([["x", {a: 1}]])33 const nextState = produce(baseState, s => {34 s.get("x")!.a++35 })36 expect(nextState.get("x")!.a).toEqual(2)37 expect(baseState.get("x")!.a).toEqual(1)38 expect(nextState).not.toBe(baseState)39 })feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 40why, one step back40})feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 41–87why, one step back4142describe("map set - proxy", () => {43 test("can assign set value", () => {44 setUseProxies(true)4546 const baseState = new Map([["x", 1]])47 const nextState = produce(baseState, s => {48 s.set("x", 2)49 })50 expect(baseState.get("x")).toEqual(1)51 expect(nextState).not.toBe(baseState)52 expect(nextState.get("x")).toEqual(2)53 })5455 test("can assign by key", () => {56 setUseProxies(true)5758 const baseState = new Map([["x", {a: 1}]])59 const nextState = produce(baseState, s => {60 s.get("x")!.a++61 })62 expect(nextState.get("x")!.a).toEqual(2)63 expect(baseState.get("x")!.a).toEqual(1)64 expect(nextState).not.toBe(baseState)65 })6667 test("deep change bubbles up", () => {68 setUseProxies(true)6970 const baseState = createBaseState()71 const nextState = produce(baseState, s => {72 s.anObject.nested.yummie = false73 })74 expect(nextState).not.toBe(baseState)75 expect(nextState.anObject).not.toBe(baseState.anObject)76 expect(baseState.anObject.nested.yummie).toBe(true)77 expect(nextState.anObject.nested.yummie).toBe(false)78 expect(nextState.anArray).toBe(baseState.anArray)79 })8081 it("can assign by key", () => {82 setUseProxies(true)83 const baseState = createBaseState()8485 const nextState = produce(baseState, s => {86 // Map.prototype.set should return the Map itself87 const res = s.aMap.set("force", true)chore: verify build during testMichel Weststratebefore adoptionef8990b
lines 88–89why, one step back88 // @ts-ignore89 if (!global.USES_BUILD)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 90why, one step back90 expect(res).toBe((s.aMap as any)[DRAFT_STATE].draft_)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 91–151why, one step back91 })92 expect(nextState).not.toBe(baseState)93 expect(nextState.aMap).not.toBe(baseState.aMap)94 expect(nextState.aMap.get("force")).toEqual(true)95 })9697 it("can use 'delete' to remove items", () => {98 const baseState = createBaseState()99100 const nextState = produce(baseState, s => {101 expect(s.aMap.has("jedi")).toBe(true)102 expect(s.aMap.delete("jedi")).toBe(true)103 expect(s.aMap.has("jedi")).toBe(false)104 })105 expect(nextState.aMap).not.toBe(baseState.aMap)106 expect(nextState.aMap.size).toBe(baseState.aMap.size - 1)107 expect(baseState.aMap.has("jedi")).toBe(true)108 expect(nextState.aMap.has("jedi")).toBe(false)109 })110111 it("support 'has'", () => {112 const baseState = createBaseState()113114 const nextState = produce(baseState, s => {115 expect(s.aMap.has("newKey")).toBe(false)116 s.aMap.set("newKey", true)117 expect(s.aMap.has("newKey")).toBe(true)118 })119 expect(nextState).not.toBe(baseState)120 expect(nextState.aMap).not.toBe(baseState.aMap)121 expect(baseState.aMap.has("newKey")).toBe(false)122 expect(nextState.aMap.has("newKey")).toBe(true)123 })124})125126function createBaseState() {127 const data = {128 anInstance: new (class {})(),129 anArray: [3, 2, {c: 3}, 1],130 aMap: new Map([131 ["jedi", {name: "Luke", skill: 10}],132 ["jediTotal", 42],133 ["force", "these aren't the droids you're looking for"]134 ] as any),135 aSet: new Set([136 "Luke",137 42,138 {139 jedi: "Yoda"140 }141 ]),142 aProp: "hi",143 anObject: {144 nested: {145 yummie: true146 },147 coffee: false148 }149 }150 return data151}