简体   繁体   English

angular 中 router.events.subscribe 的单元测试

[英]unit test for router.events.subscribe in angular

I am subscribing to router.events to set the value of variable.我订阅 router.events 来设置变量的值。 Which is used in my service named getLastUpdatedTime.在我的名为 getLastUpdatedTime 的服务中使用。 I want to write unit test for the same.我想为此编写单元测试。 My service returns an object.我的服务返回 object。

  lastUpdatedSubcription: Subscription;
  hours: string;
  days: string;

  constructor(
    public router: Router,
    public activatedRoute: ActivatedRoute,
    public utilService: UtilService) {
      this.routerEvent();
  }

 /**
  * @description Update the last updated hours in footer
  */
  routerEvent() {
    this.router.events.subscribe(event => {
      let page: string;
      if (event instanceof NavigationEnd) {
        if (event.urlAfterRedirects === '/account/region/detail' || event.urlAfterRedirects === '/account/currency/detail') {
          page = 'accountDetail';
        } else {
          page = 'account';
        }
        this.lastUpdatedSubcription = this.utilService.getLastUpdatedTime(url.getLastUpdatedTime, page)
          .subscribe((lastUpdatedTime: LastUpdatedTime) => {
            this.hours = lastUpdatedTime.hours;
            this.days = lastUpdatedTime.days;
          });
      }
    });
  }```
import { CommonModule } from '@angular/common';
import { HttpClientModule } from '@angular/common/http';
import { RouterTestingModule } from '@angular/router/testing';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { SharedModule } from 'src/app/shared/shared.module';
import { NavigationEnd, Router } from '@angular/router';
import { UtilService } from 'src/app/shared/services';
import { of, Observable } from 'rxjs';
import { ConsoleReporter } from 'jasmine';

fdescribe('NxAccountComponent', () => {
  let component: NxAccountComponent;
  let fixture: ComponentFixture<NxAccountComponent>;
  let util: UtilService;

  class MockRouter {
    public ne = new NavigationEnd(0, 'account/region/detail', '/account/region/detail');
    public events = new Observable(observer => {
      console.log('in class ob');
      observer.next(this.ne);
      observer.complete();
    });
  }


  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [NxAccountComponent],
      imports: [
        HttpClientModule,
        CommonModule,
        SharedModule,
        BrowserAnimationsModule,
        RouterTestingModule,
        NxAccountRoutingModule
      ],
      providers: [NxAccountService, UtilService,
        { provide: RouterTestingModule, useClass: MockRouter}]
    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(NxAccountComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    util = TestBed.get(UtilService);
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('test', () => {
    const mockData = {
      hours: '1',
      days: '2'
    };
    spyOn(util, 'getLastUpdatedTime').and.returnValue(of(mockData));
    component.routerEvent();
    expect(component.hours).toEqual(mockData.hours);
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM