Skip to content
Myr

frozen.js

This file

Path

__tests__/frozen.js

201 lines · 5.2 KiB · JavaScript

Last changed by

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

History of this file

Take it

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

feat: drop typescript@3.0 supportaleclarsonbefore adoption66e51e1

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

chore: update import paths in testsaleclarsonbefore adoptioncdc09b1

line 2why, one step back
2import produce, {setUseProxies, setAutoFreeze} from "../src/index"

feat: drop typescript@3.0 supportaleclarsonbefore adoption66e51e1

lines 3–9why, one step back
3
4const {isFrozen} = Object56runTests("proxy", true)7runTests("es5", false)89function runTests(name, useProxies) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

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

lines 11–14why, one step back
11 beforeAll(() => {
12 setUseProxies(useProxies)13 setAutoFreeze(true)14 })

feat: drop typescript@3.0 supportaleclarsonbefore adoption66e51e1

line 15why, one step back
15

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 16–25why, one step back
16 it("never freezes the base state", () => {
17 const base = {arr: [1], obj: {a: 1}}18 const next = produce(base, draft => {19 draft.arr.push(1)20 })21 expect(isFrozen(base)).toBeFalsy()22 expect(isFrozen(base.arr)).toBeFalsy()23 expect(isFrozen(next)).toBeTruthy()24 expect(isFrozen(next.arr)).toBeTruthy()25 })

feat: drop typescript@3.0 supportaleclarsonbefore adoption66e51e1

line 26why, one step back
26

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 27–34why, one step back
27 it("never freezes reused state", () => {
28 const base = {arr: [1], obj: {a: 1}}29 const next = produce(base, draft => {30 draft.arr.push(1)31 })32 expect(next.obj).toBe(base.obj)33 expect(isFrozen(next.obj)).toBeFalsy()34 })

feat: drop typescript@3.0 supportaleclarsonbefore adoption66e51e1

line 35why, one step back
35

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 36–44why, one step back
36 describe("the result is always auto-frozen when", () => {
37 it("the root draft is mutated (and no error is thrown)", () => {38 const base = {}39 const next = produce(base, draft => {40 draft.a = 141 })42 expect(next).not.toBe(base)43 expect(isFrozen(next)).toBeTruthy()44 })

feat: drop typescript@3.0 supportaleclarsonbefore adoption66e51e1

line 45why, one step back
45

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 46–54why, one step back
46 it("a nested draft is mutated (and no error is thrown)", () => {
47 const base = {a: {}}48 const next = produce(base, draft => {49 draft.a.b = 150 })51 expect(next).not.toBe(base)52 expect(isFrozen(next)).toBeTruthy()53 expect(isFrozen(next.a)).toBeTruthy()54 })

feat: drop typescript@3.0 supportaleclarsonbefore adoption66e51e1

line 55why, one step back
55

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 56–62why, one step back
56 it("a new object replaces the entire draft", () => {
57 const obj = {a: {b: {}}}58 const next = produce({}, () => obj)59 expect(next).toBe(obj)60 expect(isFrozen(next)).toBeTruthy()61 expect(isFrozen(next.a)).toBeTruthy()62 expect(isFrozen(next.a.b)).toBeTruthy()

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 63why, one step back
63 })

feat: drop typescript@3.0 supportaleclarsonbefore adoption66e51e1

line 64why, one step back
64

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 66why, one step back
66 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 67–73why, one step back
67 const next = produce(base, draft => {
68 draft.a = {b: []}69 })70 expect(next).not.toBe(base)71 expect(isFrozen(next)).toBeTruthy()72 expect(isFrozen(next.a)).toBeTruthy()73 expect(isFrozen(next.b)).toBeTruthy()

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 74why, one step back
74 })

feat: drop typescript@3.0 supportaleclarsonbefore adoption66e51e1

line 75why, one step back
75

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 88–91why, one step back
88 it("a nested draft is returned", () => {
89 const base = {a: {}}90 const next = produce(base, draft => draft.a)91 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 92why, one step back
92 expect(isFrozen(next)).toBeTruthy()

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 93why, one step back
93 })

feat: drop typescript@3.0 supportaleclarsonbefore adoption66e51e1

line 94why, one step back
94

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 100why, one step back
100 })

feat: drop typescript@3.0 supportaleclarsonbefore adoption66e51e1

line 101why, one step back
101

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 102–107why, one step back
102 it("the producer is a no-op", () => {
103 const base = {a: {}}104 const next = produce(base, () => {})105 expect(next).toBe(base)106 expect(isFrozen(next)).toBeTruthy()107 expect(isFrozen(next.a)).toBeTruthy()

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 108why, one step back
108 })

feat: drop typescript@3.0 supportaleclarsonbefore adoption66e51e1

line 109why, one step back
109

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 116why, one step back
116 })

feat: drop typescript@3.0 supportaleclarsonbefore adoption66e51e1

line 117why, one step back
117

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 122–125why, one step back
122 expect(isFrozen(next)).toBeTruthy()
123 expect(isFrozen(next.a)).toBeTruthy()124 })125 })

feat: drop typescript@3.0 supportaleclarsonbefore adoption66e51e1

line 126why, one step back
126

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 127–137why, one step back
127 it("can handle already frozen trees", () => {
128 const a = []129 const b = {a: a}130 Object.freeze(a)131 Object.freeze(b)132 const n = produce(b, draft => {133 draft.c = true134 draft.a.push(3)135 })136 expect(n).toEqual({c: true, a: [3]})137 })

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

lines 138–140why, one step back
138
139 it("will freeze maps", () => {140 const base = new Map()

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

line 141why, one step back
141

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

lines 142–175why, one step back
142 const res = produce(base, draft => {
143 draft.set("a", 1)144 })145 expect(() => res.set("b", 2)).toThrow(146 "This object has been frozen and should not be mutated"147 )148 expect(() => res.clear()).toThrow(149 "This object has been frozen and should not be mutated"150 )151 expect(() => res.delete("b")).toThrow(152 "This object has been frozen and should not be mutated"153 )154155 // 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 })164 expect(() => base.add(2)).toThrow(165 "This object has been frozen and should not be mutated"166 )167 expect(() => base.delete(1)).toThrow(168 "This object has been frozen and should not be mutated"169 )170 expect(() => base.clear()).toThrow(171 "This object has been frozen and should not be mutated"172 )173174 // In draft, still editable175 expect(produce(res, d => void d.add(2))).not.toBe(res)

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

lines 176–198why, one step back
176 })
177178 it("Map#get() of frozen object will became draftable", () => {179 const base = {180 map: new Map([181 ["a", new Map([["a", true], ["b", true], ["c", true]])],182 ["b", new Map([["a", true]])],183 ["c", new Map([["a", true]])]184 ])185 }186187 // This will freeze maps188 const frozen = produce(base, draft => {})189190 // https://github.com/immerjs/immer/issues/472191 produce(frozen, draft => {192 // if (useProxies) debugger193 ;["b", "c"].forEach(other => {194 const m = draft.map.get(other)195196 m.delete("a")197 })198 })

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

line 199why, one step back
199 })

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 200why, one step back
200 })

feat: drop typescript@3.0 supportaleclarsonbefore adoption66e51e1

line 201why, one step back
201}