vendredi 27 juillet 2018

Jest unit test - global variable modified in a function which is being tested is undefined

I'm new to JEST and trying to write unit test cases for a tsx file. I'm trying to test a function which takes in an argument and sets the value of a global variable (x) equal to the argument passed. When I run the test, I get the value as undefined. Here is the code :

Function to be tested

  countrySelectedHandler (selectedCountry) {
    this.selectedCountry = selectedCountry.target.value;
    this.countrySelected.emit(selectedCountry.target.value);
  }

Unit Test

  it('should set the passed argument as selected country', () => {
    let country = { target : { value: '' } } ;
    country.target.value = 'India';
    obj.languageSelectedHandler(country);
    expect(obj.selectedLanguage).toEqual('India');
  });
});

Aucun commentaire:

Enregistrer un commentaire