Testing branches jasmine angular

2019-08-28 05:00发布

问题:

I want to test following function:

     ngOnInit() {
        this.hmDataService.loadData().subscribe((data) => {
          this.description= data.description;
          this.date= data.date;
          const restoredID= this.localStorage.getItem('key');
          if (restoredID) {
            this.type = restoredID.toString();
            this.selectedMenu= this.option.filter((rf: any) => {
              return rf.value === restoredID;
            })[0];
          }
          this.createTable(this.id, this.data, this.type);
        });
  }

I need it for the test coverage which expect from me to test the branches.

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

    import { ChartComponent } from './chart.component';

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

      beforeEach(async(() => {
        TestBed.configureTestingModule({
          declarations: [ ChartComponent ]
        })
        .compileComponents();
      }));

      beforeEach(() => {
        fixture = TestBed.createComponent(ChartComponent);
        component = fixture.componentInstance;

      });

      it('should create', () => {
        expect(component).toBeTruthy();
      });
    it('should create table after page refresh', fakeAsync(() => {
      const restoredID = 'porsche';
      const spy = spyOn(component, 'createTableStructure');
      fixture.detectChanges();
      expect(component.selectedPaper.value).toEqual('porsche');
      expect(spy).toHaveBeenCalledTimes(1);
      }));
     });

How can I test it in right way? I need to test the branch and my restoreID variable is a function variable not global. The current test is not working