Javascript 如何使用jasmine在ngOnInit中测试settimeout函数

Javascript 如何使用jasmine在ngOnInit中测试settimeout函数,javascript,angular,typescript,karma-jasmine,Javascript,Angular,Typescript,Karma Jasmine,我需要为一个内部有setTimeout()调用的ngOnInit函数编写一个测试,但是我找不到应该怎么做我是jasmine测试用例的新手,请帮助 app.component.ts: ngOnInit(): void { if(sessionStorage.getItem("login") === "loggedIN"){ this.isNav = true; this.isUserLogged = false;

我需要为一个内部有setTimeout()调用的ngOnInit函数编写一个测试,但是我找不到应该怎么做我是jasmine测试用例的新手,请帮助

app.component.ts:

ngOnInit(): void {
     if(sessionStorage.getItem("login") === "loggedIN"){
      this.isNav = true;
      this.isUserLogged = false;
      this.loginservice.isLoggedIn = true;
      setTimeout(()=>{
        const isHidden = this.eleRef.nativeElement.querySelector(".header-nav-items");
        isHidden.removeAttribute("hidden");
        this.eleRef.nativeElement.querySelector(".]navigation").classList.remove("theme-black");
        this.eleRef.nativeElement.querySelector(".horizontal-right").classList.remove("profile");
        this.eleRef.nativeElement.querySelector(".horizontal-right").classList.remove("theme-black");
      },500);
     
     }

    }
app.component.spec.ts:

describe('AppComponent', () => {
  let component: AppComponent;
  let fixture: ComponentFixture<AppComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [ RouterTestingModule ],
      schemas: [ CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA ],
      declarations: [ AppComponent ]
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(AppComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });
description('AppComponent',()=>{
let组件:AppComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
导入:[RouterTestingModule],
模式:[自定义元素模式,无错误模式],
声明:[AppComponent]
}).compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(AppComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它('应该创建应用',()=>{
const fixture=TestBed.createComponent(AppComponent);
常量app=fixture.componentInstance;
expect(app.toBeTruthy();
});
我可以在这里继续写。

考虑一下代码

const isHidden = this.eleRef.nativeElement.querySelector(".header-nav-items");
isHidden.removeAttribute("hidden");
this.eleRef.nativeElement.querySelector(".]navigation").classList.remove("theme-black");
this.eleRef.nativeElement.querySelector(".horizontal-right").classList.remove("profile");
this.eleRef.nativeElement.querySelector(".horizontal-right").classList.remove("theme-black");
这段代码只是简单地切换类,因此我们可以利用类绑定来实现这一点

在TS文件中,我们将继续使用

ngOnInit(): void {
  if(sessionStorage.getItem("login") === "loggedIN") {
    this.isNav = true;
    this.isUserLogged = false;
    this.loginservice.isLoggedIn = true;
  }
}
我们可以进一步改进上述代码以

ngOnInit(): void {
  this.isNav = sessionStorage.getItem("login") === "loggedIN";
  this.isUserLogged = !this.isNav;
  this.loginservice.isLoggedIn = this.isNav;
}
在HTML中

  <div class='header-nav-items' *ngIf='!isNav'></div>
  <div class='navigation' [class.theme-black]='!isNav'></div>
  <div class='horizontal-right'[class.profile]='!isNav' [class.theme-black]='!isNav'></div>


上面可能没有回答测试setTimeout函数的问题,但这是实现上述测试的更好方法

为此添加角度标记我认为更好的方法是取消setTimeout函数,并将其中的代码移动到
AfterViewInit
生命周期hook@OwenKelvin,我试过使用Afterview但它只能通过添加settimeout来工作,否则可以通过使用afterviewinit发布代码来显示方法