test: 100% coverage for observer

This commit is contained in:
Evan You
2018-09-21 09:52:46 -04:00
parent bf38fea313
commit bb0e15de4d
6 changed files with 65 additions and 38 deletions

View File

@@ -11,18 +11,21 @@ const builtInSymbols = new Set(
.filter(value => typeof value === 'symbol')
)
function get(
target: any,
key: string | symbol,
receiver: any,
toObservable: (t: any) => any
) {
const res = Reflect.get(target, key, receiver)
if (typeof key === 'symbol' && builtInSymbols.has(key)) {
return res
function makeGetter(isImmutable: boolean) {
return function get(target: any, key: string | symbol, receiver: any) {
const res = Reflect.get(target, key, receiver)
if (typeof key === 'symbol' && builtInSymbols.has(key)) {
return res
}
track(target, OperationTypes.GET, key)
return res !== null && typeof res === 'object'
? isImmutable
? // need to lazy access immutable and observable here to avoid
// circular dependency
immutable(res)
: observable(res)
: res
}
track(target, OperationTypes.GET, key)
return res !== null && typeof res === 'object' ? toObservable(res) : res
}
function set(
@@ -37,6 +40,7 @@ function set(
const result = Reflect.set(target, key, value, receiver)
// don't trigger if target is something up in the prototype chain of original
if (target === unwrap(receiver)) {
/* istanbul ignore else */
if (__DEV__) {
const extraInfo = { oldValue, newValue: value }
if (!hadKey) {
@@ -60,6 +64,7 @@ function deleteProperty(target: any, key: string | symbol): boolean {
const oldValue = target[key]
const result = Reflect.deleteProperty(target, key)
if (hadKey) {
/* istanbul ignore else */
if (__DEV__) {
trigger(target, OperationTypes.DELETE, key, { oldValue })
} else {
@@ -81,8 +86,7 @@ function ownKeys(target: any): (string | number | symbol)[] {
}
export const mutableHandlers: ProxyHandler<any> = {
get: (target: any, key: string | symbol, receiver: any) =>
get(target, key, receiver, observable),
get: makeGetter(false),
set,
deleteProperty,
has,
@@ -90,8 +94,7 @@ export const mutableHandlers: ProxyHandler<any> = {
}
export const immutableHandlers: ProxyHandler<any> = {
get: (target: any, key: string | symbol, receiver: any) =>
get(target, key, receiver, LOCKED ? immutable : observable),
get: makeGetter(true),
set(target: any, key: string | symbol, value: any, receiver: any): boolean {
if (LOCKED) {

View File

@@ -34,6 +34,7 @@ function add(value: any) {
const hadKey = proto.has.call(target, value)
const result = proto.add.call(target, value)
if (!hadKey) {
/* istanbul ignore else */
if (__DEV__) {
trigger(target, OperationTypes.ADD, value, { value })
} else {
@@ -51,6 +52,7 @@ function set(key: any, value: any) {
const oldValue = proto.get.call(target, key)
const result = proto.set.call(target, key, value)
if (value !== oldValue) {
/* istanbul ignore else */
if (__DEV__) {
const extraInfo = { oldValue, newValue: value }
if (!hadKey) {
@@ -77,6 +79,7 @@ function deleteEntry(key: any) {
// forward the operation before queueing reactions
const result = proto.delete.call(target, key)
if (hadKey) {
/* istanbul ignore else */
if (__DEV__) {
trigger(target, OperationTypes.DELETE, key, { oldValue })
} else {
@@ -94,6 +97,7 @@ function clear() {
// forward the operation before queueing reactions
const result = proto.clear.call(target)
if (hadItems) {
/* istanbul ignore else */
if (__DEV__) {
trigger(target, OperationTypes.CLEAR, void 0, { oldTarget })
} else {
@@ -158,22 +162,21 @@ const immutableInstrumentations: any = {
}
})
function getInstrumented(
target: any,
key: string | symbol,
receiver: any,
instrumentations: any
) {
target = instrumentations.hasOwnProperty(key) ? instrumentations : target
return Reflect.get(target, key, receiver)
function makeInstrumentationGetter(instrumentations: any) {
return function getInstrumented(
target: any,
key: string | symbol,
receiver: any
) {
target = instrumentations.hasOwnProperty(key) ? instrumentations : target
return Reflect.get(target, key, receiver)
}
}
export const mutableCollectionHandlers: ProxyHandler<any> = {
get: (target: any, key: string | symbol, receiver: any) =>
getInstrumented(target, key, receiver, mutableInstrumentations)
get: makeInstrumentationGetter(mutableInstrumentations)
}
export const immutableCollectionHandlers: ProxyHandler<any> = {
get: (target: any, key: string | symbol, receiver: any) =>
getInstrumented(target, key, receiver, immutableInstrumentations)
get: makeInstrumentationGetter(immutableInstrumentations)
}