Просмотр исходного кода

fix watch in firefox again + e2e test for select2

Evan You 10 лет назад
Родитель
Сommit
b38b4b2907

+ 19 - 8
src/runtime/instance/state.js

@@ -10,6 +10,7 @@ import {
 import {
   warn,
   hasOwn,
+  isArray,
   isPlainObject,
   bind,
   validateProp
@@ -120,19 +121,29 @@ function initWatch (vm) {
   if (watch) {
     for (let key in watch) {
       let handler = watch[key]
-      let options
-      if (isPlainObject(handler)) {
-        options = handler
-        handler = handler.handler
-      }
-      if (typeof handler === 'string') {
-        handler = vm[handler]
+      if (isArray(handler)) {
+        for (let i = 0; i < handler.length; i++) {
+          createWatcher(vm, key, handler[i])
+        }
+      } else {
+        createWatcher(vm, key, handler)
       }
-      vm.$watch(key, handler, options)
     }
   }
 }
 
+function createWatcher (vm, key, handler) {
+  let options
+  if (isPlainObject(handler)) {
+    options = handler
+    handler = handler.handler
+  }
+  if (typeof handler === 'string') {
+    handler = vm[handler]
+  }
+  vm.$watch(key, handler, options)
+}
+
 export function stateMixin (Vue) {
   Object.defineProperty(Vue.prototype, '$data', {
     get () {

+ 1 - 1
src/runtime/util/options.js

@@ -306,7 +306,7 @@ export function mergeOptions (parent, child, vm) {
     mergeField(key)
   }
   for (key in child) {
-    if (!(key in parent)) {
+    if (!hasOwn(parent, key)) {
       mergeField(key)
     }
   }

+ 18 - 0
test/e2e/custom-assertions/attributePresent.js

@@ -0,0 +1,18 @@
+exports.assertion = function (selector, attr) {
+  this.message = 'Testing if element <' + selector + '> has attribute ' + attr + '.'
+  this.expected = true
+  this.value = function (res) {
+    return res.value
+  }
+  this.pass = function (val) {
+    return val === this.expected
+  }
+  this.command = function (cb) {
+    var self = this
+    return this.api.execute(function (selector, attr) {
+      return document.querySelector(selector).hasAttribute(attr)
+    }, [selector, attr], function (res) {
+      cb.call(self, res)
+    })
+  }
+}

+ 45 - 0
test/e2e/specs/select2.js

@@ -0,0 +1,45 @@
+module.exports = {
+  'select2': function (browser) {
+    browser
+    .url('http://localhost:8080/examples/select2/')
+      .waitForElementVisible('.select2', 1000)
+      .assert.elementPresent('select')
+      .assert.containsText('p', 'Selected: 0')
+      .assert.containsText('span.select2', 'Select one')
+
+      .click('span.select2')
+      .assert.elementCount('.select2-results__option', 3)
+      .assert.containsText('.select2-results__option:nth-child(1)', 'Select one')
+      .assert.containsText('.select2-results__option:nth-child(2)', 'Hello')
+      .assert.containsText('.select2-results__option:nth-child(3)', 'World')
+      .assert.attributePresent('.select2-results__option:nth-child(1)', 'aria-disabled')
+
+      .click('.select2-results__option:nth-child(2)')
+      .assert.elementCount('.select2-results__option', 0)
+      .assert.containsText('p', 'Selected: 1')
+      .assert.containsText('span.select2', 'Hello')
+
+      // test dynamic options
+      .execute(function () {
+        vm.options.push({ id: 3, text: 'Vue' })
+      })
+      .click('span.select2')
+      .assert.elementCount('.select2-results__option', 4)
+      .assert.containsText('.select2-results__option:nth-child(1)', 'Select one')
+      .assert.containsText('.select2-results__option:nth-child(2)', 'Hello')
+      .assert.containsText('.select2-results__option:nth-child(3)', 'World')
+      .assert.containsText('.select2-results__option:nth-child(4)', 'Vue')
+
+      .click('.select2-results__option:nth-child(4)')
+      .assert.elementCount('.select2-results__option', 0)
+      .assert.containsText('p', 'Selected: 3')
+      .assert.containsText('span.select2', 'Vue')
+
+      .execute(function () {
+        vm.selected = 2
+      })
+      .assert.containsText('p', 'Selected: 2')
+      .assert.containsText('span.select2', 'World')
+      .end()
+  }
+}