Skip to content
Myr

current.js

This file

Path

__tests__/current.js

212 lines · 4.3 KiB · JavaScript

Last changed by

chore: Merge branch 'master' of github.com:immerjs/immerMichel Weststratebefore adoption6c62eec

History of this file

Take it

Immer 7Michel Weststratebefore adoption467ea5d

lines 1–16why, one step back
1import {
2 setUseProxies,3 setAutoFreeze,4 enableAllPlugins,5 current,6 immerable,7 isDraft,8 produce,9 original10} from "../src/immer"1112enableAllPlugins()1314runTests("proxy", true)15runTests("es5", false)16

chore: Merge branch 'master' of github.com:immerjs/immerMichel Weststratebefore adoption6c62eec

lines 17–18why, one step back
17const isProd = process.env.NODE_ENV === "production"
18

Immer 7Michel Weststratebefore adoption467ea5d

lines 19–28why, one step back
19function runTests(name, useProxies) {
20 describe("current - " + name, () => {21 beforeAll(() => {22 setAutoFreeze(true)23 setUseProxies(useProxies)24 })2526 it("must be called on draft", () => {27 expect(() => {28 current({})

chore: Merge branch 'master' of github.com:immerjs/immerMichel Weststratebefore adoption6c62eec

lines 29–33why, one step back
29 }).toThrowError(
30 isProd31 ? "[Immer] minified error nr: 22 '[object Object]'. Find the full error at: https://bit.ly/3cXEKWf"32 : "[Immer] 'current' expects a draft, got: [object Object]"33 )

Immer 7Michel Weststratebefore adoption467ea5d

lines 34–212why, one step back
34 })
3536 it("can handle simple arrays", () => {37 const base = [{x: 1}]38 let c39 const next = produce(base, draft => {40 expect(current(draft)).toEqual(base)41 draft[0].x++42 c = current(draft)43 expect(c).toEqual([{x: 2}])44 expect(Array.isArray(c))45 draft[0].x++46 })47 expect(next).toEqual([{x: 3}])48 expect(c).toEqual([{x: 2}])49 expect(isDraft(c)).toBe(false)50 })5152 it("won't freeze", () => {53 const base = {x: 1}54 const next = produce(base, draft => {55 draft.x++56 expect(Object.isFrozen(current(draft))).toBe(false)57 })58 })5960 it("returns original without changes", () => {61 const base = {}62 produce(base, draft => {63 expect(original(draft)).toBe(base)64 expect(current(draft)).toBe(base)65 })66 })6768 it("can handle property additions", () => {69 const base = {}70 produce(base, draft => {71 draft.x = true72 const c = current(draft)73 expect(c).not.toBe(base)74 expect(c).not.toBe(draft)75 expect(c).toEqual({76 x: true77 })78 })79 })8081 it("can handle property deletions", () => {82 const base = {83 x: 184 }85 produce(base, draft => {86 delete draft.x87 const c = current(draft)88 expect(c).not.toBe(base)89 expect(c).not.toBe(draft)90 expect(c).toEqual({})91 })92 })9394 it("won't reflect changes over time", () => {95 const base = {96 x: 197 }98 produce(base, draft => {99 draft.x++100 const c = current(draft)101 expect(c).toEqual({102 x: 2103 })104 draft.x++105 expect(c).toEqual({106 x: 2107 })108 })109 })110111 it("will find drafts inside objects", () => {112 const base = {113 x: 1,114 y: {115 z: 2116 },117 z: {}118 }119 produce(base, draft => {120 draft.y.z++121 draft.y = {122 nested: draft.y123 }124 const c = current(draft)125 expect(c).toEqual({126 x: 1,127 y: {128 nested: {129 z: 3130 }131 },132 z: {}133 })134 expect(isDraft(c.y.nested)).toBe(false)135 expect(c.z).toBe(base.z)136 expect(c.y.nested).not.toBe(draft.y.nested)137 })138 })139140 it("handles map - 1", () => {141 const base = new Map([["a", {x: 1}]])142 produce(base, draft => {143 expect(current(draft)).toBe(base)144 draft.delete("a")145 let c = current(draft)146 expect(current(draft)).not.toBe(base)147 expect(current(draft)).not.toBe(draft)148 expect(c).toEqual(new Map())149 const obj = {}150 draft.set("b", obj)151 expect(c).toEqual(new Map())152 expect(current(draft)).toEqual(new Map([["b", obj]]))153 expect(c).toBeInstanceOf(Map)154 })155 })156157 it("handles map - 2", () => {158 const base = new Map([["a", {x: 1}]])159 produce(base, draft => {160 draft.get("a").x++161 const c = current(draft)162 expect(c).not.toBe(base)163 expect(c).toEqual(new Map([["a", {x: 2}]]))164 draft.get("a").x++165 expect(c).toEqual(new Map([["a", {x: 2}]]))166 })167 })168169 it("handles set", () => {170 const base = new Set([1])171 produce(base, draft => {172 expect(current(draft)).toBe(base)173 draft.add(2)174 const c = current(draft)175 expect(c).toEqual(new Set([1, 2]))176 expect(c).not.toBe(draft)177 expect(c).not.toBe(base)178 draft.add(3)179 expect(c).toEqual(new Set([1, 2]))180 expect(c).toBeInstanceOf(Set)181 })182 })183184 it("handles simple class", () => {185 class Counter {186 [immerable] = true187 current = 0188189 inc() {190 this.current++191 }192 }193194 const counter1 = new Counter()195 produce(counter1, draft => {196 expect(current(draft)).toBe(counter1)197 draft.inc()198 const c = current(draft)199 expect(c).not.toBe(draft)200 expect(c.current).toBe(1)201 c.inc()202 expect(c.current).toBe(2)203 expect(draft.current).toBe(1)204 draft.inc()205 draft.inc()206 expect(c.current).toBe(2)207 expect(draft.current).toBe(3)208 expect(c).toBeInstanceOf(Counter)209 })210 })211 })212}