line-stream.test.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {LineStream} from '../src';
  2. import QUnit from 'qunit';
  3. QUnit.module('LineStream', {
  4. beforeEach() {
  5. this.lineStream = new LineStream();
  6. }
  7. });
  8. QUnit.test('empty inputs produce no tokens', function(assert) {
  9. let data = false;
  10. this.lineStream.on('data', function() {
  11. data = true;
  12. });
  13. this.lineStream.push('');
  14. assert.ok(!data, 'no tokens were produced');
  15. });
  16. QUnit.test('splits on newlines', function(assert) {
  17. const lines = [];
  18. this.lineStream.on('data', function(line) {
  19. lines.push(line);
  20. });
  21. this.lineStream.push('#EXTM3U\nmovie.ts\n');
  22. assert.strictEqual(2, lines.length, 'two lines are ready');
  23. assert.strictEqual('#EXTM3U', lines.shift(), 'the first line is the first token');
  24. assert.strictEqual('movie.ts', lines.shift(), 'the second line is the second token');
  25. });
  26. QUnit.test('empty lines become empty strings', function(assert) {
  27. const lines = [];
  28. this.lineStream.on('data', function(line) {
  29. lines.push(line);
  30. });
  31. this.lineStream.push('\n\n');
  32. assert.strictEqual(2, lines.length, 'two lines are ready');
  33. assert.strictEqual('', lines.shift(), 'the first line is empty');
  34. assert.strictEqual('', lines.shift(), 'the second line is empty');
  35. });
  36. QUnit.test('handles lines broken across appends', function(assert) {
  37. const lines = [];
  38. this.lineStream.on('data', function(line) {
  39. lines.push(line);
  40. });
  41. this.lineStream.push('#EXTM');
  42. assert.strictEqual(0, lines.length, 'no lines are ready');
  43. this.lineStream.push('3U\nmovie.ts\n');
  44. assert.strictEqual(2, lines.length, 'two lines are ready');
  45. assert.strictEqual('#EXTM3U', lines.shift(), 'the first line is the first token');
  46. assert.strictEqual('movie.ts', lines.shift(), 'the second line is the second token');
  47. });
  48. QUnit.test('stops sending events after deregistering', function(assert) {
  49. const temporaryLines = [];
  50. const temporary = function(line) {
  51. temporaryLines.push(line);
  52. };
  53. const permanentLines = [];
  54. const permanent = function(line) {
  55. permanentLines.push(line);
  56. };
  57. this.lineStream.on('data', temporary);
  58. this.lineStream.on('data', permanent);
  59. this.lineStream.push('line one\n');
  60. assert.strictEqual(
  61. temporaryLines.length,
  62. permanentLines.length,
  63. 'both callbacks receive the event'
  64. );
  65. assert.ok(this.lineStream.off('data', temporary), 'a listener was removed');
  66. this.lineStream.push('line two\n');
  67. assert.strictEqual(1, temporaryLines.length, 'no new events are received');
  68. assert.strictEqual(2, permanentLines.length, 'new events are still received');
  69. });