original.js
This file
Path
__tests__/original.js
59 lines · 1.7 KiB · JavaScript
Last changed by
Immer 7Michel Weststratebefore adoption467ea5d
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960"use strict"import produce, {original, setUseProxies, enableAllPlugins} from "../src/immer"
enableAllPlugins()
describe("original", () => { const baseState = { a: [], b: {} }
it("should return the original from the draft", () => { setUseProxies(true)
produce(baseState, draftState => { expect(original(draftState)).toBe(baseState) expect(original(draftState.a)).toBe(baseState.a) expect(original(draftState.b)).toBe(baseState.b) })
setUseProxies(false)
produce(baseState, draftState => { expect(original(draftState)).toBe(baseState) expect(original(draftState.a)).toBe(baseState.a) expect(original(draftState.b)).toBe(baseState.b) }) })
it("should return the original from the proxy", () => { produce(baseState, draftState => { expect(original(draftState)).toBe(baseState) expect(original(draftState.a)).toBe(baseState.a) expect(original(draftState.b)).toBe(baseState.b) }) })
it("should throw undefined for new values on the draft", () => { produce(baseState, draftState => { draftState.c = {} draftState.d = 3 expect(() => original(draftState.c)).toThrowErrorMatchingInlineSnapshot( `"[Immer] 'original' expects a draft, got: [object Object]"` ) expect(() => original(draftState.d)).toThrowErrorMatchingInlineSnapshot( `"[Immer] 'original' expects a draft, got: 3"` ) }) })
it("should return undefined for an object that is not proxied", () => { expect(() => original({})).toThrowErrorMatchingInlineSnapshot( `"[Immer] 'original' expects a draft, got: [object Object]"` ) expect(() => original(3)).toThrowErrorMatchingInlineSnapshot( `"[Immer] 'original' expects a draft, got: 3"` ) })})