gpt4 book ai didi

angular - 使用 jasmine spy 测试 cdk 覆盖

转载 作者:行者123 更新时间:2023-12-03 08:53:11 25 4
gpt4 key购买 nike

与测试 mat-dialog 类似,测试 cdk 覆盖的好方法是什么?

MatDialog Service Unit Test Angular 6 Error

示例服务调用:

openNotifications(origin: HTMLElement) {
this.refreshNotifications()
.subscribe();
const overlayRef = this.overlay.create({
positionStrategy: this.overlay.position().flexibleConnectedTo(origin).withPositions([
{
originX: 'center',
originY: 'bottom',
overlayX: 'center',
overlayY: 'top',
}
]),
hasBackdrop: true,
});
const portal = new ComponentPortal(NotificationsDropdownComponent);
overlayRef.attach(portal);
overlayRef.backdropClick().subscribe(() => {
overlayRef.detach();
this.refreshNotificationCount().subscribe();
});
}

最佳答案

我创建了一个overlaySpyHelper,本质上我们仍然使用Overlay服务,但是我们监视create方法并返回一个模拟overlayRef

import {TestBed} from '@angular/core/testing';

import {NotificationsService} from './notifications.service';
import {UserNotificationsService} from '../swag-api/services';
import {Overlay, OverlayModule} from '@angular/cdk/overlay';
import {of} from 'rxjs';
import {UserNotificationsResponse} from '../swag-api/models';




describe('Given NotificationsService', () => {
let notificationsService: NotificationsService;
let userNotificationsServiceSpy: jasmine.SpyObj<UserNotificationsService>;
let overlayService: Overlay;

beforeEach(() => {
const spy1 = jasmine.createSpyObj('UserNotificationsService',
{
UserNotificationsGetCount: of({count: 5}),
UserNotificationsGetAll: of(getMockNotifications())
});
TestBed.configureTestingModule({
imports: [
OverlayModule,
],
providers: [
NotificationsService,
{provide: UserNotificationsService, useValue: spy1},
],
});

notificationsService = TestBed.get(NotificationsService);
userNotificationsServiceSpy = TestBed.get(UserNotificationsService);
overlayService = TestBed.get(Overlay);
});

fdescribe('When openNotifications', () => {
let createSpy;
let overlayRefSpyObj;
let simulateClickBackdropEvent;

beforeEach(() => {
({createSpy, overlayRefSpyObj, simulateClickBackdropEvent} = overlaySpyHelper(overlayService));
expect(userNotificationsServiceSpy.UserNotificationsGetAll.calls.count()).toBe(0);
notificationsService.openNotifications(null);
});

it('overlay created', () => {
expect(createSpy).toHaveBeenCalled();
});

it('then notifications refreshed', () => {
expect(userNotificationsServiceSpy.UserNotificationsGetAll.calls.count()).toBe(1);
});

it('then overlay opened', () => {
// overlayRef.attach();
expect(overlayRefSpyObj.attach).toHaveBeenCalled();
expect(overlayRefSpyObj.backdropClick().subscribe).toHaveBeenCalled();
expect(overlayRefSpyObj.detach).not.toHaveBeenCalled();
});

it('When backdrop clicked, close overlay and refresh count', () => {
expect(userNotificationsServiceSpy.UserNotificationsGetCount.calls.count()).toBe(0);
simulateClickBackdropEvent.callback();
expect(overlayRefSpyObj.detach).toHaveBeenCalled();
expect(userNotificationsServiceSpy.UserNotificationsGetCount.calls.count()).toBe(1);
});

});
});

function overlaySpyHelper(overlayService) {
const simulateClickBackdropEvent = {callback: null};
const overlayRefSpyObj = jasmine.createSpyObj({
attach: null,
backdropClick: jasmine.createSpyObj({subscribe: null}),
detach: null,
});
const createSpy = spyOn(overlayService, 'create').and.returnValue(overlayRefSpyObj);
overlayRefSpyObj.backdropClick().subscribe.and.callFake((cb) => {
simulateClickBackdropEvent.callback = cb;
});
return {createSpy, overlayRefSpyObj, simulateClickBackdropEvent};
}

关于angular - 使用 jasmine spy 测试 cdk 覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57582302/

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