current.js
This file
Path
__tests__/current.js
212 lines · 4.3 KiB · JavaScript
Last changed by
chore: Merge branch 'master' of github.com:immerjs/immerMichel Weststratebefore adoption6c62eec
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213import { setUseProxies, setAutoFreeze, enableAllPlugins, current, immerable, isDraft, produce, original} from "../src/immer"
enableAllPlugins()
runTests("proxy", true)runTests("es5", false)
const isProd = process.env.NODE_ENV === "production"
function runTests(name, useProxies) { describe("current - " + name, () => { beforeAll(() => { setAutoFreeze(true) setUseProxies(useProxies) })
it("must be called on draft", () => { expect(() => { current({}) }).toThrowError( isProd ? "[Immer] minified error nr: 22 '[object Object]'. Find the full error at: https://bit.ly/3cXEKWf" : "[Immer] 'current' expects a draft, got: [object Object]" ) })
it("can handle simple arrays", () => { const base = [{x: 1}] let c const next = produce(base, draft => { expect(current(draft)).toEqual(base) draft[0].x++ c = current(draft) expect(c).toEqual([{x: 2}]) expect(Array.isArray(c)) draft[0].x++ }) expect(next).toEqual([{x: 3}]) expect(c).toEqual([{x: 2}]) expect(isDraft(c)).toBe(false) })
it("won't freeze", () => { const base = {x: 1} const next = produce(base, draft => { draft.x++ expect(Object.isFrozen(current(draft))).toBe(false) }) })
it("returns original without changes", () => { const base = {} produce(base, draft => { expect(original(draft)).toBe(base) expect(current(draft)).toBe(base) }) })
it("can handle property additions", () => { const base = {} produce(base, draft => { draft.x = true const c = current(draft) expect(c).not.toBe(base) expect(c).not.toBe(draft) expect(c).toEqual({ x: true }) }) })
it("can handle property deletions", () => { const base = { x: 1 } produce(base, draft => { delete draft.x const c = current(draft) expect(c).not.toBe(base) expect(c).not.toBe(draft) expect(c).toEqual({}) }) })
it("won't reflect changes over time", () => { const base = { x: 1 } produce(base, draft => { draft.x++ const c = current(draft) expect(c).toEqual({ x: 2 }) draft.x++ expect(c).toEqual({ x: 2 }) }) })
it("will find drafts inside objects", () => { const base = { x: 1, y: { z: 2 }, z: {} } produce(base, draft => { draft.y.z++ draft.y = { nested: draft.y } const c = current(draft) expect(c).toEqual({ x: 1, y: { nested: { z: 3 } }, z: {} }) expect(isDraft(c.y.nested)).toBe(false) expect(c.z).toBe(base.z) expect(c.y.nested).not.toBe(draft.y.nested) }) })
it("handles map - 1", () => { const base = new Map([["a", {x: 1}]]) produce(base, draft => { expect(current(draft)).toBe(base) draft.delete("a") let c = current(draft) expect(current(draft)).not.toBe(base) expect(current(draft)).not.toBe(draft) expect(c).toEqual(new Map()) const obj = {} draft.set("b", obj) expect(c).toEqual(new Map()) expect(current(draft)).toEqual(new Map([["b", obj]])) expect(c).toBeInstanceOf(Map) }) })
it("handles map - 2", () => { const base = new Map([["a", {x: 1}]]) produce(base, draft => { draft.get("a").x++ const c = current(draft) expect(c).not.toBe(base) expect(c).toEqual(new Map([["a", {x: 2}]])) draft.get("a").x++ expect(c).toEqual(new Map([["a", {x: 2}]])) }) })
it("handles set", () => { const base = new Set([1]) produce(base, draft => { expect(current(draft)).toBe(base) draft.add(2) const c = current(draft) expect(c).toEqual(new Set([1, 2])) expect(c).not.toBe(draft) expect(c).not.toBe(base) draft.add(3) expect(c).toEqual(new Set([1, 2])) expect(c).toBeInstanceOf(Set) }) })
it("handles simple class", () => { class Counter { [immerable] = true current = 0
inc() { this.current++ } }
const counter1 = new Counter() produce(counter1, draft => { expect(current(draft)).toBe(counter1) draft.inc() const c = current(draft) expect(c).not.toBe(draft) expect(c.current).toBe(1) c.inc() expect(c.current).toBe(2) expect(draft.current).toBe(1) draft.inc() draft.inc() expect(c.current).toBe(2) expect(draft.current).toBe(3) expect(c).toBeInstanceOf(Counter) }) }) })}