-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpin.test.ts
More file actions
155 lines (124 loc) · 3.91 KB
/
pin.test.ts
File metadata and controls
155 lines (124 loc) · 3.91 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { should, expect } from 'chai'; should();
import emission from '../../shared/emission';
import group from '../group';
import { Pin } from '../pin';
import { Source } from '../source';
import filter from '../filter';
import map from '../map';
describe('Pin', () => {
describe('.from()', () => {
it('should receive from data another pin.', done => {
let s = new Source();
s.to(new Pin()).subscribe(data => {
data.should.equal('all work and no play ...');
done();
});
s.send('all work and no play ...');
});
it('should receive data from multiple other pins.', () => {
let a = new Source(); let b = new Source();
let _ = 0;
group(a, b).to(new Pin()).subscribe(n => {
_ += n;
});
a.send(1); b.send(2); a.send(3);
_.should.equal(6);
});
it('should receive data from a chain of pins.', () => {
let a = new Source(); let b = new Source();
let _: number[] = [];
group(
group(a, b).to(new Pin()),
b.to(new Pin())
).to(new Pin()).subscribe(n => {
_.push(n);
});
a.send(2); b.send(3); a.send(5);
_.should.be.eql([2, 3, 3, 5]);
});
it('should work properly with a cycle of pins.', done => {
let a = new Source();
let b = new Pin();
let c = new Pin();
a.to(b)
.to(filter((n: number) => n < 5))
.to(map((n: number) => n + 1))
.to(b)
.to(filter((n: number) => n >= 5))
.to(c);
c.subscribe(n => {
n.should.equal(5);
done();
});
a.send(0);
});
it('should throw an error when is invoked after the pin\'s observable was accessed.', () => {
let a = new Source(); let b = new Pin();
b.observable;
expect(() => {b.from(a)}).to.throw();
});
});
describe('.to()', () => {
it('should send data to another pin.', done => {
let a = new Source(); let b = new Pin();
a.to(new Pin()).to(b);
b.observable.subscribe(() => done());
a.send();
});
it('should send data to multiple other pins.', () => {
let a = new Source(); let b = new Pin();
let x = false;
let y = false;
a.to(b).to(new Pin()).subscribe(() => x = true);
b.to(new Pin()).subscribe(() => y = true);
x.should.be.false;
y.should.be.false;
a.send();
x.should.be.true;
y.should.be.true;
});
it('should pass down the same context object.', () => {
let ctx = {};
let x = false; let y = false;
let a = new Source();
let b = new Pin().from(a);
b.to(new Pin()).observable.subscribe(e => x = e.context == ctx);
b.to(new Pin()).observable.subscribe(e => y = e.context == ctx);
a.emit(emission(undefined, ctx));
x.should.be.true;
y.should.be.true;
});
});
describe('.clear()', () => {
it('should clear the pin.', () => {
let a = new Source(); let b = new Pin(); let called = false;
a.to(b).subscribe(() => called = true);
a.send();
called.should.be.true;
called = false;
b.clear(); b.subscribe(() => called = true);
a.send();
called.should.be.false;
});
});
describe('.locked', () => {
it('should be false before `.observable` is accessed and true afterwards.', () => {
let a = new Pin();
a.locked.should.be.false;
a.observable;
a.locked.should.be.true;
});
it('should be false before `.subscribe()` is called and true afterwards.', () => {
let a = new Pin();
a.locked.should.be.false;
a.subscribe(() => {});
a.locked.should.be.true;
});
it('should lock all pins that are connected to this pin.', () => {
let a = new Pin(); let b = new Pin(); let c = group(a, b).to(new Pin());
a.locked.should.be.false; b.locked.should.be.false;
c.observable;
a.locked.should.be.true; b.locked.should.be.true;
});
});
});