How to mock Onclose event in Jest?

Issue

I am trying to test below code with Jest framework and AngularJS 1.8.

 $(".classA").timepicker({
      showButtonPanel: true,
      timeFormat: "hh:mm",
      onClose: function () {
          $scope.varA=1;
      }
 });

My jest test case is like below:

 describe('when scope.someMethod() is called ', function(){
        test('Should check varA is 1 when scope.someMethod() is called' , function(){
            $.fn.timepicker =  ({}) => {onClose: callback => {callback();}};
            scope.someMethod();
            expect(scope.varA).toEqual(1);
        });
 });

But when I debug the test case, the variable is not set. Please provide suggestions.

Edit:

also tried with, but could not solve.

  $.fn.timepicker =  jest.fn((obj) => {
                return {
                    onClose: callback => {
                        callback();
                    }
                }});

Solution

This worked for me.

   $.fn.timepicker =  jest.fn((obj) => {
                return obj.onClose();
                });

Answered By – prem30488

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published