frozen.js
This file
Path
__tests__/frozen.js
269 lines · 6.4 KiB · JavaScript
Last changed by
Fix: freezing of Map values during finalization (#1150)Chetan Satputebefore adoptionb4f71a6
History of this fileTake it
The walk stopped after 200 commits: lines older than 07f5cc9 point at it.
chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 1why, one step back1"use strict"Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 2–3why, one step back2import {3 produce,chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 4–5why, one step back4 setUseProxies,5 setAutoFreeze,Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 6–7why, one step back6 freeze,7 enableMapSetchore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 8–9why, one step back8} from "../src/immer"9Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 10why, one step back10enableMapSet()chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 11–15why, one step back1112const {isFrozen} = Object1314runTests("proxy", true)15Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 16why, one step back16function runTests(name) {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 17–149why, one step back17 describe("auto freeze - " + name, () => {18 beforeAll(() => {19 setAutoFreeze(true)20 })2122 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 })3233 it("freezes reused base state", () => {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)39 expect(isFrozen(next.obj)).toBeTruthy()40 })4142 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 })5152 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 })6162 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()69 })7071 it("a new object is added to the root draft", () => {72 const base = {}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()80 })8182 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 })9394 it("a nested draft is returned", () => {95 const base = {a: {}}96 const next = produce(base, draft => draft.a)97 expect(next).toBe(base.a)98 expect(isFrozen(next)).toBeTruthy()99 })100101 it("the base state is returned", () => {102 const base = {}103 const next = produce(base, () => base)104 expect(next).toBe(base)105 expect(isFrozen(next)).toBeTruthy()106 })107108 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()114 })115116 it("the root draft is returned", () => {117 const base = {a: {}}118 const next = produce(base, draft => draft)119 expect(next).toBe(base)120 expect(isFrozen(next)).toBeTruthy()121 expect(isFrozen(next.a)).toBeTruthy()122 })123124 it("a new object replaces a primitive base", () => {125 const obj = {a: {}}126 const next = produce(null, () => obj)127 expect(next).toBe(obj)128 expect(isFrozen(next)).toBeTruthy()129 expect(isFrozen(next.a)).toBeTruthy()130 })131 })132133 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 })144145 it("will freeze maps", () => {146 const base = new Map()147148 const res = produce(base, draft => {149 draft.set("a", 1)Fix: freezing of Map values during finalization (#1150)Chetan Satputebefore adoptionb4f71a6
line 150why, one step back150 draft.set("o", {b: 1})chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 151–154why, one step back151 })152 expect(() => res.set("b", 2)).toThrowErrorMatchingSnapshot()153 expect(() => res.clear()).toThrowErrorMatchingSnapshot()154 expect(() => res.delete("b")).toThrowErrorMatchingSnapshot()Fix: freezing of Map values during finalization (#1150)Chetan Satputebefore adoptionb4f71a6
line 155why, one step back155 expect(Object.isFrozen(res.get("o"))).toBe(true)chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 156–223why, one step back156157 // In draft, still editable158 expect(produce(res, d => void d.set("a", 2))).not.toBe(res)159 })160161 it("will freeze sets", () => {162 const base = new Set()163 const res = produce(base, draft => {164 base.add(1)165 })166 expect(() => base.add(2)).toThrowErrorMatchingSnapshot()167 expect(() => base.delete(1)).toThrowErrorMatchingSnapshot()168 expect(() => base.clear()).toThrowErrorMatchingSnapshot()169170 // In draft, still editable171 expect(produce(res, d => void d.add(2))).not.toBe(res)172 })173174 it("Map#get() of frozen object will became draftable", () => {175 const base = {176 map: new Map([177 [178 "a",179 new Map([180 ["a", true],181 ["b", true],182 ["c", true]183 ])184 ],185 ["b", new Map([["a", true]])],186 ["c", new Map([["a", true]])]187 ])188 }189190 // This will freeze maps191 const frozen = produce(base, draft => {})192193 // https://github.com/immerjs/immer/issues/472194 produce(frozen, draft => {195 ;["b", "c"].forEach(other => {196 const m = draft.map.get(other)197198 m.delete("a")199 })200 })201 })202203 it("never freezes non-enumerable fields #590", () => {204 const component = {}205 Object.defineProperty(component, "state", {206 value: {x: 1},207 enumerable: false,208 writable: true,209 configurable: true210 })211212 const state = {213 x: 1214 }215216 const state2 = produce(state, draft => {217 draft.ref = component218 })219220 expect(() => {221 state2.ref.state.x++222 }).not.toThrow()223 expect(state2.ref.state.x).toBe(2)fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e
line 224why, one step back224 expect(component.state.x).toBe(2)chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 225–269why, one step back225 })226227 it("never freezes symbolic fields #590", () => {228 const component = {}229 const symbol = Symbol("test")230 Object.defineProperty(component, symbol, {231 value: {x: 1},232 enumerable: true,233 writable: true,234 configurable: true235 })236237 const state = {238 x: 1239 }240241 const state2 = produce(state, draft => {242 draft.ref = component243 })244245 expect(() => {246 state2.ref[symbol].x++247 }).not.toThrow()248 expect(state2.ref[symbol].x).toBe(2)249 })250 })251}252253test("freeze - shallow", () => {254 const obj1 = {hello: {world: true}}255 const res = freeze(obj1)256257 expect(res).toBe(obj1)258 expect(Object.isFrozen(res)).toBe(true)259 expect(Object.isFrozen(res.hello)).toBe(false)260})261262test("freeze - deep", () => {263 const obj1 = {hello: {world: true}}264 const res = freeze(obj1, true)265266 expect(res).toBe(obj1)267 expect(Object.isFrozen(res)).toBe(true)268 expect(Object.isFrozen(res.hello)).toBe(true)269})