From f54db2ccda399f2677e4ca7588018cb31545a2b4 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Thu, 8 Mar 2012 15:00:38 -0800 Subject: chore(directives,widgets): reorg the code under directive/ dir --- test/directive/aSpec.js | 46 ++ test/directive/booleanAttrDirSpecs.js | 111 +++ test/directive/formSpec.js | 205 ++++++ test/directive/inputSpec.js | 961 ++++++++++++++++++++++++++ test/directive/ngBindSpec.js | 113 +++ test/directive/ngClassSpec.js | 204 ++++++ test/directive/ngClickSpec.js | 26 + test/directive/ngCloakSpec.js | 49 ++ test/directive/ngControllerSpec.js | 65 ++ test/directive/ngEventDirsSpec.js | 25 + test/directive/ngIncludeSpec.js | 274 ++++++++ test/directive/ngInitSpec.js | 16 + test/directive/ngNonBindableSpec.js | 20 + test/directive/ngPluralizeSpec.js | 136 ++++ test/directive/ngRepeatSpec.js | 282 ++++++++ test/directive/ngShowHideSpec.js | 43 ++ test/directive/ngStyleSpec.js | 88 +++ test/directive/ngSwitchSpec.js | 63 ++ test/directive/ngViewSpec.js | 413 +++++++++++ test/directive/scriptSpec.js | 44 ++ test/directive/selectSpec.js | 855 +++++++++++++++++++++++ test/directive/styleSpec.js | 31 + test/directivesSpec.js | 599 ---------------- test/markupSpec.js | 180 ----- test/service/compilerSpec.js | 18 +- test/widget/formSpec.js | 205 ------ test/widget/inputSpec.js | 961 -------------------------- test/widget/selectSpec.js | 809 ---------------------- test/widgetsSpec.js | 1223 --------------------------------- 29 files changed, 4087 insertions(+), 3978 deletions(-) create mode 100644 test/directive/aSpec.js create mode 100644 test/directive/booleanAttrDirSpecs.js create mode 100644 test/directive/formSpec.js create mode 100644 test/directive/inputSpec.js create mode 100644 test/directive/ngBindSpec.js create mode 100644 test/directive/ngClassSpec.js create mode 100644 test/directive/ngClickSpec.js create mode 100644 test/directive/ngCloakSpec.js create mode 100644 test/directive/ngControllerSpec.js create mode 100644 test/directive/ngEventDirsSpec.js create mode 100644 test/directive/ngIncludeSpec.js create mode 100644 test/directive/ngInitSpec.js create mode 100644 test/directive/ngNonBindableSpec.js create mode 100644 test/directive/ngPluralizeSpec.js create mode 100644 test/directive/ngRepeatSpec.js create mode 100644 test/directive/ngShowHideSpec.js create mode 100644 test/directive/ngStyleSpec.js create mode 100644 test/directive/ngSwitchSpec.js create mode 100644 test/directive/ngViewSpec.js create mode 100644 test/directive/scriptSpec.js create mode 100644 test/directive/selectSpec.js create mode 100644 test/directive/styleSpec.js delete mode 100644 test/directivesSpec.js delete mode 100644 test/markupSpec.js delete mode 100644 test/widget/formSpec.js delete mode 100644 test/widget/inputSpec.js delete mode 100644 test/widget/selectSpec.js delete mode 100644 test/widgetsSpec.js (limited to 'test') diff --git a/test/directive/aSpec.js b/test/directive/aSpec.js new file mode 100644 index 00000000..8aa2449d --- /dev/null +++ b/test/directive/aSpec.js @@ -0,0 +1,46 @@ +'use strict'; + +describe('a', function() { + var element; + + + afterEach(function(){ + dealoc(element); + }); + + + it('should prevent default action to be executed when href is empty', + inject(function($rootScope, $compile) { + var orgLocation = document.location.href, + preventDefaultCalled = false, + event; + + element = $compile('empty link')($rootScope); + + if (msie < 9) { + + event = document.createEventObject(); + expect(event.returnValue).not.toBeDefined(); + element[0].fireEvent('onclick', event); + expect(event.returnValue).toEqual(false); + + } else { + + event = document.createEvent('MouseEvent'); + event.initMouseEvent( + 'click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); + + event.preventDefaultOrg = event.preventDefault; + event.preventDefault = function() { + preventDefaultCalled = true; + if (this.preventDefaultOrg) this.preventDefaultOrg(); + }; + + element[0].dispatchEvent(event); + + expect(preventDefaultCalled).toEqual(true); + } + + expect(document.location.href).toEqual(orgLocation); + })); +}); diff --git a/test/directive/booleanAttrDirSpecs.js b/test/directive/booleanAttrDirSpecs.js new file mode 100644 index 00000000..2c47d81a --- /dev/null +++ b/test/directive/booleanAttrDirSpecs.js @@ -0,0 +1,111 @@ +'use strict'; + +describe('boolean attr directives', function() { + var element; + + afterEach(function() { + dealoc(element); + }); + + + it('should bind href', inject(function($rootScope, $compile) { + element = $compile('')($rootScope) + $rootScope.url = 'http://server' + $rootScope.$digest(); + expect(element.attr('href')).toEqual('http://server'); + })); + + + it('should bind disabled', inject(function($rootScope, $compile) { + element = $compile('')($rootScope) + $rootScope.isDisabled = false; + $rootScope.$digest(); + expect(element.attr('disabled')).toBeFalsy(); + $rootScope.isDisabled = true; + $rootScope.$digest(); + expect(element.attr('disabled')).toBeTruthy(); + })); + + + it('should bind checked', inject(function($rootScope, $compile) { + element = $compile('')($rootScope) + $rootScope.isChecked = false; + $rootScope.$digest(); + expect(element.attr('checked')).toBeFalsy(); + $rootScope.isChecked=true; + $rootScope.$digest(); + expect(element.attr('checked')).toBeTruthy(); + })); + + + it('should bind selected', inject(function($rootScope, $compile) { + element = $compile('')($rootScope) + jqLite(document.body).append(element) + $rootScope.isSelected=false; + $rootScope.$digest(); + expect(element.children()[1].selected).toBeFalsy(); + $rootScope.isSelected=true; + $rootScope.$digest(); + expect(element.children()[1].selected).toBeTruthy(); + })); + + + it('should bind readonly', inject(function($rootScope, $compile) { + element = $compile('')($rootScope) + $rootScope.isReadonly=false; + $rootScope.$digest(); + expect(element.attr('readOnly')).toBeFalsy(); + $rootScope.isReadonly=true; + $rootScope.$digest(); + expect(element.attr('readOnly')).toBeTruthy(); + })); + + + it('should bind multiple', inject(function($rootScope, $compile) { + element = $compile('')($rootScope) + $rootScope.isMultiple=false; + $rootScope.$digest(); + expect(element.attr('multiple')).toBeFalsy(); + $rootScope.isMultiple='multiple'; + $rootScope.$digest(); + expect(element.attr('multiple')).toBeTruthy(); + })); + + + it('should bind src', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope) + $rootScope.url = 'http://localhost/'; + $rootScope.$digest(); + expect(element.attr('src')).toEqual('http://localhost/'); + })); + + + it('should bind href and merge with other attrs', inject(function($rootScope, $compile) { + element = $compile('')($rootScope); + $rootScope.url = 'http://server'; + $rootScope.rel = 'REL'; + $rootScope.$digest(); + expect(element.attr('href')).toEqual('http://server'); + expect(element.attr('rel')).toEqual('REL'); + })); + + + it('should bind Text with no Bindings', inject(function($compile, $rootScope) { + forEach(['checked', 'disabled', 'multiple', 'readonly', 'selected'], function(name) { + element = $compile('
')($rootScope) + $rootScope.$digest(); + expect(element.attr(name)).toBe(name); + dealoc(element); + }); + + element = $compile('
')($rootScope) + $rootScope.$digest(); + expect(element.attr('src')).toEqual('some'); + dealoc(element); + + element = $compile('
')($rootScope) + $rootScope.$digest(); + expect(element.attr('href')).toEqual('some'); + dealoc(element); + })); +}); diff --git a/test/directive/formSpec.js b/test/directive/formSpec.js new file mode 100644 index 00000000..6387241f --- /dev/null +++ b/test/directive/formSpec.js @@ -0,0 +1,205 @@ +'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('
')(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( + '' + + '' + + '
')(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('
'); + $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('')(scope); + doc.bind('submit', callback); + + browserTrigger(doc, 'submit'); + expect(callback).toHaveBeenCalledOnce(); + }); + + + it('should publish form to scope', function() { + doc = $compile('
')(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('
')(scope); + + expect(scope.obj).toBeDefined(); + expect(scope.obj.myForm).toBeTruthy(); + }); + + + it('should chain nested forms', function() { + doc = jqLite( + '' + + '' + + '' + + '' + + ''); + $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( + '' + + '' + + '' + + '' + + ''); + $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('
'); + $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( + '
' + + '' + + '
')(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/directive/inputSpec.js b/test/directive/inputSpec.js new file mode 100644 index 00000000..d9f6b7b3 --- /dev/null +++ b/test/directive/inputSpec.js @@ -0,0 +1,961 @@ +'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('')($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('
' + inputHtml + '
'); + 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(''); + + scope.$apply(function() { + scope.name = 'misko'; + }); + + expect(inputElm.val()).toBe('misko'); + }); + + + it('should call $destroy on element remove', function() { + compileInput(''); + + 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(''); + + changeInputValueTo('adam'); + expect(scope.name).toEqual('adam'); + }); + + + it('should update the model and trim the value', function() { + compileInput(''); + + changeInputValueTo(' a '); + expect(scope.name).toEqual('a'); + }); + + + it('should allow complex reference binding', function() { + compileInput(''); + + scope.$apply(function() { + scope.obj = { abc: { name: 'Misko'} }; + }); + expect(inputElm.val()).toEqual('Misko'); + }); + + + it('should ignore input without ng:model attr', function() { + compileInput(''); + + 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(''); + 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(''); + + 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(''); + scope.$apply(function() { + scope.value = 0; + }); + + expect(inputElm.val()).toBe('0'); + }); + + + describe('pattern', function() { + + it('should validate in-lined pattern', function() { + compileInput(''); + 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(''); + 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(''); + + 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(''); + + 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(''); + + 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(''); + + 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(''); + + 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(''); + + 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(''); + + scope.$apply(function() { + scope.age = 10; + }); + + changeInputValueTo(''); + expect(scope.age).toBeNull(); + expect(inputElm).toBeValid(); + }); + + + describe('min', function() { + + it('should validate', function() { + compileInput(''); + 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(''); + 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(''); + + 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(''); + + 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(''); + + 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(''); + 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( + '' + + '' + + ''); + + 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( + '' + + ''); + + 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(''); + + 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(''); + + 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(''); + + browserTrigger(inputElm, 'click'); + expect(scope.checkbox).toBe(true); + + browserTrigger(inputElm, 'click'); + expect(scope.checkbox).toBe(false); + }); + + + it('should allow custom enumeration', function() { + compileInput(''); + + 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(''); + 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(''); + 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(''); + + // 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(''); + + 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(''); + + changeInputValueTo(' , '); + expect(inputElm).toBeInvalid(); + }); + + + it('should convert empty string to an empty array', function() { + compileInput(''); + + changeInputValueTo(''); + expect(scope.list).toEqual([]); + }); + }); + + describe('required', function() { + + it('should allow bindings on required', function() { + compileInput(''); + + 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(''); + + scope.$apply(function() { + scope.required = true; + }); + + expect(inputElm).toBeInvalid(); + }); + + + it('should be $invalid but $pristine if not touched', function() { + compileInput(''); + + 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(''); + changeInputValueTo('a'); + changeInputValueTo(''); + expect(scope.foo).toBe(''); + }); + + + it('should set $invalid when model undefined', function() { + compileInput(''); + scope.$digest(); + expect(inputElm).toBeInvalid(); + }) + }); + + + describe('ng:change', function() { + + it('should $eval expression after new value is set in the model', function() { + compileInput(''); + + 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(''); + + 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(''); + + 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(''); + + 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/directive/ngBindSpec.js b/test/directive/ngBindSpec.js new file mode 100644 index 00000000..e8c07494 --- /dev/null +++ b/test/directive/ngBindSpec.js @@ -0,0 +1,113 @@ +'use strict'; + +describe('ng:bind-*', function() { + var element; + + + afterEach(function() { + dealoc(element); + }); + + + describe('ng:bind', function() { + + it('should set text', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope); + expect(element.text()).toEqual(''); + $rootScope.a = 'misko'; + $rootScope.$digest(); + expect(element.hasClass('ng-binding')).toEqual(true); + expect(element.text()).toEqual('misko'); + })); + + it('should set text to blank if undefined', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope); + $rootScope.a = 'misko'; + $rootScope.$digest(); + expect(element.text()).toEqual('misko'); + $rootScope.a = undefined; + $rootScope.$digest(); + expect(element.text()).toEqual(''); + $rootScope.a = null; + $rootScope.$digest(); + expect(element.text()).toEqual(''); + })); + + it('should set html', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope); + $rootScope.html = '
hello
'; + $rootScope.$digest(); + expect(lowercase(element.html())).toEqual('
hello
'); + })); + + it('should set unsafe html', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope); + $rootScope.html = '
hello
'; + $rootScope.$digest(); + expect(lowercase(element.html())).toEqual('
hello
'); + })); + + it('should suppress rendering of falsy values', inject(function($rootScope, $compile) { + element = $compile('
{{ null }}{{ undefined }}{{ "" }}-{{ 0 }}{{ false }}
')($rootScope); + $rootScope.$digest(); + expect(element.text()).toEqual('-0false'); + })); + + it('should render object as JSON ignore $$', inject(function($rootScope, $compile) { + element = $compile('
{{ {key:"value", $$key:"hide"} }}
')($rootScope); + $rootScope.$digest(); + expect(fromJson(element.text())).toEqual({key:'value'}); + })); + }); + + + describe('ng:bind-template', function() { + + it('should ng:bind-template', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope); + $rootScope.name = 'Misko'; + $rootScope.$digest(); + expect(element.hasClass('ng-binding')).toEqual(true); + expect(element.text()).toEqual('Hello Misko!'); + })); + + it('should render object as JSON ignore $$', inject(function($rootScope, $compile) { + element = $compile('
{{ {key:"value", $$key:"hide"}  }}
')($rootScope); + $rootScope.$digest(); + expect(fromJson(element.text())).toEqual({key:'value'}); + })); + }); + + + describe('ng:bind-attr', function() { + it('should bind attributes', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope); + $rootScope.$digest(); + expect(element.attr('src')).toEqual('http://localhost/mysrc'); + expect(element.attr('alt')).toEqual('myalt'); + })); + + it('should not pretty print JSON in attributes', inject(function($rootScope, $compile) { + element = $compile('{{ {a:1} }}')($rootScope); + $rootScope.$digest(); + expect(element.attr('alt')).toEqual('{"a":1}'); + })); + + it('should remove special attributes on false', inject(function($rootScope, $compile) { + element = $compile('')($rootScope); + var input = element[0]; + expect(input.disabled).toEqual(false); + expect(input.readOnly).toEqual(false); + expect(input.checked).toEqual(false); + + $rootScope.disabled = true; + $rootScope.readonly = true; + $rootScope.checked = true; + $rootScope.$digest(); + + expect(input.disabled).toEqual(true); + expect(input.readOnly).toEqual(true); + expect(input.checked).toEqual(true); + })); + }); +}); diff --git a/test/directive/ngClassSpec.js b/test/directive/ngClassSpec.js new file mode 100644 index 00000000..a8b6b17e --- /dev/null +++ b/test/directive/ngClassSpec.js @@ -0,0 +1,204 @@ +'use strict'; + +describe('ng:class', function() { + var element; + + + afterEach(function() { + dealoc(element); + }); + + + it('should add new and remove old classes dynamically', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope); + $rootScope.dynClass = 'A'; + $rootScope.$digest(); + expect(element.hasClass('existing')).toBe(true); + expect(element.hasClass('A')).toBe(true); + + $rootScope.dynClass = 'B'; + $rootScope.$digest(); + expect(element.hasClass('existing')).toBe(true); + expect(element.hasClass('A')).toBe(false); + expect(element.hasClass('B')).toBe(true); + + delete $rootScope.dynClass; + $rootScope.$digest(); + expect(element.hasClass('existing')).toBe(true); + expect(element.hasClass('A')).toBe(false); + expect(element.hasClass('B')).toBe(false); + })); + + + it('should support adding multiple classes via an array', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope); + $rootScope.$digest(); + expect(element.hasClass('existing')).toBeTruthy(); + expect(element.hasClass('A')).toBeTruthy(); + expect(element.hasClass('B')).toBeTruthy(); + })); + + + it('should support adding multiple classes conditionally via a map of class names to boolean' + + 'expressions', inject(function($rootScope, $compile) { + var element = $compile( + '
' + + '
')($rootScope); + $rootScope.conditionA = true; + $rootScope.$digest(); + expect(element.hasClass('existing')).toBeTruthy(); + expect(element.hasClass('A')).toBeTruthy(); + expect(element.hasClass('B')).toBeFalsy(); + expect(element.hasClass('AnotB')).toBeTruthy(); + + $rootScope.conditionB = function() { return true }; + $rootScope.$digest(); + expect(element.hasClass('existing')).toBeTruthy(); + expect(element.hasClass('A')).toBeTruthy(); + expect(element.hasClass('B')).toBeTruthy(); + expect(element.hasClass('AnotB')).toBeFalsy(); + })); + + + it('should support adding multiple classes via a space delimited string', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope); + $rootScope.$digest(); + expect(element.hasClass('existing')).toBeTruthy(); + expect(element.hasClass('A')).toBeTruthy(); + expect(element.hasClass('B')).toBeTruthy(); + })); + + + it('should preserve class added post compilation with pre-existing classes', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope); + $rootScope.dynClass = 'A'; + $rootScope.$digest(); + expect(element.hasClass('existing')).toBe(true); + + // add extra class, change model and eval + element.addClass('newClass'); + $rootScope.dynClass = 'B'; + $rootScope.$digest(); + + expect(element.hasClass('existing')).toBe(true); + expect(element.hasClass('B')).toBe(true); + expect(element.hasClass('newClass')).toBe(true); + })); + + + it('should preserve class added post compilation without pre-existing classes"', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope); + $rootScope.dynClass = 'A'; + $rootScope.$digest(); + expect(element.hasClass('A')).toBe(true); + + // add extra class, change model and eval + element.addClass('newClass'); + $rootScope.dynClass = 'B'; + $rootScope.$digest(); + + expect(element.hasClass('B')).toBe(true); + expect(element.hasClass('newClass')).toBe(true); + })); + + + it('should preserve other classes with similar name"', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope); + $rootScope.dynCls = 'panel'; + $rootScope.$digest(); + $rootScope.dynCls = 'foo'; + $rootScope.$digest(); + expect(element[0].className).toBe('ui-panel ui-selected ng-scope foo'); + })); + + + it('should not add duplicate classes', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope); + $rootScope.dynCls = 'panel'; + $rootScope.$digest(); + expect(element[0].className).toBe('panel bar ng-scope'); + })); + + + it('should remove classes even if it was specified via class attribute', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope); + $rootScope.dynCls = 'panel'; + $rootScope.$digest(); + $rootScope.dynCls = 'window'; + $rootScope.$digest(); + expect(element[0].className).toBe('bar ng-scope window'); + })); + + + it('should remove classes even if they were added by another code', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope); + $rootScope.dynCls = 'foo'; + $rootScope.$digest(); + element.addClass('foo'); + $rootScope.dynCls = ''; + $rootScope.$digest(); + })); + + + it('should convert undefined and null values to an empty string', inject(function($rootScope, $compile) { + element = $compile('
')($rootScope); + $rootScope.dynCls = [undefined, null]; + $rootScope.$digest(); + })); + + + it('should ng:class odd/even', inject(function($rootScope, $compile) { + element = $compile('