From 606666d5a9008ab72f7cce9803b26310c636eef8 Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 12 Jul 2017 15:50:09 +0800 Subject: [PATCH] test: fix Object.prototype.watch related warnings --- src/core/instance/state.js | 5 ++++- src/core/util/env.js | 3 +++ src/core/util/options.js | 4 ++++ test/helpers/test-object-option.js | 4 ++-- test/unit/features/options/watch.spec.js | 4 ++-- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/core/instance/state.js b/src/core/instance/state.js index 656c753a583..3441c60c83a 100644 --- a/src/core/instance/state.js +++ b/src/core/instance/state.js @@ -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) { diff --git a/src/core/util/env.js b/src/core/util/env.js index 3609e6186e2..52f5067a690 100644 --- a/src/core/util/env.js +++ b/src/core/util/env.js @@ -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 { diff --git a/src/core/util/options.js b/src/core/util/options.js index 3a86509ae74..899a2575406 100644 --- a/src/core/util/options.js +++ b/src/core/util/options.js @@ -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 diff --git a/test/helpers/test-object-option.js b/test/helpers/test-object-option.js index c8361dc0e92..491126585c6 100644 --- a/test/helpers/test-object-option.js +++ b/test/helpers/test-object-option.js @@ -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) diff --git a/test/unit/features/options/watch.spec.js b/test/unit/features/options/watch.spec.js index 071d1ad8331..e16b2f8312d 100644 --- a/test/unit/features/options/watch.spec.js +++ b/test/unit/features/options/watch.spec.js @@ -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: {