Skip to content

Commit 37fcbaa

Browse files
author
ygj6
authored
fix(reactive): align behavior with vue-next (#689)
1 parent badff82 commit 37fcbaa

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/reactivity/reactive.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { AnyObject } from '../types/basic'
22
import { getRegisteredVueOrDefault } from '../runtimeContext'
3-
import { isPlainObject, def, warn, isArray, hasOwn, noopFn } from '../utils'
3+
import {
4+
isPlainObject,
5+
def,
6+
warn,
7+
isArray,
8+
hasOwn,
9+
noopFn,
10+
isObject,
11+
} from '../utils'
412
import { isComponentInstance, defineComponentInstance } from '../utils/helper'
513
import { RefKey } from '../utils/symbols'
614
import { isRef, UnwrapRef } from './ref'
@@ -190,10 +198,11 @@ export function shallowReactive(obj: any): any {
190198
* Make obj reactivity
191199
*/
192200
export function reactive<T extends object>(obj: T): UnwrapRef<T> {
193-
if (__DEV__ && !obj) {
194-
warn('"reactive()" is called without provide an "object".')
195-
// @ts-ignore
196-
return
201+
if (!isObject(obj)) {
202+
if (__DEV__) {
203+
warn('"reactive()" is called without provide an "object".')
204+
}
205+
return obj as any
197206
}
198207

199208
if (

test/v3/reactivity/reactive.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ describe('reactivity/reactive', () => {
142142
test('non-observable values', () => {
143143
const assertValue = (value: any) => {
144144
expect(isReactive(reactive(value))).toBe(false)
145+
expect(reactive(value)).toBe(value)
145146
// expect(warnSpy).toHaveBeenLastCalledWith(`value cannot be made reactive: ${String(value)}`);
146147
}
147148

@@ -167,7 +168,7 @@ describe('reactivity/reactive', () => {
167168
const d = new Date()
168169
expect(reactive(d)).toBe(d)
169170

170-
expect(warn).toBeCalledTimes(3)
171+
expect(warn).toBeCalledTimes(12)
171172
expect(
172173
warn.mock.calls.map((call) => {
173174
expect(call[0]).toBe(

0 commit comments

Comments
 (0)