Skip to content

Commit df04942

Browse files
committed
test: fix tests
1 parent 781177e commit df04942

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

packages/reactivity/__tests__/ref.spec.ts

-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ describe('reactivity/ref', () => {
196196
test('unref', () => {
197197
expect(unref(1)).toBe(1)
198198
expect(unref(ref(1))).toBe(1)
199-
expect(unref(() => 1)).toBe(1)
200199
})
201200

202201
test('shallowRef', () => {

packages/reactivity/src/ref.ts

+16-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
hasChanged,
1212
IfAny,
1313
isFunction,
14-
isPlainObject
14+
isString,
15+
isObject
1516
} from '@vue/shared'
1617
import {
1718
isProxy,
@@ -316,7 +317,7 @@ export function toRefs<T extends object>(object: T): ToRefs<T> {
316317
}
317318
const ret: any = isArray(object) ? new Array(object.length) : {}
318319
for (const key in object) {
319-
ret[key] = toRef(object, key)
320+
ret[key] = propertyToRef(object, key)
320321
}
321322
return ret
322323
}
@@ -409,20 +410,24 @@ export function toRef(
409410
return source
410411
} else if (isFunction(source)) {
411412
return new GetterRefImpl(source as () => unknown) as any
412-
} else if (isPlainObject(source) && key) {
413-
const val = (source as Record<string, any>)[key]
414-
return isRef(val)
415-
? val
416-
: (new ObjectRefImpl(
417-
source as Record<string, any>,
418-
key,
419-
defaultValue
420-
) as any)
413+
} else if (isObject(source) && isString(key)) {
414+
return propertyToRef(source, key, defaultValue)
421415
} else {
422416
return ref(source)
423417
}
424418
}
425419

420+
function propertyToRef(source: object, key: string, defaultValue?: unknown) {
421+
const val = (source as any)[key]
422+
return isRef(val)
423+
? val
424+
: (new ObjectRefImpl(
425+
source as Record<string, any>,
426+
key,
427+
defaultValue
428+
) as any)
429+
}
430+
426431
// corner case when use narrows type
427432
// Ex. type RelativePath = string & { __brand: unknown }
428433
// RelativePath extends object -> true

0 commit comments

Comments
 (0)