From 3485ecb3d52ed9300f392b07502f4dd008c94bfc Mon Sep 17 00:00:00 2001 From: ygj6 Date: Wed, 19 May 2021 17:26:06 +0800 Subject: [PATCH] fix(shallowReactive): align behavior with vue-next (#696) --- src/reactivity/reactive.ts | 8 +++++--- test/v3/reactivity/reactive.spec.ts | 30 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/reactivity/reactive.ts b/src/reactivity/reactive.ts index fec1cead..37779587 100644 --- a/src/reactivity/reactive.ts +++ b/src/reactivity/reactive.ts @@ -141,9 +141,11 @@ function mockObserver(value: any = {}): any { export function shallowReactive(obj: T): T export function shallowReactive(obj: any): any { - if (__DEV__ && !obj) { - warn('"shallowReactive()" is called without provide an "object".') - return + if (!isObject(obj)) { + if (__DEV__) { + warn('"shallowReactive()" 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 55768369..75f9e824 100644 --- a/test/v3/reactivity/reactive.spec.ts +++ b/test/v3/reactivity/reactive.spec.ts @@ -207,4 +207,34 @@ describe('reactivity/reactive', () => { expect(isReactive(props.n)).toBe(true) }) }) + + test('should shallowReactive non-observable values', () => { + const assertValue = (value: any) => { + expect(shallowReactive(value)).toBe(value) + } + + // number + assertValue(1) + // string + assertValue('foo') + // boolean + assertValue(false) + // null + assertValue(null) + // undefined + assertValue(undefined) + // symbol + const s = Symbol() + assertValue(s) + + expect(warn).toBeCalledTimes(6) + expect( + warn.mock.calls.map((call) => { + expect(call[0]).toBe( + '[Vue warn]: "shallowReactive()" is called without provide an "object".' + ) + }) + ) + warn.mockReset() + }) })