Skip to content

Commit

Permalink
add required directive support and test case
Browse files Browse the repository at this point in the history
  • Loading branch information
lordfriend committed Nov 8, 2014
1 parent 1b5d770 commit 1914c7d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/nya-bs-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ nyaBsSelect.directive('nyaBsSelect', ['$parse', '$document', '$timeout', functio
// for debug
nyaBsSelectCtrl.setId($element.attr('id'));

// required validator
if (isMultiple) {
ngCtrl.$isEmpty = function(value) {
return !value || value.length === 0;
};
}

var valueExpFn,
valueExpGetter = $parse(nyaBsSelectCtrl.valueExp);
if(nyaBsSelectCtrl.valueExp) {
Expand Down Expand Up @@ -454,4 +461,4 @@ nyaBsSelect.directive('nyaBsSelect', ['$parse', '$document', '$timeout', functio
};
}
};
}]);
}]);
70 changes: 67 additions & 3 deletions test/spec/dynamic-feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,13 @@ describe('Features contains ngModel, option auto generate, etc;', function() {
'</ol>');
$compile(selectElement)($scope);


// we don't mock user click in this unit test. we will test user interaction in e2e test.

$scope.model = changeModel($scope.options);

$scope.$digest();

var list = selectElement.find('ul').children(),
index,
length = list.length,
Expand All @@ -232,7 +235,7 @@ describe('Features contains ngModel, option auto generate, etc;', function() {
if(liElement.hasClass('nya-bs-option')) {
scopeOfOption = liElement.scope();
value = scopeOfOption.option.name;
if(indexOfWithProperty({name: value}, $scope.model, ['name'])) {
if(indexOfWithProperty({name: value}, $scope.model, ['name']) !== -1) {
expect(liElement).toHaveClass('selected');
} else {
expect(liElement).not.toHaveClass('selected');
Expand All @@ -251,6 +254,8 @@ describe('Features contains ngModel, option auto generate, etc;', function() {
'</ol>');
$compile(selectElement)($scope);

$scope.$digest();

var list = selectElement.find('ul').children(),
index,
length = list.length,
Expand All @@ -267,16 +272,75 @@ describe('Features contains ngModel, option auto generate, etc;', function() {
if(!lastGroup || group != lastGroup) {
// this is the first item in the group. should have class "first-in-group"

expect(liElement).hasClass('first-in-group');
expect(liElement).toHaveClass('first-in-group');
} else {

// otherwise, shouldn't have that class.
expect(liElement).not.hasClass('first-in-group');
expect(liElement).not.toHaveClass('first-in-group');
}
lastGroup = group;
}
}

});

it('should work with ngRequired and required directive in single selection mode', function() {
$scope.options = changeGroups(options);

// single selection
var formElement = angular.element('<form name="testForm">' +
'<ol class="nya-bs-select" ng-model="model" required>' +
'<li nya-bs-option="option in options group by option.group">' +
'<a>{{option.name}}</a>' +
'</li>' +
'</ol>' +
'</form>');
$compile(formElement)($scope);

var selectElement = formElement.children();
$scope.$digest();

// should have an invalid class
expect(selectElement).toHaveClass('ng-invalid-required');
expect(selectElement).not.toHaveClass('ng-valid-required');

// should have an valid class

$scope.model = $scope.options[0];

$scope.$digest();

expect(selectElement).toHaveClass('ng-valid-required');
expect(selectElement).not.toHaveClass('ng-invalid-required');
});

it('should work with ngRequired and required directive in multiple selection mode', function() {
$scope.options = changeGroups(options);
$scope.model = [];
// single selection
var formElement = angular.element('<form name="testForm">' +
'<ol class="nya-bs-select" ng-model="model" required multiple>' +
'<li nya-bs-option="option in options group by option.group">' +
'<a>{{option.name}}</a>' +
'</li>' +
'</ol>' +
'</form>');
$compile(formElement)($scope);

var selectElement = formElement.children();
$scope.$digest();

// should have an invalid class
expect(selectElement).toHaveClass('ng-invalid-required');
expect(selectElement).not.toHaveClass('ng-valid-required');

// should have an valid class

$scope.model = changeModel($scope.options);

$scope.$digest();

expect(selectElement).toHaveClass('ng-valid-required');
expect(selectElement).not.toHaveClass('ng-invalid-required');
});
});

0 comments on commit 1914c7d

Please sign in to comment.