Skip to content

Commit

Permalink
test: fix Object.prototype.watch related warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jul 12, 2017
1 parent 1f9e924 commit 606666d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/core/instance/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
hasOwn,
isReserved,
handleError,
nativeWatch,
validateProp,
isPlainObject,
isReservedAttribute
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions src/core/util/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/core/util/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import config from '../config'
import { warn } from './debug'
import { nativeWatch } from './env'
import { set } from '../observer/index'

import {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/test-object-option.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test/unit/features/options/watch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ describe('Options watch', () => {
spy = jasmine.createSpy('watch')
})

testObjectOption('watch')

it('basic usage', done => {
const vm = new Vue({
data: {
Expand All @@ -24,8 +26,6 @@ describe('Options watch', () => {
}).then(done)
})

testObjectOption('watch')

it('string method name', done => {
const vm = new Vue({
data: {
Expand Down

0 comments on commit 606666d

Please sign in to comment.