mardi 13 juillet 2021

How should I write out the unit testing if statement for this if statement for these query params?

.ts file:

ngOnInit(): void {
    if (
      this.activatedRoute.snapshot.queryParamMap.has('esign') && 
      this.activatedRoute.snapshot.queryParamMap.has('registration')
    ) {
      this.eSignAndRegistration = true;
    }

.spec.ts file:

  let esign_registration = false;
  const mockActivatedRoute = {
    snapshot: {
      queryParamMap: {
        has: (response) => {
          if (response == 'esign' && response == 'registration') {
            return esign_registration;
          }

test:

  fit("should show the 1st mat-card if the param is 'esign&registration'", () => {
    esign_registration = true;
    fixture.detectChanges();
    component.ngOnInit();
    fixture.detectChanges();
    const compiled = fixture.nativeElement;
    expect(component.eSignAndRegistration).toBeTruthy();
  });

The error keeps coming as "Expected undefined to be truthy." How does the 'has' if statement need to be written to get the unit test to work? I have tried

if (response == 'esign' && 'registration')

and

if (response == 'esign&registration')

and whole variety of other methods, but they all kept giving the same error.

Aucun commentaire:

Enregistrer un commentaire