How to pass a token for jasmine requests

316 Views Asked by At

I implemented my tests in an application without authentication, but when it was implemented my tests stopped working. I have an API to generate a token, how can I put this token in my services inside jasmine?

Below is one of the services that is returning authentication error, it is implemented as follows:

   it('Given_CalledSaveData_When_MonitoringStepIsCalled_Then_toHaveBeenCalled', () => {
       
        const ufs: IUf = {
        };

        const modelBase: IModelBase<IUf[]> = {
        };

        const organizationModel: IOrganizationModel = {
        };

        organizationModel.Lawfirm.Address = {
        };

        const orgData: IModelBase<IOrganizationModel> = {
        };


        spyOn(service, 'getUfs').and.returnValue(of(modelBase));
        spyOn(service, 'getOrganizationData').and.returnValue(of(orgData));

        component.ngOnInit();

        component.saveData();
        expect(service.getUfs).toHaveBeenCalled();

        modelBase.Success = false;

        component.saveData();
        expect(service.getUfs).toHaveBeenCalled();
        
    });

I could mock this part, if so how could I do this?

What about the API that returns me a token would have some way to fix this using it without mocking it?

UPDATE

My ngOnInit:

 updateModel(): boolean {
if (!this.storageService.getIsFirsUse()) {
  this.legaloneService.getOrganizationData().subscribe(m => {
    this.getApiModel(m.Model);
  });
} else {
  this.getApiModel(null);
}
return true;
}

  ngOnInit() {
    this.updateModel();
  }

My SaveData:

  saveData() {
const organizationModel: IOrganizationModel = {
  ManagingUser: {
    Name: this.usuarioPrincipal,
    Email: this.emailUsuarioPrincipal
  },
  Lawfirm: {
    LawfirmName: this.officeName,
    Cnpj: this.cnpj,
    Logo: {
      ExternalId: this.importedFileData ? this.importedFileData.fileName : 'KEEP',
      Url: 'KEEP'
    }
  }
};
if (this.displayAddress) {
  organizationModel.Lawfirm.Address = {
    ZipCode: this.cep,
    Street: this.street,
    StateId: this.selectedUf ? this.selectedUf.Id : null,
    State: this.selectedUf ? this.selectedUf.Value : null,
    CityId: this.selectedCity ? this.selectedCity.Id : null,
    City: this.selectedCity ? this.selectedCity.Value : null,
    Number: this.number,
    Neighborhood: this.neighborhood,
    AdditionalInformation: this.complement
  };
}


return new Promise<boolean>((resolve, reject) => {
  this.legaloneService.saveOrganizationData(organizationModel).subscribe(m => {
    if (!m.Success) {
      const mapFiels: { [id: string]: string } = {};
      mapFiels['managinguser.name'] = 'Usuáro principal';
      mapFiels['managinguser.email'] = 'Email do usuário principal';
      mapFiels['lawfirm.lawfirmname'] = 'Nome do escritório';
      mapFiels['lawfirm.cnpj'] = 'CNPJ';
      mapFiels['lawfirm.address.city'] = 'Cidade';
      this.errorHandlingServiceService.displayErrorFromAPII(m, mapFiels);
    } else {
      this.storageService.setFirstUse();
    }
    resolve(m.Success);
  },
    this.errorHandlingServiceService.cathError
  );
});
}
0

There are 0 best solutions below