|
1 | | -import type { ComponentRef, Type, ɵCssSelectorList } from '@angular/core'; |
| 1 | +import type { ɵComponentType, ɵDirectiveDef } from '@angular/core'; |
2 | 2 | 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'; |
35 | 4 |
|
36 | 5 | const attributesToRemovePatterns = ['__ngContext__']; |
37 | 6 |
|
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) => { |
45 | 8 | 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>; |
48 | 11 | const componentName = componentDef.selectors[0][0] as string; |
49 | 12 | const nodes = Array.from(componentRef.location.nativeElement.childNodes).map(printer).join(''); |
50 | 13 | const attributes = Object.keys(componentInstance).filter((key) => !attributesToRemovePatterns.includes(key)); |
@@ -80,11 +43,9 @@ const print = ( |
80 | 43 | ).replace(/\n^\s*\n/gm, '\n'); |
81 | 44 | }; |
82 | 45 |
|
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; |
86 | 47 |
|
87 | 48 | export = { |
88 | 49 | print, |
89 | 50 | test, |
90 | | -}; |
| 51 | +} as OldPlugin; |
0 commit comments