how to test $parsers.push and $formatters.push on angular with jasmine

Viewed 3327

I cannot force angular to fire $parsers.push when updating the input so, having this directive with the above test, how do you fire the $parsers.push ?

mainApp.directive('amountConverter', function() {
  return {
    restrict: 'A',
    require: 'ngModel',
    link: function(scope, element, attrs, ngModelController) {
      var isInt = function(value) {
          return !isNaN(parseInt(value, 10)) && (parseFloat(value, 10) == parseInt(value, 10));
        },
        convert = function(initial, multiplier, text) {
          var amount = text.replace(initial, "");
          if (!isInt(amount)) {
            throw " Amount not a number";
          }

          return amount * multiplier;
        };

      //convert data from view format to model format
      ngModelController.$parsers.push(function(value) {
        if (value === null || value === undefined) {
          return null;
        }

        var text = value.toUpperCase();
        var amount = 0;
        var initial = text.substring(text.length - 1);

        if (initial === "B") {
          amount = convert(initial, 1000000000, text);
        } else if (initial === "M") {
          amount = convert(initial, 1000000, text);
        } else if (initial === "K") {
          amount = convert(initial, 1000, text);
        } else if (initial === "T") {
          amount = convert(initial, 1000, text);
        } else {
          return value;
        }

        element[0].value = amount;

        return amount; //converted
      });
    }
  };
});

    describe('Directives', function () {

        var element, scope;

        beforeEach(module('mainApp'));

        beforeEach(inject(function ($compile, $rootScope) {
            scope = $rootScope;
            element = angular.element('<input type="text" data-ng-model="Amount" amount-Converter/>');
            $compile(element)(scope);
                    scope.$digest();
    }));

    //////Jasmine Test
    describe('amountConverter', function () {                  

        it('should return change element state after click to be visible', function () {
                        element.scope().Amount = '10k';
                        element.scope().$apply();
                        expect(element[0].value).toBe('10000');

                    });
                });
            });
2 Answers
Related