Ver código fonte

options watch tests

Evan You 10 anos atrás
pai
commit
818faa7d58

+ 8 - 1
src/core/instance/lifecycle.js

@@ -117,7 +117,14 @@ export function lifecycleMixin (Vue: Class<Component>) {
 
   Vue.prototype.$forceUpdate = function () {
     const vm: Component = this
-    vm._watcher.update()
+    if (vm._watcher) {
+      vm._watcher.update()
+    }
+    if (vm._watchers.length) {
+      for (let i = 0; i < vm._watchers.length; i++) {
+        vm._watchers[i].update()
+      }
+    }
   }
 
   Vue.prototype.$destroy = function () {

+ 18 - 0
test/unit/features/options/methods.spec.js

@@ -0,0 +1,18 @@
+import Vue from 'vue'
+
+describe('Options methods', () => {
+  it('should have correct context', () => {
+    const vm = new Vue({
+      data: {
+        a: 1
+      },
+      methods: {
+        plus () {
+          this.a++
+        }
+      }
+    })
+    vm.plus()
+    expect(vm.a).toBe(2)
+  })
+})

+ 121 - 0
test/unit/features/options/watch.spec.js

@@ -0,0 +1,121 @@
+import Vue from 'vue'
+
+describe('Options watch', () => {
+  let spy
+  beforeEach(() => {
+    spy = jasmine.createSpy('watch')
+  })
+
+  it('basic usage', done => {
+    const vm = new Vue({
+      data: {
+        a: 1
+      },
+      watch: {
+        a: spy
+      }
+    })
+    expect(spy).not.toHaveBeenCalled()
+    vm.a = 2
+    expect(spy).not.toHaveBeenCalled()
+    waitForUpdate(() => {
+      expect(spy).toHaveBeenCalledWith(2, 1)
+    }).then(done)
+  })
+
+  it('string method name', done => {
+    const vm = new Vue({
+      data: {
+        a: 1
+      },
+      watch: {
+        a: 'onChange'
+      },
+      methods: {
+        onChange: spy
+      }
+    })
+    expect(spy).not.toHaveBeenCalled()
+    vm.a = 2
+    expect(spy).not.toHaveBeenCalled()
+    waitForUpdate(() => {
+      expect(spy).toHaveBeenCalledWith(2, 1)
+    }).then(done)
+  })
+
+  it('multiple cbs (after option merge)', done => {
+    const spy1 = jasmine.createSpy('watch')
+    const Test = Vue.extend({
+      watch: {
+        a: spy1
+      }
+    })
+    const vm = new Test({
+      data: { a: 1 },
+      watch: {
+        a: spy
+      }
+    })
+    vm.a = 2
+    waitForUpdate(() => {
+      expect(spy1).toHaveBeenCalledWith(2, 1)
+      expect(spy).toHaveBeenCalledWith(2, 1)
+    }).then(done)
+  })
+
+  it('with option: immediate', done => {
+    const vm = new Vue({
+      data: { a: 1 },
+      watch: {
+        a: {
+          handler: spy,
+          immediate: true
+        }
+      }
+    })
+    expect(spy).toHaveBeenCalledWith(1)
+    vm.a = 2
+    waitForUpdate(() => {
+      expect(spy).toHaveBeenCalledWith(2, 1)
+    }).then(done)
+  })
+
+  it('with option: deep', done => {
+    const vm = new Vue({
+      data: { a: { b: 1 }},
+      watch: {
+        a: {
+          handler: spy,
+          deep: true
+        }
+      }
+    })
+    const oldA = vm.a
+    expect(spy).not.toHaveBeenCalled()
+    vm.a.b = 2
+    expect(spy).not.toHaveBeenCalled()
+    waitForUpdate(() => {
+      expect(spy).toHaveBeenCalledWith(vm.a, vm.a)
+      vm.a = { b: 3 }
+    }).then(() => {
+      expect(spy).toHaveBeenCalledWith(vm.a, oldA)
+    }).then(done)
+  })
+
+  it('replace $data', done => {
+    const vm = new Vue({
+      data: {
+        a: 1
+      },
+      watch: {
+        a: spy
+      }
+    })
+    expect(spy).not.toHaveBeenCalled()
+    vm.$data = { a: 2 }
+    expect(spy).not.toHaveBeenCalled()
+    waitForUpdate(() => {
+      expect(spy).toHaveBeenCalledWith(2, 1)
+    }).then(done)
+  })
+})

+ 4 - 4
test/unit/modules/observer/watcher.spec.js

@@ -69,10 +69,10 @@ describe('Watcher', () => {
     expect(watcher2.value).toBeUndefined()
     vm.$data = { b: { c: 3 }, e: 4 }
     waitForUpdate(() => {
-      expect(watcher1.value).toBe(2)
-      expect(watcher2.value).toBeUndefined()
-      expect(spy).not.toHaveBeenCalledWith(3, 2)
-      expect(spy2).not.toHaveBeenCalledWith(4, undefined)
+      expect(watcher1.value).toBe(3)
+      expect(watcher2.value).toBe(4)
+      expect(spy).toHaveBeenCalledWith(3, 2)
+      expect(spy2).toHaveBeenCalledWith(4, undefined)
     }).then(done)
   })