Skip to content
Myr

frozen.js

This file

Path

__tests__/frozen.js

248 lines · 5.9 KiB · JavaScript

Last changed by

Immer 7Michel Weststratebefore adoption467ea5d

History of this file

Take it

The walk stopped after 200 commits: lines older than dd25143 point at it.

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 1why, one step back
1"use strict"

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 2–6why, one step back
2import produce, {
3 setUseProxies,4 setAutoFreeze,5 enableAllPlugins6} from "../src/immer"

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 7why, one step back
7

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 8–9why, one step back
8enableAllPlugins()
9

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

lines 10–15why, one step back
10const {isFrozen} = Object
1112runTests("proxy", true)13runTests("es5", false)1415function runTests(name, useProxies) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 16why, one step back
16 describe("auto freeze - " + name, () => {

fix: Merge branch 'oriSomething-fix-472'Michel Weststratebefore adoption7a8e4e4

lines 17–20why, one step back
17 beforeAll(() => {
18 setUseProxies(useProxies)19 setAutoFreeze(true)20 })

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 21why, one step back
21

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 22–31why, one step back
22 it("never freezes the base state", () => {
23 const base = {arr: [1], obj: {a: 1}}24 const next = produce(base, draft => {25 draft.arr.push(1)26 })27 expect(isFrozen(base)).toBeFalsy()28 expect(isFrozen(base.arr)).toBeFalsy()29 expect(isFrozen(next)).toBeTruthy()30 expect(isFrozen(next.arr)).toBeTruthy()31 })

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 32why, one step back
32

Immer 7Michel Weststratebefore adoption467ea5d

line 33why, one step back
33 it("freezes reused base state", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 34–38why, one step back
34 const base = {arr: [1], obj: {a: 1}}
35 const next = produce(base, draft => {36 draft.arr.push(1)37 })38 expect(next.obj).toBe(base.obj)

Immer 7Michel Weststratebefore adoption467ea5d

line 39why, one step back
39 expect(isFrozen(next.obj)).toBeTruthy()

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 40why, one step back
40 })

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 41why, one step back
41

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 42–50why, one step back
42 describe("the result is always auto-frozen when", () => {
43 it("the root draft is mutated (and no error is thrown)", () => {44 const base = {}45 const next = produce(base, draft => {46 draft.a = 147 })48 expect(next).not.toBe(base)49 expect(isFrozen(next)).toBeTruthy()50 })

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 51why, one step back
51

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 52–60why, one step back
52 it("a nested draft is mutated (and no error is thrown)", () => {
53 const base = {a: {}}54 const next = produce(base, draft => {55 draft.a.b = 156 })57 expect(next).not.toBe(base)58 expect(isFrozen(next)).toBeTruthy()59 expect(isFrozen(next.a)).toBeTruthy()60 })

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 61why, one step back
61

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 62–68why, one step back
62 it("a new object replaces the entire draft", () => {
63 const obj = {a: {b: {}}}64 const next = produce({}, () => obj)65 expect(next).toBe(obj)66 expect(isFrozen(next)).toBeTruthy()67 expect(isFrozen(next.a)).toBeTruthy()68 expect(isFrozen(next.a.b)).toBeTruthy()

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 69why, one step back
69 })

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 70why, one step back
70

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 71why, one step back
71 it("a new object is added to the root draft", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 72why, one step back
72 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 73–79why, one step back
73 const next = produce(base, draft => {
74 draft.a = {b: []}75 })76 expect(next).not.toBe(base)77 expect(isFrozen(next)).toBeTruthy()78 expect(isFrozen(next.a)).toBeTruthy()79 expect(isFrozen(next.b)).toBeTruthy()

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 80why, one step back
80 })

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 81why, one step back
81

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 82–93why, one step back
82 it("a new object is added to a nested draft", () => {
83 const base = {a: {}}84 const next = produce(base, draft => {85 draft.a.b = {c: {}}86 })87 expect(next).not.toBe(base)88 expect(isFrozen(next)).toBeTruthy()89 expect(isFrozen(next.a)).toBeTruthy()90 expect(isFrozen(next.a.b)).toBeTruthy()91 expect(isFrozen(next.a.b.c)).toBeTruthy()92 })93

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 94–97why, one step back
94 it("a nested draft is returned", () => {
95 const base = {a: {}}96 const next = produce(base, draft => draft.a)97 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 98why, one step back
98 expect(isFrozen(next)).toBeTruthy()

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 99why, one step back
99 })

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 100why, one step back
100

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 101–104why, one step back
101 it("the base state is returned", () => {
102 const base = {}103 const next = produce(base, () => base)104 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 105why, one step back
105 expect(isFrozen(next)).toBeTruthy()

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 106why, one step back
106 })

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 107why, one step back
107

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 108–113why, one step back
108 it("the producer is a no-op", () => {
109 const base = {a: {}}110 const next = produce(base, () => {})111 expect(next).toBe(base)112 expect(isFrozen(next)).toBeTruthy()113 expect(isFrozen(next.a)).toBeTruthy()

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 114why, one step back
114 })

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 115why, one step back
115

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 116–119why, one step back
116 it("the root draft is returned", () => {
117 const base = {a: {}}118 const next = produce(base, draft => draft)119 expect(next).toBe(base)

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 120why, one step back
120 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 121why, one step back
121 expect(isFrozen(next.a)).toBeTruthy()

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 122why, one step back
122 })

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 123why, one step back
123

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 124–127why, one step back
124 it("a new object replaces a primitive base", () => {
125 const obj = {a: {}}126 const next = produce(null, () => obj)127 expect(next).toBe(obj)

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 128–131why, one step back
128 expect(isFrozen(next)).toBeTruthy()
129 expect(isFrozen(next.a)).toBeTruthy()130 })131 })

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 132why, one step back
132

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 133–143why, one step back
133 it("can handle already frozen trees", () => {
134 const a = []135 const b = {a: a}136 Object.freeze(a)137 Object.freeze(b)138 const n = produce(b, draft => {139 draft.c = true140 draft.a.push(3)141 })142 expect(n).toEqual({c: true, a: [3]})143 })

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 144–146why, one step back
144
145 it("will freeze maps", () => {146 const base = new Map()

fix: Merge branch 'oriSomething-fix-472'Michel Weststratebefore adoption7a8e4e4

line 147why, one step back
147

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 148–150why, one step back
148 const res = produce(base, draft => {
149 draft.set("a", 1)150 })

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 151–153why, one step back
151 expect(() => res.set("b", 2)).toThrowErrorMatchingSnapshot()
152 expect(() => res.clear()).toThrowErrorMatchingSnapshot()153 expect(() => res.delete("b")).toThrowErrorMatchingSnapshot()

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 154–163why, one step back
154
155 // In draft, still editable156 expect(produce(res, d => void d.set("a", 2))).not.toBe(res)157 })158159 it("will freeze sets", () => {160 const base = new Set()161 const res = produce(base, draft => {162 base.add(1)163 })

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 164–166why, one step back
164 expect(() => base.add(2)).toThrowErrorMatchingSnapshot()
165 expect(() => base.delete(1)).toThrowErrorMatchingSnapshot()166 expect(() => base.clear()).toThrowErrorMatchingSnapshot()

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 167–169why, one step back
167
168 // In draft, still editable169 expect(produce(res, d => void d.add(2))).not.toBe(res)

fix: Merge branch 'oriSomething-fix-472'Michel Weststratebefore adoption7a8e4e4

lines 170–174why, one step back
170 })
171172 it("Map#get() of frozen object will became draftable", () => {173 const base = {174 map: new Map([

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 175–182why, one step back
175 [
176 "a",177 new Map([178 ["a", true],179 ["b", true],180 ["c", true]181 ])182 ],

fix: Merge branch 'oriSomething-fix-472'Michel Weststratebefore adoption7a8e4e4

lines 183–198why, one step back
183 ["b", new Map([["a", true]])],
184 ["c", new Map([["a", true]])]185 ])186 }187188 // This will freeze maps189 const frozen = produce(base, draft => {})190191 // https://github.com/immerjs/immer/issues/472192 produce(frozen, draft => {193 ;["b", "c"].forEach(other => {194 const m = draft.map.get(other)195196 m.delete("a")197 })198 })

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 199–221why, one step back
199 })
200201 it("never freezes non-enumerable fields #590", () => {202 const component = {}203 Object.defineProperty(component, "state", {204 value: {x: 1},205 enumerable: false,206 writable: true,207 configurable: true208 })209210 const state = {211 x: 1212 }213214 const state2 = produce(state, draft => {215 draft.ref = component216 })217218 expect(() => {219 state2.ref.state.x++220 }).not.toThrow()221 expect(state2.ref.state.x).toBe(2)

Immer 7Michel Weststratebefore adoption467ea5d

lines 222–245why, one step back
222 })
223224 it("never freezes symbolic fields #590", () => {225 const component = {}226 const symbol = Symbol("test")227 Object.defineProperty(component, symbol, {228 value: {x: 1},229 enumerable: true,230 writable: true,231 configurable: true232 })233234 const state = {235 x: 1236 }237238 const state2 = produce(state, draft => {239 draft.ref = component240 })241242 expect(() => {243 state2.ref[symbol].x++244 }).not.toThrow()245 expect(state2.ref[symbol].x).toBe(2)

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

line 246why, one step back
246 })

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 247why, one step back
247 })

fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143

line 248why, one step back
248}