import { watch, reactive, computed, nextTick, ref, h } from '../src/index'
-import { render, nodeOps, serializeInner } from '@vue/runtime-test'
+import { render, nodeOps, serializeInner, mockWarn } from '@vue/runtime-test'
import {
ITERATE_KEY,
DebuggerEvent,
// reference: https://vue-composition-api-rfc.netlify.com/api.html#watch
describe('api: watch', () => {
+ mockWarn()
+
it('basic usage', async () => {
const state = reactive({ count: 0 })
let dummy
expect(cb).toHaveBeenCalled()
})
- it('ignore lazy', async () => {
+ it('ignore lazy option when using simple callback', async () => {
const count = ref(0)
let dummy
watch(
{ lazy: true }
)
expect(dummy).toBeUndefined()
+ expect(`lazy option is only respected`).toHaveBeenWarned()
await nextTick()
expect(dummy).toBe(0)
} from './errorHandling'
import { onBeforeUnmount } from './apiLifecycle'
import { queuePostRenderEffect } from './renderer'
+import { warn } from './warning'
export type WatchHandler<T = any> = (
value: T,
scheduler: applyCb ? () => scheduler(applyCb) : scheduler
})
- if (!lazy || !cb) {
+ if (lazy && cb) {
+ oldValue = runner()
+ } else {
+ if (__DEV__ && lazy && !cb) {
+ warn(
+ `watch() lazy option is only respected when using the ` +
+ `watch(getter, callback) signature.`
+ )
+ }
if (applyCb) {
scheduler(applyCb)
} else {
scheduler(runner)
}
- } else {
- oldValue = runner()
}
recordEffect(runner)