current.js
This file
Path
__tests__/current.js
206 lines · 4.1 KiB · JavaScript
Last changed by
Immer 7Michel Weststratebefore adoption467ea5d
History of this fileTake it
Immer 7Michel Weststratebefore adoption467ea5d
lines 1–206why, one step back1import {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)1617function runTests(name, useProxies) {18 describe("current - " + name, () => {19 beforeAll(() => {20 setAutoFreeze(true)21 setUseProxies(useProxies)22 })2324 it("must be called on draft", () => {25 expect(() => {26 current({})27 }).toThrowError("[Immer] 'current' expects a draft, got: [object Object]")28 })2930 it("can handle simple arrays", () => {31 const base = [{x: 1}]32 let c33 const next = produce(base, draft => {34 expect(current(draft)).toEqual(base)35 draft[0].x++36 c = current(draft)37 expect(c).toEqual([{x: 2}])38 expect(Array.isArray(c))39 draft[0].x++40 })41 expect(next).toEqual([{x: 3}])42 expect(c).toEqual([{x: 2}])43 expect(isDraft(c)).toBe(false)44 })4546 it("won't freeze", () => {47 const base = {x: 1}48 const next = produce(base, draft => {49 draft.x++50 expect(Object.isFrozen(current(draft))).toBe(false)51 })52 })5354 it("returns original without changes", () => {55 const base = {}56 produce(base, draft => {57 expect(original(draft)).toBe(base)58 expect(current(draft)).toBe(base)59 })60 })6162 it("can handle property additions", () => {63 const base = {}64 produce(base, draft => {65 draft.x = true66 const c = current(draft)67 expect(c).not.toBe(base)68 expect(c).not.toBe(draft)69 expect(c).toEqual({70 x: true71 })72 })73 })7475 it("can handle property deletions", () => {76 const base = {77 x: 178 }79 produce(base, draft => {80 delete draft.x81 const c = current(draft)82 expect(c).not.toBe(base)83 expect(c).not.toBe(draft)84 expect(c).toEqual({})85 })86 })8788 it("won't reflect changes over time", () => {89 const base = {90 x: 191 }92 produce(base, draft => {93 draft.x++94 const c = current(draft)95 expect(c).toEqual({96 x: 297 })98 draft.x++99 expect(c).toEqual({100 x: 2101 })102 })103 })104105 it("will find drafts inside objects", () => {106 const base = {107 x: 1,108 y: {109 z: 2110 },111 z: {}112 }113 produce(base, draft => {114 draft.y.z++115 draft.y = {116 nested: draft.y117 }118 const c = current(draft)119 expect(c).toEqual({120 x: 1,121 y: {122 nested: {123 z: 3124 }125 },126 z: {}127 })128 expect(isDraft(c.y.nested)).toBe(false)129 expect(c.z).toBe(base.z)130 expect(c.y.nested).not.toBe(draft.y.nested)131 })132 })133134 it("handles map - 1", () => {135 const base = new Map([["a", {x: 1}]])136 produce(base, draft => {137 expect(current(draft)).toBe(base)138 draft.delete("a")139 let c = current(draft)140 expect(current(draft)).not.toBe(base)141 expect(current(draft)).not.toBe(draft)142 expect(c).toEqual(new Map())143 const obj = {}144 draft.set("b", obj)145 expect(c).toEqual(new Map())146 expect(current(draft)).toEqual(new Map([["b", obj]]))147 expect(c).toBeInstanceOf(Map)148 })149 })150151 it("handles map - 2", () => {152 const base = new Map([["a", {x: 1}]])153 produce(base, draft => {154 draft.get("a").x++155 const c = current(draft)156 expect(c).not.toBe(base)157 expect(c).toEqual(new Map([["a", {x: 2}]]))158 draft.get("a").x++159 expect(c).toEqual(new Map([["a", {x: 2}]]))160 })161 })162163 it("handles set", () => {164 const base = new Set([1])165 produce(base, draft => {166 expect(current(draft)).toBe(base)167 draft.add(2)168 const c = current(draft)169 expect(c).toEqual(new Set([1, 2]))170 expect(c).not.toBe(draft)171 expect(c).not.toBe(base)172 draft.add(3)173 expect(c).toEqual(new Set([1, 2]))174 expect(c).toBeInstanceOf(Set)175 })176 })177178 it("handles simple class", () => {179 class Counter {180 [immerable] = true181 current = 0182183 inc() {184 this.current++185 }186 }187188 const counter1 = new Counter()189 produce(counter1, draft => {190 expect(current(draft)).toBe(counter1)191 draft.inc()192 const c = current(draft)193 expect(c).not.toBe(draft)194 expect(c.current).toBe(1)195 c.inc()196 expect(c.current).toBe(2)197 expect(draft.current).toBe(1)198 draft.inc()199 draft.inc()200 expect(c.current).toBe(2)201 expect(draft.current).toBe(3)202 expect(c).toBeInstanceOf(Counter)203 })204 })205 })206}