const spyB = jest.fn(returnThis)
const spyC = jest.fn(returnThis)
const spyD = jest.fn(returnThis)
+ const spyE = jest.fn(returnThis)
let ctx: any
const Comp = {
baz: {
qux: 3
},
- qux: 4
+ qux: 4,
+ dot: {
+ path: 5
+ }
}
},
watch: {
},
qux: {
handler: 'onQuxChange'
- }
+ },
+ 'dot.path': spyE
},
methods: {
onFooChange: spyA,
await nextTick()
expect(spyD).toHaveBeenCalledTimes(1)
assertCall(spyD, 0, [5, 4])
+
+ ctx.dot.path++
+ await nextTick()
+ expect(spyE).toHaveBeenCalledTimes(1)
+ assertCall(spyE, 0, [6, 5])
})
test('watch array', async () => {
publicThis: ComponentPublicInstance,
key: string
) {
- const getter = () => (publicThis as any)[key]
+ const getter = key.includes('.')
+ ? createPathGetter(publicThis, key)
+ : () => (publicThis as any)[key]
if (isString(raw)) {
const handler = ctx[raw]
if (isFunction(handler)) {
}
}
} else if (__DEV__) {
- warn(`Invalid watch option: "${key}"`)
+ warn(`Invalid watch option: "${key}"`, raw)
+ }
+}
+
+function createPathGetter(ctx: any, path: string) {
+ const segments = path.split('.')
+ return () => {
+ let cur = ctx
+ for (let i = 0; i < segments.length && cur; i++) {
+ cur = cur[segments[i]]
+ }
+ return cur
}
}