|
1 |
| -import { Component, NgModule } from '@angular/core'; |
| 1 | +import { Component, NgModule, Type } from '@angular/core'; |
2 | 2 | import { TestBed, ComponentFixture } from '@angular/core/testing';
|
3 | 3 | import { By } from '@angular/platform-browser';
|
4 | 4 | import { getQueriesForElement, prettyDOM } from 'dom-testing-library';
|
5 | 5 |
|
| 6 | +import { Options, Result, ComponentInput } from './models'; |
| 7 | + |
6 | 8 | @Component({ selector: 'test-component', template: '' })
|
7 | 9 | class TestComponent {}
|
8 | 10 |
|
9 |
| -export async function createComponent( |
10 |
| - template: string, |
11 |
| - { detectChanges = true, declarations = [], providers = [], imports = [], schemas = [] } = {}, |
12 |
| -): Promise<{ |
13 |
| - container: any; |
14 |
| - get: (token: any, notFoundValue?: any) => any; |
15 |
| - getComponentInstance: <T>(selector: string) => T; |
16 |
| - debug: () => void; |
17 |
| - detectChanges: (checkNoChanges?: boolean) => void; |
18 |
| - fixture: any; |
19 |
| - queryByPlaceholderText: any; |
20 |
| - queryAllByPlaceholderText: any; |
21 |
| - getByPlaceholderText: any; |
22 |
| - getAllByPlaceholderText: any; |
23 |
| - queryByText: any; |
24 |
| - queryAllByText: any; |
25 |
| - getByText: any; |
26 |
| - getAllByText: any; |
27 |
| - queryByLabelText: any; |
28 |
| - queryAllByLabelText: any; |
29 |
| - getByLabelText: any; |
30 |
| - getAllByLabelText: any; |
31 |
| - queryByAltText: any; |
32 |
| - queryAllByAltText: any; |
33 |
| - getByAltText: any; |
34 |
| - getAllByAltText: any; |
35 |
| - queryByTestId: any; |
36 |
| - queryAllByTestId: any; |
37 |
| - getByTestId: any; |
38 |
| - getAllByTestId: any; |
39 |
| - queryByTitle: any; |
40 |
| - queryAllByTitle: any; |
41 |
| - getByTitle: any; |
42 |
| - getAllByTitle: any; |
43 |
| - queryByValue: any; |
44 |
| - queryAllByValue: any; |
45 |
| - getByValue: any; |
46 |
| - getAllByValue: any; |
47 |
| -}> { |
| 11 | +export async function createComponent(template: string, options: Options): Promise<Result>; |
| 12 | +export async function createComponent<T>(component: ComponentInput<T>, options: Options): Promise<Result>; |
| 13 | +export async function createComponent<T>( |
| 14 | + templateOrComponent: string | ComponentInput<T>, |
| 15 | + { detectChanges = true, declarations = [], providers = [], imports = [], schemas = [] }: Options, |
| 16 | +): Promise<Result> { |
| 17 | + const isTemplate = typeof templateOrComponent === 'string'; |
| 18 | + const extraDeclarations = isTemplate ? [TestComponent] : []; |
| 19 | + |
48 | 20 | TestBed.configureTestingModule({
|
49 |
| - declarations: [TestComponent, ...declarations], |
| 21 | + declarations: [...declarations, ...extraDeclarations], |
50 | 22 | providers: [...providers],
|
51 | 23 | imports: [...imports],
|
52 | 24 | schemas: [...schemas],
|
53 | 25 | });
|
54 | 26 |
|
55 |
| - TestBed.overrideComponent(TestComponent, { |
56 |
| - set: { |
57 |
| - template, |
58 |
| - }, |
59 |
| - }); |
| 27 | + if (isTemplate) { |
| 28 | + TestBed.overrideComponent(TestComponent, { |
| 29 | + set: { |
| 30 | + template: <string>templateOrComponent, |
| 31 | + }, |
| 32 | + }); |
| 33 | + } |
60 | 34 |
|
61 | 35 | await TestBed.compileComponents();
|
62 | 36 |
|
63 |
| - const fixture = TestBed.createComponent(TestComponent); |
64 |
| - if (detectChanges) { |
65 |
| - fixture.detectChanges(); |
| 37 | + let fixture; |
| 38 | + if (isTemplate) { |
| 39 | + fixture = TestBed.createComponent(TestComponent); |
| 40 | + if (detectChanges) { |
| 41 | + fixture.detectChanges(); |
| 42 | + } |
| 43 | + } else { |
| 44 | + const { component, parameters = [] } = <ComponentInput<T>>templateOrComponent; |
| 45 | + fixture = TestBed.createComponent(component); |
| 46 | + for (const key of Object.keys(parameters)) { |
| 47 | + fixture.componentInstance[key] = parameters[key]; |
| 48 | + } |
66 | 49 | }
|
67 | 50 |
|
68 | 51 | // Currently this isn't perfect because the typings from dom-testing-library are for TS 2.8
|
69 | 52 | return {
|
70 | 53 | fixture,
|
71 | 54 | container: fixture.nativeElement,
|
72 | 55 | get: TestBed.get,
|
73 |
| - getComponentInstance: <T>(selector: string) => fixture.debugElement.query(By.css(selector)).componentInstance as T, |
| 56 | + getComponentInstance: <C>(selectorOrComponent: string | C) => |
| 57 | + typeof selectorOrComponent === 'string' |
| 58 | + ? fixture.debugElement.query(By.css(selectorOrComponent)).componentInstance |
| 59 | + : fixture.componentInstance, |
74 | 60 | debug: () => console.log(prettyDOM(fixture.nativeElement)),
|
75 | 61 | detectChanges: (checkNoChanges?: boolean) => fixture.detectChanges(checkNoChanges),
|
76 | 62 | ...getQueriesForElement(fixture.nativeElement),
|
|
0 commit comments