improved frozen test
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 6990c33 · Browse files · 2 files·+49 added−41 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+49 added−41 removed
- tests:+49 added−41 removed
Provenance
- Commit
1fa3715- Tree
27fc8e0- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
6990c33
2 files changed+49 added−41 removed
modified:__tests__/base.js+2 added-2 removed
142 unmodified lines143 expect(nextState.anArray).not.toBe(baseState.anArray)144 expect(enumerableOnly(nextState.anArray)).toEqual([1, 2, 3, {c: 5}])145 })146 147- it.only("should support sorting arrays - 2", () => {147+ it.skip("should support sorting arrays - 2", () => {148 const nextState = immer([], s => {149 debugger150 s.unshift("x")151 // console.dir(s)4 unmodified lines156 })157 expect(nextState).toEqual(["x"])158 })159 160- it.only("should support sorting arrays - 2", () => {160+ it.skip("should support sorting arrays - 2", () => {161 const nextState = immer(baseState, s => {162 debugger163 s.anArray.unshift("x")164 s.anArray[3].c = 4162 unmodified linesmodified:__tests__/frozen.js+47 added-39 removed
122345456786781011121314151617910111213141516192021221718192021222324252425262726272829293031303334353631323334353637383938394041424344454041424344454647484950515253475455564957"use strict"import immer from ".."import * as immerProxy from ".."import * as immerEs5 from "../es5"import deepFreeze from "deep-freeze"
describe("auto freeze", () => { const baseState = { object: {a: 1}, array: [1, 2] }runBaseTest("proxy", immerProxy)runBaseTest("es5", immerEs5)
it("should freeze objects after modifications", () => { expect(Object.isFrozen(baseState.object)).toBe(false) // initially not frozen const next = immer(baseState, draft => { draft.object.c = 2 }) expect(Object.isFrozen(next.object)).toBe(true) expect(Object.isFrozen(next)).toBe(true) expect(Object.isFrozen(next.array)).toBe(false)function runBaseTest(name, lib) { describe("auto freeze - " + name, () => { const immer = lib.default const baseState = { object: {a: 1}, array: [1, 2] }
expect(() => { next.object.a = 2 }).toThrow(/Cannot assign to read only property/) }) it("should freeze objects after modifications", () => { expect(Object.isFrozen(baseState.object)).toBe(false) // initially not frozen const next = immer(baseState, draft => { draft.object.c = 2 }) expect(Object.isFrozen(next.object)).toBe(true) expect(Object.isFrozen(next)).toBe(true) expect(Object.isFrozen(next.array)).toBe(false)
it("should freeze arrays after modifications", () => { expect(Object.isFrozen(baseState.object)).toBe(false) // initially not frozen const next = immer(baseState, draft => { draft.array.push(3) expect(() => { next.object.a = 2 }).toThrow(/Cannot assign to read only property/) }) 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(/Cannot add\/remove sealed array elements/) }) it("should freeze arrays after modifications", () => { expect(Object.isFrozen(baseState.object)).toBe(false) // initially not frozen const next = immer(baseState, draft => { draft.array.push(3) }) expect(Object.isFrozen(next.object)).toBe(false) // not touched expect(Object.isFrozen(next)).toBe(true) expect(Object.isFrozen(next.array)).toBe(true)
it("can handle already frozen trees", () => { const a = [] const b = {a: a} Object.freeze(a) Object.freeze(b) const n = immer(b, draft => { draft.c = true draft.a.push(3) expect(() => { next.array.shift() }).toThrow(/Cannot add\/remove sealed array elements/) })
it("can handle already frozen trees", () => { const a = [] const b = {a: a} Object.freeze(a) Object.freeze(b) const n = immer(b, draft => { draft.c = true draft.a.push(3) }) expect(n).toEqual({c: true, a: [3]}) expect(n).toEqual({c: true, a: [3]}) }) })})}