const spyA = jest.fn(returnThis)
const spyB = jest.fn(returnThis)
const spyC = jest.fn(returnThis)
+ const spyD = jest.fn(returnThis)
let ctx: any
const Comp = {
bar: 2,
baz: {
qux: 3
- }
+ },
+ qux: 4
}
},
watch: {
baz: {
handler: spyC,
deep: true
+ },
+ qux: {
+ handler: 'onQuxChange'
}
},
methods: {
- onFooChange: spyA
+ onFooChange: spyA,
+ onQuxChange: spyD
},
render() {
ctx = this
expect(spyC).toHaveBeenCalledTimes(1)
// new and old objects have same identity
assertCall(spyC, 0, [{ qux: 4 }, { qux: 4 }])
+
+ ctx.qux++
+ await nextTick()
+ expect(spyD).toHaveBeenCalledTimes(1)
+ assertCall(spyD, 0, [5, 4])
})
test('watch array', async () => {
test('Expected a function as watch handler', () => {
const Comp = {
watch: {
- foo: 'notExistingMethod'
+ foo: 'notExistingMethod',
+ foo2: {
+ handler: 'notExistingMethod2'
+ }
},
render() {}
}
expect(
'Invalid watch handler specified by key "notExistingMethod"'
).toHaveBeenWarned()
+ expect(
+ 'Invalid watch handler specified by key "notExistingMethod2"'
+ ).toHaveBeenWarned()
})
test('Invalid watch option', () => {
type WatchOptionItem =
| string
| WatchCallback
- | { handler: WatchCallback } & WatchOptions
+ | { handler: WatchCallback | string } & WatchOptions
type ComponentWatchOptionItem = WatchOptionItem | WatchOptionItem[]
if (isArray(raw)) {
raw.forEach(r => createWatcher(r, ctx, publicThis, key))
} else {
- watch(getter, raw.handler.bind(publicThis), raw)
+ const handler = isFunction(raw.handler)
+ ? raw.handler.bind(publicThis)
+ : (ctx[raw.handler] as WatchCallback)
+ if (isFunction(handler)) {
+ watch(getter, handler, raw)
+ } else if (__DEV__) {
+ warn(`Invalid watch handler specified by key "${raw.handler}"`, handler)
+ }
}
} else if (__DEV__) {
warn(`Invalid watch option: "${key}"`)