Skip to content

Commit

Permalink
fix(shallowReactive): align behavior with vue-next (#696)
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 committed May 19, 2021
1 parent 73edb33 commit 3485ecb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/reactivity/reactive.ts
Expand Up @@ -141,9 +141,11 @@ function mockObserver(value: any = {}): any {

export function shallowReactive<T extends object = any>(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 (
Expand Down
30 changes: 30 additions & 0 deletions test/v3/reactivity/reactive.spec.ts
Expand Up @@ -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()
})
})

0 comments on commit 3485ecb

Please sign in to comment.