-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathhelpers.ts
98 lines (85 loc) · 3.2 KB
/
helpers.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// see https://github.com/angular/flex-layout/blob/master/src/lib/utils/testing/helpers.ts
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Type, DebugElement } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { extendObject } from './object-extend'; // tslint:disable-line:no-unused-variable
export type ComponentClazzFn = () => Type<any>;
/**
* Function generator that captures a Component Type accessor and enables
* `expectTemplate( )` to be reusable for *any* captured Component class.
*
* NOTE: These Generators (aka Partial Functions) are used only in
* the Karma/Jasmine testing.
*/
export function makeExpectDOMFrom(getClass: ComponentClazzFn) {
let createTestComponent;
// Return actual `expectTemplate()` function
return function expectTemplate(template: string, key?: string, value?: any): any {
if (!createTestComponent) {
createTestComponent = makeCreateTestComponent(getClass);
}
let fixture = createTestComponent(template);
if (key) {
let instance = fixture.componentInstance;
instance[key] = value;
}
fixture.detectChanges();
return expectNativeEl(fixture);
};
}
/**
* Function generator that captures a Component Type accessor and enables
* `createTestComponent( )` to be reusable for *any* captured Component class.
*/
export function makeCreateTestComponent(getClass: ComponentClazzFn) {
let componentAny: Type<any>;
// Return actual `createTestComponent()` function
return function createTestComponent(template: string, styles?: any): ComponentFixture<Type<any>> {
if (!componentAny) {
// Defer access to Component class to enable metadata to be configured properly...
componentAny = getClass();
}
return TestBed.overrideComponent(componentAny, {
set: {
template: template,
styles: styles || []
}
}).createComponent(componentAny);
};
}
/**
*
*/
export function expectNativeEl(fixture: ComponentFixture<any>, instanceOptions?: any): any {
extendObject(fixture.componentInstance, instanceOptions || {});
fixture.detectChanges();
return expect(fixture.debugElement.children[0].nativeElement);
}
/**
* With the specified Component Type and template,
* create a component and perform a CSS query to find the nativeElement
* associated with that query selector.
*/
export function makeExpectDOMForQuery(getClass: ComponentClazzFn) {
let createTestComponent;
// Return actual `expectTemplate()` function
return function expectDomForQuery(template: string, selector: string, index = 0): any {
if (!createTestComponent) {
createTestComponent = makeCreateTestComponent(getClass);
}
let fixture = createTestComponent(template);
fixture.detectChanges();
let nodes = queryFor(fixture, selector);
return nodes.length > index ? expect(nodes[index].nativeElement) : null;
};
}
export function queryFor(fixture: ComponentFixture<any>, selector: string): DebugElement[] {
return fixture.debugElement.queryAll(By.css(selector));
}