forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngControllerSpec.js
110 lines (88 loc) · 3.44 KB
/
ngControllerSpec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
'use strict';
describe('ngController', function() {
var element;
beforeEach(module(function($controllerProvider) {
$controllerProvider.register('PublicModule', function() {
this.mark = 'works';
});
}));
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';
};
$window.Public = function() {
this.mark = 'works';
}
}));
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 publish controller into scope', inject(function($compile, $rootScope) {
element = $compile('<div ng-controller="Public as p">{{p.mark}}</div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('works');
}));
it('should publish controller into scope from module', inject(function($compile, $rootScope) {
element = $compile('<div ng-controller="PublicModule as p">{{p.mark}}</div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('works');
}));
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');
}));
it('should allow mixing with ngRepeat which inherits the item sub-scope', inject(function($compile, $rootScope){
$rootScope.Counter = function($scope){
$scope.count = $scope.$index;
};
element = $compile('<div><div ng-repeat="i in [1,2]" ng-controller="Counter">{{count}};</div></div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('0;1;');
}));
it('should allow mixing with ngInclude', inject(function($compile, $rootScope, $httpBackend) {
$rootScope.Includer = function($scope) {
$scope.name = 'Included';
};
element = $compile('<div><div ng-controller="Includer" ng-include="\'url\'"></div></div>')($rootScope);
$httpBackend.expect('GET', 'url').respond('{{name}}');
$rootScope.$digest();
$httpBackend.flush();
expect(element.text()).toEqual('Included');
dealoc($rootScope);
}));
});