Parcourir la source

tests for events api

Evan You il y a 11 ans
Parent
commit
11d847f17f
3 fichiers modifiés avec 118 ajouts et 76 suppressions
  1. 3 0
      src/api/events.js
  2. 0 76
      test/unit/specs/api/event_spec.js
  3. 115 0
      test/unit/specs/api/events_spec.js

+ 3 - 0
src/api/events.js

@@ -7,9 +7,12 @@ var _ = require('../util')
  * @param {Function} fn
  */
 
+var hookRE = /^hook:/
 exports.$on = function (event, fn) {
   (this._events[event] || (this._events[event] = []))
     .push(fn)
+  // hooks do not get broadcasted so we can skip them
+  if (hookRE.test(event)) return
   // increment all parent event count by 1.
   // pay a small cost here to optimize for $broadcast.
   var parent = this.$parent

+ 0 - 76
test/unit/specs/api/event_spec.js

@@ -1,76 +0,0 @@
-var Vue = require('../../../../src/vue')
-
-describe('Events API', function () {
-
-  var e, spy
-  beforeEach(function () {
-    e = new Vue()
-    spy = jasmine.createSpy('emitter')
-  })
-  
-  it('$on', function () {
-    e.$on('test', spy)
-    e.$emit('test', 1, 2 ,3, 4)
-    expect(spy.calls.count()).toBe(1)
-    expect(spy).toHaveBeenCalledWith(1, 2, 3, 4)
-  })
-
-  it('$once', function () {
-    e.$once('test', spy)
-    e.$emit('test', 1, 2 ,3)
-    e.$emit('test', 2, 3, 4)
-    expect(spy.calls.count()).toBe(1)
-    expect(spy).toHaveBeenCalledWith(1, 2, 3)
-  })
-
-  it('$off', function () {
-    e.$on('test1', spy)
-    e.$on('test2', spy)
-    e.$off()
-    e.$emit('test1')
-    e.$emit('test2')
-    expect(spy.calls.count()).toBe(0)
-  })
-
-  it('$off event', function () {
-    e.$on('test1', spy)
-    e.$on('test2', spy)
-    e.$off('test1')
-    e.$off('test1') // test off something that's already off
-    e.$emit('test1', 1)
-    e.$emit('test2', 2)
-    expect(spy.calls.count()).toBe(1)
-    expect(spy).toHaveBeenCalledWith(2)
-  })
-
-  it('$off event + fn', function () {
-    var spy2 = jasmine.createSpy('emitter')
-    e.$on('test', spy)
-    e.$on('test', spy2)
-    e.$off('test', spy)
-    e.$emit('test', 1, 2, 3)
-    expect(spy.calls.count()).toBe(0)
-    expect(spy2.calls.count()).toBe(1)
-    expect(spy2).toHaveBeenCalledWith(1, 2, 3)
-  })
-
-  it('$emit cancel', function () {
-    expect(e._eventCancelled).toBe(false)
-    e.$on('test', function () {
-      return false
-    })
-    e.$emit('test')
-    expect(e._eventCancelled).toBe(true)
-    e.$emit('other')
-    expect(e._eventCancelled).toBe(false)
-  })
-
-  it('$broadcast', function () {
-    // TODO
-  })
-
-  it('$dispatch', function () {
-    // TODO
-  })
-
-})

+ 115 - 0
test/unit/specs/api/events_spec.js

@@ -0,0 +1,115 @@
+var Vue = require('../../../../src/vue')
+
+describe('Events API', function () {
+
+  var vm, spy
+  beforeEach(function () {
+    vm = new Vue()
+    spy = jasmine.createSpy('emitter')
+  })
+  
+  it('$on', function () {
+    vm.$on('test', function () {
+      // expect correct context
+      expect(this).toBe(vm)
+      spy.apply(this, arguments)
+    })
+    vm.$emit('test', 1, 2 ,3, 4)
+    expect(spy.calls.count()).toBe(1)
+    expect(spy).toHaveBeenCalledWith(1, 2, 3, 4)
+  })
+
+  it('$once', function () {
+    vm.$once('test', spy)
+    vm.$emit('test', 1, 2 ,3)
+    vm.$emit('test', 2, 3, 4)
+    expect(spy.calls.count()).toBe(1)
+    expect(spy).toHaveBeenCalledWith(1, 2, 3)
+  })
+
+  it('$off', function () {
+    vm.$on('test1', spy)
+    vm.$on('test2', spy)
+    vm.$off()
+    vm.$emit('test1')
+    vm.$emit('test2')
+    expect(spy.calls.count()).toBe(0)
+  })
+
+  it('$off event', function () {
+    vm.$on('test1', spy)
+    vm.$on('test2', spy)
+    vm.$off('test1')
+    vm.$off('test1') // test off something that's already off
+    vm.$emit('test1', 1)
+    vm.$emit('test2', 2)
+    expect(spy.calls.count()).toBe(1)
+    expect(spy).toHaveBeenCalledWith(2)
+  })
+
+  it('$off event + fn', function () {
+    var spy2 = jasmine.createSpy('emitter')
+    vm.$on('test', spy)
+    vm.$on('test', spy2)
+    vm.$off('test', spy)
+    vm.$emit('test', 1, 2, 3)
+    expect(spy.calls.count()).toBe(0)
+    expect(spy2.calls.count()).toBe(1)
+    expect(spy2).toHaveBeenCalledWith(1, 2, 3)
+  })
+
+  it('$broadcast', function () {
+    var child1 = vm.$addChild()
+    var child2 = vm.$addChild()
+    var child3 = child1.$addChild()
+    child1.$on('test', spy)
+    child2.$on('test', spy)
+    child3.$on('test', spy)
+    vm.$broadcast('test')
+    expect(spy.calls.count()).toBe(3)
+  })
+
+  it('$broadcast optimization', function () {
+    var child = vm.$addChild()
+    var child2 = child.$addChild()
+    child.$on('test', spy)
+    // child2's $emit & $broadcast
+    // shouldn't get called if no child listens to the event
+    child2.$emit = spy
+    child2.$broadcast = spy
+    vm.$broadcast('test')
+    expect(spy.calls.count()).toBe(1)
+  })
+
+  it('$broadcast cancel', function () {
+    var child = vm.$addChild()
+    var child2 = child.$addChild()
+    child.$on('test', function () {
+      return false
+    })
+    child2.$on('test', spy)
+    vm.$broadcast('test')
+    expect(spy.calls.count()).toBe(0)
+  })
+
+  it('$dispatch', function () {
+    var child = vm.$addChild()
+    var child2 = child.$addChild()
+    child.$on('test', spy)
+    vm.$on('test', spy)
+    child2.$dispatch('test')
+    expect(spy.calls.count()).toBe(2)
+  })
+
+  it('$dispatch cancel', function () {
+    var child = vm.$addChild()
+    var child2 = child.$addChild()
+    child.$on('test', function () {
+      return false
+    })
+    vm.$on('test', spy)
+    child2.$dispatch('test')
+    expect(spy.calls.count()).toBe(0)
+  })
+
+})