Search code examples
angular6karma-jasmineangular-test

Unable to validate a structure of JSON in a unit test and also the result doesn't match the expected outcome


I am trying to run a unit test for a function which is called when the response of http.post is received.

  handleSuccessResponseForUserProfileRequest(res: HttpSentEvent|HttpHeaderResponse|HttpResponse<any>|HttpProgressEvent|HttpUserEvent<any>) {
    const ev = <HttpEvent<any>>(res);
    if (ev.type === HttpEventType.Response) {
      console.log('response from server: body ',ev.body);
      const isResponseStructureOK: boolean = this.helper.validateServerResponseStructure(ev.body);
      if (isResponseStructureOK) {
        const response: ServerResponseAPI = ev.body;
        this.userProfileSubject.next(new Result(response.result, response['additional-info']));
      } else {
        this.userProfileSubject.next(new Result('error', 'Invalid response structure from server'));
      }
    } else {
    }
  }

validateServerResponseStructure checks that the structure of the response is ok. It should have result and additional-info keys

validateServerResponseStructure(res: any): boolean {
      const keys = Object.keys(res);
      const isTypeCorrect: boolean = (
        ['result', 'additional-info'].every(key => keys.includes(key))
        && keys.length === 2);
      return isTypeCorrect;

  }

The unit case I have written is

fit('should return if user information isn\'t stored', () => {
  const body = JSON.stringify({"result":"success", "additional-info":"some additional info"});
  const receivedHttpEvent = new HttpResponse({body:body});
  const userService: UserManagementService = TestBed.get(UserManagementService);
  const helperService:HelperService = TestBed.get(HelperService);
  spyOn(userService['userProfileSubject'],'next');
  //spyOn(helperService,'validateServerResponseStructure').and.returnValue(true);
  userService.handleSuccessResponseForUserProfileRequest(receivedHttpEvent);
  const expectedResult = new Result('success', 'some additional info');
  expect(userService['userProfileSubject'].next).toHaveBeenCalledWith(expectedResult);

});

If I don't spy on validateServerResponseStructure then my test case fails because validateServerResponseStructure fails even though to me it looks that the structure is ok.

Expected spy next to have been called with [ Result({ result: 'success', additionalInfo: 'some additional info' }) ] but actual calls were [ Result({ result: 'error', additionalInfo: 'Invalid response structure from server' }) ].

If I spy on validateServerResponseStructure and return true then I get error

Expected spy next to have been called with [ Result({ result: 'success', additionalInfo: 'some additional info' }) ] but actual calls were [ Result({ result: undefined, additionalInfo: undefined }) ].

Interestingly, if I add the following two prints then they show different values!!

console.log('extracted response ',response);
        console.log('sending response '+response.result + 'and additional info' +response['additional-info']);

I see

extracted response  {"result":"success","additional-info":"some additional info"}
sending response undefined and additional info undefined

What am I doing wrong?


Solution

  • Interestingly the code works if I change the type <T> in httpResponse in unit test to Object instead of string.

    const body = {"result":"success", "additional-info":"some additional info"};
          const receivedHttpEvent = new HttpResponse({body:body});