Angular Проверка переключателя в наблюдаемом - PullRequest
0 голосов
/ 28 апреля 2020

Я новичок в Angular и пытаюсь выяснить, как проверить оператор switch внутри наблюдаемого при тестировании жасмина. Есть какие-нибудь мысли?

Любая помощь была бы замечательной.

private getConditionTypes(): void {

this.listDataService.getConditionTypes().subscribe(data => {

  data.forEach(ct => {
    switch (ct.code) {
      case 'LT':
        ct.symbol = '<';
        break;
      case 'GT':
        ct.symbol = '>';
        break;
      case 'ET':
        ct.symbol = '=';
        break;
      case 'LTE':
        ct.symbol = '<=';
        break;
      case 'GTE':
        ct.symbol = '>=';
        break;
    }
  });
  this.listDataStore.updateConditionTypes(data);
});

}

1 Ответ

0 голосов
/ 28 апреля 2020

Просто следуйте документам:

function asyncData<T>(data: T) {
  return defer(() => Promise.resolve(data));
}

describe('Example test', () => {
  let component: MyComponent;
  let fixture: ComponentFixture<MyComponent>;

  let getConditionTypesSpy: any;

  beforeEach(() => {

    const fakeListDataService = jasmine.createSpyObj('ListDataService', ['getConditionTypes']);

    getConditionTypesSpy = fakeListDataService.getConditionTypes.and.returnValue('>');

    TestBed.configureTestingModule({
      declarations: [ MyComponent ],
      providers:    [
        { provide: ListDataService, useValue: fakeListDataService }
      ]
    });

    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
    fixture.detectChanges()
  });

  it('should test greater then in switch', fakeAsync(() => {

    // call getConditionTypes (it'll call the observable)
    component.getConditionTypes();

    fixture.detectChanges();

    tick();

    // check whatever you want


  }));
});

...