Skip to content

Commit

Permalink
test: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 31, 2023
1 parent cf05a02 commit f02b60d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
1 change: 0 additions & 1 deletion packages/reactivity/__tests__/ref.spec.ts
Expand Up @@ -196,7 +196,6 @@ describe('reactivity/ref', () => {
test('unref', () => {
expect(unref(1)).toBe(1)
expect(unref(ref(1))).toBe(1)
expect(unref(() => 1)).toBe(1)
})

test('shallowRef', () => {
Expand Down
27 changes: 16 additions & 11 deletions packages/reactivity/src/ref.ts
Expand Up @@ -11,7 +11,8 @@ import {
hasChanged,
IfAny,
isFunction,
isPlainObject
isString,
isObject
} from '@vue/shared'
import {
isProxy,
Expand Down Expand Up @@ -219,7 +220,7 @@ export function toRefs<T extends object>(object: T): ToRefs<T> {
}
const ret: any = isArray(object) ? new Array(object.length) : {}
for (const key in object) {
ret[key] = toRef(object, key)
ret[key] = propertyToRef(object, key)
}
return ret
}
Expand Down Expand Up @@ -285,20 +286,24 @@ export function toRef(
return source
} else if (isFunction(source)) {
return new GetterRefImpl(source as () => unknown) as any
} else if (isPlainObject(source) && key) {
const val = (source as Record<string, any>)[key]
return isRef(val)
? val
: (new ObjectRefImpl(
source as Record<string, any>,
key,
defaultValue
) as any)
} else if (isObject(source) && isString(key)) {
return propertyToRef(source, key, defaultValue)
} else {
return ref(source)
}
}

function propertyToRef(source: object, key: string, defaultValue?: unknown) {
const val = (source as any)[key]
return isRef(val)
? val
: (new ObjectRefImpl(
source as Record<string, any>,
key,
defaultValue
) as any)
}

// corner case when use narrows type
// Ex. type RelativePath = string & { __brand: unknown }
// RelativePath extends object -> true
Expand Down

0 comments on commit f02b60d

Please sign in to comment.