Skip to content
Myr

frozen.js

This file

Path

__tests__/frozen.js

57 lines · 1.9 KiB · JavaScript

Last changed by

transpile es5 codeMichel Weststratebefore adoptionf012154

History of this file

Take it

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

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

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 2–3why, one step back
2import * as immerProxy from "../src/immer"
3import * as immerEs5 from "../src/es5"

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

line 4why, one step back
4import deepFreeze from "deep-freeze"

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

line 5why, one step back
5

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 6–7why, one step back
6runTests("proxy", immerProxy)
7runTests("es5", immerEs5)

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

line 8why, one step back
8

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 9why, one step back
9function runTests(name, lib) {

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

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

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 11why, one step back
11 const produce = lib.default

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

lines 12–15why, one step back
12 const baseState = {
13 object: {a: 1},14 array: [1, 2]15 }

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

line 16why, one step back
16

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

lines 17–18why, one step back
17 it("should freeze objects after modifications", () => {
18 expect(Object.isFrozen(baseState.object)).toBe(false) // initially not frozen

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 19why, one step back
19 const next = produce(baseState, draft => {

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

lines 20–24why, one step back
20 draft.object.c = 2
21 })22 expect(Object.isFrozen(next.object)).toBe(true)23 expect(Object.isFrozen(next)).toBe(true)24 expect(Object.isFrozen(next.array)).toBe(false)

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

line 25why, one step back
25

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

lines 26–28why, one step back
26 expect(() => {
27 next.object.a = 228 }).toThrow(/Cannot assign to read only property/)

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

lines 29–30why, one step back
29 })
30

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

lines 31–32why, one step back
31 it("should freeze arrays after modifications", () => {
32 expect(Object.isFrozen(baseState.object)).toBe(false) // initially not frozen

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 33why, one step back
33 const next = produce(baseState, draft => {

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

lines 34–38why, one step back
34 draft.array.push(3)
35 })36 expect(Object.isFrozen(next.object)).toBe(false) // not touched37 expect(Object.isFrozen(next)).toBe(true)38 expect(Object.isFrozen(next.array)).toBe(true)

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

line 39why, one step back
39

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

lines 40–42why, one step back
40 expect(() => {
41 next.array.shift()42 }).toThrow(/Cannot add\/remove sealed array elements/)

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

line 43why, one step back
43 })

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

lines 44–49why, one step back
44
45 it("can handle already frozen trees", () => {46 const a = []47 const b = {a: a}48 Object.freeze(a)49 Object.freeze(b)

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 50why, one step back
50 const n = produce(b, draft => {

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

lines 51–55why, one step back
51 draft.c = true
52 draft.a.push(3)53 })54 expect(n).toEqual({c: true, a: [3]})55 })

Merge pull request #15 from mweststrate/feature/freezeMichel Weststratebefore adoption0d24494

line 56why, one step back
56 })

Merge pull request #33 from mweststrate/fix/32Michel Weststratebefore adoptionfae0957

line 57why, one step back
57}