UNPKG

3.16 kBJavaScriptView Raw
1/* eslint-disable max-lines-per-function */
2/* eslint-disable func-names */
3const StreamClient = require('../index');
4const PassThroughStream = require('stream').PassThrough;
5
6describe('StreamClient', function() {
7 this.timeout(10000);
8
9 describe('#buildStreamPromise(response)', function () {
10 it('should emit close when #disconnect() is called', function(done) {
11 let client = new StreamClient({token: 'abcdef'});
12 let stream = new PassThroughStream();
13 let response_mock = { data: stream };
14 client.on('close', () => done());
15 client.buildStreamPromise(response_mock).catch((e) => done(e));
16 setTimeout(() => client.disconnect(), 1000);
17 stream.destroy();
18 });
19
20 it('should emit close when the response stream is destroyed', function(done) {
21 let client = new StreamClient({token: 'abcdef'});
22 let stream = new PassThroughStream();
23 let response_mock = { data: stream };
24 client.on('close', () => done());
25 client.buildStreamPromise(response_mock).catch((e) => done(e));
26 setTimeout(() => stream.destroy(), 1000);
27 stream.destroy();
28 });
29
30 it('should emit tweet without a timeout when it is streamed in', function(done) {
31 let client = new StreamClient({token: 'abcdef'});
32 let stream = new PassThroughStream();
33 let response_mock = { data: stream };
34 client.on('tweet', () => done());
35 client.buildStreamPromise(response_mock).catch((e) => done(e));
36 stream.write(`{"data":{"test":"dave"}}\r\n`);
37 stream.destroy();
38 });
39
40 it('should reject with an error when stream emits error', function(done) {
41 let client = new StreamClient({token: 'abcdef'});
42 let stream = new PassThroughStream();
43 let response_mock = { data: stream };
44 client.buildStreamPromise(response_mock).catch(() => done());
45 stream.emit('error', new Error('Fake error'));
46 stream.destroy();
47 });
48 });
49
50 describe('#connect({ params: object, max_connects: number})', function() {
51 it('should reject after max_reconnects attempts', function(done) {
52 let client = new StreamClient({
53 token: 'abcdef',
54 timeout: 1
55 });
56
57 client.connect({max_reconnects: 1}).catch((error) => {
58 if(error.reconnects) {
59 done();
60 } else {
61 done(error);
62 }
63 });
64 });
65
66 it('should resolve on disconnect', function(done) {
67 let client = new StreamClient({
68 token: 'abcdef',
69 timeout: 1000
70 });
71
72 client.connect({max_reconnects: 1})
73 .then(() => done())
74 .catch((error) => done(error));
75
76 client.disconnect();
77 });
78
79 it('should reject on unretriable error', function(done) {
80 let client = new StreamClient({
81 token: 'abcdef',
82 timeout: 1000
83 });
84
85 client.buildConnection = () => {
86 let error_fake = new Error();
87 error_fake.status = 401;
88 return Promise.reject(error_fake);
89 };
90
91 client.connect({max_reconnects: 1}).catch((error) => {
92 if(error.reconnects) {
93 done(error);
94 } else {
95 done();
96 }
97 });
98 });
99 });
100});