-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathevent-bus.service.handler.ts
More file actions
76 lines (62 loc) · 2.28 KB
/
event-bus.service.handler.ts
File metadata and controls
76 lines (62 loc) · 2.28 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
// in actual implementation replace public api reference with 'jasmine-unit-test-generator'
import { ParsedClassDependency, ClassOptions, DependencyHandler, defaultDependencyHandler, DependencyHandlerOptions } from '../../../src/public-api';
export default {
run(result: ClassOptions, dep: ParsedClassDependency, options: DependencyHandlerOptions) {
defaultDependencyHandler.run(result, dep, options);
const usedEvents = findUsedEvents(dep, options);
if (usedEvents.length) {
const eventNames: string[] = [];
usedEvents.forEach((ev, index) => {
const name = eventNames[index] = ev.substr(0, 1).toLowerCase() + ev.substr(1).replace(/<.*>/, '') + 'Subject';
result.declarations.push({
name,
type: `ReplaySubject<${ev}>`
});
result.initializers.push({
name,
value: `new ReplaySubject<${ev}>(1)`
});
// find import for this event
const eventImportPath = options.allImports.find(v => v.names.includes(ev));
if (eventImportPath) { // should never be false
result.imports.push({
names: [ev],
path: eventImportPath.path
});
}
});
// these will be deduped
result.imports.push({
names: ['ReplaySubject'],
path: 'rxjs'
})
// warning, strange formatting is intentional to create correct output!
result.initializers.push({
value:
`${options.variableName}.of.and.callFake((ev) => {
${usedEvents.map((ev, index) => {
return ` ${index === 0 ? 'if' : '} else if'} (ev === ${ev}) {
return ${eventNames[index]};
`;
}).join('')} } else {
throw new Error('event:' + ev + ' not mocked');
}
})`
});
}
},
test(dep: ParsedClassDependency) {
return dep.type === 'EventBusService';
}
} as DependencyHandler;
function findUsedEvents(dep: ParsedClassDependency, options: DependencyHandlerOptions): string[] {
const result: string[] = [];
const regex = new RegExp(`${dep.name}\.of\\\(([^)]*)\\\)`, 'g');
let matches: RegExpExecArray | null;
while (matches = regex.exec(options.sourceCode)) {
if (result.indexOf(matches[1]) === -1) {
result.push(decodeURIComponent(matches[1]));
}
}
return result;
}