added tests for `current`
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent b3b43b9 · Browse files · 2 files·+221 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+221 added−0 removed
- tests:+221 added−0 removed
Provenance
- Commit
0321d34- Tree
fba7720- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
b3b43b9
2 files changed+221 added−0 removed
added:__tests__/__snapshots__/current.js.snap+25 added
1+// Jest Snapshot v1, https://goo.gl/fbAQLP2+3+exports[`auto freeze - es5 will freeze maps 1`] = `"[Immer] This object has been frozen and should not be mutated"`;4+5+exports[`auto freeze - es5 will freeze maps 2`] = `"[Immer] This object has been frozen and should not be mutated"`;6+7+exports[`auto freeze - es5 will freeze maps 3`] = `"[Immer] This object has been frozen and should not be mutated"`;8+9+exports[`auto freeze - es5 will freeze sets 1`] = `"[Immer] This object has been frozen and should not be mutated"`;10+11+exports[`auto freeze - es5 will freeze sets 2`] = `"[Immer] This object has been frozen and should not be mutated"`;12+13+exports[`auto freeze - es5 will freeze sets 3`] = `"[Immer] This object has been frozen and should not be mutated"`;14+15+exports[`auto freeze - proxy will freeze maps 1`] = `"[Immer] This object has been frozen and should not be mutated"`;16+17+exports[`auto freeze - proxy will freeze maps 2`] = `"[Immer] This object has been frozen and should not be mutated"`;18+19+exports[`auto freeze - proxy will freeze maps 3`] = `"[Immer] This object has been frozen and should not be mutated"`;20+21+exports[`auto freeze - proxy will freeze sets 1`] = `"[Immer] This object has been frozen and should not be mutated"`;22+23+exports[`auto freeze - proxy will freeze sets 2`] = `"[Immer] This object has been frozen and should not be mutated"`;24+25+exports[`auto freeze - proxy will freeze sets 3`] = `"[Immer] This object has been frozen and should not be mutated"`;added:__tests__/current.js+196 added
1+import produce, {2+ setUseProxies,3+ setAutoFreeze,4+ enableAllPlugins,5+ current,6+ immerable7+} from "../src/immer"8+9+enableAllPlugins()10+11+runTests("proxy", true)12+runTests("es5", false)13+14+function runTests(name, useProxies) {15+ describe("current - " + name, () => {16+ beforeAll(() => {17+ setAutoFreeze(true)18+ setUseProxies(useProxies)19+ })20+21+ it("returns undraftable values", () => {22+ ;[null, undefined, 1, true, "test"].forEach(value => {23+ // TODO: or throw?24+ produce(value, draft => {25+ expect(current(draft)).toBe(value)26+ })27+ })28+ })29+30+ it("can handle simple objects", () => {31+ const base = {x: 1}32+ let c33+ const next = produce(base, draft => {34+ draft.x++35+ c = current(draft)36+ draft.x++37+ })38+ expect(next).toEqual({x: 3})39+ expect(c).toEqual({x: 2})40+ expect(isDraft(c)).toBe(false)41+ })42+43+ it("won't freeze", () => {44+ const base = {x: 1}45+ const next = produce(base, draft => {46+ draft.x++47+ expect(Object.isFrozen(current(draft))).toBe(false)48+ })49+ })50+51+ it("returns original without changes", () => {52+ const base = {}53+ produce(base, draft => {54+ expect(original(draft)).toBe(base)55+ expect(current(draft)).toBe(base)56+ })57+ })58+59+ it("can handle property additions", () => {60+ const base = {}61+ produce(base, draft => {62+ draft.x = true63+ const c = current(draft)64+ expect(c).not.toBe(base)65+ expect(c).not.toBe(draft)66+ expect(c).toEqual({67+ x: true68+ })69+ })70+ })71+72+ it("can handle property deletions", () => {73+ const base = {74+ x: 175+ }76+ produce(base, draft => {77+ delete draft.x78+ const c = current(draft)79+ expect(c).not.toBe(base)80+ expect(c).not.toBe(draft)81+ expect(c).toEqual({})82+ })83+ })84+85+ it("won't reflect changes over time", () => {86+ const base = {87+ x: 188+ }89+ produce(base, draft => {90+ draft.x++91+ const c = current(draft)92+ expect(c).toEqual({93+ x: 294+ })95+ draft.x++96+ expect(c).toEqual({97+ x: 298+ })99+ })100+ })101+102+ it("will find drafts inside objects", () => {103+ const base = {104+ x: 1,105+ y: {106+ z: 2107+ }108+ }109+ produce(base, draft => {110+ draft.y.z++111+ draft.y = {112+ nested: draft.y113+ }114+ const c = current(draft)115+ expect(c).toEqual({116+ x: 1,117+ y: {118+ nested: {119+ z: 3120+ }121+ }122+ })123+ expect(isDraft(c.y.nested)).toBe(false)124+ expect(c.y.nested).not.toBe(draft.y.nested)125+ })126+ })127+128+ it("handles map - 1", () => {129+ const base = new Map([["a", {x: 1}]])130+ produce(base, draft => {131+ expect(current(draft)).toBe(draft)132+ draft.delete("a")133+ let c = current(draft)134+ expect(current(draft)).not.toBe(draft)135+ expect(c).toEqual(new Map())136+ const obj = {}137+ draft.set("b", obj)138+ expect(c).toEqual(new Map())139+ expect(current(draft)).toEqual(new Map([["b", obj]]))140+ expect(current(draft).get("b")).toBe(obj)141+ })142+ })143+144+ it("handles map - 2", () => {145+ const base = new Map([["a", {x: 1}]])146+ produce(base, draft => {147+ draft.get("a").x++148+ const c = current(draft)149+ expect(c).not.toBe(draft)150+ expect(c).toEqual(new Map([["a", {x: 2}]]))151+ draft.get("a").x++152+ expect(c).toEqual(new Map([["a", {x: 2}]]))153+ })154+ })155+156+ it("handles set", () => {157+ const base = new Set([1])158+ produce(base, draft => {159+ expect(current(draft)).toBe(draft)160+ base.add(2)161+ const c = current(draft)162+ expect(c).toEqual(new Set([1, 2]))163+ expect(c).not.toBe(draft)164+ base.add(3)165+ expect(c).toEqual(new Set([1, 2]))166+ })167+ })168+169+ it("handles simple class", () => {170+ class Counter {171+ [immerable] = true172+ current = 0173+174+ inc() {175+ this.current++176+ }177+ }178+179+ const c = new Counter()180+ produce(c, draft => {181+ expect(current(draft)).toBe(draft)182+ draft.inc()183+ const c = current(draft)184+ expect(c).not.toBe(draft)185+ expect(c.current).toBe(1)186+ c.inc()187+ expect(c.current).toBe(2)188+ expect(draft.current).toBe(1)189+ draft.inc()190+ draft.inc()191+ expect(c.current).toBe(2)192+ expect(draft.current).toBe(3)193+ })194+ })195+ })196+}