-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
component-bed.ts
44 lines (42 loc) · 1.41 KB
/
component-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
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
import { CommonModule } from '@angular/common';
import { DebugElement, NgModule, NO_ERRORS_SCHEMA, Type } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
type ComponentBedOptions = Pick<NgModule, 'providers' | 'declarations' | 'imports'>;
export interface ComponentBed<T> {
bed: TestBed;
fixture: ComponentFixture<T>;
nativeElement: HTMLElement;
debugElement: DebugElement;
component: T;
}
export function createComponentBed<T>(
component: Type<T>,
options: ComponentBedOptions = {
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
};
}