gpt4 book ai didi

angular - 如何对@viewChild ElementRef Angular 进行单元测试

转载 作者:行者123 更新时间:2023-12-04 11:35:46 24 4
gpt4 key购买 nike

我的组件文件有以下代码

@ViewChild('clusterCard', { static: false }) clusterCard: ElementRef;



highLightTheClusterCard(point: PickupClusterPoint) {
if (point) {
const card: HTMLElement = _get(this.clusterCard, 'nativeElement');
const selectedPoint: PositioningPoint = this.lane.data.selectedPostioningPoint;

/* if card is available, position point of the card and the cluster is same and infowindow over the cluster is open then
highlight the card */
if (card && _isEqual(point.pointData, selectedPoint) && point.openInfoWindow) {
card.scrollIntoView();
card['style'].borderLeft = `5px solid ${this.lane.data.color || 'transparent'}`;
} else {
card['style'].borderLeft = `5px solid transparent`;
}
}
}

ngAfterViewChecked(): void {
...some code

this.pickupClusterPointsService.positioningPoint$
.pipe(skip(1), takeUntil(this.unsubscriber.done))
.subscribe((point: PickupClusterPoint) => {
this.highLightTheClusterCard(point);
});
}
HTML文件
    <div #clusterCard>
<pickup-cluster-stop-card
..some code
>
</pickup-cluster-stop-card>
</div>
我想对 highLightTheClusterCard 方法进行单元测试。我正进入(状态

TypeError: Cannot read property 'pipe' of undefinederror properties

and TypeError: Cannot set property 'borderLeft' of undefinedat


单元测试文件
  beforeEach(() => {

...some code

fixture = TestBed.createComponent(RouteLaneComponent);
component = fixture.componentInstance;

....some code

fixture.detectChanges();
});

fdescribe('highLightTheClusterCard', () => {
it('should expect cluster card to be defined ', () => {
// component.clusterCard.nativeElement = new HTMLElement();
component.clusterCard = new ElementRef({ nativeElement: jasmine.createSpyObj('nativeElement', ['scrollIntoView', 'style'])});
component.highLightTheClusterCard({ pointData: new PositioningPoint(), openInfoWindow: true} as PickupClusterPoint);
// expect(component.clusterCard).toBeDefined();
// expect(component.clusterCard.nativeElement.scrollIntoView).toHaveBeenCalled();
});
});
我读了这个 How to mock a nativeElement.focus() in Angular 4 spec file
但是,我仍然无法使它变绿。
  MockService(PickupClusterPointsService, {
...more code
positioningPoint$: of(undefined),
}),
解决方案:我已添加 positioningPoint$: of(undefined)在模拟服务中。 MockService 位于 Provider 内部。你可以看到上面的线条。
describe('highLightTheClusterCard', () => {
it('should expect cluster card to be highlighted when hover over infowindow ', () => {
component.lane.data.selectedPostioningPoint = new PositioningPoint();
component.lane.data.color = '#2196F3';
component.clusterCard = {
nativeElement: jasmine.createSpyObj('nativeElement', ['scrollIntoView', 'style'])
};

component.highLightTheClusterCard({ pointData: new PositioningPoint(), openInfoWindow: true} as PickupClusterPoint);

expect(component.clusterCard).toBeDefined();
expect(component.clusterCard.nativeElement.scrollIntoView).toHaveBeenCalled();
expect(component.clusterCard.nativeElement.style.borderLeft).toBe('5px solid #2196F3');
});
it('should expect cluster card not to be highlighted when hover out from the infowindow', () => {
component.lane.data.selectedPostioningPoint = new PositioningPoint();
component.clusterCard = {
nativeElement: jasmine.createSpyObj('nativeElement', ['scrollIntoView', 'style'])
};
component.highLightTheClusterCard({ pointData: new PositioningPoint(), openInfoWindow: false} as PickupClusterPoint);

expect(component.clusterCard).toBeDefined();
expect(component.clusterCard.nativeElement.style.borderLeft).toBe('5px solid transparent');
});
});

最佳答案

你似乎有几个问题。
你的第一个错误

TypeError: Cannot read property 'pipe' of undefined error properties


来自您没有正确实例化您的pickupClusterPointsService 服务。
第二个错误

TypeError: Cannot set property 'borderLeft' of undefined at


我还不确定

关于angular - 如何对@viewChild ElementRef Angular 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65053473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com