stream.test.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import QUnit from 'qunit';
  2. import Stream from '../src/stream';
  3. QUnit.module('stream', {
  4. beforeEach() {
  5. this.stream = new Stream();
  6. },
  7. afterEach() {
  8. this.stream.dispose();
  9. }
  10. });
  11. QUnit.test('trigger calls listeners', function(assert) {
  12. const args = [];
  13. this.stream.on('test', function(data) {
  14. args.push(data);
  15. });
  16. this.stream.trigger('test', 1);
  17. this.stream.trigger('test', 2);
  18. assert.deepEqual(args, [1, 2]);
  19. });
  20. QUnit.test('callbacks can remove themselves', function(assert) {
  21. const args1 = [];
  22. const args2 = [];
  23. const args3 = [];
  24. const arg2Fn = (event) => {
  25. args2.push(event);
  26. this.stream.off('test', arg2Fn);
  27. };
  28. this.stream.on('test', (event) => {
  29. args1.push(event);
  30. });
  31. this.stream.on('test', arg2Fn);
  32. this.stream.on('test', (event) => {
  33. args3.push(event);
  34. });
  35. this.stream.trigger('test', 1);
  36. this.stream.trigger('test', 2);
  37. assert.deepEqual(args1, [1, 2], 'first callback ran all times');
  38. assert.deepEqual(args2, [1], 'second callback removed after first run');
  39. assert.deepEqual(args3, [1, 2], 'third callback ran all times');
  40. });