2020-02-22 15:19:10 +08:00
|
|
|
import {
|
|
|
|
watch,
|
|
|
|
watchEffect,
|
|
|
|
reactive,
|
|
|
|
computed,
|
|
|
|
nextTick,
|
|
|
|
ref,
|
2020-11-27 22:31:50 +08:00
|
|
|
defineComponent,
|
|
|
|
getCurrentInstance,
|
|
|
|
ComponentInternalInstance,
|
|
|
|
ComponentPublicInstance
|
2020-02-22 15:19:10 +08:00
|
|
|
} from '../src/index'
|
2020-11-27 22:31:50 +08:00
|
|
|
import {
|
|
|
|
render,
|
|
|
|
nodeOps,
|
|
|
|
serializeInner,
|
|
|
|
TestElement,
|
2021-02-09 15:00:32 +08:00
|
|
|
h,
|
|
|
|
createApp
|
2020-11-27 22:31:50 +08:00
|
|
|
} from '@vue/runtime-test'
|
2019-12-04 00:30:24 +08:00
|
|
|
import {
|
|
|
|
ITERATE_KEY,
|
|
|
|
DebuggerEvent,
|
|
|
|
TrackOpTypes,
|
2020-07-31 06:29:38 +08:00
|
|
|
TriggerOpTypes,
|
2020-10-07 06:16:20 +08:00
|
|
|
triggerRef,
|
2020-11-27 22:31:50 +08:00
|
|
|
shallowRef,
|
|
|
|
Ref
|
2019-12-04 00:30:24 +08:00
|
|
|
} from '@vue/reactivity'
|
2019-08-27 10:47:38 +08:00
|
|
|
|
2019-08-23 10:15:39 +08:00
|
|
|
// reference: https://vue-composition-api-rfc.netlify.com/api.html#watch
|
|
|
|
|
2019-08-24 03:32:19 +08:00
|
|
|
describe('api: watch', () => {
|
2020-02-22 15:19:10 +08:00
|
|
|
it('effect', async () => {
|
2019-08-27 10:47:38 +08:00
|
|
|
const state = reactive({ count: 0 })
|
|
|
|
let dummy
|
2020-02-22 15:19:10 +08:00
|
|
|
watchEffect(() => {
|
2019-08-27 10:47:38 +08:00
|
|
|
dummy = state.count
|
|
|
|
})
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
|
|
|
|
state.count++
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('watching single source: getter', async () => {
|
|
|
|
const state = reactive({ count: 0 })
|
|
|
|
let dummy
|
|
|
|
watch(
|
|
|
|
() => state.count,
|
|
|
|
(count, prevCount) => {
|
|
|
|
dummy = [count, prevCount]
|
2019-10-15 00:15:09 +08:00
|
|
|
// assert types
|
|
|
|
count + 1
|
2020-02-14 01:08:21 +08:00
|
|
|
if (prevCount) {
|
|
|
|
prevCount + 1
|
|
|
|
}
|
2019-08-27 10:47:38 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
state.count++
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toMatchObject([1, 0])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('watching single source: ref', async () => {
|
|
|
|
const count = ref(0)
|
|
|
|
let dummy
|
|
|
|
watch(count, (count, prevCount) => {
|
|
|
|
dummy = [count, prevCount]
|
2019-10-15 00:15:09 +08:00
|
|
|
// assert types
|
|
|
|
count + 1
|
2020-02-14 01:08:21 +08:00
|
|
|
if (prevCount) {
|
|
|
|
prevCount + 1
|
|
|
|
}
|
2019-08-27 10:47:38 +08:00
|
|
|
})
|
|
|
|
count.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toMatchObject([1, 0])
|
|
|
|
})
|
|
|
|
|
2020-07-21 00:39:22 +08:00
|
|
|
it('watching single source: array', async () => {
|
|
|
|
const array = reactive([] as number[])
|
|
|
|
const spy = jest.fn()
|
|
|
|
watch(array, spy)
|
|
|
|
array.push(1)
|
|
|
|
await nextTick()
|
|
|
|
expect(spy).toBeCalledTimes(1)
|
|
|
|
expect(spy).toBeCalledWith([1], expect.anything(), expect.anything())
|
|
|
|
})
|
|
|
|
|
2020-08-15 05:35:49 +08:00
|
|
|
it('should not fire if watched getter result did not change', async () => {
|
|
|
|
const spy = jest.fn()
|
|
|
|
const n = ref(0)
|
|
|
|
watch(() => n.value % 2, spy)
|
|
|
|
|
|
|
|
n.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(spy).toBeCalledTimes(1)
|
|
|
|
|
|
|
|
n.value += 2
|
|
|
|
await nextTick()
|
|
|
|
// should not be called again because getter result did not change
|
|
|
|
expect(spy).toBeCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
2019-08-27 10:47:38 +08:00
|
|
|
it('watching single source: computed ref', async () => {
|
|
|
|
const count = ref(0)
|
|
|
|
const plus = computed(() => count.value + 1)
|
|
|
|
let dummy
|
|
|
|
watch(plus, (count, prevCount) => {
|
|
|
|
dummy = [count, prevCount]
|
2019-10-15 00:15:09 +08:00
|
|
|
// assert types
|
|
|
|
count + 1
|
2020-02-14 01:08:21 +08:00
|
|
|
if (prevCount) {
|
|
|
|
prevCount + 1
|
|
|
|
}
|
2019-08-27 10:47:38 +08:00
|
|
|
})
|
|
|
|
count.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toMatchObject([2, 1])
|
|
|
|
})
|
|
|
|
|
2020-04-17 21:55:41 +08:00
|
|
|
it('watching primitive with deep: true', async () => {
|
|
|
|
const count = ref(0)
|
|
|
|
let dummy
|
|
|
|
watch(
|
|
|
|
count,
|
|
|
|
(c, prevCount) => {
|
|
|
|
dummy = [c, prevCount]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
deep: true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
count.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toMatchObject([1, 0])
|
|
|
|
})
|
|
|
|
|
2020-05-04 21:27:28 +08:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2019-08-27 10:47:38 +08:00
|
|
|
it('watching multiple sources', async () => {
|
|
|
|
const state = reactive({ count: 1 })
|
|
|
|
const count = ref(1)
|
|
|
|
const plus = computed(() => count.value + 1)
|
|
|
|
|
|
|
|
let dummy
|
|
|
|
watch([() => state.count, count, plus], (vals, oldVals) => {
|
|
|
|
dummy = [vals, oldVals]
|
2019-10-15 00:15:09 +08:00
|
|
|
// assert types
|
|
|
|
vals.concat(1)
|
|
|
|
oldVals.concat(1)
|
2019-08-27 10:47:38 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
state.count++
|
|
|
|
count.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toMatchObject([[2, 2, 3], [1, 1, 2]])
|
|
|
|
})
|
|
|
|
|
2019-10-16 04:44:14 +08:00
|
|
|
it('watching multiple sources: readonly array', async () => {
|
|
|
|
const state = reactive({ count: 1 })
|
|
|
|
const status = ref(false)
|
|
|
|
|
|
|
|
let dummy
|
|
|
|
watch([() => state.count, status] as const, (vals, oldVals) => {
|
|
|
|
dummy = [vals, oldVals]
|
2020-03-23 23:11:00 +08:00
|
|
|
const [count] = vals
|
|
|
|
const [, oldStatus] = oldVals
|
2019-10-16 04:44:14 +08:00
|
|
|
// assert types
|
|
|
|
count + 1
|
|
|
|
oldStatus === true
|
|
|
|
})
|
|
|
|
|
|
|
|
state.count++
|
2020-01-20 22:43:58 +08:00
|
|
|
status.value = true
|
2019-10-16 04:44:14 +08:00
|
|
|
await nextTick()
|
2020-01-20 22:43:58 +08:00
|
|
|
expect(dummy).toMatchObject([[2, true], [1, false]])
|
2019-10-16 04:44:14 +08:00
|
|
|
})
|
|
|
|
|
2020-05-18 23:02:51 +08:00
|
|
|
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 })
|
|
|
|
})
|
|
|
|
|
2020-05-04 21:27:28 +08:00
|
|
|
it('warn invalid watch source', () => {
|
|
|
|
// @ts-ignore
|
|
|
|
watch(1, () => {})
|
2020-05-18 23:02:51 +08:00
|
|
|
expect(`Invalid watch source`).toHaveBeenWarned()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('warn invalid watch source: multiple sources', () => {
|
|
|
|
watch([1], () => {})
|
2020-05-04 21:27:28 +08:00
|
|
|
expect(`Invalid watch source`).toHaveBeenWarned()
|
|
|
|
})
|
|
|
|
|
2020-02-22 15:19:10 +08:00
|
|
|
it('stopping the watcher (effect)', async () => {
|
2019-08-27 10:47:38 +08:00
|
|
|
const state = reactive({ count: 0 })
|
|
|
|
let dummy
|
2020-02-22 15:19:10 +08:00
|
|
|
const stop = watchEffect(() => {
|
2019-08-27 10:47:38 +08:00
|
|
|
dummy = state.count
|
|
|
|
})
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
|
|
|
|
stop()
|
|
|
|
state.count++
|
|
|
|
await nextTick()
|
|
|
|
// should not update
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
})
|
|
|
|
|
2020-02-22 15:19:10 +08:00
|
|
|
it('stopping the watcher (with source)', async () => {
|
|
|
|
const state = reactive({ count: 0 })
|
|
|
|
let dummy
|
|
|
|
const stop = watch(
|
|
|
|
() => state.count,
|
|
|
|
count => {
|
|
|
|
dummy = count
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
state.count++
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
|
|
|
|
stop()
|
|
|
|
state.count++
|
|
|
|
await nextTick()
|
|
|
|
// should not update
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
})
|
|
|
|
|
2020-02-18 12:14:19 +08:00
|
|
|
it('cleanup registration (effect)', async () => {
|
2019-08-27 10:47:38 +08:00
|
|
|
const state = reactive({ count: 0 })
|
|
|
|
const cleanup = jest.fn()
|
|
|
|
let dummy
|
2020-02-22 15:19:10 +08:00
|
|
|
const stop = watchEffect(onCleanup => {
|
2019-08-27 10:47:38 +08:00
|
|
|
onCleanup(cleanup)
|
|
|
|
dummy = state.count
|
|
|
|
})
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
|
|
|
|
state.count++
|
|
|
|
await nextTick()
|
|
|
|
expect(cleanup).toHaveBeenCalledTimes(1)
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
|
|
|
|
stop()
|
|
|
|
expect(cleanup).toHaveBeenCalledTimes(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('cleanup registration (with source)', async () => {
|
|
|
|
const count = ref(0)
|
|
|
|
const cleanup = jest.fn()
|
|
|
|
let dummy
|
|
|
|
const stop = watch(count, (count, prevCount, onCleanup) => {
|
|
|
|
onCleanup(cleanup)
|
|
|
|
dummy = count
|
|
|
|
})
|
2020-02-18 12:14:19 +08:00
|
|
|
|
|
|
|
count.value++
|
2019-08-27 10:47:38 +08:00
|
|
|
await nextTick()
|
2020-02-18 12:14:19 +08:00
|
|
|
expect(cleanup).toHaveBeenCalledTimes(0)
|
|
|
|
expect(dummy).toBe(1)
|
2019-08-27 10:47:38 +08:00
|
|
|
|
|
|
|
count.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(cleanup).toHaveBeenCalledTimes(1)
|
2020-02-18 12:14:19 +08:00
|
|
|
expect(dummy).toBe(2)
|
2019-08-27 10:47:38 +08:00
|
|
|
|
|
|
|
stop()
|
|
|
|
expect(cleanup).toHaveBeenCalledTimes(2)
|
|
|
|
})
|
|
|
|
|
2020-09-18 11:17:21 +08:00
|
|
|
it('flush timing: pre (default)', async () => {
|
2019-08-27 23:35:22 +08:00
|
|
|
const count = ref(0)
|
2020-09-18 11:17:21 +08:00
|
|
|
const count2 = ref(0)
|
|
|
|
|
2020-02-18 12:14:19 +08:00
|
|
|
let callCount = 0
|
2020-09-18 11:17:21 +08:00
|
|
|
let result1
|
|
|
|
let result2
|
|
|
|
const assertion = jest.fn((count, count2Value) => {
|
2020-02-18 12:14:19 +08:00
|
|
|
callCount++
|
|
|
|
// on mount, the watcher callback should be called before DOM render
|
2020-09-18 11:17:21 +08:00
|
|
|
// on update, should be called before the count is updated
|
|
|
|
const expectedDOM = callCount === 1 ? `` : `${count - 1}`
|
|
|
|
result1 = serializeInner(root) === expectedDOM
|
|
|
|
|
|
|
|
// in a pre-flush callback, all state should have been updated
|
|
|
|
const expectedState = callCount - 1
|
|
|
|
result2 = count === expectedState && count2Value === expectedState
|
2019-08-27 23:35:22 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
const Comp = {
|
|
|
|
setup() {
|
2020-02-22 15:19:10 +08:00
|
|
|
watchEffect(() => {
|
2020-09-18 11:17:21 +08:00
|
|
|
assertion(count.value, count2.value)
|
2019-08-27 23:35:22 +08:00
|
|
|
})
|
|
|
|
return () => count.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
render(h(Comp), root)
|
|
|
|
expect(assertion).toHaveBeenCalledTimes(1)
|
2020-09-18 11:17:21 +08:00
|
|
|
expect(result1).toBe(true)
|
|
|
|
expect(result2).toBe(true)
|
2019-08-27 23:35:22 +08:00
|
|
|
|
|
|
|
count.value++
|
2020-09-18 11:17:21 +08:00
|
|
|
count2.value++
|
2019-08-27 23:35:22 +08:00
|
|
|
await nextTick()
|
2020-09-18 11:17:21 +08:00
|
|
|
// two mutations should result in 1 callback execution
|
2019-08-27 23:35:22 +08:00
|
|
|
expect(assertion).toHaveBeenCalledTimes(2)
|
2020-09-18 11:17:21 +08:00
|
|
|
expect(result1).toBe(true)
|
|
|
|
expect(result2).toBe(true)
|
2019-08-27 23:35:22 +08:00
|
|
|
})
|
|
|
|
|
2020-09-18 11:17:21 +08:00
|
|
|
it('flush timing: post', async () => {
|
2019-08-27 23:35:22 +08:00
|
|
|
const count = ref(0)
|
2020-09-18 11:17:21 +08:00
|
|
|
let result
|
|
|
|
const assertion = jest.fn(count => {
|
|
|
|
result = serializeInner(root) === `${count}`
|
2019-08-27 23:35:22 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
const Comp = {
|
|
|
|
setup() {
|
2020-02-22 15:19:10 +08:00
|
|
|
watchEffect(
|
2019-08-27 23:35:22 +08:00
|
|
|
() => {
|
2020-09-18 11:17:21 +08:00
|
|
|
assertion(count.value)
|
2019-08-27 23:35:22 +08:00
|
|
|
},
|
2020-09-18 11:17:21 +08:00
|
|
|
{ flush: 'post' }
|
2019-08-27 23:35:22 +08:00
|
|
|
)
|
|
|
|
return () => count.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
render(h(Comp), root)
|
|
|
|
expect(assertion).toHaveBeenCalledTimes(1)
|
2020-09-18 11:17:21 +08:00
|
|
|
expect(result).toBe(true)
|
2019-08-27 23:35:22 +08:00
|
|
|
|
|
|
|
count.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(assertion).toHaveBeenCalledTimes(2)
|
2020-09-18 11:17:21 +08:00
|
|
|
expect(result).toBe(true)
|
2019-08-27 23:35:22 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('flush timing: sync', async () => {
|
|
|
|
const count = ref(0)
|
|
|
|
const count2 = ref(0)
|
2019-08-27 10:47:38 +08:00
|
|
|
|
2019-08-27 23:35:22 +08:00
|
|
|
let callCount = 0
|
2020-07-17 23:17:29 +08:00
|
|
|
let result1
|
|
|
|
let result2
|
2019-08-27 23:35:22 +08:00
|
|
|
const assertion = jest.fn(count => {
|
|
|
|
callCount++
|
|
|
|
// on mount, the watcher callback should be called before DOM render
|
|
|
|
// on update, should be called before the count is updated
|
|
|
|
const expectedDOM = callCount === 1 ? `` : `${count - 1}`
|
2020-07-17 23:17:29 +08:00
|
|
|
result1 = serializeInner(root) === expectedDOM
|
2019-08-27 10:47:38 +08:00
|
|
|
|
2019-08-27 23:35:22 +08:00
|
|
|
// in a sync callback, state mutation on the next line should not have
|
|
|
|
// executed yet on the 2nd call, but will be on the 3rd call.
|
|
|
|
const expectedState = callCount < 3 ? 0 : 1
|
2020-07-17 23:17:29 +08:00
|
|
|
result2 = count2.value === expectedState
|
2019-08-27 23:35:22 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
const Comp = {
|
|
|
|
setup() {
|
2020-02-22 15:19:10 +08:00
|
|
|
watchEffect(
|
2019-08-27 23:35:22 +08:00
|
|
|
() => {
|
|
|
|
assertion(count.value)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
flush: 'sync'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return () => count.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
render(h(Comp), root)
|
|
|
|
expect(assertion).toHaveBeenCalledTimes(1)
|
2020-07-17 23:17:29 +08:00
|
|
|
expect(result1).toBe(true)
|
|
|
|
expect(result2).toBe(true)
|
2019-08-27 10:47:38 +08:00
|
|
|
|
2019-08-27 23:35:22 +08:00
|
|
|
count.value++
|
|
|
|
count2.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(assertion).toHaveBeenCalledTimes(3)
|
2020-07-17 23:17:29 +08:00
|
|
|
expect(result1).toBe(true)
|
|
|
|
expect(result2).toBe(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not fire on component unmount w/ flush: post', async () => {
|
|
|
|
const toggle = ref(true)
|
|
|
|
const cb = jest.fn()
|
|
|
|
const Comp = {
|
|
|
|
setup() {
|
2020-09-18 11:17:21 +08:00
|
|
|
watch(toggle, cb, { flush: 'post' })
|
2020-07-17 23:17:29 +08:00
|
|
|
},
|
|
|
|
render() {}
|
|
|
|
}
|
|
|
|
const App = {
|
|
|
|
render() {
|
|
|
|
return toggle.value ? h(Comp) : null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
render(h(App), nodeOps.createElement('div'))
|
|
|
|
expect(cb).not.toHaveBeenCalled()
|
|
|
|
toggle.value = false
|
|
|
|
await nextTick()
|
|
|
|
expect(cb).not.toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should fire on component unmount w/ flush: pre', async () => {
|
|
|
|
const toggle = ref(true)
|
|
|
|
const cb = jest.fn()
|
|
|
|
const Comp = {
|
|
|
|
setup() {
|
|
|
|
watch(toggle, cb, { flush: 'pre' })
|
|
|
|
},
|
|
|
|
render() {}
|
|
|
|
}
|
|
|
|
const App = {
|
|
|
|
render() {
|
|
|
|
return toggle.value ? h(Comp) : null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
render(h(App), nodeOps.createElement('div'))
|
|
|
|
expect(cb).not.toHaveBeenCalled()
|
|
|
|
toggle.value = false
|
|
|
|
await nextTick()
|
|
|
|
expect(cb).toHaveBeenCalledTimes(1)
|
2019-08-27 23:35:22 +08:00
|
|
|
})
|
2019-08-27 10:47:38 +08:00
|
|
|
|
2020-08-04 04:49:13 +08:00
|
|
|
// #1763
|
|
|
|
it('flush: pre watcher watching props should fire before child update', async () => {
|
|
|
|
const a = ref(0)
|
|
|
|
const b = ref(0)
|
2020-08-05 01:20:09 +08:00
|
|
|
const c = ref(0)
|
2020-08-04 04:49:13 +08:00
|
|
|
const calls: string[] = []
|
|
|
|
|
|
|
|
const Comp = {
|
|
|
|
props: ['a', 'b'],
|
|
|
|
setup(props: any) {
|
|
|
|
watch(
|
|
|
|
() => props.a + props.b,
|
|
|
|
() => {
|
2020-08-05 01:20:09 +08:00
|
|
|
calls.push('watcher 1')
|
|
|
|
c.value++
|
|
|
|
},
|
|
|
|
{ flush: 'pre' }
|
|
|
|
)
|
|
|
|
|
|
|
|
// #1777 chained pre-watcher
|
|
|
|
watch(
|
|
|
|
c,
|
|
|
|
() => {
|
|
|
|
calls.push('watcher 2')
|
2020-08-04 04:49:13 +08:00
|
|
|
},
|
|
|
|
{ flush: 'pre' }
|
|
|
|
)
|
|
|
|
return () => {
|
2020-08-05 01:20:09 +08:00
|
|
|
c.value
|
2020-08-04 04:49:13 +08:00
|
|
|
calls.push('render')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const App = {
|
|
|
|
render() {
|
|
|
|
return h(Comp, { a: a.value, b: b.value })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render(h(App), nodeOps.createElement('div'))
|
|
|
|
expect(calls).toEqual(['render'])
|
|
|
|
|
|
|
|
// both props are updated
|
|
|
|
// should trigger pre-flush watcher first and only once
|
|
|
|
// then trigger child render
|
|
|
|
a.value++
|
|
|
|
b.value++
|
|
|
|
await nextTick()
|
2020-08-05 01:20:09 +08:00
|
|
|
expect(calls).toEqual(['render', 'watcher 1', 'watcher 2', 'render'])
|
2020-08-04 04:49:13 +08:00
|
|
|
})
|
|
|
|
|
2020-08-14 21:50:23 +08:00
|
|
|
// #1852
|
|
|
|
it('flush: post watcher should fire after template refs updated', async () => {
|
|
|
|
const toggle = ref(false)
|
|
|
|
let dom: TestElement | null = null
|
|
|
|
|
|
|
|
const App = {
|
|
|
|
setup() {
|
|
|
|
const domRef = ref<TestElement | null>(null)
|
|
|
|
|
|
|
|
watch(
|
|
|
|
toggle,
|
|
|
|
() => {
|
|
|
|
dom = domRef.value
|
|
|
|
},
|
|
|
|
{ flush: 'post' }
|
|
|
|
)
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
return toggle.value ? h('p', { ref: domRef }) : null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render(h(App), nodeOps.createElement('div'))
|
|
|
|
expect(dom).toBe(null)
|
|
|
|
|
|
|
|
toggle.value = true
|
|
|
|
await nextTick()
|
|
|
|
expect(dom!.tag).toBe('p')
|
|
|
|
})
|
|
|
|
|
2019-08-27 23:35:22 +08:00
|
|
|
it('deep', async () => {
|
|
|
|
const state = reactive({
|
|
|
|
nested: {
|
|
|
|
count: ref(0)
|
|
|
|
},
|
2019-08-28 02:42:05 +08:00
|
|
|
array: [1, 2, 3],
|
2019-08-28 03:01:01 +08:00
|
|
|
map: new Map([['a', 1], ['b', 2]]),
|
|
|
|
set: new Set([1, 2, 3])
|
2019-08-27 23:35:22 +08:00
|
|
|
})
|
2019-08-27 10:47:38 +08:00
|
|
|
|
2019-08-27 23:35:22 +08:00
|
|
|
let dummy
|
|
|
|
watch(
|
|
|
|
() => state,
|
|
|
|
state => {
|
2019-08-28 03:01:01 +08:00
|
|
|
dummy = [
|
|
|
|
state.nested.count,
|
|
|
|
state.array[0],
|
|
|
|
state.map.get('a'),
|
|
|
|
state.set.has(1)
|
|
|
|
]
|
2019-08-27 23:35:22 +08:00
|
|
|
},
|
|
|
|
{ deep: true }
|
|
|
|
)
|
2019-08-27 10:47:38 +08:00
|
|
|
|
2019-08-27 23:35:22 +08:00
|
|
|
state.nested.count++
|
|
|
|
await nextTick()
|
2019-08-28 03:01:01 +08:00
|
|
|
expect(dummy).toEqual([1, 1, 1, true])
|
2019-08-27 23:35:22 +08:00
|
|
|
|
|
|
|
// nested array mutation
|
2019-08-28 02:42:05 +08:00
|
|
|
state.array[0] = 2
|
2019-08-27 23:35:22 +08:00
|
|
|
await nextTick()
|
2019-08-28 03:01:01 +08:00
|
|
|
expect(dummy).toEqual([1, 2, 1, true])
|
2019-08-28 02:42:05 +08:00
|
|
|
|
|
|
|
// nested map mutation
|
|
|
|
state.map.set('a', 2)
|
|
|
|
await nextTick()
|
2019-08-28 03:01:01 +08:00
|
|
|
expect(dummy).toEqual([1, 2, 2, true])
|
|
|
|
|
|
|
|
// nested set mutation
|
|
|
|
state.set.delete(1)
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toEqual([1, 2, 2, false])
|
2019-08-27 23:35:22 +08:00
|
|
|
})
|
|
|
|
|
2020-08-24 02:41:11 +08:00
|
|
|
it('watching deep ref', async () => {
|
|
|
|
const count = ref(0)
|
|
|
|
const double = computed(() => count.value * 2)
|
|
|
|
const state = reactive([count, double])
|
|
|
|
|
|
|
|
let dummy
|
|
|
|
watch(
|
|
|
|
() => state,
|
|
|
|
state => {
|
|
|
|
dummy = [state[0].value, state[1].value]
|
|
|
|
},
|
|
|
|
{ deep: true }
|
|
|
|
)
|
|
|
|
|
|
|
|
count.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toEqual([1, 2])
|
|
|
|
})
|
|
|
|
|
2020-02-18 12:14:19 +08:00
|
|
|
it('immediate', async () => {
|
2019-08-27 23:35:22 +08:00
|
|
|
const count = ref(0)
|
|
|
|
const cb = jest.fn()
|
2020-02-18 12:14:19 +08:00
|
|
|
watch(count, cb, { immediate: true })
|
|
|
|
expect(cb).toHaveBeenCalledTimes(1)
|
2019-08-27 23:35:22 +08:00
|
|
|
count.value++
|
|
|
|
await nextTick()
|
2020-02-18 12:14:19 +08:00
|
|
|
expect(cb).toHaveBeenCalledTimes(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('immediate: triggers when initial value is null', async () => {
|
|
|
|
const state = ref(null)
|
|
|
|
const spy = jest.fn()
|
|
|
|
watch(() => state.value, spy, { immediate: true })
|
|
|
|
expect(spy).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('immediate: triggers when initial value is undefined', async () => {
|
|
|
|
const state = ref()
|
|
|
|
const spy = jest.fn()
|
|
|
|
watch(() => state.value, spy, { immediate: true })
|
|
|
|
expect(spy).toHaveBeenCalled()
|
|
|
|
state.value = 3
|
|
|
|
await nextTick()
|
|
|
|
expect(spy).toHaveBeenCalledTimes(2)
|
|
|
|
// testing if undefined can trigger the watcher
|
|
|
|
state.value = undefined
|
|
|
|
await nextTick()
|
|
|
|
expect(spy).toHaveBeenCalledTimes(3)
|
|
|
|
// it shouldn't trigger if the same value is set
|
|
|
|
state.value = undefined
|
|
|
|
await nextTick()
|
|
|
|
expect(spy).toHaveBeenCalledTimes(3)
|
2019-08-27 23:35:22 +08:00
|
|
|
})
|
|
|
|
|
2020-02-22 15:19:10 +08:00
|
|
|
it('warn immediate option when using effect', async () => {
|
2019-12-19 00:46:59 +08:00
|
|
|
const count = ref(0)
|
|
|
|
let dummy
|
2020-02-22 15:19:10 +08:00
|
|
|
watchEffect(
|
2019-12-19 00:46:59 +08:00
|
|
|
() => {
|
|
|
|
dummy = count.value
|
|
|
|
},
|
2020-02-22 15:19:10 +08:00
|
|
|
// @ts-ignore
|
2020-02-18 12:14:19 +08:00
|
|
|
{ immediate: false }
|
2019-12-19 00:46:59 +08:00
|
|
|
)
|
|
|
|
expect(dummy).toBe(0)
|
2020-02-18 12:14:19 +08:00
|
|
|
expect(`"immediate" option is only respected`).toHaveBeenWarned()
|
2019-12-19 00:46:59 +08:00
|
|
|
|
|
|
|
count.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
})
|
|
|
|
|
2020-02-25 01:03:02 +08:00
|
|
|
it('warn and not respect deep option when using effect', async () => {
|
|
|
|
const arr = ref([1, [2]])
|
2020-03-23 23:11:00 +08:00
|
|
|
const spy = jest.fn()
|
2020-02-25 01:03:02 +08:00
|
|
|
watchEffect(
|
|
|
|
() => {
|
|
|
|
spy()
|
|
|
|
return arr
|
|
|
|
},
|
|
|
|
// @ts-ignore
|
|
|
|
{ deep: true }
|
|
|
|
)
|
|
|
|
expect(spy).toHaveBeenCalledTimes(1)
|
|
|
|
;(arr.value[1] as Array<number>)[0] = 3
|
|
|
|
await nextTick()
|
|
|
|
expect(spy).toHaveBeenCalledTimes(1)
|
|
|
|
expect(`"deep" option is only respected`).toHaveBeenWarned()
|
|
|
|
})
|
|
|
|
|
2019-08-27 23:35:22 +08:00
|
|
|
it('onTrack', async () => {
|
2019-08-29 00:13:36 +08:00
|
|
|
const events: DebuggerEvent[] = []
|
2019-08-27 23:35:22 +08:00
|
|
|
let dummy
|
|
|
|
const onTrack = jest.fn((e: DebuggerEvent) => {
|
|
|
|
events.push(e)
|
|
|
|
})
|
|
|
|
const obj = reactive({ foo: 1, bar: 2 })
|
2020-02-22 15:19:10 +08:00
|
|
|
watchEffect(
|
2019-08-27 23:35:22 +08:00
|
|
|
() => {
|
|
|
|
dummy = [obj.foo, 'bar' in obj, Object.keys(obj)]
|
|
|
|
},
|
|
|
|
{ onTrack }
|
|
|
|
)
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toEqual([1, true, ['foo', 'bar']])
|
|
|
|
expect(onTrack).toHaveBeenCalledTimes(3)
|
|
|
|
expect(events).toMatchObject([
|
|
|
|
{
|
2019-08-29 00:13:36 +08:00
|
|
|
target: obj,
|
2019-12-04 00:30:24 +08:00
|
|
|
type: TrackOpTypes.GET,
|
2019-08-27 23:35:22 +08:00
|
|
|
key: 'foo'
|
|
|
|
},
|
|
|
|
{
|
2019-08-29 00:13:36 +08:00
|
|
|
target: obj,
|
2019-12-04 00:30:24 +08:00
|
|
|
type: TrackOpTypes.HAS,
|
2019-08-27 23:35:22 +08:00
|
|
|
key: 'bar'
|
|
|
|
},
|
|
|
|
{
|
2019-08-29 00:13:36 +08:00
|
|
|
target: obj,
|
2019-12-04 00:30:24 +08:00
|
|
|
type: TrackOpTypes.ITERATE,
|
2019-08-27 23:35:22 +08:00
|
|
|
key: ITERATE_KEY
|
|
|
|
}
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('onTrigger', async () => {
|
2019-08-29 00:13:36 +08:00
|
|
|
const events: DebuggerEvent[] = []
|
2019-08-27 23:35:22 +08:00
|
|
|
let dummy
|
|
|
|
const onTrigger = jest.fn((e: DebuggerEvent) => {
|
|
|
|
events.push(e)
|
|
|
|
})
|
|
|
|
const obj = reactive({ foo: 1 })
|
2020-02-22 15:19:10 +08:00
|
|
|
watchEffect(
|
2019-08-27 23:35:22 +08:00
|
|
|
() => {
|
|
|
|
dummy = obj.foo
|
|
|
|
},
|
|
|
|
{ onTrigger }
|
|
|
|
)
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
|
|
|
|
obj.foo++
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toBe(2)
|
|
|
|
expect(onTrigger).toHaveBeenCalledTimes(1)
|
|
|
|
expect(events[0]).toMatchObject({
|
2019-12-04 00:30:24 +08:00
|
|
|
type: TriggerOpTypes.SET,
|
2019-08-27 23:35:22 +08:00
|
|
|
key: 'foo',
|
|
|
|
oldValue: 1,
|
|
|
|
newValue: 2
|
|
|
|
})
|
|
|
|
|
2020-08-21 05:48:28 +08:00
|
|
|
// @ts-ignore
|
2019-08-27 23:35:22 +08:00
|
|
|
delete obj.foo
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toBeUndefined()
|
|
|
|
expect(onTrigger).toHaveBeenCalledTimes(2)
|
|
|
|
expect(events[1]).toMatchObject({
|
2019-12-04 00:30:24 +08:00
|
|
|
type: TriggerOpTypes.DELETE,
|
2019-08-27 23:35:22 +08:00
|
|
|
key: 'foo',
|
|
|
|
oldValue: 2
|
|
|
|
})
|
|
|
|
})
|
2020-07-20 01:30:24 +08:00
|
|
|
|
|
|
|
it('should work sync', () => {
|
|
|
|
const v = ref(1)
|
|
|
|
let calls = 0
|
|
|
|
|
|
|
|
watch(
|
|
|
|
v,
|
|
|
|
() => {
|
|
|
|
++calls
|
|
|
|
},
|
|
|
|
{
|
|
|
|
flush: 'sync'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(calls).toBe(0)
|
|
|
|
v.value++
|
|
|
|
expect(calls).toBe(1)
|
|
|
|
})
|
2020-07-31 06:29:38 +08:00
|
|
|
|
2020-10-07 06:16:20 +08:00
|
|
|
test('should force trigger on triggerRef when watching a shallow ref', async () => {
|
|
|
|
const v = shallowRef({ a: 1 })
|
2020-07-31 06:29:38 +08:00
|
|
|
let sideEffect = 0
|
|
|
|
watch(v, obj => {
|
|
|
|
sideEffect = obj.a
|
|
|
|
})
|
|
|
|
|
|
|
|
v.value = v.value
|
|
|
|
await nextTick()
|
|
|
|
// should not trigger
|
|
|
|
expect(sideEffect).toBe(0)
|
|
|
|
|
|
|
|
v.value.a++
|
|
|
|
await nextTick()
|
|
|
|
// should not trigger
|
|
|
|
expect(sideEffect).toBe(0)
|
|
|
|
|
|
|
|
triggerRef(v)
|
|
|
|
await nextTick()
|
|
|
|
// should trigger now
|
|
|
|
expect(sideEffect).toBe(2)
|
|
|
|
})
|
2020-09-16 22:52:31 +08:00
|
|
|
|
|
|
|
// #2125
|
|
|
|
test('watchEffect should not recursively trigger itself', async () => {
|
|
|
|
const spy = jest.fn()
|
|
|
|
const price = ref(10)
|
|
|
|
const history = ref<number[]>([])
|
|
|
|
watchEffect(() => {
|
|
|
|
history.value.push(price.value)
|
|
|
|
spy()
|
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
expect(spy).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
2020-10-07 06:16:20 +08:00
|
|
|
|
|
|
|
// #2231
|
|
|
|
test('computed refs should not trigger watch if value has no change', async () => {
|
|
|
|
const spy = jest.fn()
|
|
|
|
const source = ref(0)
|
|
|
|
const price = computed(() => source.value === 0)
|
|
|
|
watch(price, spy)
|
|
|
|
source.value++
|
|
|
|
await nextTick()
|
|
|
|
source.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(spy).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
2020-11-27 22:31:50 +08:00
|
|
|
|
|
|
|
// https://github.com/vuejs/vue-next/issues/2381
|
|
|
|
test('$watch should always register its effects with itw own instance', async () => {
|
|
|
|
let instance: ComponentInternalInstance | null
|
|
|
|
let _show: Ref<boolean>
|
|
|
|
|
|
|
|
const Child = defineComponent({
|
|
|
|
render: () => h('div'),
|
|
|
|
mounted() {
|
|
|
|
instance = getCurrentInstance()
|
|
|
|
},
|
|
|
|
unmounted() {}
|
|
|
|
})
|
|
|
|
|
|
|
|
const Comp = defineComponent({
|
|
|
|
setup() {
|
|
|
|
const comp = ref<ComponentPublicInstance | undefined>()
|
|
|
|
const show = ref(true)
|
|
|
|
_show = show
|
|
|
|
return { comp, show }
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
return this.show
|
|
|
|
? h(Child, {
|
|
|
|
ref: vm => void (this.comp = vm as ComponentPublicInstance)
|
|
|
|
})
|
|
|
|
: null
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
// this call runs while Comp is currentInstance, but
|
|
|
|
// the effect for this `$watch` should nontheless be registered with Child
|
|
|
|
this.comp!.$watch(() => this.show, () => void 0)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
render(h(Comp), nodeOps.createElement('div'))
|
|
|
|
|
|
|
|
expect(instance!).toBeDefined()
|
|
|
|
expect(instance!.effects).toBeInstanceOf(Array)
|
|
|
|
expect(instance!.effects!.length).toBe(1)
|
|
|
|
|
|
|
|
_show!.value = false
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
await nextTick()
|
|
|
|
|
|
|
|
expect(instance!.effects![0].active).toBe(false)
|
|
|
|
})
|
2021-02-09 15:00:32 +08:00
|
|
|
|
|
|
|
test('this.$watch should pass `this.proxy` to watch source as the first argument ', () => {
|
|
|
|
let instance: any
|
|
|
|
const source = jest.fn()
|
|
|
|
|
|
|
|
const Comp = defineComponent({
|
|
|
|
render() {},
|
|
|
|
created(this: any) {
|
|
|
|
instance = this
|
|
|
|
this.$watch(source, function() {})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
createApp(Comp).mount(root)
|
|
|
|
|
|
|
|
expect(instance).toBeDefined()
|
|
|
|
expect(source).toHaveBeenCalledWith(instance)
|
|
|
|
})
|
2021-03-27 03:52:45 +08:00
|
|
|
|
2021-05-25 23:10:11 +08:00
|
|
|
test('should not leak `this.proxy` to setup()', () => {
|
|
|
|
const source = jest.fn()
|
|
|
|
|
|
|
|
const Comp = defineComponent({
|
|
|
|
render() {},
|
|
|
|
setup() {
|
|
|
|
watch(source, () => {})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
createApp(Comp).mount(root)
|
|
|
|
// should not have any arguments
|
|
|
|
expect(source.mock.calls[0]).toMatchObject([])
|
|
|
|
})
|
|
|
|
|
2021-03-27 03:52:45 +08:00
|
|
|
// #2728
|
|
|
|
test('pre watcher callbacks should not track dependencies', async () => {
|
|
|
|
const a = ref(0)
|
|
|
|
const b = ref(0)
|
|
|
|
const updated = jest.fn()
|
|
|
|
|
|
|
|
const Child = defineComponent({
|
|
|
|
props: ['a'],
|
|
|
|
updated,
|
|
|
|
watch: {
|
|
|
|
a() {
|
|
|
|
b.value
|
|
|
|
}
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
return h('div', this.a)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const Parent = defineComponent({
|
|
|
|
render() {
|
|
|
|
return h(Child, { a: a.value })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
createApp(Parent).mount(root)
|
|
|
|
|
|
|
|
a.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(updated).toHaveBeenCalledTimes(1)
|
|
|
|
|
|
|
|
b.value++
|
|
|
|
await nextTick()
|
|
|
|
// should not track b as dependency of Child
|
|
|
|
expect(updated).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
2021-04-08 04:19:04 +08:00
|
|
|
|
|
|
|
test('watching keypath', async () => {
|
|
|
|
const spy = jest.fn()
|
|
|
|
const Comp = defineComponent({
|
|
|
|
render() {},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
a: {
|
|
|
|
b: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
'a.b': spy
|
|
|
|
},
|
|
|
|
created(this: any) {
|
|
|
|
this.$watch('a.b', spy)
|
|
|
|
},
|
|
|
|
mounted(this: any) {
|
|
|
|
this.a.b++
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
createApp(Comp).mount(root)
|
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
expect(spy).toHaveBeenCalledTimes(2)
|
|
|
|
})
|
2021-05-25 23:10:11 +08:00
|
|
|
|
2021-05-08 06:03:35 +08:00
|
|
|
it('watching sources: ref<any[]>', async () => {
|
|
|
|
const foo = ref([1])
|
|
|
|
const spy = jest.fn()
|
|
|
|
watch(foo, () => {
|
|
|
|
spy()
|
|
|
|
})
|
|
|
|
foo.value = foo.value.slice()
|
|
|
|
await nextTick()
|
|
|
|
expect(spy).toBeCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('watching multiple sources: computed', async () => {
|
|
|
|
let count = 0
|
|
|
|
const value = ref('1')
|
|
|
|
const plus = computed(() => !!value.value)
|
|
|
|
watch([plus], () => {
|
|
|
|
count++
|
|
|
|
})
|
|
|
|
value.value = '2'
|
|
|
|
await nextTick()
|
|
|
|
expect(plus.value).toBe(true)
|
|
|
|
expect(count).toBe(0)
|
|
|
|
})
|
2019-08-24 03:32:19 +08:00
|
|
|
})
|