Skip to content
Myr

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 file

Take 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 back
1"use strict"

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

lines 2–4why, one step back
2import produce, {
3 setUseProxies,4 setAutoFreeze,

feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f

lines 5–6why, one step back
5 enableAllPlugins,
6 freeze

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

line 7why, one step back
7} from "../src/immer"

test: more recursive produce testsAlec Larsonbefore adoption8adfb31

line 8why, one step back
8

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

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

test: more recursive produce testsAlec Larsonbefore adoption8adfb31

lines 11–16why, one step back
11const {isFrozen} = Object
1213runTests("proxy", true)14runTests("es5", false)1516function runTests(name, useProxies) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

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

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

test: more recursive produce testsAlec Larsonbefore adoption8adfb31

line 22why, one step back
22

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 23–32why, one step back
23 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 back
33

Immer 7Michel Weststratebefore adoption467ea5d

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 35–39why, one step back
35 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 back
40 expect(isFrozen(next.obj)).toBeTruthy()

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 41why, one step back
41 })

test: more recursive produce testsAlec Larsonbefore adoption8adfb31

line 42why, one step back
42

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 43–51why, one step back
43 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 back
52

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 53–61why, one step back
53 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 back
62

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 63–69why, one step back
63 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 back
70 })

test: more recursive produce testsAlec Larsonbefore adoption8adfb31

line 71why, one step back
71

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 73why, one step back
73 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 back
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()

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 81why, one step back
81 })

test: more recursive produce testsAlec Larsonbefore adoption8adfb31

line 82why, one step back
82

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 83–94why, one step back
83 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 })94

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 95–98why, one step back
95 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 back
99 expect(isFrozen(next)).toBeTruthy()

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 100why, one step back
100 })

test: more recursive produce testsAlec Larsonbefore adoption8adfb31

line 101why, one step back
101

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 102–105why, one step back
102 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 back
106 expect(isFrozen(next)).toBeTruthy()

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 107why, one step back
107 })

test: more recursive produce testsAlec Larsonbefore adoption8adfb31

line 108why, one step back
108

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 109–114why, one step back
109 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 back
115 })

test: more recursive produce testsAlec Larsonbefore adoption8adfb31

line 116why, one step back
116

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 123why, one step back
123 })

test: more recursive produce testsAlec Larsonbefore adoption8adfb31

line 124why, one step back
124

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 125–128why, one step back
125 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 back
129 expect(isFrozen(next)).toBeTruthy()
130 expect(isFrozen(next.a)).toBeTruthy()131 })132 })

test: more recursive produce testsAlec Larsonbefore adoption8adfb31

line 133why, one step back
133

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 134–144why, one step back
134 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 back
145
146 it("will freeze maps", () => {147 const base = new Map()

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

line 148why, one step back
148

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

lines 149–151why, one step back
149 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 back
152 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 back
155
156 // 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 back
165 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 back
168
169 // 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 back
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 back
176 [
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 back
184 ["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 back
200 })
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 back
223 })
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 back
247 })

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 248why, one step back
248 })

test: more recursive produce testsAlec Larsonbefore adoption8adfb31

line 249why, one step back
249}

feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f

lines 250–267why, one step back
250
251test("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})