Search code examples
angularjsjestjsangularjs-scope

How to mock Onclose event in Jest?


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();
                    });