-
Notifications
You must be signed in to change notification settings - Fork 309
/
AngularSnapshotSerializer.js
67 lines (59 loc) · 1.56 KB
/
AngularSnapshotSerializer.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
'use strict';
const printAttributes = (val, attributes, print, indent, colors, opts) => {
return attributes
.sort()
.map(attribute => {
return (
opts.spacing +
indent(colors.prop.open + attribute + colors.prop.close + '=') +
colors.value.open +
(val.componentInstance[attribute] &&
val.componentInstance[attribute].constructor
? '{[Function ' +
val.componentInstance[attribute].constructor.name +
']}'
: `"${val.componentInstance[attribute]}"`) +
colors.value.close
);
})
.join('');
};
const print = (val, print, indent, opts, colors) => {
let componentAttrs = '';
const componentName = val.componentRef._elDef.element.name;
const nodes = (val.componentRef._view.nodes || [])
.filter(node => node && node.hasOwnProperty('renderElement'))
.map(node => Array.from(node.renderElement.childNodes).map(print).join(''))
.join(opts.edgeSpacing);
const attributes = Object.keys(val.componentInstance);
if (attributes.length) {
componentAttrs += printAttributes(
val,
attributes,
print,
indent,
colors,
opts
);
}
return (
'<' +
componentName +
componentAttrs +
(componentAttrs.length ? '\n' : '') +
'>\n' +
indent(nodes) +
'\n</' +
componentName +
'>'
);
};
const test = val =>
val !== undefined &&
val !== null &&
typeof val === 'object' &&
Object.prototype.hasOwnProperty.call(val, 'componentRef');
module.exports = {
print: print,
test: test
};