aboutsummaryrefslogtreecommitdiffstats
path: root/test/ng/directive/ngControllerSpec.js
diff options
context:
space:
mode:
Diffstat (limited to 'test/ng/directive/ngControllerSpec.js')
-rw-r--r--test/ng/directive/ngControllerSpec.js65
1 files changed, 65 insertions, 0 deletions
diff --git a/test/ng/directive/ngControllerSpec.js b/test/ng/directive/ngControllerSpec.js
new file mode 100644
index 00000000..832a683d
--- /dev/null
+++ b/test/ng/directive/ngControllerSpec.js
@@ -0,0 +1,65 @@
+'use strict';
+
+describe('ng-controller', function() {
+ var element;
+
+ beforeEach(inject(function($window) {
+ $window.Greeter = function($scope) {
+ // private stuff (not exported to scope)
+ this.prefix = 'Hello ';
+
+ // public stuff (exported to scope)
+ var ctrl = this;
+ $scope.name = 'Misko';
+ $scope.greet = function(name) {
+ return ctrl.prefix + name + ctrl.suffix;
+ };
+
+ $scope.protoGreet = bind(this, this.protoGreet);
+ };
+ $window.Greeter.prototype = {
+ suffix: '!',
+ protoGreet: function(name) {
+ return this.prefix + name + this.suffix;
+ }
+ };
+
+ $window.Child = function($scope) {
+ $scope.name = 'Adam';
+ };
+ }));
+
+ afterEach(function() {
+ dealoc(element);
+ });
+
+
+ it('should instantiate controller and bind methods', inject(function($compile, $rootScope) {
+ element = $compile('<div ng-controller="Greeter">{{greet(name)}}</div>')($rootScope);
+ $rootScope.$digest();
+ expect(element.text()).toBe('Hello Misko!');
+ }));
+
+
+ it('should allow nested controllers', inject(function($compile, $rootScope) {
+ element = $compile('<div ng-controller="Greeter"><div ng-controller="Child">{{greet(name)}}</div></div>')($rootScope);
+ $rootScope.$digest();
+ expect(element.text()).toBe('Hello Adam!');
+ dealoc(element);
+
+ element = $compile('<div ng-controller="Greeter"><div ng-controller="Child">{{protoGreet(name)}}</div></div>')($rootScope);
+ $rootScope.$digest();
+ expect(element.text()).toBe('Hello Adam!');
+ }));
+
+
+ it('should instantiate controller defined on scope', inject(function($compile, $rootScope) {
+ $rootScope.Greeter = function($scope) {
+ $scope.name = 'Vojta';
+ };
+
+ element = $compile('<div ng-controller="Greeter">{{name}}</div>')($rootScope);
+ $rootScope.$digest();
+ expect(element.text()).toBe('Vojta');
+ }));
+});