Return undefined for unwrapped values / update tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by RichieAHB · committed by RichieAHB · parent 590025f · Browse files · 3 files·+37 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+37 added−7 removed
- tests:+32 added−5 removed
- other:+5 added−2 removed
Provenance
- Commit
f0934ab- Tree
a3c955a- Author
RichieAHB <richard.beddington@guardian.co.uk>- Committer
RichieAHB <richard.beddington@guardian.co.uk>- Parent
590025f
3 files changed+37 added−7 removed
modified:__tests__/original.js+32 added-5 removed
12234567891011121314151617181920212223242526272811293031323334351819203637383940414243444546474849"use strict"import produce, {original} from "../src/immer"import produce, {original, setUseProxies} from "../src/immer"
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", () => { const nextState = produce(baseState, draftState => { 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 same object from an object that is not proxied", () => { const a = [] expect(original(a)).toBe(a) it("should return undefined for new values on the draft", () => { produce(baseState, draftState => { draftState.c = {} draftState.d = 3 expect(original(draftState.c)).toBeUndefined() expect(original(draftState.d)).toBeUndefined() }) })
it("should return undefined for an object that is not proxied", () => { expect(original({})).toBeUndefined() expect(original(3)).toBeUndefined() })})modified:src/common.js+4 added-1 removed
55 unmodified lines56 return value57 }58 59 export function original(value) {60- return value && value[PROXY_STATE] ? value[PROXY_STATE].base : value60+ if (value && value[PROXY_STATE]) {61+ return value[PROXY_STATE].base62+ }63+ // otherwise return undefined64 }65 66 const assign =67 Object.assign ||108 unmodified linesmodified:readme.md+1 added-1 removed
351 unmodified lines352 ```353 354 ## Extracting the original object from a proxied instance355 356-Immer exposes a named export `original` that will get the original object from the proxied instance inside `produce`. A good example of when this can be useful is when searching for nodes in a tree-like state using strict equality.356+Immer exposes a named export `original` that will get the original object from the proxied instance inside `produce` (or return `undefined` for unproxied values). A good example of when this can be useful is when searching for nodes in a tree-like state using strict equality.357 358 ```js359 const baseState = { users: [{ name: "Richie" }] };360 const nextState = produce(baseState, draftState => {247 unmodified lines