current.js
This file
Path
__tests__/current.js
246 lines · 5.1 KiB · JavaScript
Last changed by
Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
History of this fileTake it
The walk stopped after 200 commits: lines older than 6555173 point at it.
fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 1–6why, one step back1import {2 setAutoFreeze,3 current,4 immerable,5 isDraft,6 produce,Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 7–9why, one step back7 original,8 freeze,9 enableMapSetfix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 10–11why, one step back10} from "../src/immer"11Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 12why, one step back12enableMapSet()fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 13–17why, one step back1314runTests("proxy", true)1516const isProd = process.env.NODE_ENV === "production"17Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 18why, one step back18function runTests(name) {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 19–28why, one step back19 describe("current - " + name, () => {20 beforeAll(() => {21 setAutoFreeze(true)22 })2324 it("must be called on draft", () => {25 expect(() => {26 current({})27 }).toThrowError(28 isProdMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 29why, one step back29 ? "[Immer] minified error nr: 10. Full error at: https://bit.ly/3cXEKWf"fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 30–114why, one step back30 : "[Immer] 'current' expects a draft, got: [object Object]"31 )32 })3334 it("can handle simple arrays", () => {35 const base = [{x: 1}]36 let c37 const next = produce(base, draft => {38 expect(current(draft)).toEqual(base)39 draft[0].x++40 c = current(draft)41 expect(c).toEqual([{x: 2}])42 expect(Array.isArray(c))43 draft[0].x++44 })45 expect(next).toEqual([{x: 3}])46 expect(c).toEqual([{x: 2}])47 expect(isDraft(c)).toBe(false)48 })4950 it("won't freeze", () => {51 const base = {x: 1}52 const next = produce(base, draft => {53 draft.x++54 expect(Object.isFrozen(current(draft))).toBe(false)55 })56 })5758 it("returns original without changes", () => {59 const base = {}60 produce(base, draft => {61 expect(original(draft)).toBe(base)62 expect(current(draft)).toBe(base)63 })64 })6566 it("can handle property additions", () => {67 const base = {}68 produce(base, draft => {69 draft.x = true70 const c = current(draft)71 expect(c).not.toBe(base)72 expect(c).not.toBe(draft)73 expect(c).toEqual({74 x: true75 })76 })77 })7879 it("can handle property deletions", () => {80 const base = {81 x: 182 }83 produce(base, draft => {84 delete draft.x85 const c = current(draft)86 expect(c).not.toBe(base)87 expect(c).not.toBe(draft)88 expect(c).toEqual({})89 })90 })9192 it("won't reflect changes over time", () => {93 const base = {94 x: 195 }96 produce(base, draft => {97 draft.x++98 const c = current(draft)99 expect(c).toEqual({100 x: 2101 })102 draft.x++103 expect(c).toEqual({104 x: 2105 })106 })107 })108109 it("will find drafts inside objects", () => {110 const base = {111 x: 1,112 y: {113 z: 2114 },Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 115–117why, one step back115 z: {},116 w: {},117 ww: freeze({})fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 118–120why, one step back118 }119 produce(base, draft => {120 draft.y.z++Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 121–124why, one step back121 draft.z = {122 nested: {123 z: 3124 }fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 125–129why, one step back125 }126 const c = current(draft)127 expect(c).toEqual({128 x: 1,129 y: {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 130why, one step back130 z: 3fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 131why, one step back131 },Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 132–134why, one step back132 z: {nested: {z: 3}},133 w: {},134 ww: {}fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 135why, one step back135 })Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 136–170why, one step back136 expect(isDraft(c)).toBe(false)137 expect(isDraft(c.y)).toBe(false)138 expect(isDraft(c.z)).toBe(false)139 expect(isDraft(c.z.nested)).toBe(false)140 expect(isDraft(c.z.nested.z)).toBe(false)141 // this works only with frozen objects, otherwise no way to tell if this was142 // a new object that was added during the recipe, that might contain drafts.143 // the recipe or not144 expect(c.w).not.toBe(base.w)145 // was frozen, so recyclable146 expect(c.ww).toBe(base.ww)147 })148 })149150 it("will find drafts inside objects - 2", () => {151 const base = {152 ar: [153 {154 x: 1155 },156 {x: 2}157 ]158 }159 produce(base, draft => {160 draft.ar[1].x++161 draft.ar = [draft.ar[1], draft.ar[0]] // swap162 const c = current(draft)163 expect(c).toEqual({164 ar: [{x: 3}, {x: 1}]165 })166 expect(isDraft(c)).toBe(false)167 expect(isDraft(c.ar)).toBe(false)168 expect(isDraft(c.ar[0])).toBe(false)169 expect(isDraft(c.ar[1])).toBe(false)170 expect(c.ar[1]).toBe(base.ar[0])fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 171–246why, one step back171 })172 })173174 it("handles map - 1", () => {175 const base = new Map([["a", {x: 1}]])176 produce(base, draft => {177 expect(current(draft)).toBe(base)178 draft.delete("a")179 let c = current(draft)180 expect(current(draft)).not.toBe(base)181 expect(current(draft)).not.toBe(draft)182 expect(c).toEqual(new Map())183 const obj = {}184 draft.set("b", obj)185 expect(c).toEqual(new Map())186 expect(current(draft)).toEqual(new Map([["b", obj]]))187 expect(c).toBeInstanceOf(Map)188 })189 })190191 it("handles map - 2", () => {192 const base = new Map([["a", {x: 1}]])193 produce(base, draft => {194 draft.get("a").x++195 const c = current(draft)196 expect(c).not.toBe(base)197 expect(c).toEqual(new Map([["a", {x: 2}]]))198 draft.get("a").x++199 expect(c).toEqual(new Map([["a", {x: 2}]]))200 })201 })202203 it("handles set", () => {204 const base = new Set([1])205 produce(base, draft => {206 expect(current(draft)).toBe(base)207 draft.add(2)208 const c = current(draft)209 expect(c).toEqual(new Set([1, 2]))210 expect(c).not.toBe(draft)211 expect(c).not.toBe(base)212 draft.add(3)213 expect(c).toEqual(new Set([1, 2]))214 expect(c).toBeInstanceOf(Set)215 })216 })217218 it("handles simple class", () => {219 class Counter {220 [immerable] = true221 current = 0222223 inc() {224 this.current++225 }226 }227228 const counter1 = new Counter()229 produce(counter1, draft => {230 expect(current(draft)).toBe(counter1)231 draft.inc()232 const c = current(draft)233 expect(c).not.toBe(draft)234 expect(c.current).toBe(1)235 c.inc()236 expect(c.current).toBe(2)237 expect(draft.current).toBe(1)238 draft.inc()239 draft.inc()240 expect(c.current).toBe(2)241 expect(draft.current).toBe(3)242 expect(c).toBeInstanceOf(Counter)243 })244 })245 })246}