stream.test.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. var
  3. stream,
  4. Stream = require('../lib/utils/stream'),
  5. QUnit = require('qunit');
  6. QUnit.module('Stream', {
  7. beforeEach: function() {
  8. stream = new Stream();
  9. stream.init();
  10. }
  11. });
  12. QUnit.test('trigger calls listeners', function(assert) {
  13. var args = [];
  14. stream.on('test', function(data) {
  15. args.push(data);
  16. });
  17. stream.trigger('test', 1);
  18. stream.trigger('test', 2);
  19. assert.deepEqual(args, [1, 2]);
  20. });
  21. QUnit.test('callbacks can remove themselves', function(assert) {
  22. var args1 = [], args2 = [], args3 = [];
  23. stream.on('test', function(event) {
  24. args1.push(event);
  25. });
  26. stream.on('test', function t(event) {
  27. args2.push(event);
  28. stream.off('test', t);
  29. });
  30. stream.on('test', function(event) {
  31. args3.push(event);
  32. });
  33. stream.trigger('test', 1);
  34. stream.trigger('test', 2);
  35. assert.deepEqual(args1, [1, 2], 'first callback ran all times');
  36. assert.deepEqual(args2, [1], 'second callback removed after first run');
  37. assert.deepEqual(args3, [1, 2], 'third callback ran all times');
  38. });