feat(watch): support directly watching reactive object in multiple sources with deep default (#1201)

This commit is contained in:
Yang Mingshan
2020-05-18 23:02:51 +08:00
committed by GitHub
parent 83b7158017
commit ba62ccd55d
2 changed files with 46 additions and 16 deletions

View File

@@ -155,12 +155,30 @@ describe('api: watch', () => {
expect(dummy).toMatchObject([[2, true], [1, false]])
})
it('watching multiple sources: reactive object (with automatic deep: true)', async () => {
const src = reactive({ count: 0 })
let dummy
watch([src], ([state]) => {
dummy = state
// assert types
state.count === 1
})
src.count++
await nextTick()
expect(dummy).toMatchObject({ count: 1 })
})
it('warn invalid watch source', () => {
// @ts-ignore
watch(1, () => {})
expect(`Invalid watch source`).toHaveBeenWarned()
})
it('warn invalid watch source: multiple sources', () => {
watch([1], () => {})
expect(`Invalid watch source`).toHaveBeenWarned()
})
it('stopping the watcher (effect)', async () => {
const state = reactive({ count: 0 })
let dummy