-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestsuite.ts
246 lines (226 loc) · 7.23 KB
/
testsuite.ts
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import {Observable as O} from 'rxjs';
import {div, mockDOMSource} from '@cycle/dom';
import RxJSAdapter from '@cycle/rxjs-adapter';
//
// test driver global state shared among all tests
export const initTestData = {
run : 0,
passed : 0,
failed : 0,
error : 0,
nest : 0,
};
export let data = initTestData;
export function test(testName, tests, isOpen = true) {
data.nest++;
if (isOpen) {
console.group(testName);
} else {
console.groupCollapsed(testName);
}
try {
tests();
} catch (err) {
console.error('Error Runing Test:', err);
data.error++;
}
console.groupEnd();
data.nest--;
if (data.nest === 0) {
console.log('%cReport: %d run; %d passed; %d failed; %d errors',
(data.run === data.passed ? 'color: green' : 'color: red'),
data.run, data.passed, data.failed, data.error);
}
}
export function expect(actual) {
return {
toBe: expected => assertIdentical(expected, actual),
toEqual: expected => assertEqual(expected, actual),
toMarble: expected => assertMarble(expected, actual),
toBeStreamOf: expected => assertStreamOf(expected, actual),
not: {
toBe: expected => assertIdentical(expected, actual, true),
toEqual: expected => assertEqual(expected, actual, true),
toMarble: expected => assertMarble(expected, actual, true),
toBeStreamOf: expected => assertStreamOf(expected, actual, true),
}
};
}
function markPass1(message, string1, negated) {
if (negated) {
console.error(message, 'color: red', string1);
data.failed++;
} else {
console.info(message, 'color: green', string1);
data.passed++;
}
};
function markPass2(message, string1, string2, negated) {
if (negated) {
console.error(message, 'color: red', string1, string2);
data.failed++;
} else {
console.info(message, 'color: green', string1, string2);
data.passed++;
}
}
export function assertIdentical(expected, actual, negated = false) {
data.run++;
try {
if (expected === actual) {
markPass1('%cactual is equal to expected "%o"', expected, negated);
} else {
markPass2('%c%o is NOT equal to expected "%o"', actual, expected, !negated);
}
} catch (err) {
console.error('Error:', err);
data.error++;
}
}
export function assertEqual(expected, actual, negated = false) {
data.run++;
try {
if (JSON.stringify(expected) === JSON.stringify(actual)) {
markPass1('%cactual is equal to expected "%o"', actual, negated);
} else {
markPass2('%c%o is NOT equal to expected "%o"', actual, expected, !negated);
}
} catch (err) {
console.error('Error:', err);
data.error++;
}
}
export function assertMarble(
expected: string, str$: O<any>, negated = false
) {
data.run++;
str2mbl$(str$).subscribe(
actual => {
if (expected === actual) {
markPass1('%cstream has expected marble diagram %o', expected, negated);
} else {
markPass2('%cstream marble diagram %o is NOT the expected %o',
actual, expected, !negated);
}
},
err => {
console.error('Error:', err);
data.error++;
}
);
}
export function assertStreamOf(
expected, stream$: O<any>, negated = false
) {
if (!stream$) { throw('stream is not defined'); };
if (typeof stream$.subscribe !== 'function') {
throw('%s is not a stream', typeof stream$);
}
stream$.subscribe(
actual => {
data.run++;
if (JSON.stringify(expected) === JSON.stringify(actual)) {
markPass1('%cstream contains expected value %o', expected, negated);
} else {
markPass2('%cstream contained %o, NOT the expected value %o',
actual, expected, !negated);
}
},
err => {
data.run++;
console.error('Error:', err);
data.error++;
}
);
}
export function str2mbl$(stream$: O<any>): O<string> {
if (!stream$) { throw('Undefined is not a stream'); };
if (typeof stream$.subscribe !== 'function') {
throw(stream$, 'is not a stream');
}
return stream$
.defaultIfEmpty('')
.reduce((acc, item) => acc + '-' + item.toString())
.map(mbl => '-' + mbl + '|');
}
export function mbl2str$(mbl: string): O<any> {
return O.from(
mbl
.replace(/--+/g, '-')
.replace(/[ |]/g, '')
.replace(/^[ |-]+/, '')
.replace(/[ |-]+$/, '')
.split('-')
.filter(each => each !== '')
.map(each => isNaN(parseInt(each)) ? each : parseInt(each))
);
}
// From: http://staltz.com/unidirectional-user-interface-architectures.html
//
// Introduced as a fully reactive unidirectional architecture based on RxJS
// Os, Model-View-Intent is the primary architectural pattern in the
// framework Cycle.js. The O event stream is a primitive used everywhere,
// and functions over Os are pieces of the architecture.
//
// Parts:
//
// Intent: function from O of user events to O of “actions”
// Model: function from O of actions to O of state
// View: function from O of state to O of rendering
// Custom element: subsection of the rendering which is in itself a UI program.
// May be implemented as MVI, or as a Web Component. Is optional to use in a View.
export function assertMVIComponent(Component) {
const nullModel = (action$, ...rest) => O.of([]);
const nullView = (model$, ...rest) => O.empty();
const nullIntent = (DOMSource, ...rest) => O.of({});
return function() {
test('component provides model, view and intent, each called once', function() {
let visits = {
model: 0,
view: 0,
intent: 0
};
Component({}, model, view, intent);
function model() { visits.model++; return O.of([]); }
function view() { visits.view++; return O.empty(); }
function intent() { visits.intent++; return O.empty(); }
expect(visits).toEqual({model: 1, view: 1, intent: 1});
});
test('intent is function from DOMSource to observable of actions', function() {
const DOM = mockDOMSource(RxJSAdapter, {
'.foo': {
'click': O.of({target: {innerHTML: '123'}})
}
});
Component({DOM}, nullModel, nullView, intent);
function intent(DOMSource, ...rest) {
const rollButton$ = DOMSource.select('.foo').events('click');
expect(rollButton$.map(evt => evt.target.innerHTML)).toBeStreamOf('123');
return O.empty();
}
});
test('model is function from actions$ to observable of models', function() {
['first action', 'second action'].forEach(expected => {
const intent = (sources, ...rest) => O.of(expected);
Component({}, model, nullView, intent);
function model(action$, ...rest) {
expect(action$).toBeStreamOf(expected);
}
});
});
test('view is function from model$ to observable of renderings', function() {
[div('first'), div('second')].forEach(expected => {
const model = (actions$, ...rest) => O.of(expected);
Component({}, model, view, nullIntent);
function view(model$, ...rest) {
expect(model$).toBeStreamOf(expected);
}
});
});
test('component returns object with DOM attribute as stream from view', () => {
const testView = (model$: O<any>) => O.of(div('testDOM'));
expect(Component({}, nullModel, testView, nullIntent).DOM)
.toBeStreamOf(div('testDOM'));
});
};
}