1

component.js

import template from './app-profile-image-uploader-dialog.html';

class ProfileImageUploaderDialogController {
  constructor() {
    'ngInject';
    this.name = 'appProfileImageUploaderDialog';
  }

  close() {
    this.dismiss({ $value: 'cancel' });
  }

  crop(croppedImage) {
    this.close({ $value: croppedImage });
  }
}

export const ProfileImageUploaderDialog = {
  selector: 'appProfileImageUploaderDialog',
  template: template,
  controller: ProfileImageUploaderDialogController,
  controllerAs: 'vm',
  bindings: {
    close: '&',
    dismiss: '&'
  }
};

component.html

<app-profile-image-uploader close-fn="vm.close()" crop-fn="vm.crop(croppedImage)"></app-profile-image-uploader>

component.spec.js

import {ProfileImageUploaderDialog} from "../../../../app/features/common/components/app-profile-image-uploader-dialog/app-profile-image-uploader-dialog.component";
import 'angular-mocks';


describe('appProfileImageUploaderDialog', () => {

  const  bindings = {
    close: jasmine.createSpy('close'),
    dismiss: jasmine.createSpy('dismiss')
  };

  beforeEach(() => {
    angular.mock.module('innoApp');
  });

  let $componentController;

  describe('appProfileImageUploaderDialog controller tests', () => {

    let component;
    beforeEach(inject((_$componentController_) => {
      $componentController = _$componentController_;
    }));

    beforeEach(() => {
      component = $componentController(ProfileImageUploaderDialog.selector, {}, bindings);
    });

    it('should have set the parameters ', () => {
      expect(component.name).toBe('appProfileImageUploaderDialog');
    });

    it('should close the modal window if close button is pressed', (() => {
      component.close();
      expect(bindings.dismiss).toHaveBeenCalledWith({ $value: 'cancel'});
    }));

    it('should close the modal window if close button is pressed', (() => {
      let croppedImage = '';
      component.crop(croppedImage);
      expect(bindings.close).toHaveBeenCalledWith({ $value:  croppedImage});
    }));
  });
});

In this test one spec is failing. Can not figure out why.

it('should close the modal window if close button is pressed', (() => {
      component.close();
      expect(bindings.dismiss).toHaveBeenCalledWith({ $value: 'cancel'});
    }));

This is the error I am getting.

Expected spy dismiss to have been called with [ Object({ $value: 'cancel' }) ] but it was never called.
5
  • Add a console.log() in your function close() to see if it is indeed called when running the test Commented Jun 7, 2018 at 9:18
  • It is not called. But perfectly works when the application runs. But not in the unit test. Commented Jun 7, 2018 at 9:24
  • And the following test calling component.crop(croppedImage) works fine? Commented Jun 7, 2018 at 9:47
  • Yes it's working fine Commented Jun 7, 2018 at 9:47
  • This is odd ... wild guess here, maybe the let component; should be outside the describe, next to let $componentController;? I think it has something to do with component instantiation at that point in time Commented Jun 7, 2018 at 9:52

1 Answer 1

1

I found the solution. At the unit test run time component.close() referes to the function which comes from the bindings. Both function names are same in here. One coming from the bindings and the function in the component.

Following changes solved the issue. Changed close() to closeModal()

closeModal() {
    this.dismiss({ $value: 'cancel' });
  }

<app-profile-image-uploader close-fn="vm.closeModal()" crop-fn="vm.crop(croppedImage)"></app-profile-image-uploader>



it('should close the modal window if close button is pressed', (() => {
      component.closeModal();
      expect(bindings.dismiss).toHaveBeenCalledWith({ $value: 'cancel'});
    }));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.