empty.ts
This file
Path
__tests__/empty.ts
141 lines · 3.6 KiB · TypeScript
Last changed by
basic map tests succeedingMichel Weststratebefore adoption735c402
History of this fileTake it
map unification WIPMichel Weststratebefore adoption9bcefb6
line 1why, one step back1import {produce, produceWithPatches, setUseProxies} from "../src"basic map tests succeedingMichel Weststratebefore adoption735c402
line 2why, one step back2import {DRAFT_STATE} from "../src/common"Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 3–7why, one step back34test("empty stub test", () => {5 expect(true).toBe(true)6})7basic map tests succeedingMichel Weststratebefore adoption735c402
line 8why, one step back8describe("map set - es5", () => {map unification WIPMichel Weststratebefore adoption9bcefb6
lines 9–15why, one step back9 test("can assign set value", () => {10 setUseProxies(false)1112 const baseState = new Map([["x", 1]])13 const nextState = produce(baseState, s => {14 s.set("x", 2)15 })basic map tests succeedingMichel Weststratebefore adoption735c402
line 16why, one step back16 expect(baseState.get("x")).toEqual(1)map unification WIPMichel Weststratebefore adoption9bcefb6
lines 17–18why, one step back17 expect(nextState).not.toBe(baseState)18 expect(nextState.get("x")).toEqual(2)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 19why, one step back19 })map unification WIPMichel Weststratebefore adoption9bcefb6
lines 20–27why, one step back2021 test("can assign by key", () => {22 setUseProxies(false)2324 const baseState = new Map([["x", {a: 1}]])25 const nextState = produce(baseState, s => {26 s.get("x")!.a++27 })basic map tests succeedingMichel Weststratebefore adoption735c402
lines 28–29why, one step back28 expect(nextState.get("x")!.a).toEqual(2)29 expect(baseState.get("x")!.a).toEqual(1)map unification WIPMichel Weststratebefore adoption9bcefb6
line 30why, one step back30 expect(nextState).not.toBe(baseState)basic map tests succeedingMichel Weststratebefore adoption735c402
lines 31–53why, one step back31 })32})3334describe("map set - proxy", () => {35 test("can assign set value", () => {36 setUseProxies(true)3738 const baseState = new Map([["x", 1]])39 const nextState = produce(baseState, s => {40 s.set("x", 2)41 })42 expect(baseState.get("x")).toEqual(1)43 expect(nextState).not.toBe(baseState)44 expect(nextState.get("x")).toEqual(2)45 })4647 test("can assign by key", () => {48 setUseProxies(true)4950 const baseState = new Map([["x", {a: 1}]])51 const nextState = produce(baseState, s => {52 s.get("x")!.a++53 })map unification WIPMichel Weststratebefore adoption9bcefb6
line 54why, one step back54 expect(nextState.get("x")!.a).toEqual(2)basic map tests succeedingMichel Weststratebefore adoption735c402
lines 55–56why, one step back55 expect(baseState.get("x")!.a).toEqual(1)56 expect(nextState).not.toBe(baseState)map unification WIPMichel Weststratebefore adoption9bcefb6
line 57why, one step back57 })basic map tests succeedingMichel Weststratebefore adoption735c402
lines 58–113why, one step back5859 test("deep change bubbles up", () => {60 setUseProxies(true)6162 const baseState = createBaseState()63 const nextState = produce(baseState, s => {64 s.anObject.nested.yummie = false65 })66 expect(nextState).not.toBe(baseState)67 expect(nextState.anObject).not.toBe(baseState.anObject)68 expect(baseState.anObject.nested.yummie).toBe(true)69 expect(nextState.anObject.nested.yummie).toBe(false)70 expect(nextState.anArray).toBe(baseState.anArray)71 })7273 it("can assign by key", () => {74 setUseProxies(true)75 const baseState = createBaseState()7677 const nextState = produce(baseState, s => {78 // Map.prototype.set should return the Map itself79 const res = s.aMap.set("force", true)80 expect(res).toBe((s.aMap as any)[DRAFT_STATE].draft)81 })82 expect(nextState).not.toBe(baseState)83 expect(nextState.aMap).not.toBe(baseState.aMap)84 expect(nextState.aMap.get("force")).toEqual(true)85 })8687 it("can use 'delete' to remove items", () => {88 const baseState = createBaseState()8990 const nextState = produce(baseState, s => {91 expect(s.aMap.has("jedi")).toBe(true)92 expect(s.aMap.delete("jedi")).toBe(true)93 expect(s.aMap.has("jedi")).toBe(false)94 })95 expect(nextState.aMap).not.toBe(baseState.aMap)96 expect(nextState.aMap.size).toBe(baseState.aMap.size - 1)97 expect(baseState.aMap.has("jedi")).toBe(true)98 expect(nextState.aMap.has("jedi")).toBe(false)99 })100101 it("support 'has'", () => {102 const baseState = createBaseState()103104 const nextState = produce(baseState, s => {105 expect(s.aMap.has("newKey")).toBe(false)106 s.aMap.set("newKey", true)107 expect(s.aMap.has("newKey")).toBe(true)108 })109 expect(nextState).not.toBe(baseState)110 expect(nextState.aMap).not.toBe(baseState.aMap)111 expect(baseState.aMap.has("newKey")).toBe(false)112 expect(nextState.aMap.has("newKey")).toBe(true)113 })Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 114why, one step back114})basic map tests succeedingMichel Weststratebefore adoption735c402
lines 115–141why, one step back115116function createBaseState() {117 const data = {118 anInstance: new (class {})(),119 anArray: [3, 2, {c: 3}, 1],120 aMap: new Map([121 ["jedi", {name: "Luke", skill: 10}],122 ["jediTotal", 42],123 ["force", "these aren't the droids you're looking for"]124 ] as any),125 aSet: new Set([126 "Luke",127 42,128 {129 jedi: "Yoda"130 }131 ]),132 aProp: "hi",133 anObject: {134 nested: {135 yummie: true136 },137 coffee: false138 }139 }140 return data141}