Skip to content

Commit 3ce21cd

Browse files
committed
refactor(serializers): use proper ts types for ng-snapshot
1 parent 17605f4 commit 3ce21cd

File tree

1 file changed

+7
-46
lines changed

1 file changed

+7
-46
lines changed

src/serializers/ng-snapshot.ts

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,13 @@
1-
import type { ComponentRef, Type, ɵCssSelectorList } from '@angular/core';
1+
import type { ɵComponentType, ɵDirectiveDef } from '@angular/core';
22
import type { ComponentFixture } from '@angular/core/testing';
3-
import type { Colors } from 'pretty-format';
4-
5-
/**
6-
* The follow interfaces are customized heavily inspired by @angular/core/core.d.ts
7-
*/
8-
interface ComponentDef {
9-
selectors: ɵCssSelectorList;
10-
}
11-
interface IvyComponentType extends Type<unknown> {
12-
ɵcmp: ComponentDef;
13-
}
14-
interface NgComponentRef extends ComponentRef<unknown> {
15-
componentType: IvyComponentType;
16-
_elDef: any; // eslint-disable-line @typescript-eslint/no-explicit-any
17-
_view: any; // eslint-disable-line @typescript-eslint/no-explicit-any
18-
}
19-
interface NgComponentFixture extends ComponentFixture<unknown> {
20-
componentRef: NgComponentRef;
21-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
22-
componentInstance: Record<string, any>;
23-
}
24-
25-
/**
26-
* The following types haven't been exported by jest so temporarily we copy typings from 'pretty-format'
27-
*/
28-
interface PluginOptions {
29-
edgeSpacing: string;
30-
min: boolean;
31-
spacing: string;
32-
}
33-
type Indent = (indentSpaces: string) => string;
34-
type Printer = (elementToSerialize: unknown) => string;
3+
import type { OldPlugin } from 'pretty-format';
354

365
const attributesToRemovePatterns = ['__ngContext__'];
376

38-
const print = (
39-
fixture: NgComponentFixture,
40-
printer: Printer,
41-
indent: Indent,
42-
opts: PluginOptions,
43-
colors: Colors,
44-
): string => {
7+
const print: OldPlugin['print'] = (fixture, printer, indent, opts, colors) => {
458
let componentAttrs = '';
46-
const { componentRef, componentInstance } = fixture;
47-
const componentDef = componentRef.componentType.ɵcmp;
9+
const { componentRef, componentInstance } = fixture as ComponentFixture<Record<string, unknown>>;
10+
const componentDef = (componentRef.componentType as ɵComponentType<unknown>).ɵcmp as ɵDirectiveDef<unknown>;
4811
const componentName = componentDef.selectors[0][0] as string;
4912
const nodes = Array.from(componentRef.location.nativeElement.childNodes).map(printer).join('');
5013
const attributes = Object.keys(componentInstance).filter((key) => !attributesToRemovePatterns.includes(key));
@@ -80,11 +43,9 @@ const print = (
8043
).replace(/\n^\s*\n/gm, '\n');
8144
};
8245

83-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
84-
const test = (val: any): boolean =>
85-
!!val && typeof val === 'object' && Object.prototype.hasOwnProperty.call(val, 'componentRef');
46+
const test: OldPlugin['test'] = (val) => !!val && typeof val === 'object' && 'componentRef' in val;
8647

8748
export = {
8849
print,
8950
test,
90-
};
51+
} as OldPlugin;

0 commit comments

Comments
 (0)