diff options
Diffstat (limited to 'test/widget')
| -rw-r--r-- | test/widget/formSpec.js | 205 | ||||
| -rw-r--r-- | test/widget/inputSpec.js | 961 | ||||
| -rw-r--r-- | test/widget/selectSpec.js | 809 |
3 files changed, 0 insertions, 1975 deletions
diff --git a/test/widget/formSpec.js b/test/widget/formSpec.js deleted file mode 100644 index 6387241f..00000000 --- a/test/widget/formSpec.js +++ /dev/null @@ -1,205 +0,0 @@ -'use strict'; - -describe('form', function() { - var doc, widget, scope, $compile; - - beforeEach(module(function($compileProvider) { - $compileProvider.directive('storeModelCtrl', function() { - return { - require: 'ngModel', - link: function(scope, elm, attr, ctrl) { - widget = ctrl; - } - }; - }); - })); - - beforeEach(inject(function($injector) { - $compile = $injector.get('$compile'); - scope = $injector.get('$rootScope'); - })); - - afterEach(function() { - dealoc(doc); - }); - - - it('should instantiate form and attach it to DOM', function() { - doc = $compile('<form>')(scope); - expect(doc.data('$form')).toBeTruthy(); - expect(doc.data('$form') instanceof FormController).toBe(true); - }); - - - it('should remove the widget when element removed', function() { - doc = $compile( - '<form name="form">' + - '<input type="text" name="alias" ng:model="value" store-model-ctrl/>' + - '</form>')(scope); - - var form = scope.form; - widget.setValidity('REQUIRED', false); - expect(form.alias).toBe(widget); - expect(form.error.REQUIRED).toEqual([widget]); - - doc.find('input').remove(); - expect(form.error.REQUIRED).toBeUndefined(); - expect(form.alias).toBeUndefined(); - }); - - - it('should prevent form submission', function() { - var startingUrl = '' + window.location; - doc = jqLite('<form name="myForm"><input type="submit" value="submit" />'); - $compile(doc)(scope); - - browserTrigger(doc.find('input')); - waitsFor( - function() { return true; }, - 'let browser breath, so that the form submision can manifest itself', 10); - - runs(function() { - expect('' + window.location).toEqual(startingUrl); - }); - }); - - - it('should not prevent form submission if action attribute present', function() { - var callback = jasmine.createSpy('submit').andCallFake(function(event) { - expect(event.isDefaultPrevented()).toBe(false); - event.preventDefault(); - }); - - doc = $compile('<form name="x" action="some.py" />')(scope); - doc.bind('submit', callback); - - browserTrigger(doc, 'submit'); - expect(callback).toHaveBeenCalledOnce(); - }); - - - it('should publish form to scope', function() { - doc = $compile('<form name="myForm"></form>')(scope); - expect(scope.myForm).toBeTruthy(); - expect(doc.data('$form')).toBeTruthy(); - expect(doc.data('$form')).toEqual(scope.myForm); - }); - - - it('should allow name to be an expression', function() { - doc = $compile('<form name="obj.myForm"></form>')(scope); - - expect(scope.obj).toBeDefined(); - expect(scope.obj.myForm).toBeTruthy(); - }); - - - it('should chain nested forms', function() { - doc = jqLite( - '<ng:form name="parent">' + - '<ng:form name="child">' + - '<input type="text" ng:model="text" name="text">' + - '</ng:form>' + - '</ng:form>'); - $compile(doc)(scope); - - var parent = scope.parent; - var child = scope.child; - var input = child.text; - - input.setValidity('MyError', false); - expect(parent.error.MyError).toEqual([input]); - expect(child.error.MyError).toEqual([input]); - - input.setValidity('MyError', true); - expect(parent.error.MyError).toBeUndefined(); - expect(child.error.MyError).toBeUndefined(); - }); - - - it('should chain nested forms in repeater', function() { - doc = jqLite( - '<ng:form name=parent>' + - '<ng:form ng:repeat="f in forms" name=child>' + - '<input type=text ng:model=text name=text>' + - '</ng:form>' + - '</ng:form>'); - $compile(doc)(scope); - - scope.$apply(function() { - scope.forms = [1]; - }); - - var parent = scope.parent; - var child = doc.find('input').scope().child; - var input = child.text; - - expect(parent).toBeDefined(); - expect(child).toBeDefined(); - expect(input).toBeDefined(); - - input.setValidity('myRule', false); - expect(input.error.myRule).toEqual(true); - expect(child.error.myRule).toEqual([input]); - expect(parent.error.myRule).toEqual([input]); - - input.setValidity('myRule', true); - expect(parent.error.myRule).toBeUndefined(); - expect(child.error.myRule).toBeUndefined(); - }); - - - it('should publish widgets', function() { - doc = jqLite('<form name="form"><input type="text" name="w1" ng:model="some" /></form>'); - $compile(doc)(scope); - - var widget = scope.form.w1; - expect(widget).toBeDefined(); - expect(widget.pristine).toBe(true); - expect(widget.dirty).toBe(false); - expect(widget.valid).toBe(true); - expect(widget.invalid).toBe(false); - }); - - - describe('validation', function() { - - beforeEach(function() { - doc = $compile( - '<form name="form">' + - '<input type="text" ng:model="name" name="name" store-model-ctrl/>' + - '</form>')(scope); - - scope.$digest(); - }); - - - it('should have ng-valid/ng-invalid css class', function() { - expect(doc).toBeValid(); - - widget.setValidity('ERROR', false); - scope.$apply(); - expect(doc).toBeInvalid(); - - widget.setValidity('ANOTHER', false); - scope.$apply(); - - widget.setValidity('ERROR', true); - scope.$apply(); - expect(doc).toBeInvalid(); - - widget.setValidity('ANOTHER', true); - scope.$apply(); - expect(doc).toBeValid(); - }); - - - it('should have ng-pristine/ng-dirty css class', function() { - expect(doc).toBePristine(); - - widget.touch(); - scope.$apply(); - expect(doc).toBeDirty(); - }); - }); -}); diff --git a/test/widget/inputSpec.js b/test/widget/inputSpec.js deleted file mode 100644 index d9f6b7b3..00000000 --- a/test/widget/inputSpec.js +++ /dev/null @@ -1,961 +0,0 @@ -'use strict'; - -describe('NgModelController', function() { - var ctrl, scope, ngModelAccessor; - - beforeEach(inject(function($rootScope, $controller) { - scope = $rootScope; - ngModelAccessor = jasmine.createSpy('ngModel accessor'); - ctrl = $controller(NgModelController, {$scope: scope, ngModel: ngModelAccessor}); - - // mock accessor (locals) - ngModelAccessor.andCallFake(function(val) { - if (isDefined(val)) scope.value = val; - return scope.value; - }); - })); - - - it('should init the properties', function() { - expect(ctrl.dirty).toBe(false); - expect(ctrl.pristine).toBe(true); - expect(ctrl.valid).toBe(true); - expect(ctrl.invalid).toBe(false); - - expect(ctrl.viewValue).toBeDefined(); - expect(ctrl.modelValue).toBeDefined(); - - expect(ctrl.formatters).toEqual([]); - expect(ctrl.parsers).toEqual([]); - }); - - - describe('touch', function() { - it('should only fire $viewTouch when pristine', function() { - var spy = jasmine.createSpy('$viewTouch'); - scope.$on('$viewTouch', spy); - - ctrl.touch(); - expect(ctrl.pristine).toBe(false); - expect(ctrl.dirty).toBe(true); - expect(spy).toHaveBeenCalledOnce(); - - spy.reset(); - ctrl.touch(); - expect(ctrl.pristine).toBe(false); - expect(ctrl.dirty).toBe(true); - expect(spy).not.toHaveBeenCalled(); - }); - }); - - - describe('setValidity', function() { - - it('should emit $invalid only when $valid', function() { - var spy = jasmine.createSpy('$invalid'); - scope.$on('$invalid', spy); - - ctrl.setValidity('ERROR', false); - expect(spy).toHaveBeenCalledOnce(); - - spy.reset(); - ctrl.setValidity('ERROR', false); - expect(spy).not.toHaveBeenCalled(); - }); - - - it('should set and unset the error', function() { - ctrl.setValidity('REQUIRED', false); - expect(ctrl.error.REQUIRED).toBe(true); - - ctrl.setValidity('REQUIRED', true); - expect(ctrl.error.REQUIRED).toBeUndefined(); - }); - - - it('should set valid/invalid', function() { - ctrl.setValidity('FIRST', false); - expect(ctrl.valid).toBe(false); - expect(ctrl.invalid).toBe(true); - - ctrl.setValidity('SECOND', false); - expect(ctrl.valid).toBe(false); - expect(ctrl.invalid).toBe(true); - - ctrl.setValidity('SECOND', true); - expect(ctrl.valid).toBe(false); - expect(ctrl.invalid).toBe(true); - - ctrl.setValidity('FIRST', true); - expect(ctrl.valid).toBe(true); - expect(ctrl.invalid).toBe(false); - }); - - - it('should emit $valid only when $invalid', function() { - var spy = jasmine.createSpy('$valid'); - scope.$on('$valid', spy); - - ctrl.setValidity('ERROR', true); - expect(spy).not.toHaveBeenCalled(); - - ctrl.setValidity('ERROR', false); - ctrl.setValidity('ERROR', true); - expect(spy).toHaveBeenCalledOnce(); - }); - }); - - - describe('view -> model', function() { - - it('should set the value to $viewValue', function() { - ctrl.read('some-val'); - expect(ctrl.viewValue).toBe('some-val'); - }); - - - it('should pipeline all registered parsers and set result to $modelValue', function() { - var log = []; - - ctrl.parsers.push(function(value) { - log.push(value); - return value + '-a'; - }); - - ctrl.parsers.push(function(value) { - log.push(value); - return value + '-b'; - }); - - ctrl.read('init'); - expect(log).toEqual(['init', 'init-a']); - expect(ctrl.modelValue).toBe('init-a-b'); - }); - - - it('should fire $viewChange only if value changed and is valid', function() { - var spy = jasmine.createSpy('$viewChange'); - scope.$on('$viewChange', spy); - - ctrl.read('val'); - expect(spy).toHaveBeenCalledOnce(); - spy.reset(); - - // invalid - ctrl.parsers.push(function() {return undefined;}); - ctrl.read('val'); - expect(spy).not.toHaveBeenCalled(); - }); - }); - - - describe('model -> view', function() { - - it('should set the value to $modelValue', function() { - scope.$apply(function() { - scope.value = 10; - }); - expect(ctrl.modelValue).toBe(10); - }); - - - it('should pipeline all registered formatters in reversed order and set result to $viewValue', - function() { - var log = []; - - ctrl.formatters.unshift(function(value) { - log.push(value); - return value + 2; - }); - - ctrl.formatters.unshift(function(value) { - log.push(value); - return value + ''; - }); - - scope.$apply(function() { - scope.value = 3; - }); - expect(log).toEqual([3, 5]); - expect(ctrl.viewValue).toBe('5'); - }); - - - it('should $render only if value changed and is valid', function() { - spyOn(ctrl, 'render'); - - scope.$apply(function() { - scope.value= 3; - }); - expect(ctrl.render).toHaveBeenCalledOnce(); - ctrl.render.reset(); - - // invalid - ctrl.formatters.push(function() {return undefined;}); - scope.$apply(function() { - scope.value= 5; - }); - expect(ctrl.render).not.toHaveBeenCalled(); - }); - }); -}); - -describe('ng:model', function() { - - it('should set css classes (ng-valid, ng-invalid, ng-pristine, ng-dirty)', - inject(function($compile, $rootScope) { - var element = $compile('<input type="email" ng:model="value" />')($rootScope); - - $rootScope.$digest(); - expect(element).toBeValid(); - expect(element).toBePristine(); - - $rootScope.$apply(function() { - $rootScope.value = 'invalid-email'; - }); - expect(element).toBeInvalid(); - expect(element).toBePristine(); - - element.val('invalid-again'); - browserTrigger(element, 'blur'); - expect(element).toBeInvalid(); - expect(element).toBeDirty(); - - element.val('vojta@google.com'); - browserTrigger(element, 'blur'); - expect(element).toBeValid(); - expect(element).toBeDirty(); - - dealoc(element); - })); -}); - - -describe('input', function() { - var formElm, inputElm, scope, $compile; - - function compileInput(inputHtml) { - formElm = jqLite('<form name="form">' + inputHtml + '</form>'); - inputElm = formElm.find('input'); - $compile(formElm)(scope); - } - - function changeInputValueTo(value) { - inputElm.val(value); - browserTrigger(inputElm, 'blur'); - } - - beforeEach(inject(function($injector) { - $compile = $injector.get('$compile'); - scope = $injector.get('$rootScope'); - })); - - afterEach(function() { - dealoc(formElm); - }); - - - it('should bind to a model', function() { - compileInput('<input type="text" ng:model="name" name="alias" ng:change="change()" />'); - - scope.$apply(function() { - scope.name = 'misko'; - }); - - expect(inputElm.val()).toBe('misko'); - }); - - - it('should call $destroy on element remove', function() { - compileInput('<input type="text" ng:model="name" name="alias" ng:change="change()" />'); - - var spy = jasmine.createSpy('on destroy'); - scope.$on('$destroy', spy); - - inputElm.remove(); - expect(spy).toHaveBeenCalled(); - }); - - - it('should update the model on "blur" event', function() { - compileInput('<input type="text" ng:model="name" name="alias" ng:change="change()" />'); - - changeInputValueTo('adam'); - expect(scope.name).toEqual('adam'); - }); - - - it('should update the model and trim the value', function() { - compileInput('<input type="text" ng:model="name" name="alias" ng:change="change()" />'); - - changeInputValueTo(' a '); - expect(scope.name).toEqual('a'); - }); - - - it('should allow complex reference binding', function() { - compileInput('<input type="text" ng:model="obj[\'abc\'].name"/>'); - - scope.$apply(function() { - scope.obj = { abc: { name: 'Misko'} }; - }); - expect(inputElm.val()).toEqual('Misko'); - }); - - - it('should ignore input without ng:model attr', function() { - compileInput('<input type="text" name="whatever" required />'); - - browserTrigger(inputElm, 'blur'); - expect(inputElm.hasClass('ng-valid')).toBe(false); - expect(inputElm.hasClass('ng-invalid')).toBe(false); - expect(inputElm.hasClass('ng-pristine')).toBe(false); - expect(inputElm.hasClass('ng-dirty')).toBe(false); - }); - - - it('should report error on assignment error', function() { - expect(function() { - compileInput('<input type="text" ng:model="throw \'\'">'); - scope.$digest(); - }).toThrow("Syntax Error: Token '''' is an unexpected token at column 7 of the expression [throw ''] starting at ['']."); - }); - - - it("should render as blank if null", function() { - compileInput('<input type="text" ng:model="age" />'); - - scope.$apply(function() { - scope.age = null; - }); - - expect(scope.age).toBeNull(); - expect(inputElm.val()).toEqual(''); - }); - - - it('should render 0 even if it is a number', function() { - compileInput('<input type="text" ng:model="value" />'); - scope.$apply(function() { - scope.value = 0; - }); - - expect(inputElm.val()).toBe('0'); - }); - - - describe('pattern', function() { - - it('should validate in-lined pattern', function() { - compileInput('<input type="text" ng:model="value" ng:pattern="/^\\d\\d\\d-\\d\\d-\\d\\d\\d\\d$/" />'); - scope.$digest(); - - changeInputValueTo('x000-00-0000x'); - expect(inputElm).toBeInvalid(); - - changeInputValueTo('000-00-0000'); - expect(inputElm).toBeValid(); - - changeInputValueTo('000-00-0000x'); - expect(inputElm).toBeInvalid(); - - changeInputValueTo('123-45-6789'); - expect(inputElm).toBeValid(); - - changeInputValueTo('x'); - expect(inputElm).toBeInvalid(); - }); - - - it('should validate pattern from scope', function() { - compileInput('<input type="text" ng:model="value" ng:pattern="regexp" />'); - scope.regexp = /^\d\d\d-\d\d-\d\d\d\d$/; - scope.$digest(); - - changeInputValueTo('x000-00-0000x'); - expect(inputElm).toBeInvalid(); - - changeInputValueTo('000-00-0000'); - expect(inputElm).toBeValid(); - - changeInputValueTo('000-00-0000x'); - expect(inputElm).toBeInvalid(); - - changeInputValueTo('123-45-6789'); - expect(inputElm).toBeValid(); - - changeInputValueTo('x'); - expect(inputElm).toBeInvalid(); - - scope.regexp = /abc?/; - - changeInputValueTo('ab'); - expect(inputElm).toBeValid(); - - changeInputValueTo('xx'); - expect(inputElm).toBeInvalid(); - }); - - - xit('should throw an error when scope pattern can\'t be found', function() { - compileInput('<input type="text" ng:model="foo" ng:pattern="fooRegexp" />'); - - expect(function() { changeInputValueTo('xx'); }). - toThrow('Expected fooRegexp to be a RegExp but was undefined'); - }); - }); - - - describe('minlength', function() { - - it('should invalid shorter than given minlenght', function() { - compileInput('<input type="text" ng:model="value" ng:minlength="3" />'); - - changeInputValueTo('aa'); - expect(scope.value).toBeUndefined(); - - changeInputValueTo('aaa'); - expect(scope.value).toBe('aaa'); - }); - }); - - - describe('maxlength', function() { - - it('should invalid shorter than given maxlenght', function() { - compileInput('<input type="text" ng:model="value" ng:maxlength="5" />'); - - changeInputValueTo('aaaaaaaa'); - expect(scope.value).toBeUndefined(); - - changeInputValueTo('aaa'); - expect(scope.value).toBe('aaa'); - }); - }); - - - // INPUT TYPES - - describe('number', function() { - - it('should not update model if view invalid', function() { - compileInput('<input type="number" ng:model="age"/>'); - - scope.$apply(function() { - scope.age = 123; - }); - expect(inputElm.val()).toBe('123'); - - try { - // to allow non-number values, we have to change type so that - // the browser which have number validation will not interfere with - // this test. IE8 won't allow it hence the catch. - inputElm[0].setAttribute('type', 'text'); - } catch (e) {} - - changeInputValueTo('123X'); - expect(inputElm.val()).toBe('123X'); - expect(scope.age).toBe(123); - expect(inputElm).toBeInvalid(); - }); - - - it('should render as blank if null', function() { - compileInput('<input type="number" ng:model="age" />'); - - scope.$apply(function() { - scope.age = null; - }); - - expect(scope.age).toBeNull(); - expect(inputElm.val()).toEqual(''); - }); - - - it('should come up blank when no value specified', function() { - compileInput('<input type="number" ng:model="age" />'); - - scope.$digest(); - expect(inputElm.val()).toBe(''); - - scope.$apply(function() { - scope.age = null; - }); - - expect(scope.age).toBeNull(); - expect(inputElm.val()).toBe(''); - }); - - - it('should parse empty string to null', function() { - compileInput('<input type="number" ng:model="age" />'); - - scope.$apply(function() { - scope.age = 10; - }); - - changeInputValueTo(''); - expect(scope.age).toBeNull(); - expect(inputElm).toBeValid(); - }); - - - describe('min', function() { - - it('should validate', function() { - compileInput('<input type="number" ng:model="value" name="alias" min="10" />'); - scope.$digest(); - - changeInputValueTo('1'); - expect(inputElm).toBeInvalid(); - expect(scope.value).toBeFalsy(); - expect(scope.form.alias.error.MIN).toBeTruthy(); - - changeInputValueTo('100'); - expect(inputElm).toBeValid(); - expect(scope.value).toBe(100); - expect(scope.form.alias.error.MIN).toBeFalsy(); - }); - }); - - - describe('max', function() { - - it('should validate', function() { - compileInput('<input type="number" ng:model="value" name="alias" max="10" />'); - scope.$digest(); - - changeInputValueTo('20'); - expect(inputElm).toBeInvalid(); - expect(scope.value).toBeFalsy(); - expect(scope.form.alias.error.MAX).toBeTruthy(); - - changeInputValueTo('0'); - expect(inputElm).toBeValid(); - expect(scope.value).toBe(0); - expect(scope.form.alias.error.MAX).toBeFalsy(); - }); - }); - - - describe('required', function() { - - it('should be valid even if value is 0', function() { - compileInput('<input type="number" ng:model="value" name="alias" required />'); - - changeInputValueTo('0'); - expect(inputElm).toBeValid(); - expect(scope.value).toBe(0); - expect(scope.form.alias.error.REQUIRED).toBeFalsy(); - }); - - it('should be valid even if value 0 is set from model', function() { - compileInput('<input type="number" ng:model="value" name="alias" required />'); - - scope.$apply(function() { - scope.value = 0; - }); - - expect(inputElm).toBeValid(); - expect(inputElm.val()).toBe('0') - expect(scope.form.alias.error.REQUIRED).toBeFalsy(); - }); - }); - }); - - describe('email', function() { - - it('should validate e-mail', function() { - compileInput('<input type="email" ng:model="email" name="alias" />'); - - var widget = scope.form.alias; - changeInputValueTo('vojta@google.com'); - - expect(scope.email).toBe('vojta@google.com'); - expect(inputElm).toBeValid(); - expect(widget.error.EMAIL).toBeUndefined(); - - changeInputValueTo('invalid@'); - expect(scope.email).toBe('vojta@google.com'); - expect(inputElm).toBeInvalid(); - expect(widget.error.EMAIL).toBeTruthy(); - }); - - - describe('EMAIL_REGEXP', function() { - - it('should validate email', function() { - expect(EMAIL_REGEXP.test('a@b.com')).toBe(true); - expect(EMAIL_REGEXP.test('a@B.c')).toBe(false); - }); - }); - }); - - - describe('url', function() { - - it('should validate url', function() { - compileInput('<input type="url" ng:model="url" name="alias" />'); - var widget = scope.form.alias; - - changeInputValueTo('http://www.something.com'); - expect(scope.url).toBe('http://www.something.com'); - expect(inputElm).toBeValid(); - expect(widget.error.URL).toBeUndefined(); - - changeInputValueTo('invalid.com'); - expect(scope.url).toBe('http://www.something.com'); - expect(inputElm).toBeInvalid(); - expect(widget.error.URL).toBeTruthy(); - }); - - - describe('URL_REGEXP', function() { - - it('should validate url', function() { - expect(URL_REGEXP.test('http://server:123/path')).toBe(true); - expect(URL_REGEXP.test('a@B.c')).toBe(false); - }); - }); - }); - - - describe('radio', function() { - - it('should update the model', function() { - compileInput( - '<input type="radio" ng:model="color" value="white" />' + - '<input type="radio" ng:model="color" value="red" />' + - '<input type="radio" ng:model="color" value="blue" />'); - - scope.$apply(function() { - scope.color = 'white'; - }); - expect(inputElm[0].checked).toBe(true); - expect(inputElm[1].checked).toBe(false); - expect(inputElm[2].checked).toBe(false); - - scope.$apply(function() { - scope.color = 'red'; - }); - expect(inputElm[0].checked).toBe(false); - expect(inputElm[1].checked).toBe(true); - expect(inputElm[2].checked).toBe(false); - - browserTrigger(inputElm[2]); - expect(scope.color).toBe('blue'); - }); - - - // TODO(vojta): change interpolate ? - xit('should allow {{expr}} as value', function() { - scope.some = 11; - compileInput( - '<input type="radio" ng:model="value" value="{{some}}" />' + - '<input type="radio" ng:model="value" value="{{other}}" />'); - - browserTrigger(inputElm[0]); - expect(scope.value).toBe(true); - - browserTrigger(inputElm[1]); - expect(scope.value).toBe(false); - }); - }); - - - describe('checkbox', function() { - - it('should ignore checkbox without ng:model attr', function() { - compileInput('<input type="checkbox" name="whatever" required />'); - - browserTrigger(inputElm, 'blur'); - expect(inputElm.hasClass('ng-valid')).toBe(false); - expect(inputElm.hasClass('ng-invalid')).toBe(false); - expect(inputElm.hasClass('ng-pristine')).toBe(false); - expect(inputElm.hasClass('ng-dirty')).toBe(false); - }); - - - it('should format booleans', function() { - compileInput('<input type="checkbox" ng:model="name" />'); - - scope.$apply(function() { - scope.name = false; - }); - expect(inputElm[0].checked).toBe(false); - - scope.$apply(function() { - scope.name = true; - }); - expect(inputElm[0].checked).toBe(true); - }); - - - it('should support type="checkbox" with non-standard capitalization', function() { - compileInput('<input type="checkBox" ng:model="checkbox" />'); - - browserTrigger(inputElm, 'click'); - expect(scope.checkbox).toBe(true); - - browserTrigger(inputElm, 'click'); - expect(scope.checkbox).toBe(false); - }); - - - it('should allow custom enumeration', function() { - compileInput('<input type="checkbox" ng:model="name" ng:true-value="y" ' + - 'ng:false-value="n">'); - - scope.$apply(function() { - scope.name = 'y'; - }); - expect(inputElm[0].checked).toBe(true); - - scope.$apply(function() { - scope.name = 'n'; - }); - expect(inputElm[0].checked).toBe(false); - - scope.$apply(function() { - scope.name = 'something else'; - }); - expect(inputElm[0].checked).toBe(false); - - browserTrigger(inputElm, 'click'); - expect(scope.name).toEqual('y'); - - browserTrigger(inputElm, 'click'); - expect(scope.name).toEqual('n'); - }); - }); - - - describe('textarea', function() { - - it("should process textarea", function() { - compileInput('<textarea ng:model="name"></textarea>'); - inputElm = formElm.find('textarea'); - - scope.$apply(function() { - scope.name = 'Adam'; - }); - expect(inputElm.val()).toEqual('Adam'); - - changeInputValueTo('Shyam'); - expect(scope.name).toEqual('Shyam'); - - changeInputValueTo('Kai'); - expect(scope.name).toEqual('Kai'); - }); - - - it('should ignore textarea without ng:model attr', function() { - compileInput('<textarea name="whatever" required></textarea>'); - inputElm = formElm.find('textarea'); - - browserTrigger(inputElm, 'blur'); - expect(inputElm.hasClass('ng-valid')).toBe(false); - expect(inputElm.hasClass('ng-invalid')).toBe(false); - expect(inputElm.hasClass('ng-pristine')).toBe(false); - expect(inputElm.hasClass('ng-dirty')).toBe(false); - }); - }); - - - describe('ng:list', function() { - - it('should parse text into an array', function() { - compileInput('<input type="text" ng:model="list" ng:list />'); - - // model -> view - scope.$apply(function() { - scope.list = ['x', 'y', 'z']; - }); - expect(inputElm.val()).toBe('x, y, z'); - - // view -> model - changeInputValueTo('1, 2, 3'); - expect(scope.list).toEqual(['1', '2', '3']); - }); - - - it("should not clobber text if model changes due to itself", function() { - // When the user types 'a,b' the 'a,' stage parses to ['a'] but if the - // $parseModel function runs it will change to 'a', in essence preventing - // the user from ever typying ','. - compileInput('<input type="text" ng:model="list" ng:list />'); - - changeInputValueTo('a '); - expect(inputElm.val()).toEqual('a '); - expect(scope.list).toEqual(['a']); - - changeInputValueTo('a ,'); - expect(inputElm.val()).toEqual('a ,'); - expect(scope.list).toEqual(['a']); - - changeInputValueTo('a , '); - expect(inputElm.val()).toEqual('a , '); - expect(scope.list).toEqual(['a']); - - changeInputValueTo('a , b'); - expect(inputElm.val()).toEqual('a , b'); - expect(scope.list).toEqual(['a', 'b']); - }); - - - xit('should require at least one item', function() { - compileInput('<input type="text" ng:model="list" ng:list required />'); - - changeInputValueTo(' , '); - expect(inputElm).toBeInvalid(); - }); - - - it('should convert empty string to an empty array', function() { - compileInput('<input type="text" ng:model="list" ng:list />'); - - changeInputValueTo(''); - expect(scope.list).toEqual([]); - }); - }); - - describe('required', function() { - - it('should allow bindings on required', function() { - compileInput('<input type="text" ng:model="value" required="{{required}}" />'); - - scope.$apply(function() { - scope.required = false; - }); - - changeInputValueTo(''); - expect(inputElm).toBeValid(); - - - scope.$apply(function() { - scope.required = true; - }); - expect(inputElm).toBeInvalid(); - - scope.$apply(function() { - scope.value = 'some'; - }); - expect(inputElm).toBeValid(); - - changeInputValueTo(''); - expect(inputElm).toBeInvalid(); - - scope.$apply(function() { - scope.required = false; - }); - expect(inputElm).toBeValid(); - }); - - - it('should invalid initial value with bound required', function() { - compileInput('<input type="text" ng:model="value" required="{{required}}" />'); - - scope.$apply(function() { - scope.required = true; - }); - - expect(inputElm).toBeInvalid(); - }); - - - it('should be $invalid but $pristine if not touched', function() { - compileInput('<input type="text" ng:model="name" name="alias" required />'); - - scope.$apply(function() { - scope.name = ''; - }); - - expect(inputElm).toBeInvalid(); - expect(inputElm).toBePristine(); - - changeInputValueTo(''); - expect(inputElm).toBeInvalid(); - expect(inputElm).toBeDirty(); - }); - - - it('should allow empty string if not required', function() { - compileInput('<input type="text" ng:model="foo" />'); - changeInputValueTo('a'); - changeInputValueTo(''); - expect(scope.foo).toBe(''); - }); - - - it('should set $invalid when model undefined', function() { - compileInput('<input type="text" ng:model="notDefiend" required />'); - scope.$digest(); - expect(inputElm).toBeInvalid(); - }) - }); - - - describe('ng:change', function() { - - it('should $eval expression after new value is set in the model', function() { - compileInput('<input type="text" ng:model="value" ng:change="change()" />'); - - scope.change = jasmine.createSpy('change').andCallFake(function() { - expect(scope.value).toBe('new value'); - }); - - changeInputValueTo('new value'); - expect(scope.change).toHaveBeenCalledOnce(); - }); - - it('should not $eval the expression if changed from model', function() { - compileInput('<input type="text" ng:model="value" ng:change="change()" />'); - - scope.change = jasmine.createSpy('change'); - scope.$apply(function() { - scope.value = true; - }); - - expect(scope.change).not.toHaveBeenCalled(); - }); - - - it('should $eval ng:change expression on checkbox', function() { - compileInput('<input type="checkbox" ng:model="foo" ng:change="changeFn()">'); - - scope.changeFn = jasmine.createSpy('changeFn'); - scope.$digest(); - expect(scope.changeFn).not.toHaveBeenCalled(); - - browserTrigger(inputElm, 'click'); - expect(scope.changeFn).toHaveBeenCalledOnce(); - }); - }); - - - describe('ng:model-instant', function() { - - it('should bind keydown, change, input events', inject(function($browser) { - compileInput('<input type="text" ng:model="value" ng:model-instant />'); - - inputElm.val('value1'); - browserTrigger(inputElm, 'keydown'); - - // should be async (because of keydown) - expect(scope.value).toBeUndefined(); - - $browser.defer.flush(); - expect(scope.value).toBe('value1'); - - inputElm.val('value2'); - browserTrigger(inputElm, 'change'); - expect(scope.value).toBe('value2'); - - if (msie < 9) return; - - inputElm.val('value3'); - browserTrigger(inputElm, 'input'); - expect(scope.value).toBe('value3'); - })); - }); -}); diff --git a/test/widget/selectSpec.js b/test/widget/selectSpec.js deleted file mode 100644 index 9db47c05..00000000 --- a/test/widget/selectSpec.js +++ /dev/null @@ -1,809 +0,0 @@ -'use strict'; - -describe('select', function() { - var scope, formElement, element, $compile; - - function compile(html) { - formElement = jqLite('<form name="form">' + html + '</form>'); - element = formElement.find('select'); - $compile(formElement)(scope); - scope.$apply(); - } - - beforeEach(inject(function($injector, $rootScope) { - scope = $rootScope; - $compile = $injector.get('$compile'); - formElement = element = null; - })); - - afterEach(function() { - dealoc(formElement); - }); - - - describe('select-one', function() { - - it('should compile children of a select without a ng:model, but not create a model for it', - function() { - compile('<select>' + - '<option selected="true">{{a}}</option>' + - '<option value="">{{b}}</option>' + - '<option>C</option>' + - '</select>'); - scope.$apply(function() { - scope.a = 'foo'; - scope.b = 'bar'; - }); - - expect(element.text()).toBe('foobarC'); - }); - - - it('should require', function() { - compile( - '<select name="select" ng:model="selection" required ng:change="change()">' + - '<option value=""></option>' + - '<option value="c">C</option>' + - '</select>'); - - scope.change = function() { - scope.log += 'change;'; - }; - - scope.$apply(function() { - scope.log = ''; - scope.selection = 'c'; - }); - - expect(scope.form.select.error.REQUIRED).toBeFalsy(); - expect(element).toBeValid(); - expect(element).toBePristine(); - - scope.$apply(function() { - scope.selection = ''; - }); - - expect(scope.form.select.error.REQUIRED).toBeTruthy(); - expect(element).toBeInvalid(); - expect(element).toBePristine(); - expect(scope.log).toEqual(''); - - element[0].value = 'c'; - browserTrigger(element, 'change'); - expect(element).toBeValid(); - expect(element).toBeDirty(); - expect(scope.log).toEqual('change;'); - }); - - - it('should not be invalid if no require', function() { - compile( - '<select name="select" ng:model="selection">' + - '<option value=""></option>' + - '<option value="c">C</option>' + - '</select>'); - - expect(element).toBeValid(); - expect(element).toBePristine(); - }); - }); - - - describe('select-multiple', function() { - - it('should support type="select-multiple"', function() { - compile( - '<select ng:model="selection" multiple>' + - '<option>A</option>' + - '<option>B</option>' + - '</select>'); - - scope.$apply(function() { - scope.selection = ['A']; - }); - - expect(element[0].childNodes[0].selected).toEqual(true); - }); - - - it('should require', function() { - compile( - '<select name="select" ng:model="selection" multiple required>' + - '<option>A</option>' + - '<option>B</option>' + - '</select>'); - - scope.$apply(function() { - scope.selection = []; - }); - - expect(scope.form.select.error.REQUIRED).toBeTruthy(); - expect(element).toBeInvalid(); - expect(element).toBePristine(); - - scope.$apply(function() { - scope.selection = ['A']; - }); - - expect(element).toBeValid(); - expect(element).toBePristine(); - - element[0].value = 'B'; - browserTrigger(element, 'change'); - expect(element).toBeValid(); - expect(element).toBeDirty(); - }); - }); - - - describe('ng:options', function() { - function createSelect(attrs, blank, unknown) { - var html = '<select'; - forEach(attrs, function(value, key) { - if (isBoolean(value)) { - if (value) html += ' ' + key; - } else { - html += ' ' + key + '="' + value + '"'; - } - }); - html += '>' + - (blank ? (isString(blank) ? blank : '<option value="">blank</option>') : '') + - (unknown ? (isString(unknown) ? unknown : '<option value="?">unknown</option>') : '') + - '</select>'; - - compile(html); - } - - function createSingleSelect(blank, unknown) { - createSelect({ - 'ng:model':'selected', - 'ng:options':'value.name for value in values' - }, blank, unknown); - } - - function createMultiSelect(blank, unknown) { - createSelect({ - 'ng:model':'selected', - 'multiple':true, - 'ng:options':'value.name for value in values' - }, blank, unknown); - } - - - it('should throw when not formated "? for ? in ?"', function() { - expect(function() { - compile('<select ng:model="selected" ng:options="i dont parse"></select>'); - }).toThrow("Expected ng:options in form of '_select_ (as _label_)? for (_key_,)?_value_ in" + - " _collection_' but got 'i dont parse'."); - }); - - - it('should render a list', function() { - createSingleSelect(); - - scope.$apply(function() { - scope.values = [{name: 'A'}, {name: 'B'}, {name: 'C'}]; - scope.selected = scope.values[0]; - }); - - var options = element.find('option'); - expect(options.length).toEqual(3); - expect(sortedHtml(options[0])).toEqual('<option value="0">A</option>'); - expect(sortedHtml(options[1])).toEqual('<option value="1">B</option>'); - expect(sortedHtml(options[2])).toEqual('<option value="2">C</option>'); - }); - - - it('should render an object', function() { - createSelect({ - 'ng:model': 'selected', - 'ng:options': 'value as key for (key, value) in object' - }); - - scope.$apply(function() { - scope.object = {'red': 'FF0000', 'green': '00FF00', 'blue': '0000FF'}; - scope.selected = scope.object.red; - }); - - var options = element.find('option'); - expect(options.length).toEqual(3); - expect(sortedHtml(options[0])).toEqual('<option value="blue">blue</option>'); - expect(sortedHtml(options[1])).toEqual('<option value="green">green</option>'); - expect(sortedHtml(options[2])).toEqual('<option value="red">red</option>'); - expect(options[2].selected).toEqual(true); - - scope.$apply(function() { - scope.object.azur = '8888FF'; - }); - - options = element.find('option'); - expect(options[3].selected).toEqual(true); - }); - - - it('should grow list', function() { - createSingleSelect(); - - scope.$apply(function() { - scope.values = []; - }); - - expect(element.find('option').length).toEqual(1); // because we add special empty option - expect(sortedHtml(element.find('option')[0])).toEqual('<option value="?"></option>'); - - scope.$apply(function() { - scope.values.push({name:'A'}); - scope.selected = scope.values[0]; - }); - - expect(element.find('option').length).toEqual(1); - expect(sortedHtml(element.find('option')[0])).toEqual('<option value="0">A</option>'); - - scope.$apply(function() { - scope.values.push({name:'B'}); - }); - - expect(element.find('option').length).toEqual(2); - expect(sortedHtml(element.find('option')[0])).toEqual('<option value="0">A</option>'); - expect(sortedHtml(element.find('option')[1])).toEqual('<option value="1">B</option>'); - }); - - - it('should shrink list', function() { - createSingleSelect(); - - scope.$apply(function() { - scope.values = [{name:'A'}, {name:'B'}, {name:'C'}]; - scope.selected = scope.values[0]; - }); - - expect(element.find('option').length).toEqual(3); - - scope.$apply(function() { - scope.values.pop(); - }); - - expect(element.find('option').length).toEqual(2); - expect(sortedHtml(element.find('option')[0])).toEqual('<option value="0">A</option>'); - expect(sortedHtml(element.find('option')[1])).toEqual('<option value="1">B</option>'); - - scope.$apply(function() { - scope.values.pop(); - }); - - expect(element.find('option').length).toEqual(1); - expect(sortedHtml(element.find('option')[0])).toEqual('<option value="0">A</option>'); - - scope.$apply(function() { - scope.values.pop(); - scope.selected = null; - }); - - expect(element.find('option').length).toEqual(1); // we add back the special empty option - }); - - - it('should shrink and then grow list', function() { - createSingleSelect(); - - scope.$apply(function() { - scope.values = [{name:'A'}, {name:'B'}, {name:'C'}]; - scope.selected = scope.values[0]; - }); - - expect(element.find('option').length).toEqual(3); - - scope.$apply(function() { - scope.values = [{name: '1'}, {name: '2'}]; - scope.selected = scope.values[0]; - }); - - expect(element.find('option').length).toEqual(2); - - scope.$apply(function() { - scope.values = [{name: 'A'}, {name: 'B'}, {name: 'C'}]; - scope.selected = scope.values[0]; - }); - - expect(element.find('option').length).toEqual(3); - }); - - - it('should update list', function() { - createSingleSelect(); - - scope.$apply(function() { - scope.values = [{name: 'A'}, {name: 'B'}, {name: 'C'}]; - scope.selected = scope.values[0]; - }); - - scope.$apply(function() { - scope.values = [{name: 'B'}, {name: 'C'}, {name: 'D'}]; - scope.selected = scope.values[0]; - }); - - var options = element.find('option'); - expect(options.length).toEqual(3); - expect(sortedHtml(options[0])).toEqual('<option value="0">B</option>'); - expect(sortedHtml(options[1])).toEqual('<option value="1">C</option>'); - expect(sortedHtml(options[2])).toEqual('<option value="2">D</option>'); - }); - - - it('should preserve existing options', function() { - createSingleSelect(true); - - scope.$apply(function() { - scope.values = []; - }); - - expect(element.find('option').length).toEqual(1); - - scope.$apply(function() { - scope.values = [{name: 'A'}]; - scope.selected = scope.values[0]; - }); - - expect(element.find('option').length).toEqual(2); - expect(jqLite(element.find('option')[0]).text()).toEqual('blank'); - expect(jqLite(element.find('option')[1]).text()).toEqual('A'); - - scope.$apply(function() { - scope.values = []; - scope.selected = null; - }); - - expect(element.find('option').length).toEqual(1); - expect(jqLite(element.find('option')[0]).text()).toEqual('blank'); - }); - - - describe('binding', function() { - - it('should bind to scope value', function() { - createSingleSelect(); - - scope.$apply(function() { - scope.values = [{name: 'A'}, {name: 'B'}]; - scope.selected = scope.values[0]; - }); - - expect(element.val()).toEqual('0'); - - scope.$apply(function() { - scope.selected = scope.values[1]; - }); - - expect(element.val()).toEqual('1'); - }); - - - it('should bind to scope value and group', function() { - createSelect({ - 'ng:model': 'selected', - 'ng:options': 'item.name group by item.group for item in values' - }); - - scope.$apply(function() { - scope.values = [{name: 'A'}, - {name: 'B', group: 'first'}, - {name: 'C', group: 'second'}, - {name: 'D', group: 'first'}, - {name: 'E', group: 'second'}]; - scope.selected = scope.values[3]; - }); - - expect(element.val()).toEqual('3'); - - var first = jqLite(element.find('optgroup')[0]); - var b = jqLite(first.find('option')[0]); - var d = jqLite(first.find('option')[1]); - expect(first.attr('label')).toEqual('first'); - expect(b.text()).toEqual('B'); - expect(d.text()).toEqual('D'); - - var second = jqLite(element.find('optgroup')[1]); - var c = jqLite(second.find('option')[0]); - var e = jqLite(second.find('option')[1]); - expect(second.attr('label')).toEqual('second'); - expect(c.text()).toEqual('C'); - expect(e.text()).toEqual('E'); - - scope.$apply(function() { - scope.selected = scope.values[0]; - }); - - expect(element.val()).toEqual('0'); - }); - - - it('should bind to scope value through experession', function() { - createSelect({ - 'ng:model': 'selected', - 'ng:options': 'item.id as item.name for item in values' - }); - - scope.$apply(function() { - scope.values = [{id: 10, name: 'A'}, {id: 20, name: 'B'}]; - scope.selected = scope.values[0].id; - }); - - expect(element.val()).toEqual('0'); - - scope.$apply(function() { - scope.selected = scope.values[1].id; - }); - - expect(element.val()).toEqual('1'); - }); - - - it('should bind to object key', function() { - createSelect({ - 'ng:model': 'selected', - 'ng:options': 'key as value for (key, value) in object' - }); - - scope.$apply(function() { - scope.object = {red: 'FF0000', green: '00FF00', blue: '0000FF'}; - scope.selected = 'green'; - }); - - expect(element.val()).toEqual('green'); - - scope.$apply(function() { - scope.selected = 'blue'; - }); - - expect(element.val()).toEqual('blue'); - }); - - - it('should bind to object value', function() { - createSelect({ - 'ng:model': 'selected', - 'ng:options': 'value as key for (key, value) in object' - }); - - scope.$apply(function() { - scope.object = {red: 'FF0000', green: '00FF00', blue:'0000FF'}; - scope.selected = '00FF00'; - }); - - expect(element.val()).toEqual('green'); - - scope.$apply(function() { - scope.selected = '0000FF'; - }); - - expect(element.val()).toEqual('blue'); - }); - - - it('should insert a blank option if bound to null', function() { - createSingleSelect(); - - scope.$apply(function() { - scope.values = [{name: 'A'}]; - scope.selected = null; - }); - - expect(element.find('option').length).toEqual(2); - expect(element.val()).toEqual(''); - expect(jqLite(element.find('option')[0]).val()).toEqual(''); - - scope.$apply(function() { - scope.selected = scope.values[0]; - }); - - expect(element.val()).toEqual('0'); - expect(element.find('option').length).toEqual(1); - }); - - - it('should reuse blank option if bound to null', function() { - createSingleSelect(true); - - scope.$apply(function() { - scope.values = [{name: 'A'}]; - scope.selected = null; - }); - - expect(element.find('option').length).toEqual(2); - expect(element.val()).toEqual(''); - expect(jqLite(element.find('option')[0]).val()).toEqual(''); - - scope.$apply(function() { - scope.selected = scope.values[0]; - }); - - expect(element.val()).toEqual('0'); - expect(element.find('option').length).toEqual(2); - }); - - - it('should insert a unknown option if bound to something not in the list', function() { - createSingleSelect(); - - scope.$apply(function() { - scope.values = [{name: 'A'}]; - scope.selected = {}; - }); - - expect(element.find('option').length).toEqual(2); - expect(element.val()).toEqual('?'); - expect(jqLite(element.find('option')[0]).val()).toEqual('?'); - - scope.$apply(function() { - scope.selected = scope.values[0]; - }); - - expect(element.val()).toEqual('0'); - expect(element.find('option').length).toEqual(1); - }); - - - it('should select correct input if previously selected option was "?"', function() { - createSingleSelect(); - - scope.$apply(function() { - scope.values = [{name: 'A'}, {name: 'B'}]; - scope.selected = {}; - }); - - expect(element.find('option').length).toEqual(3); - expect(element.val()).toEqual('?'); - expect(element.find('option').eq(0).val()).toEqual('?'); - - browserTrigger(element.find('option').eq(1)); - expect(element.val()).toEqual('0'); - expect(element.find('option').eq(0).prop('selected')).toBeTruthy(); - expect(element.find('option').length).toEqual(2); - }); - }); - - - describe('blank option', function () { - - it('should be compiled as template, be watched and updated', function () { - var option; - createSingleSelect('<option value="">blank is {{blankVal}}</option>'); - - scope.$apply(function() { - scope.blankVal = 'so blank'; - scope.values = [{name: 'A'}]; - }); - - // check blank option is first and is compiled - expect(element.find('option').length).toBe(2); - option = element.find('option').eq(0); - expect(option.val()).toBe(''); - expect(option.text()).toBe('blank is so blank'); - - scope.$apply(function() { - scope.blankVal = 'not so blank'; - }); - - // check blank option is first and is compiled - expect(element.find('option').length).toBe(2); - option = element.find('option').eq(0); - expect(option.val()).toBe(''); - expect(option.text()).toBe('blank is not so blank'); - }); - - - it('should support binding via ng:bind-template attribute', function () { - var option; - createSingleSelect('<option value="" ng:bind-template="blank is {{blankVal}}"></option>'); - - scope.$apply(function() { - scope.blankVal = 'so blank'; - scope.values = [{name: 'A'}]; - }); - - // check blank option is first and is compiled - expect(element.find('option').length).toBe(2); - option = element.find('option').eq(0); - expect(option.val()).toBe(''); - expect(option.text()).toBe('blank is so blank'); - }); - - - it('should support biding via ng:bind attribute', function () { - var option; - createSingleSelect('<option value="" ng:bind="blankVal"></option>'); - - scope.$apply(function() { - scope.blankVal = 'is blank'; - scope.values = [{name: 'A'}]; - }); - - // check blank option is first and is compiled - expect(element.find('option').length).toBe(2); - option = element.find('option').eq(0); - expect(option.val()).toBe(''); - expect(option.text()).toBe('is blank'); - }); - - - it('should be rendered with the attributes preserved', function () { - var option; - createSingleSelect('<option value="" class="coyote" id="road-runner" ' + - 'custom-attr="custom-attr">{{blankVal}}</option>'); - - scope.$apply(function() { - scope.blankVal = 'is blank'; - }); - - // check blank option is first and is compiled - option = element.find('option').eq(0); - expect(option.hasClass('coyote')).toBeTruthy(); - expect(option.attr('id')).toBe('road-runner'); - expect(option.attr('custom-attr')).toBe('custom-attr'); - }); - }); - - - describe('on change', function() { - - it('should update model on change', function() { - createSingleSelect(); - - scope.$apply(function() { - scope.values = [{name: 'A'}, {name: 'B'}]; - scope.selected = scope.values[0]; - }); - - expect(element.val()).toEqual('0'); - - element.val('1'); - browserTrigger(element, 'change'); - expect(scope.selected).toEqual(scope.values[1]); - }); - - - it('should update model on change through expression', function() { - createSelect({ - 'ng:model': 'selected', - 'ng:options': 'item.id as item.name for item in values' - }); - - scope.$apply(function() { - scope.values = [{id: 10, name: 'A'}, {id: 20, name: 'B'}]; - scope.selected = scope.values[0].id; - }); - - expect(element.val()).toEqual('0'); - - element.val('1'); - browserTrigger(element, 'change'); - expect(scope.selected).toEqual(scope.values[1].id); - }); - - - it('should update model to null on change', function() { - createSingleSelect(true); - - scope.$apply(function() { - scope.values = [{name: 'A'}, {name: 'B'}]; - scope.selected = scope.values[0]; - element.val('0'); - }); - - element.val(''); - browserTrigger(element, 'change'); - expect(scope.selected).toEqual(null); - }); - }); - - - describe('select-many', function() { - - it('should read multiple selection', function() { - createMultiSelect(); - - scope.$apply(function() { - scope.values = [{name: 'A'}, {name: 'B'}]; - scope.selected = []; - }); - - expect(element.find('option').length).toEqual(2); - expect(element.find('option')[0].selected).toBeFalsy(); - expect(element.find('option')[1].selected).toBeFalsy(); - - scope.$apply(function() { - scope.selected.push(scope.values[1]); - }); - - expect(element.find('option').length).toEqual(2); - expect(element.find('option')[0].selected).toBeFalsy(); - expect(element.find('option')[1].selected).toBeTruthy(); - - scope.$apply(function() { - scope.selected.push(scope.values[0]); - }); - - expect(element.find('option').length).toEqual(2); - expect(element.find('option')[0].selected).toBeTruthy(); - expect(element.find('option')[1].selected).toBeTruthy(); - }); - - - it('should update model on change', function() { - createMultiSelect(); - - scope.$apply(function() { - scope.values = [{name: 'A'}, {name: 'B'}]; - scope.selected = []; - }); - - element.find('option')[0].selected = true; - - browserTrigger(element, 'change'); - expect(scope.selected).toEqual([scope.values[0]]); - }); - - it('should select from object', function() { - createSelect({ - 'ng:model':'selected', - 'multiple':true, - 'ng:options':'key as value for (key,value) in values' - }); - scope.values = {'0':'A', '1':'B'}; - - scope.selected = ['1']; - scope.$digest(); - expect(element.find('option')[1].selected).toBe(true); - - element.find('option')[0].selected = true; - browserTrigger(element, 'change'); - expect(scope.selected).toEqual(['0', '1']); - - element.find('option')[1].selected = false; - browserTrigger(element, 'change'); - expect(scope.selected).toEqual(['0']); - }); - }); - - - describe('ng:required', function() { - - it('should allow bindings on ng:required', function() { - createSelect({ - 'ng:model': 'value', - 'ng:options': 'item.name for item in values', - 'ng:required': '{{required}}' - }, true); - - - scope.$apply(function() { - scope.values = [{name: 'A', id: 1}, {name: 'B', id: 2}]; - scope.required = false; - }); - - element.val(''); - browserTrigger(element, 'change'); - expect(element).toBeValid(); - - scope.$apply(function() { - scope.required = true; - }); - expect(element).toBeInvalid(); - - scope.$apply(function() { - scope.value = scope.values[0]; - }); - expect(element).toBeValid(); - - element.val(''); - browserTrigger(element, 'change'); - expect(element).toBeInvalid(); - - scope.$apply(function() { - scope.required = false; - }); - expect(element).toBeValid(); - }); - }); - }); -}); |
