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

unit test for v-model debounce

Evan You 11 лет назад
Родитель
Сommit
fdc0151067
2 измененных файлов с 49 добавлено и 0 удалено
  1. 33 0
      test/unit/specs/directives/model_spec.js
  2. 16 0
      test/unit/specs/util/lang_spec.js

+ 33 - 0
test/unit/specs/directives/model_spec.js

@@ -592,5 +592,38 @@ if (_.inBrowser) {
       })
     })
 
+    it('support debounce', function (done) {
+      var spy = jasmine.createSpy()
+      var vm = new Vue({
+        el: el,
+        data: {
+          test: 'a'
+        },
+        watch: {
+          test: spy
+        },
+        template: '<input v-model="test" debounce="100">'
+      })
+      el.firstChild.value = 'b'
+      trigger(el.firstChild, 'input')
+      setTimeout(function () {
+        el.firstChild.value = 'c'
+        trigger(el.firstChild, 'input')
+      }, 10)
+      setTimeout(function () {
+        el.firstChild.value = 'd'
+        trigger(el.firstChild, 'input')
+      }, 20)
+      setTimeout(function () {
+        expect(spy.calls.count()).toBe(0)
+        expect(vm.test).toBe('a')
+      }, 30)
+      setTimeout(function () {
+        expect(spy.calls.count()).toBe(1)
+        expect(vm.test).toBe('d')
+        done()
+      }, 200)
+    })
+
   })
 }

+ 16 - 0
test/unit/specs/util/lang_spec.js

@@ -116,4 +116,20 @@ describe('Util - Language Enhancement', function () {
     expect(desc.enumerable).toBe(true)
   })
 
+  it('debounce', function (done) {
+    var count = 0
+    var fn = _.debounce(function () {
+      count++
+    }, 100)
+    fn()
+    setTimeout(fn, 10)
+    setTimeout(fn, 20)
+    setTimeout(function () {
+      expect(count).toBe(0)
+    }, 30)
+    setTimeout(function () {
+      expect(count).toBe(1)
+      done()
+    }, 200)
+  })
 })