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 8adfb31 point at it.
test: more recursive produce testsAlec Larsonbefore adoption8adfb31
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"test: more recursive produce testsAlec Larsonbefore adoption8adfb31
line 8why, one step back8Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 9–10why, one step back9enableAllPlugins()10test: more recursive produce testsAlec Larsonbefore adoption8adfb31
lines 11–16why, one step back11const {isFrozen} = Object1213runTests("proxy", true)14runTests("es5", false)1516function runTests(name, useProxies) {style: use tabsuse-tabsbefore adoptiona9c0fd0
line 17why, one step back17 describe("auto freeze - " + name, () => {fix: Merge branch 'oriSomething-fix-472'Michel Weststratebefore adoption7a8e4e4
lines 18–21why, one step back18 beforeAll(() => {19 setUseProxies(useProxies)20 setAutoFreeze(true)21 })test: more recursive produce testsAlec Larsonbefore adoption8adfb31
line 22why, one step back22style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 23–32why, one step back23 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 })test: more recursive produce testsAlec Larsonbefore adoption8adfb31
line 33why, one step back33Immer 7Michel Weststratebefore adoption467ea5d
line 34why, one step back34 it("freezes reused base state", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
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()style: use tabsuse-tabsbefore adoptiona9c0fd0
line 41why, one step back41 })test: more recursive produce testsAlec Larsonbefore adoption8adfb31
line 42why, one step back42style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 43–51why, one step back43 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 })test: more recursive produce testsAlec Larsonbefore adoption8adfb31
line 52why, one step back52style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 53–61why, one step back53 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 })test: more recursive produce testsAlec Larsonbefore adoption8adfb31
line 62why, one step back62BREAKING CHANGE: In development mode, any new value stored in a tree will be deeply frozen. Fixes #412 #363 #260 #230 #313 #229Michel Weststratebefore adoption9fa2581
lines 63–69why, one step back63 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()style: use tabsuse-tabsbefore adoptiona9c0fd0
line 70why, one step back70 })test: more recursive produce testsAlec Larsonbefore adoption8adfb31
line 71why, one step back71BREAKING CHANGE: In development mode, any new value stored in a tree will be deeply frozen. Fixes #412 #363 #260 #230 #313 #229Michel Weststratebefore adoption9fa2581
line 72why, one step back72 it("a new object is added to the root draft", () => {style: use tabsuse-tabsbefore adoptiona9c0fd0
line 73why, one step back73 const base = {}BREAKING CHANGE: In development mode, any new value stored in a tree will be deeply frozen. Fixes #412 #363 #260 #230 #313 #229Michel Weststratebefore adoption9fa2581
lines 74–80why, one step back74 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()style: use tabsuse-tabsbefore adoptiona9c0fd0
line 81why, one step back81 })test: more recursive produce testsAlec Larsonbefore adoption8adfb31
line 82why, one step back82BREAKING CHANGE: In development mode, any new value stored in a tree will be deeply frozen. Fixes #412 #363 #260 #230 #313 #229Michel Weststratebefore adoption9fa2581
lines 83–94why, one step back83 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 })94style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 95–98why, one step back95 it("a nested draft is returned", () => {96 const base = {a: {}}97 const next = produce(base, draft => draft.a)98 expect(next).toBe(base.a)BREAKING CHANGE: In development mode, any new value stored in a tree will be deeply frozen. Fixes #412 #363 #260 #230 #313 #229Michel Weststratebefore adoption9fa2581
line 99why, one step back99 expect(isFrozen(next)).toBeTruthy()style: use tabsuse-tabsbefore adoptiona9c0fd0
line 100why, one step back100 })test: more recursive produce testsAlec Larsonbefore adoption8adfb31
line 101why, one step back101style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 102–105why, one step back102 it("the base state is returned", () => {103 const base = {}104 const next = produce(base, () => base)105 expect(next).toBe(base)BREAKING CHANGE: In development mode, any new value stored in a tree will be deeply frozen. Fixes #412 #363 #260 #230 #313 #229Michel Weststratebefore adoption9fa2581
line 106why, one step back106 expect(isFrozen(next)).toBeTruthy()style: use tabsuse-tabsbefore adoptiona9c0fd0
line 107why, one step back107 })test: more recursive produce testsAlec Larsonbefore adoption8adfb31
line 108why, one step back108BREAKING CHANGE: In development mode, any new value stored in a tree will be deeply frozen. Fixes #412 #363 #260 #230 #313 #229Michel Weststratebefore adoption9fa2581
lines 109–114why, one step back109 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()style: use tabsuse-tabsbefore adoptiona9c0fd0
line 115why, one step back115 })test: more recursive produce testsAlec Larsonbefore adoption8adfb31
line 116why, one step back116BREAKING CHANGE: In development mode, any new value stored in a tree will be deeply frozen. Fixes #412 #363 #260 #230 #313 #229Michel Weststratebefore adoption9fa2581
lines 117–120why, one step back117 it("the root draft is returned", () => {118 const base = {a: {}}119 const next = produce(base, draft => draft)120 expect(next).toBe(base)style: use tabsuse-tabsbefore adoptiona9c0fd0
line 121why, one step back121 expect(isFrozen(next)).toBeTruthy()BREAKING CHANGE: In development mode, any new value stored in a tree will be deeply frozen. Fixes #412 #363 #260 #230 #313 #229Michel Weststratebefore adoption9fa2581
line 122why, one step back122 expect(isFrozen(next.a)).toBeTruthy()style: use tabsuse-tabsbefore adoptiona9c0fd0
line 123why, one step back123 })test: more recursive produce testsAlec Larsonbefore adoption8adfb31
line 124why, one step back124BREAKING CHANGE: In development mode, any new value stored in a tree will be deeply frozen. Fixes #412 #363 #260 #230 #313 #229Michel Weststratebefore adoption9fa2581
lines 125–128why, one step back125 it("a new object replaces a primitive base", () => {126 const obj = {a: {}}127 const next = produce(null, () => obj)128 expect(next).toBe(obj)style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 129–132why, one step back129 expect(isFrozen(next)).toBeTruthy()130 expect(isFrozen(next.a)).toBeTruthy()131 })132 })test: more recursive produce testsAlec Larsonbefore adoption8adfb31
line 133why, one step back133style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 134–144why, one step back134 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 })feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 145–147why, one step back145146 it("will freeze maps", () => {147 const base = new Map()fix: Merge branch 'oriSomething-fix-472'Michel Weststratebefore adoption7a8e4e4
line 148why, one step back148feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 149–151why, one step back149 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()feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
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()feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 168–170why, one step back168169 // In draft, still editable170 expect(produce(res, d => void d.add(2))).not.toBe(res)fix: Merge branch 'oriSomething-fix-472'Michel Weststratebefore adoption7a8e4e4
lines 171–175why, one step back171 })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 ],fix: Merge branch 'oriSomething-fix-472'Michel Weststratebefore adoption7a8e4e4
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)feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
line 247why, one step back247 })style: use tabsuse-tabsbefore adoptiona9c0fd0
line 248why, one step back248 })test: more recursive produce testsAlec Larsonbefore adoption8adfb31
line 249why, one step back249}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})