嗨,我正在尝试为具有可观察性的组件编写角度代码,但无法测试订阅功能。任何帮助,将不胜感激。
这是我的组件,具有可观察的:
import {Component, AfterViewInit, ElementRef, Renderer2, ViewEncapsulation, OnInit, ViewChild, OnDestroy} from '@angular/core';
import {BroadcastService, MsalService} from '@azure/msal-angular';
import {NavigationEnd, NavigationStart, Router, RouterEvent} from '@angular/router';
import {ScrollPanel} from 'primeng/primeng';
import {UserService} from './core/user.service';
import {Subscription} from 'rxjs';
import {RoleService} from './core/role.service';
import {environment} from '../environments/environment';
// import {trigger, state, style, transition, animate, AnimationEvent} from '@angular/animations';
enum MenuOrientation {
STATIC,
OVERLAY,
SLIM,
HORIZONTAL
}
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.sass'],
encapsulation: ViewEncapsulation.None
})
export class AppComponent implements OnInit, AfterViewInit, OnDestroy {
public isLoggedIn = undefined;
public isNavigating = false;
private subscription: Subscription = new Subscription();
private environmentName: string;
private environmentUrl: string;
private loggedInUserRole: string;
constructor(private router: Router, private authService: MsalService, public renderer: Renderer2,
private userService: UserService, private broadcastService: BroadcastService, private roleService: RoleService) {
this.environmentName = environment.environmentName;
this.environmentUrl = environment.serverUrl;
}
ngOnInit(): void {
if (this.authService.getUser() !== null || this.authService.getUser() !== undefined) {
console.log('getUser');
this.userService.userDetails = this.authService.getUser();
}
if (this.environmentName === 'QA' || this.environmentName === 'LOCAL' || this.environmentName === 'QA-STAGING') {
console.log('environmentName');
this.authService.acquireTokenSilent(['api://012fdc3a-c966-4312-9b5c-301f097c1803/server']);
} else {
this.authService.acquireTokenSilent(['api://58a80bb5-906b-4ec0-9b41-7a78a07125af/server']);
}
this.subscription.add(
this.broadcastService.subscribe('msal:acquireTokenSuccess', (payload) => {
// do something here
console.log('acquire token success ' + JSON.stringify(payload));
this.roleService.checkServerEventReviewers().subscribe(res => {
this.userService.userDetails.role = res ? 'Data Steward' : 'Mosaic Consumer';
if (this.isLoggedIn !== true) {
const redirectUri = sessionStorage.getItem('redirectUri');
if (redirectUri !== undefined || redirectUri !== null) {
this.router.navigateByUrl(redirectUri);
}
}
this.isLoggedIn = true;
};
这是我正在尝试的规格文件:
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { Subscription } from 'rxjs';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import { BroadcastService, MsalService } from '@azure/msal-angular';
import { MSAL_CONFIG } from '@azure/msal-angular/dist/msal.service';
import { RoleService } from './core/role.service';
import { WindowService } from './core/window.service';
import { HttpClient, HttpHandler } from '@angular/common/http';
import { environment } from '../environments/environment';
import mockData from '../tests/dummy.json';
describe('AppComponent', () => {
beforeEach(() => {
let subscription: Subscription = new Subscription();
TestBed.configureTestingModule({
imports: [RouterTestingModule],
declarations: [AppComponent],
providers: [WindowService, RoleService, HttpClient, HttpHandler, BroadcastService, MsalService,
{
provide: MSAL_CONFIG, // MsalService needs config, this provides it.
useFactory: () => ({ // Note this is an arrow fn that returns the config object
redirectUri: window.location.origin + '/',
clientID: mockData.clientID,
}),
}],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
});
describe(':', () => {
function setup() {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
const compiled = fixture.debugElement.nativeElement;
return {fixture, app, compiled};
}
it('Should create the app', () => {
const {app} = setup();
expect(app).toBeDefined();
});
it('Init with QA environment', () => {
const {app} = setup();
spyOn(app.authService, 'getUser').and.returnValue(mockData.userDetails);
spyOn(app.authService, 'acquireTokenSilent').and.returnValue('msal:acquireTokenSuccess');
app.ngOnInit();
app.subscription.add(
app.broadcastService.subscribe('msal:acquireTokenSuccess', () => {
// do something here
});
);
我是否尝试正确访问订阅功能。还是我应该做不同的事情?谢谢。
这不是您问题的直接答案,而是更多的间接问题。将所有这些业务逻辑都包含在组件中可能不是一个好主意。我建议使用库来协助您进行所有状态管理(我们使用NgRX)。然后,可以将许多这种逻辑移入服务或其他更易于测试的纯方法。