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
The walk stopped after 200 commits: lines older than fbae585 point at it.
docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585
line 1why, one step back1"use strict"Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 2–4why, one step back2import produce, {3 setUseProxies,4 setAutoFreeze,feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f
lines 5–6why, one step back5 enableAllPlugins,6 freezeMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 7why, one step back7} from "../src/immer"docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585
line 8why, one step back8Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 9–10why, one step back9enableAllPlugins()10docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585
lines 11–33why, one step back11const {isFrozen} = Object1213runTests("proxy", true)14runTests("es5", false)1516function runTests(name, useProxies) {17 describe("auto freeze - " + name, () => {18 beforeAll(() => {19 setUseProxies(useProxies)20 setAutoFreeze(true)21 })2223 it("never freezes the base state", () => {24 const base = {arr: [1], obj: {a: 1}}25 const next = produce(base, draft => {26 draft.arr.push(1)27 })28 expect(isFrozen(base)).toBeFalsy()29 expect(isFrozen(base.arr)).toBeFalsy()30 expect(isFrozen(next)).toBeTruthy()31 expect(isFrozen(next.arr)).toBeTruthy()32 })33Immer 7Michel Weststratebefore adoption467ea5d
line 34why, one step back34 it("freezes reused base state", () => {docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585
lines 35–39why, one step back35 const base = {arr: [1], obj: {a: 1}}36 const next = produce(base, draft => {37 draft.arr.push(1)38 })39 expect(next.obj).toBe(base.obj)Immer 7Michel Weststratebefore adoption467ea5d
line 40why, one step back40 expect(isFrozen(next.obj)).toBeTruthy()docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585
lines 41–151why, one step back41 })4243 describe("the result is always auto-frozen when", () => {44 it("the root draft is mutated (and no error is thrown)", () => {45 const base = {}46 const next = produce(base, draft => {47 draft.a = 148 })49 expect(next).not.toBe(base)50 expect(isFrozen(next)).toBeTruthy()51 })5253 it("a nested draft is mutated (and no error is thrown)", () => {54 const base = {a: {}}55 const next = produce(base, draft => {56 draft.a.b = 157 })58 expect(next).not.toBe(base)59 expect(isFrozen(next)).toBeTruthy()60 expect(isFrozen(next.a)).toBeTruthy()61 })6263 it("a new object replaces the entire draft", () => {64 const obj = {a: {b: {}}}65 const next = produce({}, () => obj)66 expect(next).toBe(obj)67 expect(isFrozen(next)).toBeTruthy()68 expect(isFrozen(next.a)).toBeTruthy()69 expect(isFrozen(next.a.b)).toBeTruthy()70 })7172 it("a new object is added to the root draft", () => {73 const base = {}74 const next = produce(base, draft => {75 draft.a = {b: []}76 })77 expect(next).not.toBe(base)78 expect(isFrozen(next)).toBeTruthy()79 expect(isFrozen(next.a)).toBeTruthy()80 expect(isFrozen(next.b)).toBeTruthy()81 })8283 it("a new object is added to a nested draft", () => {84 const base = {a: {}}85 const next = produce(base, draft => {86 draft.a.b = {c: {}}87 })88 expect(next).not.toBe(base)89 expect(isFrozen(next)).toBeTruthy()90 expect(isFrozen(next.a)).toBeTruthy()91 expect(isFrozen(next.a.b)).toBeTruthy()92 expect(isFrozen(next.a.b.c)).toBeTruthy()93 })9495 it("a nested draft is returned", () => {96 const base = {a: {}}97 const next = produce(base, draft => draft.a)98 expect(next).toBe(base.a)99 expect(isFrozen(next)).toBeTruthy()100 })101102 it("the base state is returned", () => {103 const base = {}104 const next = produce(base, () => base)105 expect(next).toBe(base)106 expect(isFrozen(next)).toBeTruthy()107 })108109 it("the producer is a no-op", () => {110 const base = {a: {}}111 const next = produce(base, () => {})112 expect(next).toBe(base)113 expect(isFrozen(next)).toBeTruthy()114 expect(isFrozen(next.a)).toBeTruthy()115 })116117 it("the root draft is returned", () => {118 const base = {a: {}}119 const next = produce(base, draft => draft)120 expect(next).toBe(base)121 expect(isFrozen(next)).toBeTruthy()122 expect(isFrozen(next.a)).toBeTruthy()123 })124125 it("a new object replaces a primitive base", () => {126 const obj = {a: {}}127 const next = produce(null, () => obj)128 expect(next).toBe(obj)129 expect(isFrozen(next)).toBeTruthy()130 expect(isFrozen(next.a)).toBeTruthy()131 })132 })133134 it("can handle already frozen trees", () => {135 const a = []136 const b = {a: a}137 Object.freeze(a)138 Object.freeze(b)139 const n = produce(b, draft => {140 draft.c = true141 draft.a.push(3)142 })143 expect(n).toEqual({c: true, a: [3]})144 })145146 it("will freeze maps", () => {147 const base = new Map()148149 const res = produce(base, draft => {150 draft.set("a", 1)151 })Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 152–154why, one step back152 expect(() => res.set("b", 2)).toThrowErrorMatchingSnapshot()153 expect(() => res.clear()).toThrowErrorMatchingSnapshot()154 expect(() => res.delete("b")).toThrowErrorMatchingSnapshot()docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585
lines 155–164why, one step back155156 // In draft, still editable157 expect(produce(res, d => void d.set("a", 2))).not.toBe(res)158 })159160 it("will freeze sets", () => {161 const base = new Set()162 const res = produce(base, draft => {163 base.add(1)164 })Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 165–167why, one step back165 expect(() => base.add(2)).toThrowErrorMatchingSnapshot()166 expect(() => base.delete(1)).toThrowErrorMatchingSnapshot()167 expect(() => base.clear()).toThrowErrorMatchingSnapshot()docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585
lines 168–175why, one step back168169 // In draft, still editable170 expect(produce(res, d => void d.add(2))).not.toBe(res)171 })172173 it("Map#get() of frozen object will became draftable", () => {174 const base = {175 map: new Map([Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 176–183why, one step back176 [177 "a",178 new Map([179 ["a", true],180 ["b", true],181 ["c", true]182 ])183 ],docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585
lines 184–199why, one step back184 ["b", new Map([["a", true]])],185 ["c", new Map([["a", true]])]186 ])187 }188189 // This will freeze maps190 const frozen = produce(base, draft => {})191192 // https://github.com/immerjs/immer/issues/472193 produce(frozen, draft => {194 ;["b", "c"].forEach(other => {195 const m = draft.map.get(other)196197 m.delete("a")198 })199 })fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 200–222why, one step back200 })201202 it("never freezes non-enumerable fields #590", () => {203 const component = {}204 Object.defineProperty(component, "state", {205 value: {x: 1},206 enumerable: false,207 writable: true,208 configurable: true209 })210211 const state = {212 x: 1213 }214215 const state2 = produce(state, draft => {216 draft.ref = component217 })218219 expect(() => {220 state2.ref.state.x++221 }).not.toThrow()222 expect(state2.ref.state.x).toBe(2)Immer 7Michel Weststratebefore adoption467ea5d
lines 223–246why, one step back223 })224225 it("never freezes symbolic fields #590", () => {226 const component = {}227 const symbol = Symbol("test")228 Object.defineProperty(component, symbol, {229 value: {x: 1},230 enumerable: true,231 writable: true,232 configurable: true233 })234235 const state = {236 x: 1237 }238239 const state2 = produce(state, draft => {240 draft.ref = component241 })242243 expect(() => {244 state2.ref[symbol].x++245 }).not.toThrow()246 expect(state2.ref[symbol].x).toBe(2)docs: fix video player scrolling on chrome, fixes #520Michel Weststratebefore adoptionfbae585
lines 247–249why, one step back247 })248 })249}feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f
lines 250–267why, one step back250251test("freeze - shallow", () => {252 const obj1 = {hello: {world: true}}253 const res = freeze(obj1)254255 expect(res).toBe(obj1)256 expect(Object.isFrozen(res)).toBe(true)257 expect(Object.isFrozen(res.hello)).toBe(false)258})259260test("freeze - deep", () => {261 const obj1 = {hello: {world: true}}262 const res = freeze(obj1, true)263264 expect(res).toBe(obj1)265 expect(Object.isFrozen(res)).toBe(true)266 expect(Object.isFrozen(res.hello)).toBe(true)267})