frozen.js
This file
Path
__tests__/frozen.js
267 lines · 6.4 KiB · JavaScript
Last changed by
feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268"use strict"import produce, { setUseProxies, setAutoFreeze, enableAllPlugins, freeze} from "../src/immer"
enableAllPlugins()
const {isFrozen} = Object
runTests("proxy", true)runTests("es5", false)
function runTests(name, useProxies) { describe("auto freeze - " + name, () => { beforeAll(() => { setUseProxies(useProxies) setAutoFreeze(true) })
it("never freezes the base state", () => { const base = {arr: [1], obj: {a: 1}} const next = produce(base, draft => { draft.arr.push(1) }) expect(isFrozen(base)).toBeFalsy() expect(isFrozen(base.arr)).toBeFalsy() expect(isFrozen(next)).toBeTruthy() expect(isFrozen(next.arr)).toBeTruthy() })
it("freezes reused base state", () => { const base = {arr: [1], obj: {a: 1}} const next = produce(base, draft => { draft.arr.push(1) }) expect(next.obj).toBe(base.obj) expect(isFrozen(next.obj)).toBeTruthy() })
describe("the result is always auto-frozen when", () => { it("the root draft is mutated (and no error is thrown)", () => { const base = {} const next = produce(base, draft => { draft.a = 1 }) expect(next).not.toBe(base) expect(isFrozen(next)).toBeTruthy() })
it("a nested draft is mutated (and no error is thrown)", () => { const base = {a: {}} const next = produce(base, draft => { draft.a.b = 1 }) expect(next).not.toBe(base) expect(isFrozen(next)).toBeTruthy() expect(isFrozen(next.a)).toBeTruthy() })
it("a new object replaces the entire draft", () => { const obj = {a: {b: {}}} const next = produce({}, () => obj) expect(next).toBe(obj) expect(isFrozen(next)).toBeTruthy() expect(isFrozen(next.a)).toBeTruthy() expect(isFrozen(next.a.b)).toBeTruthy() })
it("a new object is added to the root draft", () => { const base = {} const next = produce(base, draft => { draft.a = {b: []} }) expect(next).not.toBe(base) expect(isFrozen(next)).toBeTruthy() expect(isFrozen(next.a)).toBeTruthy() expect(isFrozen(next.b)).toBeTruthy() })
it("a new object is added to a nested draft", () => { const base = {a: {}} const next = produce(base, draft => { draft.a.b = {c: {}} }) expect(next).not.toBe(base) expect(isFrozen(next)).toBeTruthy() expect(isFrozen(next.a)).toBeTruthy() expect(isFrozen(next.a.b)).toBeTruthy() expect(isFrozen(next.a.b.c)).toBeTruthy() })
it("a nested draft is returned", () => { const base = {a: {}} const next = produce(base, draft => draft.a) expect(next).toBe(base.a) expect(isFrozen(next)).toBeTruthy() })
it("the base state is returned", () => { const base = {} const next = produce(base, () => base) expect(next).toBe(base) expect(isFrozen(next)).toBeTruthy() })
it("the producer is a no-op", () => { const base = {a: {}} const next = produce(base, () => {}) expect(next).toBe(base) expect(isFrozen(next)).toBeTruthy() expect(isFrozen(next.a)).toBeTruthy() })
it("the root draft is returned", () => { const base = {a: {}} const next = produce(base, draft => draft) expect(next).toBe(base) expect(isFrozen(next)).toBeTruthy() expect(isFrozen(next.a)).toBeTruthy() })
it("a new object replaces a primitive base", () => { const obj = {a: {}} const next = produce(null, () => obj) expect(next).toBe(obj) expect(isFrozen(next)).toBeTruthy() expect(isFrozen(next.a)).toBeTruthy() }) })
it("can handle already frozen trees", () => { const a = [] const b = {a: a} Object.freeze(a) Object.freeze(b) const n = produce(b, draft => { draft.c = true draft.a.push(3) }) expect(n).toEqual({c: true, a: [3]}) })
it("will freeze maps", () => { const base = new Map()
const res = produce(base, draft => { draft.set("a", 1) }) expect(() => res.set("b", 2)).toThrowErrorMatchingSnapshot() expect(() => res.clear()).toThrowErrorMatchingSnapshot() expect(() => res.delete("b")).toThrowErrorMatchingSnapshot()
// In draft, still editable expect(produce(res, d => void d.set("a", 2))).not.toBe(res) })
it("will freeze sets", () => { const base = new Set() const res = produce(base, draft => { base.add(1) }) expect(() => base.add(2)).toThrowErrorMatchingSnapshot() expect(() => base.delete(1)).toThrowErrorMatchingSnapshot() expect(() => base.clear()).toThrowErrorMatchingSnapshot()
// In draft, still editable expect(produce(res, d => void d.add(2))).not.toBe(res) })
it("Map#get() of frozen object will became draftable", () => { const base = { map: new Map([ [ "a", new Map([ ["a", true], ["b", true], ["c", true] ]) ], ["b", new Map([["a", true]])], ["c", new Map([["a", true]])] ]) }
// This will freeze maps const frozen = produce(base, draft => {})
// https://github.com/immerjs/immer/issues/472 produce(frozen, draft => { ;["b", "c"].forEach(other => { const m = draft.map.get(other)
m.delete("a") }) }) })
it("never freezes non-enumerable fields #590", () => { const component = {} Object.defineProperty(component, "state", { value: {x: 1}, enumerable: false, writable: true, configurable: true })
const state = { x: 1 }
const state2 = produce(state, draft => { draft.ref = component })
expect(() => { state2.ref.state.x++ }).not.toThrow() expect(state2.ref.state.x).toBe(2) })
it("never freezes symbolic fields #590", () => { const component = {} const symbol = Symbol("test") Object.defineProperty(component, symbol, { value: {x: 1}, enumerable: true, writable: true, configurable: true })
const state = { x: 1 }
const state2 = produce(state, draft => { draft.ref = component })
expect(() => { state2.ref[symbol].x++ }).not.toThrow() expect(state2.ref[symbol].x).toBe(2) }) })}
test("freeze - shallow", () => { const obj1 = {hello: {world: true}} const res = freeze(obj1)
expect(res).toBe(obj1) expect(Object.isFrozen(res)).toBe(true) expect(Object.isFrozen(res.hello)).toBe(false)})
test("freeze - deep", () => { const obj1 = {hello: {world: true}} const res = freeze(obj1, true)
expect(res).toBe(obj1) expect(Object.isFrozen(res)).toBe(true) expect(Object.isFrozen(res.hello)).toBe(true)})