improve tests for freezing of return values
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by aleclarson · committed by aleclarson · parent 172d76a · Browse files · 2 files·+101 added−44 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+101 added−44 removed
- tests:+101 added−44 removed
Provenance
- Commit
41fdc55- Tree
94d31e8- Author
aleclarson <alec.stanford.larson@gmail.com>- Committer
aleclarson <alec.stanford.larson@gmail.com>- Parent
172d76a
2 files changed+101 added−44 removed
modified:__tests__/base.js-16 removed
870 unmodified lines871 expect(next).not.toBe(base)872 expect(next.user).toEqual(user)873 })874 875- if (freeze)876- it("should freeze new data well", () => {877- const base = {}878- const next = produce(879- base,880- draft => {881- draft.x = {y: [{z: true}]}882- },883- listener884- )885- expect(Object.isFrozen(next)).toBe(true)886- expect(Object.isFrozen(next.x)).toBe(true)887- expect(Object.isFrozen(next.x.y)).toBe(true)888- expect(Object.isFrozen(next.x.y[0].z)).toBe(true)889- })890-875 it("should structurally share identical objects in the tree", () => {876 const base = {bear: {legs: 4}, eagle: {legs: 3}}877 const next = produce(878 base,476 unmodified linesmodified:__tests__/frozen.js+101 added-28 removed
122345678910111213131415161415161718192021222324181920212526272829232425303132332728293435363738394041424344454647484950515253543233343555565758596061373839624142434445466364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212311 unmodified lines"use strict"import deepFreeze from "deep-freeze"import produce, {setUseProxies, setAutoFreeze} from "../src/immer"
const {isFrozen} = Object
runTests("proxy", true)runTests("es5", false)
function runTests(name, useProxies) { describe("auto freeze - " + name, () => { setUseProxies(useProxies) setAutoFreeze(true)
const baseState = { object: {a: 1}, array: [1, 2] } 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("should freeze objects after modifications", () => { expect(Object.isFrozen(baseState.object)).toBe(false) // initially not frozen const next = produce(baseState, draft => { draft.object.c = 2 it("never freezes reused state", () => { const base = {arr: [1], obj: {a: 1}} const next = produce(base, draft => { draft.arr.push(1) }) expect(Object.isFrozen(next.object)).toBe(true) expect(Object.isFrozen(next)).toBe(true) expect(Object.isFrozen(next.array)).toBe(false) expect(next.obj).toBe(base.obj) expect(isFrozen(next.obj)).toBeFalsy() })
expect(() => { next.object.a = 2 }).toThrow(/Cannot assign to read only property/) 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("should freeze arrays after modifications", () => { expect(Object.isFrozen(baseState.object)).toBe(false) // initially not frozen const next = produce(baseState, draft => { draft.array.push(3) describe("the result is never auto-frozen when", () => { it("the producer is a no-op", () => { const base = {} const next = produce(base, () => {}) expect(next).toBe(base) expect(isFrozen(next)).toBeFalsy() }) expect(Object.isFrozen(next.object)).toBe(false) // not touched expect(Object.isFrozen(next)).toBe(true) expect(Object.isFrozen(next.array)).toBe(true)
expect(() => { next.array.shift() }).toThrow( // Exception message for older / newer Node.js version. /Cannot add\/remove sealed array elements|Cannot assign to read only property '0' of object '\[object Array\]'/ ) it("the root draft is returned", () => { const base = {} const next = produce(base, draft => draft) expect(next).toBe(base) expect(isFrozen(next)).toBeFalsy() })
it("a nested draft is returned", () => { const base = {a: {}} const next = produce(base, draft => draft.a) expect(next).toBe(base.a) expect(isFrozen(next)).toBeFalsy() })
it("the base state is returned", () => { const base = {} const next = produce(base, () => base) expect(next).toBe(base) expect(isFrozen(next)).toBeFalsy() })
it("a new object replaces a primitive base", () => { const obj = {} const next = produce(null, () => obj) expect(next).toBe(obj) expect(isFrozen(next)).toBeFalsy() })
it("a new object replaces the entire draft", () => { const obj = {a: {b: {}}} const next = produce({}, () => obj) expect(next).toBe(obj) expect(isFrozen(next)).toBeFalsy() expect(isFrozen(next.a)).toBeFalsy() expect(isFrozen(next.a.b)).toBeFalsy() })
it("a new object is added to the root draft", () => { const base = {} const next = produce(base, draft => { draft.a = {} }) expect(next).not.toBe(base) expect(isFrozen(next)).toBeTruthy() expect(isFrozen(next.a)).toBeFalsy() })
it("a new object is added to a nested draft", () => { const base = {a: {}} 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.a.b)).toBeFalsy() }) })
it("can handle already frozen trees", () => { const a = []11 unmodified lines