-
Notifications
You must be signed in to change notification settings - Fork 308
/
ng-snapshot.ts
110 lines (100 loc) · 3.62 KB
/
ng-snapshot.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
import type { ComponentRef, DebugNode, Type, ɵCssSelectorList } from '@angular/core';
import type { ComponentFixture } from '@angular/core/testing';
import type { Colors } from 'pretty-format';
/**
* The follow interfaces are customized heavily inspired by @angular/core/core.d.ts
*/
interface ComponentDef {
selectors: ɵCssSelectorList;
}
interface IvyComponentType extends Type<unknown> {
ɵcmp: ComponentDef;
}
interface NgComponentRef extends ComponentRef<unknown> {
componentType: IvyComponentType;
_elDef: any; // eslint-disable-line @typescript-eslint/no-explicit-any
_view: any; // eslint-disable-line @typescript-eslint/no-explicit-any
}
interface NgComponentFixture extends ComponentFixture<unknown> {
componentRef: NgComponentRef;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
componentInstance: Record<string, any>;
}
interface VEDebugNode {
renderElement: {
childNodes: DebugNode[];
};
}
/**
* The following types haven't been exported by jest so temporarily we copy typings from 'pretty-format'
*/
interface PluginOptions {
edgeSpacing: string;
min: boolean;
spacing: string;
}
type Indent = (indentSpaces: string) => string;
type Printer = (elementToSerialize: unknown) => string;
const attributesToRemovePatterns = ['__ngContext__'];
const ivyEnabled = (): boolean => {
// Should be required lazily, since it will throw an exception
// `Cannot resolve parameters...`.
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires
const { ɵivyEnabled } = require('@angular/core');
return ɵivyEnabled;
};
const print = (fixture: unknown, print: Printer, indent: Indent, opts: PluginOptions, colors: Colors): string => {
let nodes = '';
let componentAttrs = '';
let componentName = '';
const componentRef = (fixture as NgComponentFixture).componentRef;
const componentInstance = (fixture as NgComponentFixture).componentInstance;
if (ivyEnabled()) {
const componentDef = componentRef.componentType.ɵcmp;
componentName = componentDef.selectors[0][0] as string;
nodes = Array.from(componentRef.location.nativeElement.childNodes).map(print).join('');
} else {
componentName = componentRef._elDef.element?.name;
nodes = (componentRef._view.nodes ?? [])
// eslint-disable-next-line no-prototype-builtins
.filter((node: VEDebugNode) => node?.hasOwnProperty('renderElement'))
.map((node: VEDebugNode) => Array.from(node.renderElement.childNodes).map(print).join(''))
.join(opts.edgeSpacing);
}
const attributes = Object.keys(componentInstance).filter((key) => !attributesToRemovePatterns.includes(key));
if (attributes.length) {
componentAttrs += attributes
.sort()
.map((attribute) => {
const compAttrVal = componentInstance[attribute];
return (
opts.spacing +
indent(`${colors.prop.open}${attribute}${colors.prop.close}=`) +
colors.value.open +
(compAttrVal && compAttrVal.constructor
? `{[Function ${compAttrVal.constructor.name}]}`
: `"${compAttrVal}"`) +
colors.value.close
);
})
.join('');
}
return (
'<' +
componentName +
componentAttrs +
(componentAttrs.length ? '\n' : '') +
'>\n' +
indent(nodes) +
'\n</' +
componentName +
'>'
).replace(/\n^\s*\n/gm, '\n');
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
const test = (val: any): boolean =>
!!val && typeof val === 'object' && Object.prototype.hasOwnProperty.call(val, 'componentRef');
export = {
print,
test,
};