2019-12-04 00:30:24 +08:00
|
|
|
import { watch, reactive, computed, nextTick, ref, h } from '../src/index'
|
2019-12-19 00:54:12 +08:00
|
|
|
import { render, nodeOps, serializeInner, mockWarn } from '@vue/runtime-test'
|
2019-12-04 00:30:24 +08:00
|
|
|
import {
|
|
|
|
ITERATE_KEY,
|
|
|
|
DebuggerEvent,
|
|
|
|
TrackOpTypes,
|
|
|
|
TriggerOpTypes
|
|
|
|
} 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', () => {
|
2019-12-19 00:54:12 +08:00
|
|
|
mockWarn()
|
|
|
|
|
2019-08-27 10:47:38 +08:00
|
|
|
it('basic usage', async () => {
|
|
|
|
const state = reactive({ count: 0 })
|
|
|
|
let dummy
|
|
|
|
watch(() => {
|
|
|
|
dummy = state.count
|
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
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
|
|
|
|
prevCount + 1
|
2019-08-27 10:47:38 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toMatchObject([0, undefined])
|
|
|
|
|
|
|
|
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
|
|
|
|
prevCount + 1
|
2019-08-27 10:47:38 +08:00
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toMatchObject([0, undefined])
|
|
|
|
|
|
|
|
count.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toMatchObject([1, 0])
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
prevCount + 1
|
2019-08-27 10:47:38 +08:00
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toMatchObject([1, undefined])
|
|
|
|
|
|
|
|
count.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toMatchObject([2, 1])
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toMatchObject([[1, 1, 2], []])
|
|
|
|
|
|
|
|
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]
|
|
|
|
let [count] = vals
|
|
|
|
let [, oldStatus] = oldVals
|
|
|
|
// assert types
|
|
|
|
count + 1
|
|
|
|
oldStatus === true
|
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toMatchObject([[1, false], []])
|
|
|
|
|
|
|
|
state.count++
|
|
|
|
status.value = false
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toMatchObject([[2, false], [1, false]])
|
|
|
|
})
|
|
|
|
|
2019-08-27 10:47:38 +08:00
|
|
|
it('stopping the watcher', async () => {
|
|
|
|
const state = reactive({ count: 0 })
|
|
|
|
let dummy
|
|
|
|
const stop = watch(() => {
|
|
|
|
dummy = state.count
|
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
|
|
|
|
stop()
|
|
|
|
state.count++
|
|
|
|
await nextTick()
|
|
|
|
// should not update
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('cleanup registration (basic)', async () => {
|
|
|
|
const state = reactive({ count: 0 })
|
|
|
|
const cleanup = jest.fn()
|
|
|
|
let dummy
|
|
|
|
const stop = watch(onCleanup => {
|
|
|
|
onCleanup(cleanup)
|
|
|
|
dummy = state.count
|
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
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
|
|
|
|
})
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
|
|
|
|
count.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(cleanup).toHaveBeenCalledTimes(1)
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
|
|
|
|
stop()
|
|
|
|
expect(cleanup).toHaveBeenCalledTimes(2)
|
|
|
|
})
|
|
|
|
|
2019-08-27 23:35:22 +08:00
|
|
|
it('flush timing: post', async () => {
|
|
|
|
const count = ref(0)
|
|
|
|
const assertion = jest.fn(count => {
|
|
|
|
expect(serializeInner(root)).toBe(`${count}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
const Comp = {
|
|
|
|
setup() {
|
|
|
|
watch(() => {
|
|
|
|
assertion(count.value)
|
|
|
|
})
|
|
|
|
return () => count.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
render(h(Comp), root)
|
|
|
|
await nextTick()
|
|
|
|
expect(assertion).toHaveBeenCalledTimes(1)
|
|
|
|
|
|
|
|
count.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(assertion).toHaveBeenCalledTimes(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('flush timing: pre', async () => {
|
|
|
|
const count = ref(0)
|
|
|
|
const count2 = ref(0)
|
|
|
|
|
|
|
|
let callCount = 0
|
|
|
|
const assertion = jest.fn((count, count2Value) => {
|
|
|
|
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}`
|
|
|
|
expect(serializeInner(root)).toBe(expectedDOM)
|
|
|
|
|
|
|
|
// in a pre-flush callback, all state should have been updated
|
|
|
|
const expectedState = callCount === 1 ? 0 : 1
|
|
|
|
expect(count2Value).toBe(expectedState)
|
|
|
|
})
|
|
|
|
|
|
|
|
const Comp = {
|
|
|
|
setup() {
|
|
|
|
watch(
|
|
|
|
() => {
|
|
|
|
assertion(count.value, count2.value)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
flush: 'pre'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return () => count.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
render(h(Comp), root)
|
|
|
|
await nextTick()
|
|
|
|
expect(assertion).toHaveBeenCalledTimes(1)
|
|
|
|
|
|
|
|
count.value++
|
|
|
|
count2.value++
|
|
|
|
await nextTick()
|
|
|
|
// two mutations should result in 1 callback execution
|
|
|
|
expect(assertion).toHaveBeenCalledTimes(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
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}`
|
|
|
|
expect(serializeInner(root)).toBe(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
|
|
|
|
expect(count2.value).toBe(expectedState)
|
|
|
|
})
|
|
|
|
|
|
|
|
const Comp = {
|
|
|
|
setup() {
|
|
|
|
watch(
|
|
|
|
() => {
|
|
|
|
assertion(count.value)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
flush: 'sync'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return () => count.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const root = nodeOps.createElement('div')
|
|
|
|
render(h(Comp), root)
|
|
|
|
await nextTick()
|
|
|
|
expect(assertion).toHaveBeenCalledTimes(1)
|
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)
|
|
|
|
})
|
2019-08-27 10:47:38 +08:00
|
|
|
|
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
|
|
|
await nextTick()
|
2019-08-28 03:01:01 +08:00
|
|
|
expect(dummy).toEqual([0, 1, 1, true])
|
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
|
|
|
})
|
|
|
|
|
|
|
|
it('lazy', async () => {
|
|
|
|
const count = ref(0)
|
|
|
|
const cb = jest.fn()
|
|
|
|
watch(count, cb, { lazy: true })
|
|
|
|
await nextTick()
|
|
|
|
expect(cb).not.toHaveBeenCalled()
|
|
|
|
count.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(cb).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
2019-12-19 00:54:12 +08:00
|
|
|
it('ignore lazy option when using simple callback', async () => {
|
2019-12-19 00:46:59 +08:00
|
|
|
const count = ref(0)
|
|
|
|
let dummy
|
|
|
|
watch(
|
|
|
|
() => {
|
|
|
|
dummy = count.value
|
|
|
|
},
|
|
|
|
{ lazy: true }
|
|
|
|
)
|
|
|
|
expect(dummy).toBeUndefined()
|
2019-12-19 00:54:12 +08:00
|
|
|
expect(`lazy option is only respected`).toHaveBeenWarned()
|
2019-12-19 00:46:59 +08:00
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toBe(0)
|
|
|
|
|
|
|
|
count.value++
|
|
|
|
await nextTick()
|
|
|
|
expect(dummy).toBe(1)
|
|
|
|
})
|
|
|
|
|
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 })
|
|
|
|
watch(
|
|
|
|
() => {
|
|
|
|
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 })
|
|
|
|
watch(
|
|
|
|
() => {
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
})
|
2019-08-24 03:32:19 +08:00
|
|
|
})
|