Emitter.spec.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import Vue from 'vue'
  2. // bring in the Emitter class, not the singleton
  3. let EmitterClass = require('exports-loader?Emitter!@/Emitter')
  4. describe ("Emitter.js", () => {
  5. let vm = new Vue()
  6. let Emitter
  7. beforeEach (() => {
  8. Emitter = new EmitterClass()
  9. })
  10. it ('registers an handler', () => {
  11. expect(Emitter.listeners.size).to.equal(0)
  12. Emitter.addListener('[event_type]', (value) => {}, vm)
  13. expect(Emitter.listeners.size).to.equal(1)
  14. })
  15. it ('unregisters a registered handler', () => {
  16. let event_type = 'atype', cb = (value) => {}
  17. should.not.exist(Emitter.listeners.get(event_type))
  18. Emitter.addListener(event_type, cb, vm)
  19. expect(Emitter.listeners.get(event_type).length).to.equal(1)
  20. Emitter.removeListener(event_type, cb, vm)
  21. expect(Emitter.listeners.get(event_type).length).to.equal(0)
  22. })
  23. it ('fires an events', (done) => {
  24. let event_type = 'syn', expected_response = 'ack'
  25. expect(Emitter.listeners.size).to.equal(0)
  26. Emitter.addListener(event_type, (value) => {
  27. expect(value).to.equal(expected_response)
  28. done()
  29. }, vm)
  30. expect(Emitter.listeners.size).to.equal(1)
  31. Emitter.emit(event_type, expected_response)
  32. })
  33. })