Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(shallowReactive): align behavior with vue-next #696

Merged
merged 1 commit into from May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
})
})