test: fix Object.prototype.watch related warnings

This commit is contained in:
Evan You 2017-07-12 15:50:09 +08:00
parent 1f9e924971
commit 606666d5a9
5 changed files with 15 additions and 5 deletions

View File

@ -20,6 +20,7 @@ import {
hasOwn,
isReserved,
handleError,
nativeWatch,
validateProp,
isPlainObject,
isReservedAttribute
@ -53,7 +54,9 @@ export function initState (vm: Component) {
observe(vm._data = {}, true /* asRootData */)
}
if (opts.computed) initComputed(vm, opts.computed)
if (opts.watch) initWatch(vm, opts.watch)
if (opts.watch && opts.watch !== nativeWatch) {
initWatch(vm, opts.watch)
}
}
function checkOptionType (vm: Component, name: string) {

View File

@ -17,6 +17,9 @@ export const isAndroid = UA && UA.indexOf('android') > 0
export const isIOS = UA && /iphone|ipad|ipod|ios/.test(UA)
export const isChrome = UA && /chrome\/\d+/.test(UA) && !isEdge
// Firefix has a "watch" function on Object.prototype...
export const nativeWatch = ({}).watch
export let supportsPassive = false
if (inBrowser) {
try {

View File

@ -2,6 +2,7 @@
import config from '../config'
import { warn } from './debug'
import { nativeWatch } from './env'
import { set } from '../observer/index'
import {
@ -172,6 +173,9 @@ ASSET_TYPES.forEach(function (type) {
* another, so we merge them as arrays.
*/
strats.watch = function (parentVal: ?Object, childVal: ?Object): ?Object {
// work around Firefox's Object.prototype.watch...
if (parentVal === nativeWatch) parentVal = undefined
if (childVal === nativeWatch) childVal = undefined
/* istanbul ignore if */
if (!childVal) return Object.create(parentVal || null)
if (!parentVal) return childVal

View File

@ -1,14 +1,14 @@
import Vue from 'vue'
export default function testObjectOption (name) {
it('should warn non object', () => {
it('should warn non object value', () => {
const options = {}
options[name] = () => {}
new Vue(options)
expect(`component option "${name}" should be an object`).toHaveBeenWarned()
})
it('don\'t warn when is an object', () => {
it('should not warn valid object value', () => {
const options = {}
options[name] = {}
new Vue(options)

View File

@ -7,6 +7,8 @@ describe('Options watch', () => {
spy = jasmine.createSpy('watch')
})
testObjectOption('watch')
it('basic usage', done => {
const vm = new Vue({
data: {
@ -24,8 +26,6 @@ describe('Options watch', () => {
}).then(done)
})
testObjectOption('watch')
it('string method name', done => {
const vm = new Vue({
data: {