From b38b4b290719087ade5403bb9f38b84e278d81b5 Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 21 Apr 2016 17:54:34 -0400 Subject: [PATCH] fix watch in firefox again + e2e test for select2 --- src/runtime/instance/state.js | 27 +++++++---- src/runtime/util/options.js | 2 +- .../e2e/custom-assertions/attributePresent.js | 18 ++++++++ test/e2e/specs/select2.js | 45 +++++++++++++++++++ 4 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 test/e2e/custom-assertions/attributePresent.js create mode 100644 test/e2e/specs/select2.js diff --git a/src/runtime/instance/state.js b/src/runtime/instance/state.js index 821235ea..c1d4bbbb 100644 --- a/src/runtime/instance/state.js +++ b/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 () { diff --git a/src/runtime/util/options.js b/src/runtime/util/options.js index 984cc491..d521d244 100644 --- a/src/runtime/util/options.js +++ b/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) } } diff --git a/test/e2e/custom-assertions/attributePresent.js b/test/e2e/custom-assertions/attributePresent.js new file mode 100644 index 00000000..7d0a0a75 --- /dev/null +++ b/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) + }) + } +} diff --git a/test/e2e/specs/select2.js b/test/e2e/specs/select2.js new file mode 100644 index 00000000..044dd96e --- /dev/null +++ b/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() + } +}