Skip to content
Myr

empty.ts

This file

Path

__tests__/empty.ts

142 lines · 3.6 KiB · TypeScript

Last changed by

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

History of this file

Take it

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 1why, one step back
1import {produce, produceWithPatches, setUseProxies} from "../src"

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 2why, one step back
2import {DRAFT_STATE} from "../src/internal"

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 3–7why, one step back
3
4test("empty stub test", () => {5 expect(true).toBe(true)6})7

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 8–12why, one step back
8describe("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 back
13 debugger

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 14–19why, one step back
14 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 back
20 })

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 21–32why, one step back
21
22 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 back
33})

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 34–142why, one step back
34
35describe("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)81 expect(res).toBe((s.aMap as any)[DRAFT_STATE].draft)82 })83 expect(nextState).not.toBe(baseState)84 expect(nextState.aMap).not.toBe(baseState.aMap)85 expect(nextState.aMap.get("force")).toEqual(true)86 })8788 it("can use 'delete' to remove items", () => {89 const baseState = createBaseState()9091 const nextState = produce(baseState, s => {92 expect(s.aMap.has("jedi")).toBe(true)93 expect(s.aMap.delete("jedi")).toBe(true)94 expect(s.aMap.has("jedi")).toBe(false)95 })96 expect(nextState.aMap).not.toBe(baseState.aMap)97 expect(nextState.aMap.size).toBe(baseState.aMap.size - 1)98 expect(baseState.aMap.has("jedi")).toBe(true)99 expect(nextState.aMap.has("jedi")).toBe(false)100 })101102 it("support 'has'", () => {103 const baseState = createBaseState()104105 const nextState = produce(baseState, s => {106 expect(s.aMap.has("newKey")).toBe(false)107 s.aMap.set("newKey", true)108 expect(s.aMap.has("newKey")).toBe(true)109 })110 expect(nextState).not.toBe(baseState)111 expect(nextState.aMap).not.toBe(baseState.aMap)112 expect(baseState.aMap.has("newKey")).toBe(false)113 expect(nextState.aMap.has("newKey")).toBe(true)114 })115})116117function createBaseState() {118 const data = {119 anInstance: new (class {})(),120 anArray: [3, 2, {c: 3}, 1],121 aMap: new Map([122 ["jedi", {name: "Luke", skill: 10}],123 ["jediTotal", 42],124 ["force", "these aren't the droids you're looking for"]125 ] as any),126 aSet: new Set([127 "Luke",128 42,129 {130 jedi: "Yoda"131 }132 ]),133 aProp: "hi",134 anObject: {135 nested: {136 yummie: true137 },138 coffee: false139 }140 }141 return data142}