Skip to content

Commit 3485ecb

Browse files
authored
fix(shallowReactive): align behavior with vue-next (#696)
1 parent 73edb33 commit 3485ecb

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/reactivity/reactive.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,11 @@ function mockObserver(value: any = {}): any {
141141

142142
export function shallowReactive<T extends object = any>(obj: T): T
143143
export function shallowReactive(obj: any): any {
144-
if (__DEV__ && !obj) {
145-
warn('"shallowReactive()" is called without provide an "object".')
146-
return
144+
if (!isObject(obj)) {
145+
if (__DEV__) {
146+
warn('"shallowReactive()" is called without provide an "object".')
147+
}
148+
return obj as any
147149
}
148150

149151
if (

test/v3/reactivity/reactive.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,34 @@ describe('reactivity/reactive', () => {
207207
expect(isReactive(props.n)).toBe(true)
208208
})
209209
})
210+
211+
test('should shallowReactive non-observable values', () => {
212+
const assertValue = (value: any) => {
213+
expect(shallowReactive(value)).toBe(value)
214+
}
215+
216+
// number
217+
assertValue(1)
218+
// string
219+
assertValue('foo')
220+
// boolean
221+
assertValue(false)
222+
// null
223+
assertValue(null)
224+
// undefined
225+
assertValue(undefined)
226+
// symbol
227+
const s = Symbol()
228+
assertValue(s)
229+
230+
expect(warn).toBeCalledTimes(6)
231+
expect(
232+
warn.mock.calls.map((call) => {
233+
expect(call[0]).toBe(
234+
'[Vue warn]: "shallowReactive()" is called without provide an "object".'
235+
)
236+
})
237+
)
238+
warn.mockReset()
239+
})
210240
})

0 commit comments

Comments
 (0)