我有一个Angular组件测试(它仅创建组件“ Edit-base-schema”,没有方法调用),在不进行其他测试的情况下可以正常运行。当我用18个组件执行所有18个测试时,相同的组件测试将失败。有趣的是,此组件在构造函数中没有依赖项注入(没有服务),并且ngOnInit方法为空。你有解释吗?
这是组件:
import { Component, OnInit, Input, ViewChild, ElementRef } from
'@angular/core';
import { DimensionTree } from '../../swagger/models/dimension-tree';
import { SchemaWithItems } from '../../swagger/models/schema-with-
items';
import { ZoomData } from '../../svg/svg-zoomable.directive';
import * as d3 from 'd3';
import { environment } from '../../../environments/environment';
@Component({
selector: 'app-edit-base-schema',
templateUrl: './edit-base-schema.component.html',
styleUrls: ['./edit-base-schema.component.scss']
})
export class EditBaseSchemaComponent implements OnInit {
transform: ZoomData = { x: 0, y: 0, k: 1.0 };
@ViewChild('imgSchema') imgSchema: ElementRef;
@ViewChild('svgElement') svgElement: ElementRef;
@ViewChild('svgHoster') svgHoster: ElementRef;
@Input()
schemaId: string;
private _schema: SchemaWithItems;
apiUrl = environment.apiUrl + '/api';
constructor() { }
ngOnInit() {
}
@Input()
get schema(): SchemaWithItems {
return this._schema;
}
set schema(sc: SchemaWithItems) {
this._schema = sc;
if (!sc) {
return;
}
setTimeout(() => {
const svgElement: HTMLElement = this.svgHoster.nativeElement;
const svgElementWidth = svgElement.offsetWidth;
const svgElementHeight = svgElement.offsetHeight;
if (svgElementWidth && svgElementHeight) {
if (sc.imageHeight > svgElementHeight || sc.imageWidth > svgElementWidth) {
const newK = Math.min(svgElementHeight / sc.imageHeight, svgElementWidth / sc.imageWidth);
this.transform = {x: 0, y: 0, k: newK};
}
}
}, 10);
}
onDrop($event: any) {
const isLinkEvent: 'Link' | '' =
$event.event.dataTransfer.getData('text');
// Coordinates
// The coordinates are relative to the image (not the schema, as the schema is not transformed)
const [x, y] = d3.clientPoint(this.imgSchema.nativeElement, $event.event);
if (isLinkEvent === 'Link') {
const data = {
width: 120, height: 80,
locationX: x,
locationY: y,
cssClasses: '',
itemPrefix: null,
fontColor: '#000',
fontFamily: 'Segoe UI', // Windows-only - no problem for empa, and uses browser-default on other systems
fontSize: 12,
itemTemplate: '',
dataNumericId: null,
url: 'https://www.empa.ch/web/empa',
schemaItemId: 0,
openLinkDirectly: true
};
this.schema.items.push(data);
}
if (!isLinkEvent) {
// Dropped $event.element
const treeData = <DimensionTree>$event.element.data;
this.schema.items.push({
width: 120, height: 80,
locationX: x,
locationY: y,
cssClasses: '',
fontColor: '#000',
fontFamily: 'Segoe UI', // Windows-only - no problem for empa, and uses browser-default on other systems
fontSize: 12,
itemPrefix: treeData.itemPrefix,
itemTemplate: '',
schemaItemId: 0
});
}
}
getHeight() {
return window.innerHeight;
}
onItemDelete(item) {
const index = this.schema.items.indexOf(item);
const copy = this.schema.items.concat([]);
copy.splice(index, 1);
this.schema.items = copy; // Set a copy of it as items
}
allowDrop(element) {
// Return true/false based on element
return true;
}
}