Skip to content
Myr

empty.ts

This file

Path

__tests__/empty.ts

151 lines · 3.7 KiB · TypeScript

Last changed by

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

History of this file

Take it

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 1–6why, one step back
1import {
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 back
7import {DRAFT_STATE} from "../src/internal"

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 8why, one step back
8

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 9–10why, one step back
9enableAllPlugins()
10

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 11–14why, one step back
11test("empty stub test", () => {
12 expect(true).toBe(true)13})14

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

lines 15–19why, one step back
15describe("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 back
20 debugger

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

lines 21–26why, one step back
21 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 back
27 })

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

lines 28–39why, one step back
28
29 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 back
40})

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

lines 41–87why, one step back
41
42describe("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 back
88 // @ts-ignore
89 if (!global.USES_BUILD)

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 90why, one step back
90 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 back
91 })
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}