-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexpr.test.ts
More file actions
117 lines (94 loc) · 3.21 KB
/
expr.test.ts
File metadata and controls
117 lines (94 loc) · 3.21 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
import { should } from 'chai'; should();
import { ErrorCallback, ContextType } from '../../shared/types';
import emission from '../../shared/emission';
import { Node } from '../node';
import { Expr } from '../expr';
import expr from '../expr';
import { Source } from '../../pin/source';
describe('Expr', () => {
it('should be a subclass of Node.', () => {
new Expr(() => {}).should.be.instanceof(Node);
});
it('should run given function.', done => {
let e = new Expr(['a', 'b'], (a: any, b: any) => a + b);
let a = new Source(); a.to(e.in('a'));
let b = new Source(); b.to(e.in('b'));
e.result.subscribe(res => {
res.should.equal(5);
done();
});
a.send(2);
b.send(3);
});
it('should throw an error if not all parameters are provided.', done => {
new Expr(['a'], (a: any) => a).
result.subscribe(() => {}, () => done());
});
it('should run given function instantly if no inputs are outlined.', done => {
new Expr(() => true).result.subscribe(() => done());
});
it('should handle erros occuring in function execution.', done => {
new Expr(() => { throw new Error(); }).
result.subscribe(() => {}, () => done());
});
it('should also pass the context to the function.', done => {
let e = new Expr(['i'], (_, ctx: ContextType) => {
ctx.name.should.equal('the dude');
done();
});
let a = new Source(); a.to(e.in('i'));
e.result.subscribe();
a.emit(emission('whatever', {name: 'the dude'}));
});
it('should run the result of the function as an async callback if the result is a function itself.', done => {
new Expr(() => (done: any) => done('hellow')).
result.subscribe(res => {
res.should.equal('hellow');
done();
});
});
it('should also provide the proper error callback to the async callback.', done => {
new Expr(() => (_: any, err: ErrorCallback) => err('yup')).
result.subscribe(() => {}, () => done());
});
describe('.result', () => {
it('should be equal to `.out("result")`', () => {
let e = new Expr(() => {});
e.result.should.equal(e.out('result'));
});
});
});
describe('expr()', () => {
it('should return a proper Expr.', done => {
let e = expr(['a', 'b'], (a: any, b: any) => a * b);
e.should.be.instanceof(Expr);
let a = new Source(); a.to(e.in('a'));
let b = new Source(); b.to(e.in('b'));
e.result.subscribe(res => {
res.should.equal(6);
done();
});
a.send(2);
b.send(3);
});
it('should create numeric inputs for the signature if no named inputs are given but the given function has inputs.', done => {
let e = expr((a: any, b: any) => b - a);
let a = new Source(); a.to(e.in(0));
let b = new Source(); b.to(e.in(1));
e.result.subscribe(val => {
val.should.equal(1);
done();
});
a.send(2);
b.send(3);
});
it('should also pass the context in `rest` param if automatically creating a signature.', done => {
let e = expr((_:any, ...[ctx]: [any, ContextType]) => {
ctx.name.should.equal('the dude');
done();
});
let a = new Source(); a.to(e.in(0));
e.result.subscribe();
a.emit(emission('whatever', {name: 'the dude'}));
});
});