UNPKG

2.25 kBJavaScriptView Raw
1/* eslint-disable max-lines-per-function */
2/* eslint-disable func-names */
3const JParseStream = require('../lib/parse_stream');
4const PassThroughStream = require('stream').PassThrough;
5
6describe('TweetStreamParser', function() {
7 this.timeout(10000);
8
9 it('should process a chunk of tweet JSON and emit a tweet event', function(done) {
10 let stream = new PassThroughStream();
11 let client = new JParseStream();
12 stream.pipe(client);
13 client.on('tweet', () => done());
14 client.on('stream-error', (e) => done(e));
15 stream.write(`{"data": {"test":"dave"}}\r\n`, 'utf8');
16 stream.destroy();
17 });
18
19 it('should process a chunk of invalid JSON and emit an stream-error event', function(done) {
20 let stream = new PassThroughStream();
21 let client = new JParseStream();
22 stream.pipe(client);
23 client.on('tweet', () => done(new Error('Should not emit a tweet')));
24 client.on('stream-error', () => done());
25 stream.write(`{"data": "test":"dave"}}\r\n`);
26 stream.destroy();
27 });
28
29 it('should process a chunk of error JSON and emit an stream-error event', function(done) {
30 let stream = new PassThroughStream();
31 let client = new JParseStream();
32 stream.pipe(client);
33 client.on('tweet', () => done(new Error('Should not emit a tweet')));
34 client.on('api-errors', () => done());
35 stream.write(`{"errors": {"test":"dave"}}\r\n`);
36 stream.destroy();
37 });
38
39 it('should process a chunk of unknown JSON and emit an stream-error event', function(done) {
40 let stream = new PassThroughStream();
41 let client = new JParseStream();
42 stream.pipe(client);
43 client.on('tweet', () => done(new Error('Should not emit a tweet')));
44 client.on('stream-error', (e) => done(e));
45 client.on('other', () => done());
46 stream.write(`{"other": {"test":"dave"}}\r\n`);
47 stream.destroy();
48 });
49
50 it('should process a heartbeat and emit a heartbeat event', function(done) {
51 let stream = new PassThroughStream();
52 let client = new JParseStream();
53 stream.pipe(client);
54 client.on('tweet', () => done(new Error('Should not emit a tweet')));
55 client.on('stream-error', (e) => done(e));
56 client.on('heartbeat', () => done());
57 stream.write(`\r\n`);
58 stream.destroy();
59 });
60});