jeudi 4 février 2021

test-failure in ng test: "Cannot call Promise.then from within a sync test"

I've read several of the posts in here which describe the issue concerning Promise.then from within a sync test. The solutions didn't work for me.

In my components constructor the Store is injected. Due to this i use provideMockStore to supply a provider for the store in the test. Perhaps this is part of the cause why the test doesn't run?

You can see in my code-snippet, that the initialization is moved to the beforeEach-Blocks. I removed all tests. When i run ng test, i still get the issue, which states that Promise.then can't be called in a sync test (see ->) screenshot.

I'm an angular beginner and don't understand the internals of the test-run. Is there something else what i could do but moving my initialization to the beforeEach-blocks?

Here is my component:

@Component({
  selector: 'kvl-leistungs-zuordnung-edit',
  templateUrl: './leistungs-zuordnung-edit.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class LeistungsZuordnungEditComponent extends MasksUtil implements OnInit, OnDestroy {

  @Input() dokumentTypSchluessel: number;
  @Input() belegRechnungsBetrag: number;
  @Input() bearbeitungsAufrufRootId: string;
  @Input() leistungsZuordnung: LeistungsZuordnung;
  @Input() moeglicheZahlungsInformationen: ZahlungsInformation[] = [];

  @Output() readonly erstattungsPositionenInBearbeitungEvent = new EventEmitter<boolean>();
  @Output() readonly leistungsZuordnungChangedEvent = new EventEmitter<LeistungsZuordnung[]>();

  leistungsZuordnungsFormArray = new FormArray([]);
  tempFormArrayValues: LeistungsZuordnung[];

  betragEingabeLimit: number;

  zwischengespeicherteDaten: any;
  betragAusgleichsControl: FormControl;
  ausgangsBetragFuerAusgleich: number;
  aufteilungImGange: boolean;

  readonly kundenTextMaxLaenge = STD_LAENGE_KUNDENTEXT;
  erstattungsEntscheidungsDisplayPipe = new ErstattungsEntscheidungsDiplayPipe();

  tarifLeistungszeitraumKombinationen$: Observable<TarifLeistungszeitraumKombination[]>;
  leistungsBereichAuswahlen$: Observable<SchluesselFormatierbar[]>[] = [];
  erstattungsArtAuswahl$: Observable<SchluesselFormatierbar[]>;
  erstattungsGrundAuswahlen$: Observable<SchluesselFormatierbar[]>[][] = [];

  betragMaskSettings = BETRAG_MASK_SETTINGS;

  beleg: Beleg;

  constructor(private readonly schluesselService: SchluesselService,
              private readonly tarifLeistungsKombinationService: TarifService,
              private readonly belegStore: Store<BelegState>,
              readonly modalService: ModalService<number>) {
    super();
  }

  ngOnInit(): void {

    this.belegStore
      .select(selectBeleg)
      .pipe(
        distinctUntilChanged(),
        untilComponentDestroyed(this)
      ).subscribe(beleg => {
      this.beleg = beleg;
    });

    this.addLeistungsZuordnungToForm(this.leistungsZuordnung, 0);
    this.ladeTarifLeistungszeitraumKombinationen();
    this.erstattungsArtAuswahl$ = this.schluesselService.loadErstattungsArten();

    if (this.erstattungsEntscheidungenAufgeteilt(0)) {
      this.editLeistungsZuordnung(0);
    } else {
      this.editErstattungsEntscheidung(0, 0);
    }

    this.leistungsZuordnungsFormArray.valueChanges
      .pipe(debounceTime(STD_DEBOUNCE_TIME),
        untilComponentDestroyed(this))
      .subscribe(() => {
        if (this.aenderungenVorhanden()) {
          const leistungsZuordnungen = this.leistungsZuordnungsFormArray.getRawValue();
          leistungsZuordnungen.forEach(lz => {
            lz.zahlungsInformation = this.moeglicheZahlungsInformationen
              .find(info => info.zahlungsEmpfaengerPaRefNr === lz.zahlungsEmpfaengerPaRefNr);
          });
          this.leistungsZuordnungChangedEvent.emit(leistungsZuordnungen);
        }
      });

  }
....

Here is the tests-code:

  describe('LeistungsZuordnungEditComponent', () => {
  let component: LeistungsZuordnungEditComponent;
  let fixture: ComponentFixture<LeistungsZuordnungEditComponent>;
  let beleg: Beleg;
  let ausgangsLeistungsZuordnungsBetrag: number;
  let neueLeistungsZuordnungsBetrag: number;
  let zahlungsInfo: ZahlungsInformation;
  let erstattungsEntscheidng: ErstattungsEntscheidung;
  let leistungsZuordnung: LeistungsZuordnung;

  beforeEach(waitForAsync(() => {

    beleg = {
      vsnr: 4711,
      personNummer: 1,
      behandlungsDatumBis: new Date(),
      behandlungsDatumVon: new Date(),
      id: '4711',
      veraenderungsZeitpunkt: new Date()
    };

    zahlungsInfo = { id: '123', zahlungsEmpfaengerPaRefNr: '456' };
    ausgangsLeistungsZuordnungsBetrag = 100;
    erstattungsEntscheidng  = { id: '', veraenderungsZeitpunkt: undefined };

    leistungsZuordnung = {
      id: '123',
      zahlungsInformation: zahlungsInfo,
      veraenderungsZeitpunkt: '',
      bemessungsGrundlageBetrag: ausgangsLeistungsZuordnungsBetrag,
      erstattungsEntscheidungen: [erstattungsEntscheidng]
    };

    neueLeistungsZuordnungsBetrag = 40;

    TestBed.configureTestingModule({
      declarations: [
        LeistungsZuordnungEditComponent,
        LeistungsBereichDisplayPipe,
        ErstattungsArtDisplayPipe,
        ErstattungsGrundDisplayPipe,
        ZahlungsEmpfaengerDisplayPipe,
        MaxValueDirective,
        MaxLengthDirective
      ],
      imports: [
        ReactiveFormsModule,
        HttpClientTestingModule,
        MaskedInputModule,
        InputDropdownModule,
        ModalModule,
        LvmFormModule,
        ArticleModule,
        InputGroupModule,
        ButtonModule
      ],
      providers: [
        FormService,
        provideMockStore({
          initialState:
            {
              beleg: {
                beleg: beleg,
                isLoading: false,
                isSaving: false,
                isLoadingVersichertePerson: false,
                versichertePersonen: 1,
                vsnr: 4711
              }
            }
        }),
      ]
    })
      .compileComponents();
  })); 

  beforeEach(waitForAsync(() => {
    fixture = TestBed.createComponent(LeistungsZuordnungEditComponent);
    component = fixture.componentInstance;
    component.leistungsZuordnung = leistungsZuordnung;
    component.dokumentTypSchluessel = 1090;
    fixture.detectChanges();
  })
);
...

Greets!

Aucun commentaire:

Enregistrer un commentaire