feat(watch): support directly watching reactive object with deep default

Also warn invalid watch sources

close #1110
This commit is contained in:
Evan You
2020-05-04 09:27:28 -04:00
parent 64ef7c76bf
commit 6b33cc4229
2 changed files with 70 additions and 27 deletions

View File

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