Browse Source

test for emitter

Evan You 11 years ago
parent
commit
fd14d5edc7
3 changed files with 67 additions and 5 deletions
  1. 1 2
      package.json
  2. 0 3
      test/unit/specs/binding_spec.js
  3. 66 0
      test/unit/specs/emitter_spec.js

+ 1 - 2
package.json

@@ -17,8 +17,7 @@
   "bugs": "https://github.com/yyx990803/vue/issues",
   "homepage": "http://vuejs.org",
   "scripts": {
-    "test": "grunt ci",
-    "j": "jasmine-node test/unit/ --verbose"
+    "test": "grunt ci"
   },
   "devDependencies": {
     "browserify": "^4.2.0",

+ 0 - 3
test/unit/specs/binding_spec.js

@@ -1,3 +0,0 @@
-/**
- * Test the binding class
- */

+ 66 - 0
test/unit/specs/emitter_spec.js

@@ -0,0 +1,66 @@
+var Emitter = require('../../../src/emitter')
+var u = undefined
+
+describe('Emitter', function () {
+
+  var e, spy
+  beforeEach(function () {
+    e = new Emitter()
+    spy = jasmine.createSpy('emitter')
+  })
+  
+  it('on', function () {
+    e.on('test', spy)
+    e.emit('test', 1, 2 ,3)
+    expect(spy.calls.count()).toBe(1)
+    expect(spy).toHaveBeenCalledWith(1, 2, 3)
+  })
+
+  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, u, u)
+  })
+
+  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('apply emit', function () {
+    e.on('test', spy)
+    e.applyEmit('test', 1)
+    e.applyEmit('test', 1, 2, 3, 4, 5)
+    expect(spy).toHaveBeenCalledWith(1)
+    expect(spy).toHaveBeenCalledWith(1, 2, 3, 4, 5)
+  })
+
+})