Предложить angular модульный тест для покрытия кода - PullRequest
1 голос
/ 21 февраля 2020
async ngOnInit() {
    if (localStorage.getItem('Authorization') && this.formdataservice.get_account_user_id()) {
      try {
        let res: any = await this.formdataservice.get_preferences().toPromise();
        const { data } = res.body;
        this.formdataservice.set_preferences(data);
      } catch (e) {}
    }
  }

Пожалуйста, предложите юнит-тест для вышеуказанного кода.

1 Ответ

0 голосов
/ 21 февраля 2020
it('should call set_preferences if Authorization is set in local storage and acount_user_id is populated', async(done) => {
  // arrange => you will have to set Authorization in local storage before the first detect changes in the beforeEach block because that's when `ngOnInit` is called. Also, mock formDataService.get_Acount_user_id() to return a truthy value;
 // also have to mock `get_preferences()` to return a promise for res;
 // wait for promises to finish
  await fixture.whenStable(); // this waits for promises to finish
 // assert => expect `set_preferences()` toHaveBeenCalledWith whatever data is. 
  done(); 
});
...