From 2430f52bb97fa9d682e5f028c977c5bf94c5ec38 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Fri, 23 Mar 2012 14:03:24 -0700 Subject: chore(module): move files around in preparation for more modules --- test/InjectorSpec.js | 764 --------- test/ScenarioSpec.js | 32 - test/angular-mocksSpec.js | 924 ---------- test/auto/injectorSpec.js | 764 +++++++++ test/directive/aSpec.js | 46 - test/directive/booleanAttrDirSpecs.js | 125 -- test/directive/formSpec.js | 294 ---- test/directive/inputSpec.js | 1119 ------------ test/directive/ngBindSpec.js | 80 - 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 | 289 ---- test/directive/ngInitSpec.js | 16 - test/directive/ngNonBindableSpec.js | 21 - test/directive/ngPluralizeSpec.js | 136 -- test/directive/ngRepeatSpec.js | 289 ---- test/directive/ngShowHideSpec.js | 43 - test/directive/ngStyleSpec.js | 88 - test/directive/ngSwitchSpec.js | 93 - test/directive/ngViewSpec.js | 459 ----- test/directive/scriptSpec.js | 44 - test/directive/selectSpec.js | 863 ---------- test/directive/styleSpec.js | 31 - test/jstd-scenario-adapter/AdapterSpecs.js | 322 ---- test/ng/anchorScrollSpec.js | 186 ++ test/ng/browserSpecs.js | 578 +++++++ test/ng/cacheFactorySpec.js | 317 ++++ test/ng/compilerSpec.js | 1811 ++++++++++++++++++++ test/ng/controllerSpec.js | 73 + test/ng/cookieStoreSpec.js | 30 + test/ng/cookiesSpec.js | 97 ++ test/ng/deferSpec.js | 113 ++ test/ng/directive/aSpec.js | 46 + test/ng/directive/booleanAttrDirSpecs.js | 125 ++ test/ng/directive/formSpec.js | 294 ++++ test/ng/directive/inputSpec.js | 1119 ++++++++++++ test/ng/directive/ngBindSpec.js | 80 + test/ng/directive/ngClassSpec.js | 204 +++ test/ng/directive/ngClickSpec.js | 26 + test/ng/directive/ngCloakSpec.js | 49 + test/ng/directive/ngControllerSpec.js | 65 + test/ng/directive/ngEventDirsSpec.js | 25 + test/ng/directive/ngIncludeSpec.js | 289 ++++ test/ng/directive/ngInitSpec.js | 16 + test/ng/directive/ngNonBindableSpec.js | 21 + test/ng/directive/ngPluralizeSpec.js | 136 ++ test/ng/directive/ngRepeatSpec.js | 289 ++++ test/ng/directive/ngShowHideSpec.js | 43 + test/ng/directive/ngStyleSpec.js | 88 + test/ng/directive/ngSwitchSpec.js | 93 + test/ng/directive/ngViewSpec.js | 459 +++++ test/ng/directive/scriptSpec.js | 44 + test/ng/directive/selectSpec.js | 863 ++++++++++ test/ng/directive/styleSpec.js | 31 + test/ng/documentSpec.js | 9 + test/ng/exceptionHandlerSpec.js | 24 + test/ng/filter/filterSpec.js | 69 + test/ng/filter/filtersSpec.js | 280 +++ test/ng/filter/limitToSpec.js | 52 + test/ng/filter/orderBySpec.js | 34 + test/ng/httpBackendSpec.js | 241 +++ test/ng/httpSpec.js | 946 ++++++++++ test/ng/interpolateSpec.js | 113 ++ test/ng/localeSpec.js | 47 + test/ng/locationSpec.js | 883 ++++++++++ test/ng/logSpec.js | 121 ++ test/ng/parseSpec.js | 631 +++++++ test/ng/qSpec.js | 831 +++++++++ test/ng/resourceSpec.js | 325 ++++ test/ng/rootScopeSpec.js | 838 +++++++++ test/ng/routeParamsSpec.js | 20 + test/ng/routeSpec.js | 471 +++++ test/ng/sanitizeSpec.js | 281 +++ test/ng/snifferSpec.js | 33 + test/ng/windowSpec.js | 7 + test/ngMock/angular-mocksSpec.js | 924 ++++++++++ test/ngScenario/ApplicationSpec.js | 141 ++ test/ngScenario/DescribeSpec.js | 122 ++ test/ngScenario/FutureSpec.js | 77 + test/ngScenario/ObjectModelSpec.js | 333 ++++ test/ngScenario/RunnerSpec.js | 116 ++ test/ngScenario/ScenarioSpec.js | 32 + test/ngScenario/SpecRunnerSpec.js | 177 ++ test/ngScenario/dslSpec.js | 629 +++++++ test/ngScenario/e2e/Runner-compiled.html | 9 + test/ngScenario/e2e/Runner.html | 9 + test/ngScenario/e2e/style.css | 11 + test/ngScenario/e2e/widgets-scenario.js | 67 + test/ngScenario/e2e/widgets.html | 99 ++ .../jstd-scenario-adapter/AdapterSpecs.js | 322 ++++ test/ngScenario/matchersSpec.js | 51 + test/ngScenario/mocks.js | 33 + test/ngScenario/output/HtmlSpec.js | 126 ++ test/ngScenario/output/jsonSpec.js | 37 + test/ngScenario/output/objectSpec.js | 40 + test/ngScenario/output/xmlSpec.js | 36 + test/sanitizerSpec.js | 281 --- test/scenario/ApplicationSpec.js | 141 -- test/scenario/DescribeSpec.js | 122 -- test/scenario/FutureSpec.js | 77 - test/scenario/ObjectModelSpec.js | 333 ---- test/scenario/RunnerSpec.js | 116 -- test/scenario/SpecRunnerSpec.js | 177 -- test/scenario/dslSpec.js | 629 ------- test/scenario/e2e/Runner-compiled.html | 9 - test/scenario/e2e/Runner.html | 9 - test/scenario/e2e/style.css | 11 - test/scenario/e2e/widgets-scenario.js | 67 - test/scenario/e2e/widgets.html | 99 -- test/scenario/matchersSpec.js | 51 - test/scenario/mocks.js | 33 - test/scenario/output/HtmlSpec.js | 126 -- test/scenario/output/jsonSpec.js | 37 - test/scenario/output/objectSpec.js | 40 - test/scenario/output/xmlSpec.js | 36 - test/service/anchorScrollSpec.js | 186 -- test/service/browserSpecs.js | 578 ------- test/service/cacheFactorySpec.js | 317 ---- test/service/compilerSpec.js | 1811 -------------------- test/service/controllerSpec.js | 73 - test/service/cookieStoreSpec.js | 30 - test/service/cookiesSpec.js | 97 -- test/service/deferSpec.js | 113 -- test/service/documentSpec.js | 9 - test/service/exceptionHandlerSpec.js | 24 - test/service/filter/filterSpec.js | 69 - test/service/filter/filtersSpec.js | 280 --- test/service/filter/limitToSpec.js | 52 - test/service/filter/orderBySpec.js | 34 - test/service/httpBackendSpec.js | 241 --- test/service/httpSpec.js | 946 ---------- test/service/interpolateSpec.js | 113 -- test/service/localeSpec.js | 47 - test/service/locationSpec.js | 883 ---------- test/service/logSpec.js | 121 -- test/service/parseSpec.js | 631 ------- test/service/qSpec.js | 831 --------- test/service/resourceSpec.js | 325 ---- test/service/routeParamsSpec.js | 20 - test/service/routeSpec.js | 471 ----- test/service/scopeSpec.js | 838 --------- test/service/snifferSpec.js | 33 - test/service/windowSpec.js | 7 - 146 files changed, 18021 insertions(+), 18021 deletions(-) delete mode 100644 test/InjectorSpec.js delete mode 100644 test/ScenarioSpec.js delete mode 100644 test/angular-mocksSpec.js create mode 100644 test/auto/injectorSpec.js delete mode 100644 test/directive/aSpec.js delete mode 100644 test/directive/booleanAttrDirSpecs.js delete mode 100644 test/directive/formSpec.js delete mode 100644 test/directive/inputSpec.js delete mode 100644 test/directive/ngBindSpec.js delete mode 100644 test/directive/ngClassSpec.js delete mode 100644 test/directive/ngClickSpec.js delete mode 100644 test/directive/ngCloakSpec.js delete mode 100644 test/directive/ngControllerSpec.js delete mode 100644 test/directive/ngEventDirsSpec.js delete mode 100644 test/directive/ngIncludeSpec.js delete mode 100644 test/directive/ngInitSpec.js delete mode 100644 test/directive/ngNonBindableSpec.js delete mode 100644 test/directive/ngPluralizeSpec.js delete mode 100644 test/directive/ngRepeatSpec.js delete mode 100644 test/directive/ngShowHideSpec.js delete mode 100644 test/directive/ngStyleSpec.js delete mode 100644 test/directive/ngSwitchSpec.js delete mode 100644 test/directive/ngViewSpec.js delete mode 100644 test/directive/scriptSpec.js delete mode 100644 test/directive/selectSpec.js delete mode 100644 test/directive/styleSpec.js delete mode 100644 test/jstd-scenario-adapter/AdapterSpecs.js create mode 100644 test/ng/anchorScrollSpec.js create mode 100644 test/ng/browserSpecs.js create mode 100644 test/ng/cacheFactorySpec.js create mode 100644 test/ng/compilerSpec.js create mode 100644 test/ng/controllerSpec.js create mode 100644 test/ng/cookieStoreSpec.js create mode 100644 test/ng/cookiesSpec.js create mode 100644 test/ng/deferSpec.js create mode 100644 test/ng/directive/aSpec.js create mode 100644 test/ng/directive/booleanAttrDirSpecs.js create mode 100644 test/ng/directive/formSpec.js create mode 100644 test/ng/directive/inputSpec.js create mode 100644 test/ng/directive/ngBindSpec.js create mode 100644 test/ng/directive/ngClassSpec.js create mode 100644 test/ng/directive/ngClickSpec.js create mode 100644 test/ng/directive/ngCloakSpec.js create mode 100644 test/ng/directive/ngControllerSpec.js create mode 100644 test/ng/directive/ngEventDirsSpec.js create mode 100644 test/ng/directive/ngIncludeSpec.js create mode 100644 test/ng/directive/ngInitSpec.js create mode 100644 test/ng/directive/ngNonBindableSpec.js create mode 100644 test/ng/directive/ngPluralizeSpec.js create mode 100644 test/ng/directive/ngRepeatSpec.js create mode 100644 test/ng/directive/ngShowHideSpec.js create mode 100644 test/ng/directive/ngStyleSpec.js create mode 100644 test/ng/directive/ngSwitchSpec.js create mode 100644 test/ng/directive/ngViewSpec.js create mode 100644 test/ng/directive/scriptSpec.js create mode 100644 test/ng/directive/selectSpec.js create mode 100644 test/ng/directive/styleSpec.js create mode 100644 test/ng/documentSpec.js create mode 100644 test/ng/exceptionHandlerSpec.js create mode 100644 test/ng/filter/filterSpec.js create mode 100644 test/ng/filter/filtersSpec.js create mode 100644 test/ng/filter/limitToSpec.js create mode 100644 test/ng/filter/orderBySpec.js create mode 100644 test/ng/httpBackendSpec.js create mode 100644 test/ng/httpSpec.js create mode 100644 test/ng/interpolateSpec.js create mode 100644 test/ng/localeSpec.js create mode 100644 test/ng/locationSpec.js create mode 100644 test/ng/logSpec.js create mode 100644 test/ng/parseSpec.js create mode 100644 test/ng/qSpec.js create mode 100644 test/ng/resourceSpec.js create mode 100644 test/ng/rootScopeSpec.js create mode 100644 test/ng/routeParamsSpec.js create mode 100644 test/ng/routeSpec.js create mode 100644 test/ng/sanitizeSpec.js create mode 100644 test/ng/snifferSpec.js create mode 100644 test/ng/windowSpec.js create mode 100644 test/ngMock/angular-mocksSpec.js create mode 100644 test/ngScenario/ApplicationSpec.js create mode 100644 test/ngScenario/DescribeSpec.js create mode 100644 test/ngScenario/FutureSpec.js create mode 100644 test/ngScenario/ObjectModelSpec.js create mode 100644 test/ngScenario/RunnerSpec.js create mode 100644 test/ngScenario/ScenarioSpec.js create mode 100644 test/ngScenario/SpecRunnerSpec.js create mode 100644 test/ngScenario/dslSpec.js create mode 100644 test/ngScenario/e2e/Runner-compiled.html create mode 100644 test/ngScenario/e2e/Runner.html create mode 100644 test/ngScenario/e2e/style.css create mode 100644 test/ngScenario/e2e/widgets-scenario.js create mode 100644 test/ngScenario/e2e/widgets.html create mode 100644 test/ngScenario/jstd-scenario-adapter/AdapterSpecs.js create mode 100644 test/ngScenario/matchersSpec.js create mode 100644 test/ngScenario/mocks.js create mode 100644 test/ngScenario/output/HtmlSpec.js create mode 100644 test/ngScenario/output/jsonSpec.js create mode 100644 test/ngScenario/output/objectSpec.js create mode 100644 test/ngScenario/output/xmlSpec.js delete mode 100644 test/sanitizerSpec.js delete mode 100644 test/scenario/ApplicationSpec.js delete mode 100644 test/scenario/DescribeSpec.js delete mode 100644 test/scenario/FutureSpec.js delete mode 100644 test/scenario/ObjectModelSpec.js delete mode 100644 test/scenario/RunnerSpec.js delete mode 100644 test/scenario/SpecRunnerSpec.js delete mode 100644 test/scenario/dslSpec.js delete mode 100644 test/scenario/e2e/Runner-compiled.html delete mode 100644 test/scenario/e2e/Runner.html delete mode 100644 test/scenario/e2e/style.css delete mode 100644 test/scenario/e2e/widgets-scenario.js delete mode 100644 test/scenario/e2e/widgets.html delete mode 100644 test/scenario/matchersSpec.js delete mode 100644 test/scenario/mocks.js delete mode 100644 test/scenario/output/HtmlSpec.js delete mode 100644 test/scenario/output/jsonSpec.js delete mode 100644 test/scenario/output/objectSpec.js delete mode 100644 test/scenario/output/xmlSpec.js delete mode 100644 test/service/anchorScrollSpec.js delete mode 100644 test/service/browserSpecs.js delete mode 100644 test/service/cacheFactorySpec.js delete mode 100644 test/service/compilerSpec.js delete mode 100644 test/service/controllerSpec.js delete mode 100644 test/service/cookieStoreSpec.js delete mode 100644 test/service/cookiesSpec.js delete mode 100644 test/service/deferSpec.js delete mode 100644 test/service/documentSpec.js delete mode 100644 test/service/exceptionHandlerSpec.js delete mode 100644 test/service/filter/filterSpec.js delete mode 100644 test/service/filter/filtersSpec.js delete mode 100644 test/service/filter/limitToSpec.js delete mode 100644 test/service/filter/orderBySpec.js delete mode 100644 test/service/httpBackendSpec.js delete mode 100644 test/service/httpSpec.js delete mode 100644 test/service/interpolateSpec.js delete mode 100644 test/service/localeSpec.js delete mode 100644 test/service/locationSpec.js delete mode 100644 test/service/logSpec.js delete mode 100644 test/service/parseSpec.js delete mode 100644 test/service/qSpec.js delete mode 100644 test/service/resourceSpec.js delete mode 100644 test/service/routeParamsSpec.js delete mode 100644 test/service/routeSpec.js delete mode 100644 test/service/scopeSpec.js delete mode 100644 test/service/snifferSpec.js delete mode 100644 test/service/windowSpec.js (limited to 'test') diff --git a/test/InjectorSpec.js b/test/InjectorSpec.js deleted file mode 100644 index 16cf1524..00000000 --- a/test/InjectorSpec.js +++ /dev/null @@ -1,764 +0,0 @@ -'use strict'; - -describe('injector', function() { - var providers; - var injector; - - beforeEach(module(function($provide) { - providers = function(name, factory, annotations) { - $provide.factory(name, extend(factory, annotations||{})); - }; - })); - beforeEach(inject(function($injector){ - injector = $injector; - })); - - - it("should return same instance from calling provider", function() { - var instance = {}, - original = instance; - providers('instance', function() { return instance; }); - expect(injector.get('instance')).toEqual(instance); - instance = 'deleted'; - expect(injector.get('instance')).toEqual(original); - }); - - - it('should inject providers', function() { - providers('a', function() {return 'Mi';}); - providers('b', function(mi) {return mi+'sko';}, {$inject:['a']}); - expect(injector.get('b')).toEqual('Misko'); - }); - - - it('should resolve dependency graph and instantiate all services just once', function() { - var log = []; - -// s1 -// / | \ -// / s2 \ -// / / | \ \ -// /s3 < s4 > s5 -// // -// s6 - - - providers('s1', function() { log.push('s1'); }, {$inject: ['s2', 's5', 's6']}); - providers('s2', function() { log.push('s2'); }, {$inject: ['s3', 's4', 's5']}); - providers('s3', function() { log.push('s3'); }, {$inject: ['s6']}); - providers('s4', function() { log.push('s4'); }, {$inject: ['s3', 's5']}); - providers('s5', function() { log.push('s5'); }); - providers('s6', function() { log.push('s6'); }); - - injector.get('s1'); - - expect(log).toEqual(['s6', 's3', 's5', 's4', 's2', 's1']); - }); - - - it('should provide useful message if no provider', function() { - expect(function() { - injector.get('idontexist'); - }).toThrow("Unknown provider: idontexistProvider <- idontexist"); - }); - - - it('should proved path to the missing provider', function() { - providers('a', function(idontexist) {return 1;}); - providers('b', function(a) {return 2;}); - expect(function() { - injector.get('b'); - }).toThrow("Unknown provider: idontexistProvider <- idontexist <- a <- b"); - }); - - - describe('invoke', function() { - var args; - - beforeEach(function() { - args = null; - providers('a', function() {return 1;}); - providers('b', function() {return 2;}); - }); - - - function fn(a, b, c, d) { - args = [this, a, b, c, d]; - return a + b + c + d; - } - - - it('should call function', function() { - fn.$inject = ['a', 'b', 'c', 'd']; - injector.invoke(fn, {name:"this"}, {c:3, d:4}); - expect(args).toEqual([{name:'this'}, 1, 2, 3, 4]); - }); - - - it('should treat array as annotations', function() { - injector.invoke(['a', 'b', 'c', 'd', fn], {name:"this"}, {c:3, d:4}); - expect(args).toEqual([{name:'this'}, 1, 2, 3, 4]); - }); - - - it('should invoke the passed-in fn with all of the dependencies as arguments', function() { - providers('c', function() {return 3;}); - providers('d', function() {return 4;}); - expect(injector.invoke(['a', 'b', 'c', 'd', fn])).toEqual(10); - }); - - - it('should fail with errors if not function or array', function() { - expect(function() { - injector.invoke({}); - }).toThrow("Argument 'fn' is not a function, got Object"); - expect(function() { - injector.invoke(['a', 123], {}); - }).toThrow("Argument 'fn' is not a function, got number"); - }); - }); - - - describe('annotation', function() { - it('should return $inject', function() { - function fn() {} - fn.$inject = ['a']; - expect(inferInjectionArgs(fn)).toBe(fn.$inject); - expect(inferInjectionArgs(function() {})).toEqual([]); - expect(inferInjectionArgs(function () {})).toEqual([]); - expect(inferInjectionArgs(function () {})).toEqual([]); - expect(inferInjectionArgs(function /* */ () {})).toEqual([]); - }); - - - it('should create $inject', function() { - // keep the multi-line to make sure we can handle it - function $f_n0 /* - */( - $a, // x, <-- looks like an arg but it is a comment - b_, /* z, <-- looks like an arg but it is a - multi-line comment - function (a, b) {} - */ - _c, - /* {some type} */ d) { extraParans();} - expect(inferInjectionArgs($f_n0)).toEqual(['$a', 'b_', '_c', 'd']); - expect($f_n0.$inject).toEqual(['$a', 'b_', '_c', 'd']); - }); - - - it('should strip leading and trailing underscores from arg name during inference', function() { - function beforeEachFn(_foo_) { /* foo = _foo_ */ }; - expect(inferInjectionArgs(beforeEachFn)).toEqual(['foo']); - }); - - - it('should handle no arg functions', function() { - function $f_n0() {} - expect(inferInjectionArgs($f_n0)).toEqual([]); - expect($f_n0.$inject).toEqual([]); - }); - - - it('should handle args with both $ and _', function() { - function $f_n0($a_) {} - expect(inferInjectionArgs($f_n0)).toEqual(['$a_']); - expect($f_n0.$inject).toEqual(['$a_']); - }); - - - it('should throw on non function arg', function() { - expect(function() { - inferInjectionArgs({}); - }).toThrow(); - }); - }); - - - it('should have $injector', function() { - var $injector = createInjector(); - expect($injector.get('$injector')).toBe($injector); - }); - - - it('should define module', function() { - var log = ''; - var injector = createInjector([function($provide) { - $provide.value('value', 'value;'); - $provide.factory('fn', valueFn('function;')); - $provide.provider('service', function() { - this.$get = valueFn('service;'); - }); - }, function(valueProvider, fnProvider, serviceProvider) { - log += valueProvider.$get() + fnProvider.$get() + serviceProvider.$get(); - }]).invoke(function(value, fn, service) { - log += '->' + value + fn + service; - }); - expect(log).toEqual('value;function;service;->value;function;service;'); - }); - - - describe('module', function() { - it('should provide $injector even when no module is requested', function() { - var $provide, - $injector = createInjector([ - angular.extend(function(p) { $provide = p; }, {$inject: ['$provide']}) - ]); - expect($injector.get('$injector')).toBe($injector); - }); - - - it('should load multiple function modules and infer inject them', function() { - var a = 'junk'; - var $injector = createInjector([ - function() { - a = 'A'; // reset to prove we ran - }, - function($provide) { - $provide.value('a', a); - }, - angular.extend(function(p, serviceA) { - p.value('b', serviceA.$get() + 'B' ); - }, {$inject:['$provide', 'aProvider']}), - ['$provide', 'bProvider', function(p, serviceB) { - p.value('c', serviceB.$get() + 'C'); - }] - ]); - expect($injector.get('a')).toEqual('A'); - expect($injector.get('b')).toEqual('AB'); - expect($injector.get('c')).toEqual('ABC'); - }); - - - it('should run symbolic modules', function() { - angularModule('myModule', []).value('a', 'abc'); - var $injector = createInjector(['myModule']); - expect($injector.get('a')).toEqual('abc'); - }); - - - it('should error on invalid module name', function() { - expect(function() { - createInjector(['IDontExist'], {}); - }).toThrow("No module: IDontExist"); - }); - - - it('should load dependant modules only once', function() { - var log = ''; - angular.module('a', [], function(){ log += 'a'; }); - angular.module('b', ['a'], function(){ log += 'b'; }); - angular.module('c', ['a', 'b'], function(){ log += 'c'; }); - createInjector(['c', 'c']); - expect(log).toEqual('abc'); - }); - - it('should execute runBlocks after injector creation', function() { - var log = ''; - angular.module('a', [], function(){ log += 'a'; }).run(function() { log += 'A'; }); - angular.module('b', ['a'], function(){ log += 'b'; }).run(function() { log += 'B'; }); - createInjector([ - 'b', - valueFn(function() { log += 'C'; }), - [valueFn(function() { log += 'D'; })] - ]); - expect(log).toEqual('abABCD'); - }); - - describe('$provide', function() { - describe('constant', function() { - it('should create configuration injectable constants', function() { - var log = []; - createInjector([ - function($provide){ - $provide.constant('abc', 123); - $provide.constant({a: 'A', b:'B'}); - return function(a) { - log.push(a); - } - }, - function(abc) { - log.push(abc); - return function(b) { - log.push(b); - } - } - ]).get('abc'); - expect(log).toEqual([123, 'A', 'B']); - }); - }); - - - describe('value', function() { - it('should configure $provide values', function() { - expect(createInjector([function($provide) { - $provide.value('value', 'abc'); - }]).get('value')).toEqual('abc'); - }); - - - it('should configure a set of values', function() { - expect(createInjector([function($provide) { - $provide.value({value: Array}); - }]).get('value')).toEqual(Array); - }); - }); - - - describe('factory', function() { - it('should configure $provide factory function', function() { - expect(createInjector([function($provide) { - $provide.factory('value', valueFn('abc')); - }]).get('value')).toEqual('abc'); - }); - - - it('should configure a set of factories', function() { - expect(createInjector([function($provide) { - $provide.factory({value: Array}); - }]).get('value')).toEqual([]); - }); - }); - - - describe('service', function() { - it('should register a class', function() { - var Type = function(value) { - this.value = value; - }; - - var instance = createInjector([function($provide) { - $provide.value('value', 123); - $provide.service('foo', Type); - }]).get('foo'); - - expect(instance instanceof Type).toBe(true); - expect(instance.value).toBe(123); - }); - - - it('should register a set of classes', function() { - var Type = function() {}; - - var injector = createInjector([function($provide) { - $provide.service({ - foo: Type, - bar: Type - }); - }]); - - expect(injector.get('foo') instanceof Type).toBe(true); - expect(injector.get('bar') instanceof Type).toBe(true); - }); - }); - - - describe('provider', function() { - it('should configure $provide provider object', function() { - expect(createInjector([function($provide) { - $provide.provider('value', { - $get: valueFn('abc') - }); - }]).get('value')).toEqual('abc'); - }); - - - it('should configure $provide provider type', function() { - function Type() {}; - Type.prototype.$get = function() { - expect(this instanceof Type).toBe(true); - return 'abc'; - }; - expect(createInjector([function($provide) { - $provide.provider('value', Type); - }]).get('value')).toEqual('abc'); - }); - - - it('should configure a set of providers', function() { - expect(createInjector([function($provide) { - $provide.provider({value: valueFn({$get:Array})}); - }]).get('value')).toEqual([]); - }); - }); - - - describe('decorator', function() { - var log, injector; - - beforeEach(function() { - log = []; - }); - - - it('should be called with the original instance', function() { - injector = createInjector([function($provide) { - $provide.value('myService', function(val) { - log.push('myService:' + val); - return 'origReturn'; - }); - - $provide.decorator('myService', function($delegate) { - return function(val) { - log.push('myDecoratedService:' + val); - var origVal = $delegate('decInput'); - return 'dec+' + origVal; - }; - }); - }]); - - var out = injector.get('myService')('input'); - log.push(out); - expect(log.join('; ')). - toBe('myDecoratedService:input; myService:decInput; dec+origReturn'); - }); - - - it('should allow multiple decorators to be applied to a service', function() { - injector = createInjector([function($provide) { - $provide.value('myService', function(val) { - log.push('myService:' + val); - return 'origReturn'; - }); - - $provide.decorator('myService', function($delegate) { - return function(val) { - log.push('myDecoratedService1:' + val); - var origVal = $delegate('decInput1'); - return 'dec1+' + origVal; - }; - }); - - $provide.decorator('myService', function($delegate) { - return function(val) { - log.push('myDecoratedService2:' + val); - var origVal = $delegate('decInput2'); - return 'dec2+' + origVal; - }; - }); - }]); - - var out = injector.get('myService')('input'); - log.push(out); - expect(log).toEqual(['myDecoratedService2:input', - 'myDecoratedService1:decInput2', - 'myService:decInput1', - 'dec2+dec1+origReturn']); - }); - - - it('should decorate services with dependencies', function() { - injector = createInjector([function($provide) { - $provide.value('dep1', 'dependency1'); - - $provide.factory('myService', ['dep1', function(dep1) { - return function(val) { - log.push('myService:' + val + ',' + dep1); - return 'origReturn'; - } - }]); - - $provide.decorator('myService', function($delegate) { - return function(val) { - log.push('myDecoratedService:' + val); - var origVal = $delegate('decInput'); - return 'dec+' + origVal; - }; - }); - }]); - - var out = injector.get('myService')('input'); - log.push(out); - expect(log.join('; ')). - toBe('myDecoratedService:input; myService:decInput,dependency1; dec+origReturn'); - }); - - - it('should allow for decorators to be injectable', function() { - injector = createInjector([function($provide) { - $provide.value('dep1', 'dependency1'); - - $provide.factory('myService', function() { - return function(val) { - log.push('myService:' + val); - return 'origReturn'; - } - }); - - $provide.decorator('myService', function($delegate, dep1) { - return function(val) { - log.push('myDecoratedService:' + val + ',' + dep1); - var origVal = $delegate('decInput'); - return 'dec+' + origVal; - }; - }); - }]); - - var out = injector.get('myService')('input'); - log.push(out); - expect(log.join('; ')). - toBe('myDecoratedService:input,dependency1; myService:decInput; dec+origReturn'); - }); - }); - }); - - - describe('error handling', function() { - it('should handle wrong argument type', function() { - expect(function() { - createInjector([ - {} - ], {}); - }).toThrow("Argument 'module' is not a function, got Object"); - }); - - - it('should handle exceptions', function() { - expect(function() { - createInjector([function() { - throw 'MyError'; - }], {}); - }).toThrow('MyError'); - }); - - - it('should decorate the missing service error with module name', function() { - angular.module('TestModule', [], function($injector) {}); - expect(function() { - createInjector(['TestModule']); - }).toThrow('Unknown provider: $injector from TestModule'); - }); - - - it('should decorate the missing service error with module function', function() { - function myModule($injector){} - expect(function() { - createInjector([myModule]); - }).toThrow('Unknown provider: $injector from ' + myModule); - }); - - - it('should decorate the missing service error with module array function', function() { - function myModule($injector){} - expect(function() { - createInjector([['$injector', myModule]]); - }).toThrow('Unknown provider: $injector from ' + myModule); - }); - - - it('should throw error when trying to inject oneself', function() { - expect(function() { - createInjector([function($provide){ - $provide.factory('service', function(service){}); - return function(service) {} - }]) - }).toThrow('Circular dependency: service'); - }); - - - it('should throw error when trying to inject circular dependency', function() { - expect(function() { - createInjector([function($provide){ - $provide.factory('a', function(b){}); - $provide.factory('b', function(a){}); - return function(a) {} - }]) - }).toThrow('Circular dependency: b <- a'); - }); - }); - }); - - - describe('retrieval', function() { - var instance, - $injector, - $provide; - - beforeEach(function() { - $injector = createInjector([ ['$provide', function(provide) { - ($provide = provide).value('instance', instance = {name:'angular'}); - }]]); - }); - - - it('should retrieve by name and cache instance', function() { - expect(instance).toEqual({name: 'angular'}); - expect($injector.get('instance')).toBe(instance); - expect($injector.get('instance')).toBe(instance); - }); - - - it('should call functions and infer arguments', function() { - expect($injector.invoke(function(instance) { return instance; })).toBe(instance); - expect($injector.invoke(function(instance) { return instance; })).toBe(instance); - }); - }); - - - describe('method invoking', function() { - var $injector; - - beforeEach(function() { - $injector = createInjector([ function($provide) { - $provide.value('book', 'moby'); - $provide.value('author', 'melville'); - }]); - }); - - - it('should invoke method', function() { - expect($injector.invoke(function(book, author) { - return author + ':' + book; - })).toEqual('melville:moby'); - expect($injector.invoke(function(book, author) { - expect(this).toEqual($injector); - return author + ':' + book;}, $injector)).toEqual('melville:moby'); - }); - - - it('should invoke method with locals', function() { - expect($injector.invoke(function(book, author) { - return author + ':' + book; - })).toEqual('melville:moby'); - expect($injector.invoke( - function(book, author, chapter) { - expect(this).toEqual($injector); - return author + ':' + book + '-' + chapter; - }, $injector, {author:'m', chapter:'ch1'})).toEqual('m:moby-ch1'); - }); - - - it('should invoke method which is annotated', function() { - expect($injector.invoke(extend(function(b, a) { - return a + ':' + b - }, {$inject:['book', 'author']}))).toEqual('melville:moby'); - expect($injector.invoke(extend(function(b, a) { - expect(this).toEqual($injector); - return a + ':' + b; - }, {$inject:['book', 'author']}), $injector)).toEqual('melville:moby'); - }); - - - it('should invoke method which is an array of annotation', function() { - expect($injector.invoke(function(book, author) { - return author + ':' + book; - })).toEqual('melville:moby'); - expect($injector.invoke(function(book, author) { - expect(this).toEqual($injector); - return author + ':' + book; - }, $injector)).toEqual('melville:moby'); - }); - - - it('should throw usefull error on wrong argument type]', function() { - expect(function() { - $injector.invoke({}); - }).toThrow("Argument 'fn' is not a function, got Object"); - }); - }); - - - describe('service instantiation', function() { - var $injector; - - beforeEach(function() { - $injector = createInjector([ function($provide) { - $provide.value('book', 'moby'); - $provide.value('author', 'melville'); - }]); - }); - - - function Type(book, author) { - this.book = book; - this.author = author; - } - Type.prototype.title = function() { - return this.author + ': ' + this.book; - }; - - - it('should instantiate object and preserve constructor property and be instanceof', function() { - var t = $injector.instantiate(Type); - expect(t.book).toEqual('moby'); - expect(t.author).toEqual('melville'); - expect(t.title()).toEqual('melville: moby'); - expect(t instanceof Type).toBe(true); - }); - - - it('should instantiate object and preserve constructor property and be instanceof ' + - 'with the array annotated type', function() { - var t = $injector.instantiate(['book', 'author', Type]); - expect(t.book).toEqual('moby'); - expect(t.author).toEqual('melville'); - expect(t.title()).toEqual('melville: moby'); - expect(t instanceof Type).toBe(true); - }); - - - it('should allow constructor to return different object', function() { - var obj = {}; - var Class = function() { - return obj; - }; - - expect($injector.instantiate(Class)).toBe(obj); - }); - - - it('should handle constructor exception', function() { - expect(function() { - $injector.instantiate(function() { throw 'MyError'; }); - }).toThrow('MyError'); - }); - - - it('should return instance if constructor returns non-object value', function() { - var A = function() { - return 10; - }; - - var B = function() { - return 'some-string'; - }; - - var C = function() { - return undefined; - }; - - expect($injector.instantiate(A) instanceof A).toBe(true); - expect($injector.instantiate(B) instanceof B).toBe(true); - expect($injector.instantiate(C) instanceof C).toBe(true); - }); - }); - - describe('protection modes', function() { - it('should prevent provider lookup in app', function() { - var $injector = createInjector([function($provide) { - $provide.value('name', 'angular'); - }]); - expect(function() { - $injector.get('nameProvider'); - }).toThrow("Unknown provider: nameProviderProvider <- nameProvider"); - }); - - - it('should prevent provider configuration in app', function() { - var $injector = createInjector([]); - expect(function() { - $injector.get('$provide').value('a', 'b'); - }).toThrow("Unknown provider: $provideProvider <- $provide"); - }); - - - it('should prevent instance lookup in module', function() { - function instanceLookupInModule(name) { throw Error('FAIL'); } - expect(function() { - createInjector([function($provide) { - $provide.value('name', 'angular') - }, instanceLookupInModule]); - }).toThrow('Unknown provider: name from ' + String(instanceLookupInModule)); - }); - }); -}); diff --git a/test/ScenarioSpec.js b/test/ScenarioSpec.js deleted file mode 100644 index cc2efd1e..00000000 --- a/test/ScenarioSpec.js +++ /dev/null @@ -1,32 +0,0 @@ -'use strict'; - -describe("ScenarioSpec: Compilation", function() { - var element; - - afterEach(function() { - dealoc(element); - }); - - - describe('compilation', function() { - it("should compile dom node and return scope", inject(function($rootScope, $compile) { - var node = jqLite('
{{b=a+1}}
')[0]; - element = $compile(node)($rootScope); - $rootScope.$digest(); - expect($rootScope.a).toEqual(1); - expect($rootScope.b).toEqual(2); - })); - - it("should compile jQuery node and return scope", inject(function($rootScope, $compile) { - element = $compile(jqLite('
{{a=123}}
'))($rootScope); - $rootScope.$digest(); - expect(jqLite(element).text()).toEqual('123'); - })); - - it("should compile text node and return scope", inject(function($rootScope, $compile) { - element = $compile('
{{a=123}}
')($rootScope); - $rootScope.$digest(); - expect(jqLite(element).text()).toEqual('123'); - })); - }); -}); diff --git a/test/angular-mocksSpec.js b/test/angular-mocksSpec.js deleted file mode 100644 index 2527ca48..00000000 --- a/test/angular-mocksSpec.js +++ /dev/null @@ -1,924 +0,0 @@ -'use strict'; - -describe('ngMock', function() { - - describe('$browser', function() { - - describe('addJs', function() { - - it('should store url, done', inject(function($browser) { - var url = 'some.js', - done = noop; - - $browser.addJs(url, done); - - var script = $browser.$$scripts.shift(); - expect(script.url).toBe(url); - expect(script.done).toBe(done); - })); - - - it('should return the script object', inject(function($browser) { - expect($browser.addJs('some.js', null, noop)).toBe($browser.$$scripts[0]); - })); - }); - }); - - - describe('TzDate', function() { - - function minutes(min) { - return min*60*1000; - } - - it('should look like a Date', function() { - var date = new angular.mock.TzDate(0,0); - expect(angular.isDate(date)).toBe(true); - }); - - it('should take millis as constructor argument', function() { - expect(new angular.mock.TzDate(0, 0).getTime()).toBe(0); - expect(new angular.mock.TzDate(0, 1283555108000).getTime()).toBe(1283555108000); - }); - - it('should take dateString as constructor argument', function() { - expect(new angular.mock.TzDate(0, '1970-01-01T00:00:00.000Z').getTime()).toBe(0); - expect(new angular.mock.TzDate(0, '2010-09-03T23:05:08.023Z').getTime()).toBe(1283555108023); - }); - - - it('should fake getLocalDateString method', function() { - //0 in -3h - var t0 = new angular.mock.TzDate(-3, 0); - expect(t0.toLocaleDateString()).toMatch('1970'); - - //0 in +0h - var t1 = new angular.mock.TzDate(0, 0); - expect(t1.toLocaleDateString()).toMatch('1970'); - - //0 in +3h - var t2 = new angular.mock.TzDate(3, 0); - expect(t2.toLocaleDateString()).toMatch('1969'); - }); - - - it('should fake getHours method', function() { - //0 in -3h - var t0 = new angular.mock.TzDate(-3, 0); - expect(t0.getHours()).toBe(3); - - //0 in +0h - var t1 = new angular.mock.TzDate(0, 0); - expect(t1.getHours()).toBe(0); - - //0 in +3h - var t2 = new angular.mock.TzDate(3, 0); - expect(t2.getHours()).toMatch(21); - }); - - - it('should fake getMinutes method', function() { - //0:15 in -3h - var t0 = new angular.mock.TzDate(-3, minutes(15)); - expect(t0.getMinutes()).toBe(15); - - //0:15 in -3.25h - var t0a = new angular.mock.TzDate(-3.25, minutes(15)); - expect(t0a.getMinutes()).toBe(30); - - //0 in +0h - var t1 = new angular.mock.TzDate(0, minutes(0)); - expect(t1.getMinutes()).toBe(0); - - //0:15 in +0h - var t1a = new angular.mock.TzDate(0, minutes(15)); - expect(t1a.getMinutes()).toBe(15); - - //0:15 in +3h - var t2 = new angular.mock.TzDate(3, minutes(15)); - expect(t2.getMinutes()).toMatch(15); - - //0:15 in +3.25h - var t2a = new angular.mock.TzDate(3.25, minutes(15)); - expect(t2a.getMinutes()).toMatch(0); - }); - - - it('should fake getSeconds method', function() { - //0 in -3h - var t0 = new angular.mock.TzDate(-3, 0); - expect(t0.getSeconds()).toBe(0); - - //0 in +0h - var t1 = new angular.mock.TzDate(0, 0); - expect(t1.getSeconds()).toBe(0); - - //0 in +3h - var t2 = new angular.mock.TzDate(3, 0); - expect(t2.getSeconds()).toMatch(0); - }); - - - it('should create a date representing new year in Bratislava', function() { - var newYearInBratislava = new angular.mock.TzDate(-1, '2009-12-31T23:00:00.000Z'); - expect(newYearInBratislava.getTimezoneOffset()).toBe(-60); - expect(newYearInBratislava.getFullYear()).toBe(2010); - expect(newYearInBratislava.getMonth()).toBe(0); - expect(newYearInBratislava.getDate()).toBe(1); - expect(newYearInBratislava.getHours()).toBe(0); - expect(newYearInBratislava.getMinutes()).toBe(0); - }); - - - it('should delegate all the UTC methods to the original UTC Date object', function() { - //from when created from string - var date1 = new angular.mock.TzDate(-1, '2009-12-31T23:00:00.000Z'); - expect(date1.getUTCFullYear()).toBe(2009); - expect(date1.getUTCMonth()).toBe(11); - expect(date1.getUTCDate()).toBe(31); - expect(date1.getUTCHours()).toBe(23); - expect(date1.getUTCMinutes()).toBe(0); - expect(date1.getUTCSeconds()).toBe(0); - - - //from when created from millis - var date2 = new angular.mock.TzDate(-1, date1.getTime()); - expect(date2.getUTCFullYear()).toBe(2009); - expect(date2.getUTCMonth()).toBe(11); - expect(date2.getUTCDate()).toBe(31); - expect(date2.getUTCHours()).toBe(23); - expect(date2.getUTCMinutes()).toBe(0); - expect(date2.getUTCSeconds()).toBe(0); - }); - - - it('should throw error when no third param but toString called', function() { - expect(function() { new angular.mock.TzDate(0,0).toString(); }). - toThrow('Method \'toString\' is not implemented in the TzDate mock'); - }); - }); - - describe('$log', function() { - var $log; - beforeEach(inject(['$log', function(log) { - $log = log; - }])); - - afterEach(inject(function($log){ - $log.reset(); - })); - - it('should provide log method', function() { - expect(function() { $log.log(''); }).not.toThrow(); - }); - - it('should provide info method', function() { - expect(function() { $log.info(''); }).not.toThrow(); - }); - - it('should provide warn method', function() { - expect(function() { $log.warn(''); }).not.toThrow(); - }); - - it('should provide error method', function() { - expect(function() { $log.error(''); }).not.toThrow(); - }); - - it('should store log messages', function() { - $log.log('fake log'); - expect($log.log.logs).toContain(['fake log']); - }); - - it('should store info messages', function() { - $log.info('fake log'); - expect($log.info.logs).toContain(['fake log']); - }); - - it('should store warn messages', function() { - $log.warn('fake log'); - expect($log.warn.logs).toContain(['fake log']); - }); - - it('should store error messages', function() { - $log.error('fake log'); - expect($log.error.logs).toContain(['fake log']); - }); - - it('should assertEmpty', function(){ - try { - $log.error(Error('MyError')); - $log.warn(Error('MyWarn')); - $log.info(Error('MyInfo')); - $log.log(Error('MyLog')); - $log.assertEmpty(); - } catch (error) { - error = error.message || error; - expect(error).toMatch(/Error: MyError/m); - expect(error).toMatch(/Error: MyWarn/m); - expect(error).toMatch(/Error: MyInfo/m); - expect(error).toMatch(/Error: MyLog/m); - } finally { - $log.reset(); - } - }); - - it('should reset state', function(){ - $log.error(Error('MyError')); - $log.warn(Error('MyWarn')); - $log.info(Error('MyInfo')); - $log.log(Error('MyLog')); - $log.reset(); - var passed = false; - try { - $log.assertEmpty(); // should not throw error! - passed = true; - } catch (e) { - passed = e; - } - expect(passed).toBe(true); - }); - }); - - describe('defer', function() { - var browser, log; - beforeEach(inject(function($browser) { - browser = $browser; - log = ''; - })); - - function logFn(text){ return function() { - log += text +';'; - }; - } - - it('should flush', function() { - browser.defer(logFn('A')); - expect(log).toEqual(''); - browser.defer.flush(); - expect(log).toEqual('A;'); - }); - - it('should flush delayed', function() { - browser.defer(logFn('A')); - browser.defer(logFn('B'), 10); - browser.defer(logFn('C'), 20); - expect(log).toEqual(''); - - expect(browser.defer.now).toEqual(0); - browser.defer.flush(0); - expect(log).toEqual('A;'); - - browser.defer.flush(); - expect(log).toEqual('A;B;C;'); - }); - - it('should defer and flush over time', function() { - browser.defer(logFn('A'), 1); - browser.defer(logFn('B'), 2); - browser.defer(logFn('C'), 3); - - browser.defer.flush(0); - expect(browser.defer.now).toEqual(0); - expect(log).toEqual(''); - - browser.defer.flush(1); - expect(browser.defer.now).toEqual(1); - expect(log).toEqual('A;'); - - browser.defer.flush(2); - expect(browser.defer.now).toEqual(3); - expect(log).toEqual('A;B;C;'); - }); - - it('should throw an exception if there is nothing to be flushed', function() { - expect(function() {browser.defer.flush();}).toThrow('No deferred tasks to be flushed'); - }); - }); - - - describe('$exceptionHandler', function() { - it('should rethrow exceptions', inject(function($exceptionHandler) { - expect(function() { $exceptionHandler('myException'); }).toThrow('myException'); - })); - - - it('should log exceptions', module(function($exceptionHandlerProvider){ - $exceptionHandlerProvider.mode('log'); - var $exceptionHandler = $exceptionHandlerProvider.$get(); - $exceptionHandler('MyError'); - expect($exceptionHandler.errors).toEqual(['MyError']); - - $exceptionHandler('MyError', 'comment'); - expect($exceptionHandler.errors[1]).toEqual(['MyError', 'comment']); - })); - - - it('should throw on wrong argument', module(function($exceptionHandlerProvider) { - expect(function() { - $exceptionHandlerProvider.mode('XXX'); - }).toThrow("Unknown mode 'XXX', only 'log'/'rethrow' modes are allowed!"); - })); - }); - - - describe('angular.mock.dump', function(){ - var d = angular.mock.dump; - - - it('should serialize primitive types', function(){ - expect(d(undefined)).toEqual('undefined'); - expect(d(1)).toEqual('1'); - expect(d(null)).toEqual('null'); - expect(d('abc')).toEqual('abc'); - }); - - - it('should serialize element', function(){ - var e = angular.element('
abc
xyz'); - expect(d(e).toLowerCase()).toEqual('
abc
xyz'); - expect(d(e[0]).toLowerCase()).toEqual('
abc
'); - }); - - it('should serialize scope', inject(function($rootScope){ - $rootScope.obj = {abc:'123'}; - expect(d($rootScope)).toMatch(/Scope\(.*\): \{/); - expect(d($rootScope)).toMatch(/{"abc":"123"}/); - })); - - - it('should be published on window', function(){ - expect(window.dump instanceof Function).toBe(true); - }); - }); - - describe('jasmine module and inject', function(){ - var log; - - beforeEach(function(){ - log = ''; - }); - - describe('module', function() { - describe('in DSL', function() { - it('should load module', module(function() { - log += 'module'; - })); - - afterEach(function() { - inject(); - expect(log).toEqual('module'); - }); - }); - - - describe('inline in test', function() { - it('should load module', function() { - module(function() { - log += 'module'; - }); - inject(); - }); - - afterEach(function() { - expect(log).toEqual('module'); - }); - }); - }); - - describe('inject', function() { - describe('in DSL', function() { - it('should load module', inject(function() { - log += 'inject'; - })); - - afterEach(function() { - expect(log).toEqual('inject'); - }); - }); - - - describe('inline in test', function() { - it('should load module', function() { - inject(function() { - log += 'inject'; - }); - }); - - afterEach(function() { - expect(log).toEqual('inject'); - }); - }); - - describe('module with inject', function() { - beforeEach(module(function(){ - log += 'module;'; - })); - - it('should inject', inject(function() { - log += 'inject;'; - })); - - afterEach(function() { - expect(log).toEqual('module;inject;') - }); - }); - }); - }); - - - describe('$httpBackend', function() { - var hb, callback, realBackendSpy; - - beforeEach(inject(function($httpBackend) { - callback = jasmine.createSpy('callback'); - hb = $httpBackend; - })); - - - it('should respond with first matched definition', function() { - hb.when('GET', '/url1').respond(200, 'content', {}); - hb.when('GET', '/url1').respond(201, 'another', {}); - - callback.andCallFake(function(status, response) { - expect(status).toBe(200); - expect(response).toBe('content'); - }); - - hb('GET', '/url1', null, callback); - expect(callback).not.toHaveBeenCalled(); - hb.flush(); - expect(callback).toHaveBeenCalledOnce(); - }); - - - it('should throw error when unexpected request', function() { - hb.when('GET', '/url1').respond(200, 'content'); - expect(function() { - hb('GET', '/xxx'); - }).toThrow('Unexpected request: GET /xxx\nNo more request expected'); - }); - - - it('should match headers if specified', function() { - hb.when('GET', '/url', null, {'X': 'val1'}).respond(201, 'content1'); - hb.when('GET', '/url', null, {'X': 'val2'}).respond(202, 'content2'); - hb.when('GET', '/url').respond(203, 'content3'); - - hb('GET', '/url', null, function(status, response) { - expect(status).toBe(203); - expect(response).toBe('content3'); - }); - - hb('GET', '/url', null, function(status, response) { - expect(status).toBe(201); - expect(response).toBe('content1'); - }, {'X': 'val1'}); - - hb('GET', '/url', null, function(status, response) { - expect(status).toBe(202); - expect(response).toBe('content2'); - }, {'X': 'val2'}); - - hb.flush(); - }); - - - it('should match data if specified', function() { - hb.when('GET', '/a/b', '{a: true}').respond(201, 'content1'); - hb.when('GET', '/a/b').respond(202, 'content2'); - - hb('GET', '/a/b', '{a: true}', function(status, response) { - expect(status).toBe(201); - expect(response).toBe('content1'); - }); - - hb('GET', '/a/b', null, function(status, response) { - expect(status).toBe(202); - expect(response).toBe('content2'); - }); - - hb.flush(); - }); - - - it('should match only method', function() { - hb.when('GET').respond(202, 'c'); - callback.andCallFake(function(status, response) { - expect(status).toBe(202); - expect(response).toBe('c'); - }); - - hb('GET', '/some', null, callback, {}); - hb('GET', '/another', null, callback, {'X-Fake': 'Header'}); - hb('GET', '/third', 'some-data', callback, {}); - hb.flush(); - - expect(callback).toHaveBeenCalled(); - }); - - - it('should preserve the order of requests', function() { - hb.when('GET', '/url1').respond(200, 'first'); - hb.when('GET', '/url2').respond(201, 'second'); - - hb('GET', '/url2', null, callback); - hb('GET', '/url1', null, callback); - - hb.flush(); - - expect(callback.callCount).toBe(2); - expect(callback.argsForCall[0]).toEqual([201, 'second', '']); - expect(callback.argsForCall[1]).toEqual([200, 'first', '']); - }); - - - describe('respond()', function() { - it('should take values', function() { - hb.expect('GET', '/url1').respond(200, 'first', {'header': 'val'}); - hb('GET', '/url1', undefined, callback); - hb.flush(); - - expect(callback).toHaveBeenCalledOnceWith(200, 'first', 'header: val'); - }); - - it('should take function', function() { - hb.expect('GET', '/some').respond(function(m, u, d, h) { - return [301, m + u + ';' + d + ';a=' + h.a, {'Connection': 'keep-alive'}]; - }); - - hb('GET', '/some', 'data', callback, {a: 'b'}); - hb.flush(); - - expect(callback).toHaveBeenCalledOnceWith(301, 'GET/some;data;a=b', 'Connection: keep-alive'); - }); - - it('should default status code to 200', function() { - callback.andCallFake(function(status, response) { - expect(status).toBe(200); - expect(response).toBe('some-data'); - }); - - hb.expect('GET', '/url1').respond('some-data'); - hb.expect('GET', '/url2').respond('some-data', {'X-Header': 'true'}); - hb('GET', '/url1', null, callback); - hb('GET', '/url2', null, callback); - hb.flush(); - expect(callback).toHaveBeenCalled(); - expect(callback.callCount).toBe(2); - }); - - - it('should default response headers to ""', function() { - hb.expect('GET', '/url1').respond(200, 'first'); - hb.expect('GET', '/url2').respond('second'); - - hb('GET', '/url1', null, callback); - hb('GET', '/url2', null, callback); - - hb.flush(); - - expect(callback.callCount).toBe(2); - expect(callback.argsForCall[0]).toEqual([200, 'first', '']); - expect(callback.argsForCall[1]).toEqual([200, 'second', '']); - }); - }); - - - describe('expect()', function() { - it('should require specified order', function() { - hb.expect('GET', '/url1').respond(200, ''); - hb.expect('GET', '/url2').respond(200, ''); - - expect(function() { - hb('GET', '/url2', null, noop, {}); - }).toThrow('Unexpected request: GET /url2\nExpected GET /url1'); - }); - - - it('should have precedence over when()', function() { - callback.andCallFake(function(status, response) { - expect(status).toBe(300); - expect(response).toBe('expect'); - }); - - hb.when('GET', '/url').respond(200, 'when'); - hb.expect('GET', '/url').respond(300, 'expect'); - - hb('GET', '/url', null, callback, {}); - hb.flush(); - expect(callback).toHaveBeenCalledOnce(); - }); - - - it ('should throw exception when only headers differs from expectation', function() { - hb.when('GET').respond(200, '', {}); - hb.expect('GET', '/match', undefined, {'Content-Type': 'application/json'}); - - expect(function() { - hb('GET', '/match', null, noop, {}); - }).toThrow('Expected GET /match with different headers\n' + - 'EXPECTED: {"Content-Type":"application/json"}\nGOT: {}'); - }); - - - it ('should throw exception when only data differs from expectation', function() { - hb.when('GET').respond(200, '', {}); - hb.expect('GET', '/match', 'some-data'); - - expect(function() { - hb('GET', '/match', 'different', noop, {}); - }).toThrow('Expected GET /match with different data\n' + - 'EXPECTED: some-data\nGOT: different'); - }); - - - it("should use when's respond() when no expect() respond is defined", function() { - callback.andCallFake(function(status, response) { - expect(status).toBe(201); - expect(response).toBe('data'); - }); - - hb.when('GET', '/some').respond(201, 'data'); - hb.expect('GET', '/some'); - hb('GET', '/some', null, callback); - hb.flush(); - - expect(callback).toHaveBeenCalled(); - expect(function() { hb.verifyNoOutstandingExpectation(); }).not.toThrow(); - }); - }); - - - describe('flush()', function() { - it('flush() should flush requests fired during callbacks', function() { - hb.when('GET').respond(200, ''); - hb('GET', '/some', null, function() { - hb('GET', '/other', null, callback); - }); - - hb.flush(); - expect(callback).toHaveBeenCalled(); - }); - - - it('should flush given number of pending requests', function() { - hb.when('GET').respond(200, ''); - hb('GET', '/some', null, callback); - hb('GET', '/some', null, callback); - hb('GET', '/some', null, callback); - - hb.flush(2); - expect(callback).toHaveBeenCalled(); - expect(callback.callCount).toBe(2); - }); - - - it('should throw exception when flushing more requests than pending', function() { - hb.when('GET').respond(200, ''); - hb('GET', '/url', null, callback); - - expect(function() {hb.flush(2);}).toThrow('No more pending request to flush !'); - expect(callback).toHaveBeenCalledOnce(); - }); - - - it('should throw exception when no request to flush', function() { - expect(function() {hb.flush();}).toThrow('No pending request to flush !'); - - hb.when('GET').respond(200, ''); - hb('GET', '/some', null, callback); - hb.flush(); - - expect(function() {hb.flush();}).toThrow('No pending request to flush !'); - }); - - - it('should throw exception if not all expectations satisfied', function() { - hb.expect('GET', '/url1').respond(); - hb.expect('GET', '/url2').respond(); - - hb('GET', '/url1', null, angular.noop); - expect(function() {hb.flush();}).toThrow('Unsatisfied requests: GET /url2'); - }); - }); - - - it('should throw an exception if no response defined', function() { - hb.when('GET', '/test'); - expect(function() { - hb('GET', '/test', null, callback); - }).toThrow('No response defined !'); - }); - - - it('should throw an exception if no response for exception and no definition', function() { - hb.expect('GET', '/url'); - expect(function() { - hb('GET', '/url', null, callback); - }).toThrow('No response defined !'); - }); - - - it('should respond undefined when JSONP method', function() { - hb.when('JSONP', '/url1').respond(200); - hb.expect('JSONP', '/url2').respond(200); - - expect(hb('JSONP', '/url1')).toBeUndefined(); - expect(hb('JSONP', '/url2')).toBeUndefined(); - }); - - - it('should not have passThrough method', function() { - expect(hb.passThrough).toBeUndefined(); - }); - - - describe('verifyExpectations', function() { - - it('should throw exception if not all expectations were satisfied', function() { - hb.expect('POST', '/u1', 'ddd').respond(201, '', {}); - hb.expect('GET', '/u2').respond(200, '', {}); - hb.expect('POST', '/u3').respond(201, '', {}); - - hb('POST', '/u1', 'ddd', noop, {}); - - expect(function() {hb.verifyNoOutstandingExpectation();}). - toThrow('Unsatisfied requests: GET /u2, POST /u3'); - }); - - - it('should do nothing when no expectation', function() { - hb.when('DELETE', '/some').respond(200, ''); - - expect(function() {hb.verifyNoOutstandingExpectation();}).not.toThrow(); - }); - - - it('should do nothing when all expectations satisfied', function() { - hb.expect('GET', '/u2').respond(200, '', {}); - hb.expect('POST', '/u3').respond(201, '', {}); - hb.when('DELETE', '/some').respond(200, ''); - - hb('GET', '/u2', noop); - hb('POST', '/u3', noop); - - expect(function() {hb.verifyNoOutstandingExpectation();}).not.toThrow(); - }); - }); - - describe('verifyRequests', function() { - - it('should throw exception if not all requests were flushed', function() { - hb.when('GET').respond(200); - hb('GET', '/some', null, noop, {}); - - expect(function() { - hb.verifyNoOutstandingRequest(); - }).toThrow('Unflushed requests: 1'); - }); - }); - - - describe('resetExpectations', function() { - - it('should remove all expectations', function() { - hb.expect('GET', '/u2').respond(200, '', {}); - hb.expect('POST', '/u3').respond(201, '', {}); - hb.resetExpectations(); - - expect(function() {hb.verifyNoOutstandingExpectation();}).not.toThrow(); - }); - - - it('should remove all pending responses', function() { - var cancelledClb = jasmine.createSpy('cancelled'); - - hb.expect('GET', '/url').respond(200, ''); - hb('GET', '/url', null, cancelledClb); - hb.resetExpectations(); - - hb.expect('GET', '/url').respond(300, ''); - hb('GET', '/url', null, callback, {}); - hb.flush(); - - expect(callback).toHaveBeenCalledOnce(); - expect(cancelledClb).not.toHaveBeenCalled(); - }); - - - it('should not remove definitions', function() { - var cancelledClb = jasmine.createSpy('cancelled'); - - hb.when('GET', '/url').respond(200, 'success'); - hb('GET', '/url', null, cancelledClb); - hb.resetExpectations(); - - hb('GET', '/url', null, callback, {}); - hb.flush(); - - expect(callback).toHaveBeenCalledOnce(); - expect(cancelledClb).not.toHaveBeenCalled(); - }); - }); - - - describe('expect/when shortcuts', function() { - angular.forEach(['expect', 'when'], function(prefix) { - angular.forEach(['GET', 'POST', 'PUT', 'DELETE', 'JSONP'], function(method) { - var shortcut = prefix + method; - it('should provide ' + shortcut + ' shortcut method', function() { - hb[shortcut]('/foo').respond('bar'); - hb(method, '/foo', undefined, callback); - hb.flush(); - expect(callback).toHaveBeenCalledOnceWith(200, 'bar', ''); - }); - }); - }); - }); - - - describe('MockHttpExpectation', function() { - - it('should accept url as regexp', function() { - var exp = new MockHttpExpectation('GET', /^\/x/); - - expect(exp.match('GET', '/x')).toBe(true); - expect(exp.match('GET', '/xxx/x')).toBe(true); - expect(exp.match('GET', 'x')).toBe(false); - expect(exp.match('GET', 'a/x')).toBe(false); - }); - - - it('should accept data as regexp', function() { - var exp = new MockHttpExpectation('POST', '/url', /\{.*?\}/); - - expect(exp.match('POST', '/url', '{"a": "aa"}')).toBe(true); - expect(exp.match('POST', '/url', '{"one": "two"}')).toBe(true); - expect(exp.match('POST', '/url', '{"one"')).toBe(false); - }); - - - it('should ignore data only if undefined (not null or false)', function() { - var exp = new MockHttpExpectation('POST', '/url', null); - expect(exp.matchData(null)).toBe(true); - expect(exp.matchData('some-data')).toBe(false); - - exp = new MockHttpExpectation('POST', '/url', undefined); - expect(exp.matchData(null)).toBe(true); - expect(exp.matchData('some-data')).toBe(true); - }); - - - it('should accept headers as function', function() { - var exp = new MockHttpExpectation('GET', '/url', undefined, function(h) { - return h['Content-Type'] == 'application/json'; - }); - - expect(exp.matchHeaders({})).toBe(false); - expect(exp.matchHeaders({'Content-Type': 'application/json', 'X-Another': 'true'})).toBe(true); - }); - }); - }); -}); - - -describe('ngMockE2E', function() { - describe('$httpBackend', function() { - var hb, realHttpBackend, callback; - - beforeEach(function() { - module(function($provide) { - callback = jasmine.createSpy('callback'); - realHttpBackend = jasmine.createSpy('real $httpBackend'); - $provide.value('$httpBackend', realHttpBackend); - $provide.decorator('$httpBackend', angular.mock.e2e.$httpBackendDecorator); - }); - inject(function($injector) { - hb = $injector.get('$httpBackend'); - }); - }); - - - describe('passThrough()', function() { - it('should delegate requests to the real backend when passThrough is invoked', function() { - hb.when('GET', /\/passThrough\/.*/).passThrough(); - hb('GET', '/passThrough/23', null, callback); - - expect(realHttpBackend). - toHaveBeenCalledOnceWith('GET', '/passThrough/23', null, callback, undefined); - }); - }); - - - describe('autoflush', function() { - it('should flush responses via $defer', inject(function($browser) { - hb.when('GET', '/foo').respond('bar'); - hb('GET', '/foo', null, callback); - - expect(callback).not.toHaveBeenCalled(); - $browser.defer.flush(); - expect(callback).toHaveBeenCalledOnce(); - })); - }); - }); -}); diff --git a/test/auto/injectorSpec.js b/test/auto/injectorSpec.js new file mode 100644 index 00000000..16cf1524 --- /dev/null +++ b/test/auto/injectorSpec.js @@ -0,0 +1,764 @@ +'use strict'; + +describe('injector', function() { + var providers; + var injector; + + beforeEach(module(function($provide) { + providers = function(name, factory, annotations) { + $provide.factory(name, extend(factory, annotations||{})); + }; + })); + beforeEach(inject(function($injector){ + injector = $injector; + })); + + + it("should return same instance from calling provider", function() { + var instance = {}, + original = instance; + providers('instance', function() { return instance; }); + expect(injector.get('instance')).toEqual(instance); + instance = 'deleted'; + expect(injector.get('instance')).toEqual(original); + }); + + + it('should inject providers', function() { + providers('a', function() {return 'Mi';}); + providers('b', function(mi) {return mi+'sko';}, {$inject:['a']}); + expect(injector.get('b')).toEqual('Misko'); + }); + + + it('should resolve dependency graph and instantiate all services just once', function() { + var log = []; + +// s1 +// / | \ +// / s2 \ +// / / | \ \ +// /s3 < s4 > s5 +// // +// s6 + + + providers('s1', function() { log.push('s1'); }, {$inject: ['s2', 's5', 's6']}); + providers('s2', function() { log.push('s2'); }, {$inject: ['s3', 's4', 's5']}); + providers('s3', function() { log.push('s3'); }, {$inject: ['s6']}); + providers('s4', function() { log.push('s4'); }, {$inject: ['s3', 's5']}); + providers('s5', function() { log.push('s5'); }); + providers('s6', function() { log.push('s6'); }); + + injector.get('s1'); + + expect(log).toEqual(['s6', 's3', 's5', 's4', 's2', 's1']); + }); + + + it('should provide useful message if no provider', function() { + expect(function() { + injector.get('idontexist'); + }).toThrow("Unknown provider: idontexistProvider <- idontexist"); + }); + + + it('should proved path to the missing provider', function() { + providers('a', function(idontexist) {return 1;}); + providers('b', function(a) {return 2;}); + expect(function() { + injector.get('b'); + }).toThrow("Unknown provider: idontexistProvider <- idontexist <- a <- b"); + }); + + + describe('invoke', function() { + var args; + + beforeEach(function() { + args = null; + providers('a', function() {return 1;}); + providers('b', function() {return 2;}); + }); + + + function fn(a, b, c, d) { + args = [this, a, b, c, d]; + return a + b + c + d; + } + + + it('should call function', function() { + fn.$inject = ['a', 'b', 'c', 'd']; + injector.invoke(fn, {name:"this"}, {c:3, d:4}); + expect(args).toEqual([{name:'this'}, 1, 2, 3, 4]); + }); + + + it('should treat array as annotations', function() { + injector.invoke(['a', 'b', 'c', 'd', fn], {name:"this"}, {c:3, d:4}); + expect(args).toEqual([{name:'this'}, 1, 2, 3, 4]); + }); + + + it('should invoke the passed-in fn with all of the dependencies as arguments', function() { + providers('c', function() {return 3;}); + providers('d', function() {return 4;}); + expect(injector.invoke(['a', 'b', 'c', 'd', fn])).toEqual(10); + }); + + + it('should fail with errors if not function or array', function() { + expect(function() { + injector.invoke({}); + }).toThrow("Argument 'fn' is not a function, got Object"); + expect(function() { + injector.invoke(['a', 123], {}); + }).toThrow("Argument 'fn' is not a function, got number"); + }); + }); + + + describe('annotation', function() { + it('should return $inject', function() { + function fn() {} + fn.$inject = ['a']; + expect(inferInjectionArgs(fn)).toBe(fn.$inject); + expect(inferInjectionArgs(function() {})).toEqual([]); + expect(inferInjectionArgs(function () {})).toEqual([]); + expect(inferInjectionArgs(function () {})).toEqual([]); + expect(inferInjectionArgs(function /* */ () {})).toEqual([]); + }); + + + it('should create $inject', function() { + // keep the multi-line to make sure we can handle it + function $f_n0 /* + */( + $a, // x, <-- looks like an arg but it is a comment + b_, /* z, <-- looks like an arg but it is a + multi-line comment + function (a, b) {} + */ + _c, + /* {some type} */ d) { extraParans();} + expect(inferInjectionArgs($f_n0)).toEqual(['$a', 'b_', '_c', 'd']); + expect($f_n0.$inject).toEqual(['$a', 'b_', '_c', 'd']); + }); + + + it('should strip leading and trailing underscores from arg name during inference', function() { + function beforeEachFn(_foo_) { /* foo = _foo_ */ }; + expect(inferInjectionArgs(beforeEachFn)).toEqual(['foo']); + }); + + + it('should handle no arg functions', function() { + function $f_n0() {} + expect(inferInjectionArgs($f_n0)).toEqual([]); + expect($f_n0.$inject).toEqual([]); + }); + + + it('should handle args with both $ and _', function() { + function $f_n0($a_) {} + expect(inferInjectionArgs($f_n0)).toEqual(['$a_']); + expect($f_n0.$inject).toEqual(['$a_']); + }); + + + it('should throw on non function arg', function() { + expect(function() { + inferInjectionArgs({}); + }).toThrow(); + }); + }); + + + it('should have $injector', function() { + var $injector = createInjector(); + expect($injector.get('$injector')).toBe($injector); + }); + + + it('should define module', function() { + var log = ''; + var injector = createInjector([function($provide) { + $provide.value('value', 'value;'); + $provide.factory('fn', valueFn('function;')); + $provide.provider('service', function() { + this.$get = valueFn('service;'); + }); + }, function(valueProvider, fnProvider, serviceProvider) { + log += valueProvider.$get() + fnProvider.$get() + serviceProvider.$get(); + }]).invoke(function(value, fn, service) { + log += '->' + value + fn + service; + }); + expect(log).toEqual('value;function;service;->value;function;service;'); + }); + + + describe('module', function() { + it('should provide $injector even when no module is requested', function() { + var $provide, + $injector = createInjector([ + angular.extend(function(p) { $provide = p; }, {$inject: ['$provide']}) + ]); + expect($injector.get('$injector')).toBe($injector); + }); + + + it('should load multiple function modules and infer inject them', function() { + var a = 'junk'; + var $injector = createInjector([ + function() { + a = 'A'; // reset to prove we ran + }, + function($provide) { + $provide.value('a', a); + }, + angular.extend(function(p, serviceA) { + p.value('b', serviceA.$get() + 'B' ); + }, {$inject:['$provide', 'aProvider']}), + ['$provide', 'bProvider', function(p, serviceB) { + p.value('c', serviceB.$get() + 'C'); + }] + ]); + expect($injector.get('a')).toEqual('A'); + expect($injector.get('b')).toEqual('AB'); + expect($injector.get('c')).toEqual('ABC'); + }); + + + it('should run symbolic modules', function() { + angularModule('myModule', []).value('a', 'abc'); + var $injector = createInjector(['myModule']); + expect($injector.get('a')).toEqual('abc'); + }); + + + it('should error on invalid module name', function() { + expect(function() { + createInjector(['IDontExist'], {}); + }).toThrow("No module: IDontExist"); + }); + + + it('should load dependant modules only once', function() { + var log = ''; + angular.module('a', [], function(){ log += 'a'; }); + angular.module('b', ['a'], function(){ log += 'b'; }); + angular.module('c', ['a', 'b'], function(){ log += 'c'; }); + createInjector(['c', 'c']); + expect(log).toEqual('abc'); + }); + + it('should execute runBlocks after injector creation', function() { + var log = ''; + angular.module('a', [], function(){ log += 'a'; }).run(function() { log += 'A'; }); + angular.module('b', ['a'], function(){ log += 'b'; }).run(function() { log += 'B'; }); + createInjector([ + 'b', + valueFn(function() { log += 'C'; }), + [valueFn(function() { log += 'D'; })] + ]); + expect(log).toEqual('abABCD'); + }); + + describe('$provide', function() { + describe('constant', function() { + it('should create configuration injectable constants', function() { + var log = []; + createInjector([ + function($provide){ + $provide.constant('abc', 123); + $provide.constant({a: 'A', b:'B'}); + return function(a) { + log.push(a); + } + }, + function(abc) { + log.push(abc); + return function(b) { + log.push(b); + } + } + ]).get('abc'); + expect(log).toEqual([123, 'A', 'B']); + }); + }); + + + describe('value', function() { + it('should configure $provide values', function() { + expect(createInjector([function($provide) { + $provide.value('value', 'abc'); + }]).get('value')).toEqual('abc'); + }); + + + it('should configure a set of values', function() { + expect(createInjector([function($provide) { + $provide.value({value: Array}); + }]).get('value')).toEqual(Array); + }); + }); + + + describe('factory', function() { + it('should configure $provide factory function', function() { + expect(createInjector([function($provide) { + $provide.factory('value', valueFn('abc')); + }]).get('value')).toEqual('abc'); + }); + + + it('should configure a set of factories', function() { + expect(createInjector([function($provide) { + $provide.factory({value: Array}); + }]).get('value')).toEqual([]); + }); + }); + + + describe('service', function() { + it('should register a class', function() { + var Type = function(value) { + this.value = value; + }; + + var instance = createInjector([function($provide) { + $provide.value('value', 123); + $provide.service('foo', Type); + }]).get('foo'); + + expect(instance instanceof Type).toBe(true); + expect(instance.value).toBe(123); + }); + + + it('should register a set of classes', function() { + var Type = function() {}; + + var injector = createInjector([function($provide) { + $provide.service({ + foo: Type, + bar: Type + }); + }]); + + expect(injector.get('foo') instanceof Type).toBe(true); + expect(injector.get('bar') instanceof Type).toBe(true); + }); + }); + + + describe('provider', function() { + it('should configure $provide provider object', function() { + expect(createInjector([function($provide) { + $provide.provider('value', { + $get: valueFn('abc') + }); + }]).get('value')).toEqual('abc'); + }); + + + it('should configure $provide provider type', function() { + function Type() {}; + Type.prototype.$get = function() { + expect(this instanceof Type).toBe(true); + return 'abc'; + }; + expect(createInjector([function($provide) { + $provide.provider('value', Type); + }]).get('value')).toEqual('abc'); + }); + + + it('should configure a set of providers', function() { + expect(createInjector([function($provide) { + $provide.provider({value: valueFn({$get:Array})}); + }]).get('value')).toEqual([]); + }); + }); + + + describe('decorator', function() { + var log, injector; + + beforeEach(function() { + log = []; + }); + + + it('should be called with the original instance', function() { + injector = createInjector([function($provide) { + $provide.value('myService', function(val) { + log.push('myService:' + val); + return 'origReturn'; + }); + + $provide.decorator('myService', function($delegate) { + return function(val) { + log.push('myDecoratedService:' + val); + var origVal = $delegate('decInput'); + return 'dec+' + origVal; + }; + }); + }]); + + var out = injector.get('myService')('input'); + log.push(out); + expect(log.join('; ')). + toBe('myDecoratedService:input; myService:decInput; dec+origReturn'); + }); + + + it('should allow multiple decorators to be applied to a service', function() { + injector = createInjector([function($provide) { + $provide.value('myService', function(val) { + log.push('myService:' + val); + return 'origReturn'; + }); + + $provide.decorator('myService', function($delegate) { + return function(val) { + log.push('myDecoratedService1:' + val); + var origVal = $delegate('decInput1'); + return 'dec1+' + origVal; + }; + }); + + $provide.decorator('myService', function($delegate) { + return function(val) { + log.push('myDecoratedService2:' + val); + var origVal = $delegate('decInput2'); + return 'dec2+' + origVal; + }; + }); + }]); + + var out = injector.get('myService')('input'); + log.push(out); + expect(log).toEqual(['myDecoratedService2:input', + 'myDecoratedService1:decInput2', + 'myService:decInput1', + 'dec2+dec1+origReturn']); + }); + + + it('should decorate services with dependencies', function() { + injector = createInjector([function($provide) { + $provide.value('dep1', 'dependency1'); + + $provide.factory('myService', ['dep1', function(dep1) { + return function(val) { + log.push('myService:' + val + ',' + dep1); + return 'origReturn'; + } + }]); + + $provide.decorator('myService', function($delegate) { + return function(val) { + log.push('myDecoratedService:' + val); + var origVal = $delegate('decInput'); + return 'dec+' + origVal; + }; + }); + }]); + + var out = injector.get('myService')('input'); + log.push(out); + expect(log.join('; ')). + toBe('myDecoratedService:input; myService:decInput,dependency1; dec+origReturn'); + }); + + + it('should allow for decorators to be injectable', function() { + injector = createInjector([function($provide) { + $provide.value('dep1', 'dependency1'); + + $provide.factory('myService', function() { + return function(val) { + log.push('myService:' + val); + return 'origReturn'; + } + }); + + $provide.decorator('myService', function($delegate, dep1) { + return function(val) { + log.push('myDecoratedService:' + val + ',' + dep1); + var origVal = $delegate('decInput'); + return 'dec+' + origVal; + }; + }); + }]); + + var out = injector.get('myService')('input'); + log.push(out); + expect(log.join('; ')). + toBe('myDecoratedService:input,dependency1; myService:decInput; dec+origReturn'); + }); + }); + }); + + + describe('error handling', function() { + it('should handle wrong argument type', function() { + expect(function() { + createInjector([ + {} + ], {}); + }).toThrow("Argument 'module' is not a function, got Object"); + }); + + + it('should handle exceptions', function() { + expect(function() { + createInjector([function() { + throw 'MyError'; + }], {}); + }).toThrow('MyError'); + }); + + + it('should decorate the missing service error with module name', function() { + angular.module('TestModule', [], function($injector) {}); + expect(function() { + createInjector(['TestModule']); + }).toThrow('Unknown provider: $injector from TestModule'); + }); + + + it('should decorate the missing service error with module function', function() { + function myModule($injector){} + expect(function() { + createInjector([myModule]); + }).toThrow('Unknown provider: $injector from ' + myModule); + }); + + + it('should decorate the missing service error with module array function', function() { + function myModule($injector){} + expect(function() { + createInjector([['$injector', myModule]]); + }).toThrow('Unknown provider: $injector from ' + myModule); + }); + + + it('should throw error when trying to inject oneself', function() { + expect(function() { + createInjector([function($provide){ + $provide.factory('service', function(service){}); + return function(service) {} + }]) + }).toThrow('Circular dependency: service'); + }); + + + it('should throw error when trying to inject circular dependency', function() { + expect(function() { + createInjector([function($provide){ + $provide.factory('a', function(b){}); + $provide.factory('b', function(a){}); + return function(a) {} + }]) + }).toThrow('Circular dependency: b <- a'); + }); + }); + }); + + + describe('retrieval', function() { + var instance, + $injector, + $provide; + + beforeEach(function() { + $injector = createInjector([ ['$provide', function(provide) { + ($provide = provide).value('instance', instance = {name:'angular'}); + }]]); + }); + + + it('should retrieve by name and cache instance', function() { + expect(instance).toEqual({name: 'angular'}); + expect($injector.get('instance')).toBe(instance); + expect($injector.get('instance')).toBe(instance); + }); + + + it('should call functions and infer arguments', function() { + expect($injector.invoke(function(instance) { return instance; })).toBe(instance); + expect($injector.invoke(function(instance) { return instance; })).toBe(instance); + }); + }); + + + describe('method invoking', function() { + var $injector; + + beforeEach(function() { + $injector = createInjector([ function($provide) { + $provide.value('book', 'moby'); + $provide.value('author', 'melville'); + }]); + }); + + + it('should invoke method', function() { + expect($injector.invoke(function(book, author) { + return author + ':' + book; + })).toEqual('melville:moby'); + expect($injector.invoke(function(book, author) { + expect(this).toEqual($injector); + return author + ':' + book;}, $injector)).toEqual('melville:moby'); + }); + + + it('should invoke method with locals', function() { + expect($injector.invoke(function(book, author) { + return author + ':' + book; + })).toEqual('melville:moby'); + expect($injector.invoke( + function(book, author, chapter) { + expect(this).toEqual($injector); + return author + ':' + book + '-' + chapter; + }, $injector, {author:'m', chapter:'ch1'})).toEqual('m:moby-ch1'); + }); + + + it('should invoke method which is annotated', function() { + expect($injector.invoke(extend(function(b, a) { + return a + ':' + b + }, {$inject:['book', 'author']}))).toEqual('melville:moby'); + expect($injector.invoke(extend(function(b, a) { + expect(this).toEqual($injector); + return a + ':' + b; + }, {$inject:['book', 'author']}), $injector)).toEqual('melville:moby'); + }); + + + it('should invoke method which is an array of annotation', function() { + expect($injector.invoke(function(book, author) { + return author + ':' + book; + })).toEqual('melville:moby'); + expect($injector.invoke(function(book, author) { + expect(this).toEqual($injector); + return author + ':' + book; + }, $injector)).toEqual('melville:moby'); + }); + + + it('should throw usefull error on wrong argument type]', function() { + expect(function() { + $injector.invoke({}); + }).toThrow("Argument 'fn' is not a function, got Object"); + }); + }); + + + describe('service instantiation', function() { + var $injector; + + beforeEach(function() { + $injector = createInjector([ function($provide) { + $provide.value('book', 'moby'); + $provide.value('author', 'melville'); + }]); + }); + + + function Type(book, author) { + this.book = book; + this.author = author; + } + Type.prototype.title = function() { + return this.author + ': ' + this.book; + }; + + + it('should instantiate object and preserve constructor property and be instanceof', function() { + var t = $injector.instantiate(Type); + expect(t.book).toEqual('moby'); + expect(t.author).toEqual('melville'); + expect(t.title()).toEqual('melville: moby'); + expect(t instanceof Type).toBe(true); + }); + + + it('should instantiate object and preserve constructor property and be instanceof ' + + 'with the array annotated type', function() { + var t = $injector.instantiate(['book', 'author', Type]); + expect(t.book).toEqual('moby'); + expect(t.author).toEqual('melville'); + expect(t.title()).toEqual('melville: moby'); + expect(t instanceof Type).toBe(true); + }); + + + it('should allow constructor to return different object', function() { + var obj = {}; + var Class = function() { + return obj; + }; + + expect($injector.instantiate(Class)).toBe(obj); + }); + + + it('should handle constructor exception', function() { + expect(function() { + $injector.instantiate(function() { throw 'MyError'; }); + }).toThrow('MyError'); + }); + + + it('should return instance if constructor returns non-object value', function() { + var A = function() { + return 10; + }; + + var B = function() { + return 'some-string'; + }; + + var C = function() { + return undefined; + }; + + expect($injector.instantiate(A) instanceof A).toBe(true); + expect($injector.instantiate(B) instanceof B).toBe(true); + expect($injector.instantiate(C) instanceof C).toBe(true); + }); + }); + + describe('protection modes', function() { + it('should prevent provider lookup in app', function() { + var $injector = createInjector([function($provide) { + $provide.value('name', 'angular'); + }]); + expect(function() { + $injector.get('nameProvider'); + }).toThrow("Unknown provider: nameProviderProvider <- nameProvider"); + }); + + + it('should prevent provider configuration in app', function() { + var $injector = createInjector([]); + expect(function() { + $injector.get('$provide').value('a', 'b'); + }).toThrow("Unknown provider: $provideProvider <- $provide"); + }); + + + it('should prevent instance lookup in module', function() { + function instanceLookupInModule(name) { throw Error('FAIL'); } + expect(function() { + createInjector([function($provide) { + $provide.value('name', 'angular') + }, instanceLookupInModule]); + }).toThrow('Unknown provider: name from ' + String(instanceLookupInModule)); + }); + }); +}); diff --git a/test/directive/aSpec.js b/test/directive/aSpec.js deleted file mode 100644 index 8aa2449d..00000000 --- a/test/directive/aSpec.js +++ /dev/null @@ -1,46 +0,0 @@ -'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 deleted file mode 100644 index 7a4244a8..00000000 --- a/test/directive/booleanAttrDirSpecs.js +++ /dev/null @@ -1,125 +0,0 @@ -'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'); - })); -}); - - -describe('ng-src', function() { - - it('should interpolate the expression and bind to src', inject(function($compile, $rootScope) { - var element = $compile('
')($rootScope) - $rootScope.$digest(); - expect(element.attr('src')).toEqual('some/'); - - $rootScope.$apply(function() { - $rootScope.id = 1; - }); - expect(element.attr('src')).toEqual('some/1'); - - dealoc(element); - })); -}); - - -describe('ng-href', function() { - - it('should interpolate the expression and bind to href', inject(function($compile, $rootScope) { - var element = $compile('
')($rootScope) - $rootScope.$digest(); - expect(element.attr('href')).toEqual('some/'); - - $rootScope.$apply(function() { - $rootScope.id = 1; - }); - expect(element.attr('href')).toEqual('some/1'); - - dealoc(element); - })); -}); diff --git a/test/directive/formSpec.js b/test/directive/formSpec.js deleted file mode 100644 index 5c34b5ad..00000000 --- a/test/directive/formSpec.js +++ /dev/null @@ -1,294 +0,0 @@ -'use strict'; - -describe('form', function() { - var doc, control, scope, $compile; - - beforeEach(module(function($compileProvider) { - $compileProvider.directive('storeModelCtrl', function() { - return { - require: 'ngModel', - link: function(scope, elm, attr, ctrl) { - control = 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('$formController')).toBeTruthy(); - expect(doc.data('$formController') instanceof FormController).toBe(true); - }); - - - it('should remove the widget when element removed', function() { - doc = $compile( - '' + - '' + - '
')(scope); - - var form = scope.myForm; - control.$setValidity('required', false); - expect(form.alias).toBe(control); - expect(form.$error.required).toEqual([control]); - - doc.find('input').remove(); - expect(form.$error.required).toBe(false); - expect(form.alias).toBeUndefined(); - }); - - - it('should use ng-form as form name', function() { - doc = $compile( - '
' + - '' + - '
')(scope); - - expect(scope.myForm).toBeDefined(); - expect(scope.myForm.alias).toBeDefined(); - }); - - - 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 when name attr is defined', function() { - doc = $compile('
')(scope); - expect(scope.myForm).toBeTruthy(); - expect(doc.data('$formController')).toBeTruthy(); - expect(doc.data('$formController')).toEqual(scope.myForm); - }); - - - it('should allow form name to be an expression', function() { - doc = $compile('
')(scope); - - expect(scope['obj.myForm']).toBeTruthy(); - }); - - - it('should support two forms on a single scope', function() { - doc = $compile( - '
' + - '
' + - '' + - '
' + - '
' + - '' + - '
' + - '
' - )(scope); - - scope.$apply(); - - expect(scope.formA.$error.required.length).toBe(1); - expect(scope.formA.$error.required).toEqual([scope.formA.firstName]); - expect(scope.formB.$error.required.length).toBe(1); - expect(scope.formB.$error.required).toEqual([scope.formB.lastName]); - - var inputA = doc.find('input').eq(0), - inputB = doc.find('input').eq(1); - - inputA.val('val1'); - browserTrigger(inputA, 'blur'); - inputB.val('val2'); - browserTrigger(inputB, 'blur'); - - expect(scope.firstName).toBe('val1'); - expect(scope.lastName).toBe('val2'); - - expect(scope.formA.$error.required).toBe(false); - expect(scope.formB.$error.required).toBe(false); - }); - - - 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('nested forms', function() { - - it('should chain nested forms', function() { - doc = jqLite( - '' + - '' + - '' + - '' + - '' + - ''); - $compile(doc)(scope); - - var parent = scope.parent, - child = scope.child, - inputA = child.inputA, - inputB = child.inputB; - - inputA.$setValidity('MyError', false); - inputB.$setValidity('MyError', false); - expect(parent.$error.MyError).toEqual([child]); - expect(child.$error.MyError).toEqual([inputA, inputB]); - - inputA.$setValidity('MyError', true); - expect(parent.$error.MyError).toEqual([child]); - expect(child.$error.MyError).toEqual([inputB]); - - inputB.$setValidity('MyError', true); - expect(parent.$error.MyError).toBe(false); - expect(child.$error.MyError).toBe(false); - }); - - - it('should deregister a child form when its DOM is removed', function() { - doc = jqLite( - '
' + - '
' + - '' + - '
' + - '
'); - $compile(doc)(scope); - scope.$apply(); - - var parent = scope.parent, - child = scope.child; - - expect(parent).toBeDefined(); - expect(child).toBeDefined(); - expect(parent.$error.required).toEqual([child]); - doc.children().remove(); //remove child - - expect(parent.child).toBeUndefined(); - expect(scope.child).toBeUndefined(); - expect(parent.$error.required).toBe(false); - }); - - - 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([child]); - - input.$setValidity('myRule', true); - expect(parent.$error.myRule).toBe(false); - expect(child.$error.myRule).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(); - - control.$setValidity('error', false); - expect(doc).toBeInvalid(); - expect(doc.hasClass('ng-valid-error')).toBe(false); - expect(doc.hasClass('ng-invalid-error')).toBe(true); - - control.$setValidity('another', false); - expect(doc.hasClass('ng-valid-error')).toBe(false); - expect(doc.hasClass('ng-invalid-error')).toBe(true); - expect(doc.hasClass('ng-valid-another')).toBe(false); - expect(doc.hasClass('ng-invalid-another')).toBe(true); - - control.$setValidity('error', true); - expect(doc).toBeInvalid(); - expect(doc.hasClass('ng-valid-error')).toBe(true); - expect(doc.hasClass('ng-invalid-error')).toBe(false); - expect(doc.hasClass('ng-valid-another')).toBe(false); - expect(doc.hasClass('ng-invalid-another')).toBe(true); - - control.$setValidity('another', true); - expect(doc).toBeValid(); - expect(doc.hasClass('ng-valid-error')).toBe(true); - expect(doc.hasClass('ng-invalid-error')).toBe(false); - expect(doc.hasClass('ng-valid-another')).toBe(true); - expect(doc.hasClass('ng-invalid-another')).toBe(false); - }); - - - it('should have ng-pristine/ng-dirty css class', function() { - expect(doc).toBePristine(); - - control.$setViewValue(''); - scope.$apply(); - expect(doc).toBeDirty(); - }); - }); -}); diff --git a/test/directive/inputSpec.js b/test/directive/inputSpec.js deleted file mode 100644 index e5f083b3..00000000 --- a/test/directive/inputSpec.js +++ /dev/null @@ -1,1119 +0,0 @@ -'use strict'; - -describe('NgModelController', function() { - var ctrl, scope, ngModelAccessor, element, parentFormCtrl; - - beforeEach(inject(function($rootScope, $controller) { - var attrs = {name: 'testAlias'}; - - parentFormCtrl = { - $setValidity: jasmine.createSpy('$setValidity'), - $setDirty: jasmine.createSpy('$setDirty') - } - - element = jqLite('
'); - element.data('$formController', parentFormCtrl); - - scope = $rootScope; - ngModelAccessor = jasmine.createSpy('ngModel accessor'); - ctrl = $controller(NgModelController, { - $scope: scope, $element: element.find('input'), ngModel: ngModelAccessor, $attrs: attrs - }); - // mock accessor (locals) - ngModelAccessor.andCallFake(function(val) { - if (isDefined(val)) scope.value = val; - return scope.value; - }); - })); - - - afterEach(function() { - dealoc(element); - }); - - - 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([]); - - expect(ctrl.$name).toBe('testAlias'); - }); - - - describe('setValidity', function() { - - it('should propagate invalid to the parent form only when valid', function() { - expect(parentFormCtrl.$setValidity).not.toHaveBeenCalled(); - ctrl.$setValidity('ERROR', false); - expect(parentFormCtrl.$setValidity).toHaveBeenCalledOnceWith('ERROR', false, ctrl); - - parentFormCtrl.$setValidity.reset(); - ctrl.$setValidity('ERROR', false); - expect(parentFormCtrl.$setValidity).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).toBe(false); - }); - - - 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() { - ctrl.$setValidity('error', true); - expect(parentFormCtrl.$setValidity).toHaveBeenCalledOnceWith('error', true, ctrl); - parentFormCtrl.$setValidity.reset(); - - ctrl.$setValidity('error', false); - expect(parentFormCtrl.$setValidity).toHaveBeenCalledOnceWith('error', false, ctrl); - parentFormCtrl.$setValidity.reset(); - ctrl.$setValidity('error', true); - expect(parentFormCtrl.$setValidity).toHaveBeenCalledOnceWith('error', true, ctrl); - }); - }); - - - describe('view -> model', function() { - - it('should set the value to $viewValue', function() { - ctrl.$setViewValue('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.$setViewValue('init'); - expect(log).toEqual(['init', 'init-a']); - expect(ctrl.$modelValue).toBe('init-a-b'); - }); - - - it('should fire viewChangeListeners when the value changes in the view (even if invalid)', - function() { - var spy = jasmine.createSpy('viewChangeListener'); - ctrl.$viewChangeListeners.push(spy); - ctrl.$setViewValue('val'); - expect(spy).toHaveBeenCalledOnce(); - spy.reset(); - - // invalid - ctrl.$parsers.push(function() {return undefined;}); - ctrl.$setViewValue('val'); - expect(spy).toHaveBeenCalledOnce(); - }); - - - it('should reset the model when the view is invalid', function() { - ctrl.$setViewValue('aaaa'); - expect(ctrl.$modelValue).toBe('aaaa'); - - // add a validator that will make any input invalid - ctrl.$parsers.push(function() {return undefined;}); - expect(ctrl.$modelValue).toBe('aaaa'); - ctrl.$setViewValue('bbbb'); - expect(ctrl.$modelValue).toBeUndefined(); - }); - - - it('should call parentForm.$setDirty only when pristine', function() { - ctrl.$setViewValue(''); - expect(ctrl.$pristine).toBe(false); - expect(ctrl.$dirty).toBe(true); - expect(parentFormCtrl.$setDirty).toHaveBeenCalledOnce(); - - parentFormCtrl.$setDirty.reset(); - ctrl.$setViewValue(''); - expect(ctrl.$pristine).toBe(false); - expect(ctrl.$dirty).toBe(true); - expect(parentFormCtrl.$setDirty).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', function() { - spyOn(ctrl, '$render'); - - scope.$apply(function() { - scope.value = 3; - }); - expect(ctrl.$render).toHaveBeenCalledOnce(); - ctrl.$render.reset(); - - ctrl.$formatters.push(function() {return 3;}); - scope.$apply(function() { - scope.value = 5; - }); - expect(ctrl.$render).not.toHaveBeenCalled(); - }); - - - it('should clear the view even if invalid', function() { - spyOn(ctrl, '$render'); - - ctrl.$formatters.push(function() {return undefined;}); - scope.$apply(function() { - scope.value = 5; - }); - expect(ctrl.$render).toHaveBeenCalledOnce(); - }); - }); -}); - -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(); - expect(element.hasClass('ng-valid-email')).toBe(true); - expect(element.hasClass('ng-invalid-email')).toBe(false); - - $rootScope.$apply(function() { - $rootScope.value = 'invalid-email'; - }); - expect(element).toBeInvalid(); - expect(element).toBePristine(); - expect(element.hasClass('ng-valid-email')).toBe(false); - expect(element.hasClass('ng-invalid-email')).toBe(true); - - element.val('invalid-again'); - browserTrigger(element, 'blur'); - expect(element).toBeInvalid(); - expect(element).toBeDirty(); - expect(element.hasClass('ng-valid-email')).toBe(false); - expect(element.hasClass('ng-invalid-email')).toBe(true); - - element.val('vojta@google.com'); - browserTrigger(element, 'blur'); - expect(element).toBeValid(); - expect(element).toBeDirty(); - expect(element.hasClass('ng-valid-email')).toBe(true); - expect(element.hasClass('ng-invalid-email')).toBe(false); - - dealoc(element); - })); - - - it('should set invalid classes on init', inject(function($compile, $rootScope) { - var element = $compile('')($rootScope); - $rootScope.$digest(); - - expect(element).toBeInvalid(); - expect(element).toHaveClass('ng-invalid-required'); - })); -}); - - -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 not set readonly or disabled property on ie7', function() { - this.addMatchers({ - toBeOff: function(attributeName) { - var actualValue = this.actual.attr(attributeName); - this.message = function() { - return "Attribute '" + attributeName + "' expected to be off but was '" + actualValue + - "' in: " + angular.mock.dump(this.actual); - } - - return !actualValue || actualValue == 'false'; - } - }); - - compileInput(''); - expect(inputElm.prop('readOnly')).toBe(false); - expect(inputElm.prop('disabled')).toBe(false); - - expect(inputElm).toBeOff('readOnly'); - expect(inputElm).toBeOff('readonly'); - expect(inputElm).toBeOff('disabled'); - }); - - - it('should cleanup it self from the parent form', function() { - compileInput(''); - - scope.$apply(); - expect(scope.form.$error.required.length).toBe(1); - - inputElm.remove(); - expect(scope.form.$error.required).toBe(false); - }); - - - 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 reset the model if view is 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).toBeUndefined(); - 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).toBe(false); - - changeInputValueTo('invalid@'); - expect(scope.email).toBeUndefined(); - 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).toBe(false); - - changeInputValueTo('invalid.com'); - expect(scope.url).toBeUndefined(); - 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'); - }); - - - it('should allow {{expr}} as value', function() { - scope.some = 11; - compileInput( - '' + - ''); - - scope.$apply(function() { - scope.value = 'blue'; - scope.some = 'blue'; - scope.other = 'red'; - }); - - expect(inputElm[0].checked).toBe(true); - expect(inputElm[1].checked).toBe(false); - - browserTrigger(inputElm[1]); - expect(scope.value).toBe('red'); - - scope.$apply(function() { - scope.other = 'non-red'; - }); - - expect(inputElm[0].checked).toBe(false); - expect(inputElm[1].checked).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'); - }); - - - it('should be required if false', function() { - compileInput(''); - - browserTrigger(inputElm, 'click'); - expect(inputElm[0].checked).toBe(true); - expect(inputElm).toBeValid(); - - browserTrigger(inputElm, 'click'); - expect(inputElm[0].checked).toBe(false); - expect(inputElm).toBeInvalid(); - }); - }); - - - 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([]); - }); - - - it('should allow custom separator', function() { - compileInput(''); - - changeInputValueTo('a,a'); - expect(scope.list).toEqual(['a,a']); - - changeInputValueTo('a:b'); - expect(scope.list).toEqual(['a', 'b']); - }); - - - it('should allow regexp as a separator', function() { - compileInput(''); - - changeInputValueTo('a,b'); - expect(scope.list).toEqual(['a', 'b']); - - changeInputValueTo('a,b: c'); - expect(scope.list).toEqual(['a', 'b', 'c']); - }); - }); - - describe('required', function() { - - it('should allow bindings on ng-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'); - })); - }); - - - describe('ng-value', function() { - - it('should evaluate and set constant expressions', function() { - compileInput('' + - '' + - ''); - scope.$digest(); - - browserTrigger(inputElm[0], 'click'); - expect(scope.selected).toBe(true); - - browserTrigger(inputElm[1], 'click'); - expect(scope.selected).toBe(false); - - browserTrigger(inputElm[2], 'click'); - expect(scope.selected).toBe(1); - }); - - - it('should watch the expression', function() { - compileInput(''); - - scope.$apply(function() { - scope.selected = scope.value = {some: 'object'}; - }); - expect(inputElm[0].checked).toBe(true); - - scope.$apply(function() { - scope.value = {some: 'other'}; - }); - expect(inputElm[0].checked).toBe(false); - - browserTrigger(inputElm, 'click'); - expect(scope.selected).toBe(scope.value); - }); - }); -}); diff --git a/test/directive/ngBindSpec.js b/test/directive/ngBindSpec.js deleted file mode 100644 index 01a07c52..00000000 --- a/test/directive/ngBindSpec.js +++ /dev/null @@ -1,80 +0,0 @@ -'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'}); - })); - }); -}); diff --git a/test/directive/ngClassSpec.js b/test/directive/ngClassSpec.js deleted file mode 100644 index 2297e343..00000000 --- a/test/directive/ngClassSpec.js +++ /dev/null @@ -1,204 +0,0 @@ -'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('