MatrixValidationSpec.js 1.8 KB
/**
 * Created by Techniv on 12/01/2017.
 */

describe('matrix-validation Spec', function(){

    var $compile, $rootScope, $timeout, template;

    beforeEach(module('matrixValidation'));
    beforeEach(inject(function (_$compile_, _$rootScope_, _$timeout_) {
        $compile = _$compile_;
        $rootScope = _$rootScope_;
        $timeout = _$timeout_;
    }));
    beforeEach(function () {
        template = $compile(
           '<form name="mForm" matrix-validator data-all="validator">\
                <span ng-repeat="row in matrix" ng-init="rowId = $index">\
                    <input name="i{{rowId}}{{$index}}" ng-repeat="item in row track by $index" data-row="rowId" data-col="$index" ng-model="row[$index]">\
                </span>\
            </form>');
    });

    beforeEach(function () {
        jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;
    });

    it('test', function(done){

        var $scope = $rootScope.$new();
        $scope.matrix = [
            [1,2,3],
            [2,3,1],
            [3,1,2]
        ];
        $scope.validator = validator;

        var element = template($scope);
        $scope.$digest();

        $timeout.flush();

        var inputs = element.find('input');

        for(var i = 0; i < inputs.length; i++){
            var input = angular.element(inputs[i]);
            var scope = input.scope();
            var name = inputs[i].name;
            var ngInput = scope.mForm[name];
            expect(ngInput.$valid).toEqual(validator.last[scope.rowId][scope.$index]);
        }

        var input = angular.element(inputs[6]);
        input.val(1);
        input.triggerHandler('change');

        setTimeout(function () {
            expect(scope.mForm.i20.$valid).toBe(false);
            expect(scope.mForm.i00.$valid).toBe(false);
            done();
        },500);

    });
});