From 37fcbaaaa44d6548d3d08fff364e6b38a47e5c1b Mon Sep 17 00:00:00 2001 From: ygj6 <82787816@qq.com> Date: Thu, 29 Apr 2021 12:08:27 +0800 Subject: [PATCH] fix(reactive): align behavior with vue-next (#689) --- src/reactivity/reactive.ts | 19 ++++++++++++++----- test/v3/reactivity/reactive.spec.ts | 3 ++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/reactivity/reactive.ts b/src/reactivity/reactive.ts index 6cdaa824..fec1cead 100644 --- a/src/reactivity/reactive.ts +++ b/src/reactivity/reactive.ts @@ -1,6 +1,14 @@ import { AnyObject } from '../types/basic' import { getRegisteredVueOrDefault } from '../runtimeContext' -import { isPlainObject, def, warn, isArray, hasOwn, noopFn } from '../utils' +import { + isPlainObject, + def, + warn, + isArray, + hasOwn, + noopFn, + isObject, +} from '../utils' import { isComponentInstance, defineComponentInstance } from '../utils/helper' import { RefKey } from '../utils/symbols' import { isRef, UnwrapRef } from './ref' @@ -190,10 +198,11 @@ export function shallowReactive(obj: any): any { * Make obj reactivity */ export function reactive(obj: T): UnwrapRef { - if (__DEV__ && !obj) { - warn('"reactive()" is called without provide an "object".') - // @ts-ignore - return + if (!isObject(obj)) { + if (__DEV__) { + warn('"reactive()" is called without provide an "object".') + } + return obj as any } if ( diff --git a/test/v3/reactivity/reactive.spec.ts b/test/v3/reactivity/reactive.spec.ts index 04034ba6..55768369 100644 --- a/test/v3/reactivity/reactive.spec.ts +++ b/test/v3/reactivity/reactive.spec.ts @@ -142,6 +142,7 @@ describe('reactivity/reactive', () => { test('non-observable values', () => { const assertValue = (value: any) => { expect(isReactive(reactive(value))).toBe(false) + expect(reactive(value)).toBe(value) // expect(warnSpy).toHaveBeenLastCalledWith(`value cannot be made reactive: ${String(value)}`); } @@ -167,7 +168,7 @@ describe('reactivity/reactive', () => { const d = new Date() expect(reactive(d)).toBe(d) - expect(warn).toBeCalledTimes(3) + expect(warn).toBeCalledTimes(12) expect( warn.mock.calls.map((call) => { expect(call[0]).toBe(