Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

fix(input): ng-required conflicts with ng-true-value #6065

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ function checkboxInputType(scope, element, attr, ctrl) {

// Override the standard `$isEmpty` because a value of `false` means empty in a checkbox.
ctrl.$isEmpty = function(value) {
return value !== trueValue;
return value !== true;
};

ctrl.$formatters.push(function(value) {
Expand Down
31 changes: 31 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,37 @@ describe('input', function() {
expect(inputElm[0].checked).toBe(false);
expect(inputElm).toBeInvalid();
});

it('should allow custom enumaration even if it is required', function() {
compileInput('<input type="checkbox" ng-model="name" ng-true-value="y" ' +
'ng-false-value="n" ng-required="true">');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also test that the expected behaviour occurs when the field is empty. I also want to see lots of

expect(inputElm).toBeValid(); // or toBeInvalid(); if it makes sense

in this spec.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, now the tests also cover validation status.

scope.$apply(function() {
scope.name = 'y';
});
expect(inputElm[0].checked).toBe(true);
expect(inputElm).toBeValid();

scope.$apply(function() {
scope.name = 'n';
});
expect(inputElm[0].checked).toBe(false);
expect(inputElm).toBeInvalid();

scope.$apply(function() {
scope.name = 'something else';
});
expect(inputElm[0].checked).toBe(false);
expect(inputElm).toBeInvalid();

browserTrigger(inputElm, 'click');
expect(scope.name).toEqual('y');
expect(inputElm).toBeValid();

browserTrigger(inputElm, 'click');
expect(scope.name).toEqual('n');
expect(inputElm).toBeInvalid();
});
});


Expand Down