event_spec.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. var Vue = require('../../../../src/vue')
  2. describe('Events API', function () {
  3. var e, spy
  4. beforeEach(function () {
  5. e = new Vue()
  6. spy = jasmine.createSpy('emitter')
  7. })
  8. it('$on', function () {
  9. e.$on('test', spy)
  10. e.$emit('test', 1, 2 ,3, 4)
  11. expect(spy.calls.count()).toBe(1)
  12. expect(spy).toHaveBeenCalledWith(1, 2, 3, 4)
  13. })
  14. it('$once', function () {
  15. e.$once('test', spy)
  16. e.$emit('test', 1, 2 ,3)
  17. e.$emit('test', 2, 3, 4)
  18. expect(spy.calls.count()).toBe(1)
  19. expect(spy).toHaveBeenCalledWith(1, 2, 3)
  20. })
  21. it('$off', function () {
  22. e.$on('test1', spy)
  23. e.$on('test2', spy)
  24. e.$off()
  25. e.$emit('test1')
  26. e.$emit('test2')
  27. expect(spy.calls.count()).toBe(0)
  28. })
  29. it('$off event', function () {
  30. e.$on('test1', spy)
  31. e.$on('test2', spy)
  32. e.$off('test1')
  33. e.$off('test1') // test off something that's already off
  34. e.$emit('test1', 1)
  35. e.$emit('test2', 2)
  36. expect(spy.calls.count()).toBe(1)
  37. expect(spy).toHaveBeenCalledWith(2)
  38. })
  39. it('$off event + fn', function () {
  40. var spy2 = jasmine.createSpy('emitter')
  41. e.$on('test', spy)
  42. e.$on('test', spy2)
  43. e.$off('test', spy)
  44. e.$emit('test', 1, 2, 3)
  45. expect(spy.calls.count()).toBe(0)
  46. expect(spy2.calls.count()).toBe(1)
  47. expect(spy2).toHaveBeenCalledWith(1, 2, 3)
  48. })
  49. it('$emit cancel', function () {
  50. expect(e._eventCancelled).toBe(false)
  51. e.$on('test', function () {
  52. return false
  53. })
  54. e.$emit('test')
  55. expect(e._eventCancelled).toBe(true)
  56. e.$emit('other')
  57. expect(e._eventCancelled).toBe(false)
  58. })
  59. it('$broadcast', function () {
  60. // TODO
  61. })
  62. it('$dispatch', function () {
  63. // TODO
  64. })
  65. })