Skip to content

Commit

Permalink
Added istanbul rules to get 100% test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Jan 9, 2020
1 parent 617772f commit f4a4701
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const ownKeys: (target: AnyObject) => PropertyKey[] =
Object.getOwnPropertyNames(obj).concat(
Object.getOwnPropertySymbols(obj) as any
)
: Object.getOwnPropertyNames
: /* istanbul ignore next */ Object.getOwnPropertyNames

export function each<T extends Objectish>(
obj: T,
Expand All @@ -108,6 +108,7 @@ export function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {
}

export function getArchtype(thing: any): Archtype {
/* istanbul ignore next */
if (!thing) die()
if (thing[DRAFT_STATE]) {
switch ((thing as Drafted)[DRAFT_STATE].type) {
Expand Down Expand Up @@ -210,7 +211,7 @@ export function shallowCopy(base: any, invokeGetters = false) {
return clone
}

export function freeze(obj: any, deep: boolean = false): void {
export function freeze(obj: any, deep: boolean): void {
if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) return
const type = getArchtype(obj)
if (type === Archtype.Set) {
Expand Down Expand Up @@ -238,6 +239,7 @@ export function createHiddenProperty(
})
}

/* istanbul ignore next */
export function die(): never {
throw new Error("Illegal state, please file a bug")
}
6 changes: 3 additions & 3 deletions src/finalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ function finalize(
finalizeTree(immer, state.draft, scope, path)

// We cannot really delete anything inside of a Set. We can only replace the whole Set.
if (immer.onDelete && !isSet(state.base)) {
if (immer.onDelete && state.type !== ProxyType.Set) {
// The `assigned` object is unreliable with ES5 drafts.
if (immer.useProxies) {
const {assigned} = state
each(assigned, (prop, exists) => {
if (!exists) immer.onDelete?.(state, prop as any)
if (!exists) immer.onDelete!(state, prop as any)
})
} else {
const {base, copy} = state
each(base, prop => {
if (!has(copy, prop)) immer.onDelete?.(state, prop as any)
if (!has(copy, prop)) immer.onDelete!(state, prop as any)
})
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/immer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
isMap,
isDraftable,
DRAFT_STATE,
NOTHING
NOTHING,
die
} from "./common"
import {ImmerScope} from "./scope"
import {
Expand All @@ -26,6 +27,7 @@ import {proxyMap} from "./map"
import {proxySet} from "./set"
import {processResult, maybeFreeze} from "./finalize"

/* istanbul ignore next */
function verifyMinified() {}

const configDefaults = {
Expand All @@ -36,7 +38,8 @@ const configDefaults = {
autoFreeze:
typeof process !== "undefined"
? process.env.NODE_ENV !== "production"
: verifyMinified.name === "verifyMinified",
: /* istanbul ignore next */
verifyMinified.name === "verifyMinified",
onAssign: null,
onDelete: null,
onCopy: null
Expand Down Expand Up @@ -163,8 +166,8 @@ export class Immer implements ProducersFns {
this.produceWithPatches(state, (draft: any) => arg1(draft, ...args))
}
// non-curried form
if (arg3)
throw new Error("A patch listener cannot be passed to produceWithPatches")
/* istanbul ignore next */
if (arg3) die()
let patches: Patch[], inversePatches: Patch[]
const nextState = this.produce(arg1, arg2, (p: Patch[], ip: Patch[]) => {
patches = p
Expand Down
3 changes: 3 additions & 0 deletions src/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface MapState extends ImmerBaseState {
}

// Make sure DraftMap declarion doesn't die if Map is not avialable...
/* istanbul ignore next */
const MapBase: MapConstructor =
typeof Map !== "undefined" ? Map : (function FakeMap() {} as any)

Expand Down Expand Up @@ -117,6 +118,7 @@ export class DraftMap<K, V> extends MapBase implements Map<K, V> {
[iteratorSymbol]: () => this.values(),
next: () => {
const r = iterator.next()
/* istanbul ignore next */
if (r.done) return r
const value = this.get(r.value)
return {
Expand All @@ -133,6 +135,7 @@ export class DraftMap<K, V> extends MapBase implements Map<K, V> {
[iteratorSymbol]: () => this.entries(),
next: () => {
const r = iterator.next()
/* istanbul ignore next */
if (r.done) return r
const value = this.get(r.value)
return {
Expand Down
6 changes: 4 additions & 2 deletions src/patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ function generateArrayPatches(
inversePatches: Patch[]
) {
let {base, assigned, copy} = state
/* istanbul ignore next */
if (!copy) die()

// Reduce complexity by ensuring `base` is never longer.
if (copy!.length < base.length) {
if (copy.length < base.length) {
// @ts-ignore
;[base, copy] = [copy, base]
;[patches, inversePatches] = [inversePatches, patches]
Expand Down Expand Up @@ -173,6 +174,7 @@ export function applyPatches<T>(draft: T, patches: Patch[]): T {
patches.forEach(patch => {
const {path, op} = patch

/* istanbul ignore next */
if (!path.length) die()

let base: any = draft
Expand All @@ -190,6 +192,7 @@ export function applyPatches<T>(draft: T, patches: Patch[]): T {
switch (type) {
case Archtype.Map:
return base.set(key, value)
/* istanbul ignore next */
case Archtype.Set:
throw new Error('Sets cannot have "replace" patches.')
default:
Expand All @@ -199,7 +202,6 @@ export function applyPatches<T>(draft: T, patches: Patch[]): T {
// @ts-ignore
return (base[key] = value)
}
break
case "add":
switch (type) {
case Archtype.Array:
Expand Down
3 changes: 2 additions & 1 deletion src/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface SetState extends ImmerBaseState {
}

// Make sure DraftSet declarion doesn't die if Map is not avialable...
/* istanbul ignore next */
const SetBase: SetConstructor =
typeof Set !== "undefined" ? Set : (function FakeSet() {} as any)

Expand Down Expand Up @@ -79,7 +80,7 @@ export class DraftSet<K, V> extends SetBase implements Set<V> {
state.copy!.delete(value) ||
(state.drafts.has(value)
? state.copy!.delete(state.drafts.get(value))
: false)
: /* istanbul ignore next */ false)
)
}

Expand Down

0 comments on commit f4a4701

Please sign in to comment.