-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
48 lines (43 loc) · 1.38 KB
/
test.js
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
const test = require('tape');
const fromFunction = require('.');
const makeMock = require('callbag-mock');
test('it emits function return values until unsub', t => {
const sink = makeMock();
const {source, emitter} = fromFunction((...args) => args.join('-'));
emitter('foo'); // won't be received since we're not subscribed yet
source(0, sink); // start subscription
emitter('bar', 456);
emitter('baz', 7, 8, 9);
sink.emit(2); // stop subscription
emitter('bin'); // won't be received since we stopped subscribing
t.deepEqual(
sink.getReceivedData(),
['bar-456', 'baz-7-8-9'], 'all return values are emitted'
);
t.end();
});
test('the emitter passes on the return value', t => {
const {source, emitter} = fromFunction(() => 'foo');
t.equal(emitter(), 'foo', 'return value is passed along');
t.end();
});
test('it should default to identity function', t => {
const sink = makeMock();
const {source, emitter} = fromFunction();
source(0, sink);
emitter('foo');
t.deepEqual(sink.getReceivedData(), ['foo']);
t.end();
});
test('the source should be shared', t => {
let first, second;
const sink1 = makeMock();
const sink2 = makeMock();
const {source, emitter} = fromFunction();
source(0, sink1);
source(0, sink2);
emitter('stuff');
t.deepEqual(sink1.getReceivedData(), ['stuff']);
t.deepEqual(sink2.getReceivedData(), ['stuff']);
t.end();
});