Skip to content
Myr

Further current impl

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Michel Weststrate · committed by Michel Weststrate · parent 7322e82 · Browse files · 2 files+41 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+41 added−20 removed

  • tests:+20 added−15 removed
  • other:+21 added−5 removed

2 files+41 added−20 removed

modified:__tests__/current.js+20 added-15 removed
10 unmodified lines
1112enableAllPlugins()1314runTests("proxy", true)15// TODO: enable16// runTests("es5", false)15runTests("es5", false)1617function runTests(name, useProxies) {18 describe("current - " + name, () => {19 beforeAll(() => {
6 unmodified lines
26 current({})27 }).toThrowError("[Immer] 'current' expects a draft, got: [object Object]")28 })2931 it("can handle simple objects", () => {32 const base = {x: 1}30 it("can handle simple arrays", () => {31 const base = [{x: 1}]32 let c33 const next = produce(base, draft => {35 draft.x++36 debugger34 expect(current(draft)).toEqual(base)35 draft[0].x++36 c = current(draft)37 expect(c).toEqual([{x: 2}])38 expect(Array.isArray(c))38 draft.x++39 draft[0].x++40 })40 expect(next).toEqual({x: 3})41 expect(c).toEqual({x: 2})41 expect(next).toEqual([{x: 3}])42 expect(c).toEqual([{x: 2}])43 expect(isDraft(c)).toBe(false)44 })4546 it("won't freeze", () => {
90 unmodified lines
137 expect(current(draft)).toBe(base)138 draft.delete("a")139 let c = current(draft)140 expect(current(draft)).not.toBe(base)141 expect(current(draft)).not.toBe(draft)142 expect(c).toEqual(new Map())143 const obj = {}144 draft.set("b", obj)145 expect(c).toEqual(new Map())146 expect(current(draft)).toEqual(new Map([["b", obj]]))145 expect(current(draft).get("b")).toBe(obj)147 expect(c).toBeInstanceOf(Map)148 })149 })150151 it("handles map - 2", () => {
11 unmodified lines
163 it("handles set", () => {164 const base = new Set([1])165 produce(base, draft => {166 expect(current(draft)).toBe(base)165 base.add(2)167 draft.add(2)168 const c = current(draft)169 expect(c).toEqual(new Set([1, 2]))170 expect(c).not.toBe(draft)171 expect(c).not.toBe(base)169 base.add(3)172 draft.add(3)173 expect(c).toEqual(new Set([1, 2]))174 expect(c).toBeInstanceOf(Set)175 })176 })177178 it("handles simple class", () => {
5 unmodified lines
184 this.current++185 }186 }187184 const c = new Counter()185 produce(c, draft => {186 expect(current(draft)).toBe(c)188 const counter1 = new Counter()189 produce(counter1, draft => {190 expect(current(draft)).toBe(counter1)191 draft.inc()192 const c = current(draft)193 expect(c).not.toBe(draft)194 expect(c.current).toBe(1)
3 unmodified lines
198 draft.inc()199 draft.inc()200 expect(c.current).toBe(2)201 expect(draft.current).toBe(3)202 expect(c).toBeInstanceOf(Counter)203 })204 })205 })206}
modified:src/core/current.ts+21 added-5 removed