frozen.js
This file
Path
__tests__/frozen.js
134 lines · 4.6 KiB · JavaScript
Last changed by
chore: update import paths in testsaleclarsonbefore adoptioncdc09b1
History of this fileTake it
The walk stopped after 200 commits: lines older than 51648ef point at it.
Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 1why, one step back1"use strict"chore: update import paths in testsaleclarsonbefore adoptioncdc09b1
line 2why, one step back2import produce, {setUseProxies, setAutoFreeze} from "../src/index"Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 3why, one step back3Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
lines 4–5why, one step back4const {isFrozen} = Object5Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
lines 6–13why, one step back6runTests("proxy", true)7runTests("es5", false)89function runTests(name, useProxies) {10 describe("auto freeze - " + name, () => {11 setUseProxies(useProxies)12 setAutoFreeze(true)13Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
lines 14–23why, one step back14 it("never freezes the base state", () => {15 const base = {arr: [1], obj: {a: 1}}16 const next = produce(base, draft => {17 draft.arr.push(1)18 })19 expect(isFrozen(base)).toBeFalsy()20 expect(isFrozen(base.arr)).toBeFalsy()21 expect(isFrozen(next)).toBeTruthy()22 expect(isFrozen(next.arr)).toBeTruthy()23 })Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 24why, one step back24Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
lines 25–28why, one step back25 it("never freezes reused state", () => {26 const base = {arr: [1], obj: {a: 1}}27 const next = produce(base, draft => {28 draft.arr.push(1)Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 29why, one step back29 })Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
lines 30–32why, one step back30 expect(next.obj).toBe(base.obj)31 expect(isFrozen(next.obj)).toBeFalsy()32 })Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
line 33why, one step back33Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
lines 34–52why, one step back34 describe("the result is always auto-frozen when", () => {35 it("the root draft is mutated (and no error is thrown)", () => {36 const base = {}37 const next = produce(base, draft => {38 draft.a = 139 })40 expect(next).not.toBe(base)41 expect(isFrozen(next)).toBeTruthy()42 })4344 it("a nested draft is mutated (and no error is thrown)", () => {45 const base = {a: {}}46 const next = produce(base, draft => {47 draft.a.b = 148 })49 expect(next).not.toBe(base)50 expect(isFrozen(next)).toBeTruthy()51 expect(isFrozen(next.a)).toBeTruthy()52 })Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
lines 53–54why, one step back53 })54Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
lines 55–60why, one step back55 describe("the result is never auto-frozen when", () => {56 it("the producer is a no-op", () => {57 const base = {}58 const next = produce(base, () => {})59 expect(next).toBe(base)60 expect(isFrozen(next)).toBeFalsy()Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
lines 61–62why, one step back61 })62Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
lines 63–119why, one step back63 it("the root draft is returned", () => {64 const base = {}65 const next = produce(base, draft => draft)66 expect(next).toBe(base)67 expect(isFrozen(next)).toBeFalsy()68 })6970 it("a nested draft is returned", () => {71 const base = {a: {}}72 const next = produce(base, draft => draft.a)73 expect(next).toBe(base.a)74 expect(isFrozen(next)).toBeFalsy()75 })7677 it("the base state is returned", () => {78 const base = {}79 const next = produce(base, () => base)80 expect(next).toBe(base)81 expect(isFrozen(next)).toBeFalsy()82 })8384 it("a new object replaces a primitive base", () => {85 const obj = {}86 const next = produce(null, () => obj)87 expect(next).toBe(obj)88 expect(isFrozen(next)).toBeFalsy()89 })9091 it("a new object replaces the entire draft", () => {92 const obj = {a: {b: {}}}93 const next = produce({}, () => obj)94 expect(next).toBe(obj)95 expect(isFrozen(next)).toBeFalsy()96 expect(isFrozen(next.a)).toBeFalsy()97 expect(isFrozen(next.a.b)).toBeFalsy()98 })99100 it("a new object is added to the root draft", () => {101 const base = {}102 const next = produce(base, draft => {103 draft.a = {}104 })105 expect(next).not.toBe(base)106 expect(isFrozen(next)).toBeTruthy()107 expect(isFrozen(next.a)).toBeFalsy()108 })109110 it("a new object is added to a nested draft", () => {111 const base = {a: {}}112 const next = produce(base, draft => {113 draft.a.b = {}114 })115 expect(next).not.toBe(base)116 expect(isFrozen(next)).toBeTruthy()117 expect(isFrozen(next.a)).toBeTruthy()118 expect(isFrozen(next.a.b)).toBeFalsy()119 })Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef
lines 120–134why, one step back120 })121122 it("can handle already frozen trees", () => {123 const a = []124 const b = {a: a}125 Object.freeze(a)126 Object.freeze(b)127 const n = produce(b, draft => {128 draft.c = true129 draft.a.push(3)130 })131 expect(n).toEqual({c: true, a: [3]})132 })133 })134}