3

I need to test one of services method and I'm not sure about how to achieve 100% code coverage for parts of code that run as result of calling some injected service method.

Service Method to be tested:

@Injectable()
export class BomRevisiosnsService {
  constructor(
    private baseService: BaseService,
    private appConstants: AppConstants,
    private dmConstants: DMConstants
  ) { }

  public getRevisionsData(): any {
    var itemId = this.appConstants.userPreferences.modelData['basicDetails']['itemId'];
    let url = this.dmConstants.URLs.GETBOMREVISIONS + itemId + "/GetRevisionsAsync";
    let headers = {
      "Content-Type": "application/json",
      UserExecutionContext: JSON.stringify(this.appConstants.userPreferences.UserBasicDetails),
    }
    if (itemId != null || itemId != undefined) {
      return this.baseService.getData(url, headers)
        .map(response => {
          // this area not getting covered.
          return response;
        });
    }
  }
}

How do I cover :

.map(response => {
  return response;
});

In the following:

Test:

it('should return response if itemId is not null or undefined', () => {
  let mockData = [1,2,3];
  let mockObservable = Observable.of(mockData);
  spyOn(baseService, 'getData').and.returnValue(mockObservable);
  appConstants.userPreferences.modelData['basicDetails']['itemId']=4; // itemId not null or undefined
  dMConstants.URLs.GETBOMREVISIONS ="dummy url";
  subject.getRevisionsData();
  expect(baseService.getData).toBe(mockData); // How do I test .map()?
});
Xesenix
  • 2,418
  • 15
  • 30
SamuraiJack
  • 5,131
  • 15
  • 89
  • 195
  • 1
    You can create a function where you pass the response data and test it later. With spyOn you can make sure it was called. – Alexander_F Jun 19 '19 at 12:31
  • Counter question - do you really think this needs testing? That seems about as testing the basic functionality of a operator like `+`. Although if you really want to check if the basic identity function used for mapping is a basic identity function, then why not separate it and write a test for that function alone? – VLAZ Jun 19 '19 at 12:48
  • @VLAZ No I don't think it will add any value to the unit testing. But I wanted to figure it out just out of curiosity. – SamuraiJack Jun 19 '19 at 13:33
  • Ever figure this out? I'm running up against this. What I'll do if make the map action into a method and test it separately. Not ideal because that method should be private but to test has to be made public (I believe). – eflat Oct 26 '19 at 19:38
  • @eflat check out the marked answer – SamuraiJack Oct 27 '19 at 08:57

1 Answers1

-1
it('should return response if itemId is not null or undefined', () => {
  ...
  const spy = spyOn(baseService.getData).and.return(mockedData)// you need to define data returned by baseService.getData
  const result = subject.getRevisionsData();
  expect(spy).toHaveBeenCalled(); // How do I test .map()?
  expect(result).toEqual(mockedData);// for simple map it will be the same thing that is returned for more complicated map it may differ
});
Xesenix
  • 2,418
  • 15
  • 30