-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
76 lines (60 loc) · 1.99 KB
/
Copy pathtest.js
File metadata and controls
76 lines (60 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';
const Bridge = require('./');
require('babel-polyfill');
describe('async bridge', () => {
let b0, b1;
beforeEach(() => {
b0 = Bridge.create();
b1 = Bridge.create();
b0.emitter = b1.receiver;
b1.emitter = b0.receiver;
});
it('should get a response from other bridge', () => {
b0.respond('test', () => 'test');
return b1.request('test').then(value => {
if (value !== 'test') throw new Error();
});
});
it('should reject on sync error', () => {
b1.respond('test', () => { throw new Error('hello!'); });
return b0.request('test').then(() => { throw new Error(); }, error => {
if (error.name !== 'Error' || error.message !== 'hello!') throw new Error();
});
});
it('should reject on async error', () => {
b1.respond('test', () => Promise.reject('test'));
return b0.request('test').then(() => { throw new Error(); }, error => {
if (error !== 'test') throw new Error();
});
});
it('should not throw error on request when emitter is broken', () => {
b1.emitter = () => { throw new Error(); };
b1.request('hi', 10000);
});
it('syncing after request sent before emitter attached works', () => {
b1.respond('test2', () => 'test');
b0.emitter = null;
const promise = b0.request('test2', null, 5000);
setTimeout(() => {
b0.emitter = b1.receiver;
b0.sync();
}, 1000);
return promise.then(response => {
if (response !== 'test') throw new Error();
});
});
it('timeouts cause an error', () => {
b1.respond('hello', () => new Promise(() => {}));
return b0.request('hello', null, 1000).then(() => { throw new Error(); }, () => {});
});
it('sync can timeout', () => {
b0.emitter = null;
return b0.sync(1000).then(() => { throw new Error(); }, () => {});
});
it('sync resolves', () => {
return b0.sync();
});
it('unhandled requests cause an error', () => {
return b0.request('hello').then(() => { throw new Error(); }, () => {});
});
});