Skip to content

Commit

Permalink
Merge 93dcfc9 into 09d69ae
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Oct 21, 2018
2 parents 09d69ae + 93dcfc9 commit c406dbf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
36 changes: 35 additions & 1 deletion __tests__/base.js
@@ -1,5 +1,5 @@
"use strict"
import produce, {setAutoFreeze, setUseProxies, nothing} from "../src/immer"
import produce, {setAutoFreeze, setUseProxies, nothing, isDraft} from "../src/immer"
import deepFreeze from "deep-freeze"
import cloneDeep from "lodash.clonedeep"
import * as lodash from "lodash"
Expand Down Expand Up @@ -1311,6 +1311,40 @@ function runBaseTest(name, useProxies, freeze, useListener) {
return freeze ? deepFreeze(data) : data
}
})

describe(`isDraft - ${name}`, () => {
beforeAll(() => {
setAutoFreeze(freeze)
setUseProxies(useProxies)
})

it('returns true for object drafts', () => {
produce({}, state => {
expect(isDraft(state)).toBeTruthy()
})
})
it('returns true for array drafts', () => {
produce([], state => {
expect(isDraft(state)).toBeTruthy()
})
})
it('returns true for objects nested in object drafts', () => {
produce({ a: { b: {} } }, state => {
expect(isDraft(state.a)).toBeTruthy()
expect(isDraft(state.a.b)).toBeTruthy()
})
})
it('returns false for new objects added to a draft', () => {
produce({}, state => {
state.a = {}
expect(isDraft(state.a)).toBeFalsy()
})
})
it('returns false for a returned draft', () => {
const result = produce({}, state => state)
expect(isDraft(result)).toBeFalsy()
})
})
}

function enumerableOnly(x) {
Expand Down
2 changes: 2 additions & 0 deletions src/immer.d.ts
Expand Up @@ -132,3 +132,5 @@ export function setUseProxies(useProxies: boolean): void
export function applyPatches<S>(state: S, patches: Patch[]): S

export function original<T>(value: T): T | void

export function isDraft(value: any): boolean
2 changes: 1 addition & 1 deletion src/immer.js
@@ -1,4 +1,4 @@
export {setAutoFreeze, setUseProxies, original} from "./common"
export {setAutoFreeze, setUseProxies, original, isProxy as isDraft} from "./common"

import {applyPatches as applyPatchesImpl} from "./patches"
import {isProxyable, getUseProxies, NOTHING} from "./common"
Expand Down
4 changes: 3 additions & 1 deletion src/immer.js.flow
Expand Up @@ -87,4 +87,6 @@ declare export function setUseProxies(useProxies: boolean): void

declare export function applyPatches<S>(state: S, patches: Patch[]): S

declare export function original<S>(value: S): ?S
declare export function original<S>(value: S): ?S

declare export function isDraft(value: any): boolean

0 comments on commit c406dbf

Please sign in to comment.