-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.specs.js
156 lines (133 loc) · 5.11 KB
/
utils.specs.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
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
156
import * as QUnit from "qunitjs"
import { curry } from "ramda"
import * as Rx from "rx"
import { decorateWith, decorateWithOne, makeFunctionDecorator } from "../utils/src/index"
let $ = Rx.Observable;
QUnit.module("utils");
QUnit.test(
"makeFunctionDecorator :: ({ before, after, name }) : o after, x before, o name",
function exec_test(assert) {
function tapEventSource(eventName) {
return function (result) {
return result.tap(_ => {
testArr.push(`value: ${_}`);
return testArr;
})
}
}
function tapStreamOutput(eventName) {
return makeFunctionDecorator({ after: tapEventSource(eventName), name: 'tapStreamOutput' })
}
const decoratorFn = tapStreamOutput('testEv');
const testArr = [];
const fnToDecorate = function testFn(param) {
return $.just(param)
};
assert.ok(typeof decoratorFn === 'function', `makeFunctionDecorator returns a function`);
const args = ['params'];
const result = decoratorFn(args, 'testFn', fnToDecorate);
result.subscribe((value) => {
assert.deepEqual(`${args[0]}`, value, `the decorated function is executed!`)
});
assert.deepEqual(testArr, [`value: ${args[0]}`], `the (after) decorating function is executed on the result of the decorated function!`);
});
QUnit.test(
"makeFunctionDecorator :: ({ before, after, name }) : x after, o before, o name",
function exec_test(assert) {
const log = curry(function log(specs, args, fnToDecorateName, fnToDecorate) {
specs.forEach((x, i) => {
testArr.push(`${x} : ${[args, fnToDecorateName, fnToDecorate][i]}`)
})
});
const specs = ['args', 'fnToDecorateName', 'fnToDecorate'];
const decoratorFn = makeFunctionDecorator({ before: log(specs), name: 'log' });
const testArr = [];
const fnToDecorate = function testFn(param) {
return $.just(param)
};
assert.ok(typeof decoratorFn === 'function', `makeFunctionDecorator returns a function`);
const args = ['params'];
const result = decoratorFn(args, 'testFn', fnToDecorate);
result.subscribe((value) => {
assert.deepEqual(`${args[0]}`, value, `the decorated function is executed!`)
});
assert.deepEqual(testArr, [
"args : params",
"fnToDecorateName : testFn",
`fnToDecorate : function testFn(param) {
return $.just(param);
}`,
], `the (after) decorating function is executed on the result of the decorated function!`);
});
QUnit.test(
"decorateWithOne:: decoratingFnSpecs, fnToDecorate",
function exec_test(assert) {
const log = curry(function log(specs, args, fnToDecorateName, fnToDecorate) {
specs.forEach((x, i) => {
testArr.push(`${x} : ${[args, fnToDecorateName, fnToDecorate][i]}`)
})
});
const specs = ['param'];
const eventName = 'testEv';
function tapEventSource(eventName) {
return function (result, fnToDecorateName, fnToDecorate) {
return result.tap(_ => {
testArr.push(`value: ${_}`);
})
}
}
function tapStreamOutput(eventName) {
return makeFunctionDecorator({ after: tapEventSource(eventName), name: 'tapStreamOutput' })
}
const testArr = [];
const fnToDecorate = function testFn(param) {
return $.just(param + '!')
};
const decoratedFn = decorateWithOne({
before: log(specs),
after: tapEventSource(eventName)
}, fnToDecorate);
assert.ok(typeof decoratedFn === 'function', `decorateWithOne returns a function`);
const result = decoratedFn('paramTest');
result.subscribe((value) => {
assert.deepEqual(`paramTest!`, value, `the decorated function is executed!`)
});
assert.deepEqual(testArr, ["param : paramTest", "value: paramTest!"],
`the decorated function executes normally together with its decoration`);
});
QUnit.test(
"decorateWith:: [decoratingFnSpecs], fnToDecorate",
function exec_test(assert) {
const log = curry(function log(specs, args, fnToDecorateName, fnToDecorate) {
specs.forEach((x, i) => {
testArr.push(`${x} : ${[args, fnToDecorateName, fnToDecorate][i]}`)
})
});
const specs = ['param'];
const eventName = 'testEv';
function tapEventSource(eventName) {
return function (result) {
return result.tap(_ => {
testArr.push(`value: ${_}`);
})
}
}
function tapStreamOutput(eventName) {
return makeFunctionDecorator({ after: tapEventSource(eventName), name: 'tapStreamOutput' })
}
const testArr = [];
const fnToDecorate = function testFn(param) {
return $.just(param + '!')
};
const decoratedFn = decorateWith([
{ before: log(specs) },
{ after: tapEventSource(eventName) }
], fnToDecorate);
assert.ok(typeof decoratedFn === 'function', `decorateWithOne returns a function`);
const result = decoratedFn('paramTest');
result.subscribe((value) => {
assert.deepEqual(`paramTest!`, value, `the decorated function is executed!`)
});
assert.deepEqual(testArr, ["param : paramTest", "value: paramTest!"],
`the decorated function executes normally together with its decoration`);
});