-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
componet-bed.ts
44 lines (43 loc) · 1.39 KB
/
componet-bed.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
import { CommonModule } from '@angular/common';
import { DebugElement, ModuleWithProviders, NO_ERRORS_SCHEMA, Provider, Type } from '@angular/core';
import { ComponentFixture, TestBed, TestBedStatic } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
type ComponentDeps = Array<Type<NzSafeAny> | ModuleWithProviders>;
export interface ComponentBed<T> {
bed: TestBedStatic;
fixture: ComponentFixture<T>;
nativeElement: HTMLElement;
debugElement: DebugElement;
component: T;
}
export function createComponentBed<T>(
component: Type<T>,
options: {
providers?: Provider[];
declarations?: ComponentDeps;
imports?: ComponentDeps;
} = {
providers: [],
declarations: [],
imports: []
}
): ComponentBed<T> {
const { imports, declarations, providers } = options;
const config = {
imports: [NoopAnimationsModule, CommonModule, ...(imports || [])],
declarations: [component, ...(declarations || [])],
schemas: [NO_ERRORS_SCHEMA],
providers: providers || []
};
const bed = TestBed.configureTestingModule(config);
const fixture = TestBed.createComponent<T>(component);
fixture.detectChanges();
return {
bed,
fixture,
nativeElement: fixture.nativeElement,
debugElement: fixture.debugElement,
component: fixture.componentInstance
};
}