Skip to content

Commit 60f1095

Browse files
committed
Assignable expressions for custom directive/component tests
1 parent df5847f commit 60f1095

File tree

1 file changed

+67
-32
lines changed

1 file changed

+67
-32
lines changed

test/AssigningSpec.js

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,25 @@ describe('uiScroll', function () {
1515
myApp.controller('MyBottomController', function($scope) {
1616
$scope.name = 'MyBottomController';
1717
});
18-
var customDirTemplate;
19-
myApp.directive('myDir', function() {
20-
return {
21-
restrict: 'E',
22-
controllerAs: 'ctrl',
23-
controller: function () { this.show = true; },
24-
template: customDirTemplate
25-
};
26-
});
27-
28-
var setDir = function(viewport) {
29-
customDirTemplate =
30-
'<div ' + (viewport ? 'ui-scroll-viewport' : '') +' style="height:200px" ng-if="ctrl.show">' +
31-
'<div ui-scroll="item in myMultipageDatasource" adapter="ctrl.adapter">' +
32-
'{{$index}}: {{item}}' +
33-
'</div>' +
34-
'</div>';
35-
};
3618

3719
beforeEach(module('myApp'));
3820

21+
var setDirective = function(options) {
22+
return function() {
23+
var directive = {
24+
restrict: 'E',
25+
controller: function() {
26+
this.show = true;
27+
}
28+
};
29+
if (options.ctrlAs) {
30+
directive.controllerAs = options.ctrlAs;
31+
}
32+
directive.template = options.template;
33+
return directive;
34+
};
35+
};
36+
3937
var executeTest = function(template, ctrlSelector, scopeContainer) {
4038
inject(function($rootScope, $compile, $timeout) {
4139
// build and render
@@ -72,7 +70,7 @@ describe('uiScroll', function () {
7270
describe('Adapter assigning', function () {
7371

7472
it('should work in simplest case (viewport)', function () {
75-
var template =
73+
var template =
7674
'<div ng-controller="MyTopController">' +
7775
'<div ng-controller="MyInnerController">' +
7876
'<div ng-controller="MyBottomController">' +
@@ -88,7 +86,7 @@ describe('uiScroll', function () {
8886
});
8987

9088
it('should work in simplest case (no viewport)', function () {
91-
var template =
89+
var template =
9290
'<div ng-controller="MyTopController">' +
9391
'<div ng-controller="MyInnerController" ng-if="name">' +
9492
'<div ng-controller="MyBottomController" ng-if="name">' +
@@ -102,7 +100,7 @@ describe('uiScroll', function () {
102100
});
103101

104102
it('should work with additional container (viewport)', function () {
105-
var template =
103+
var template =
106104
'<div ng-controller="MyTopController">' +
107105
'<div ng-controller="MyInnerController">' +
108106
'<div ng-controller="MyBottomController">' +
@@ -118,7 +116,7 @@ describe('uiScroll', function () {
118116
});
119117

120118
it('should work with additional container (no viewport)', function () {
121-
var template =
119+
var template =
122120
'<div ng-controller="MyTopController">' +
123121
'<div ng-controller="MyInnerController" ng-if="name">' +
124122
'<div ng-controller="MyBottomController" ng-if="name">' +
@@ -132,7 +130,7 @@ describe('uiScroll', function () {
132130
});
133131

134132
it('should work for "on" syntax (viewport)', function () {
135-
var template =
133+
var template =
136134
'<div ng-controller="MyTopController">' +
137135
'<div ng-controller="MyInnerController" ng-if="name">' +
138136
'<div ng-controller="MyBottomController" ng-if="name">' +
@@ -148,7 +146,7 @@ describe('uiScroll', function () {
148146
});
149147

150148
it('should work for "on" syntax (no viewport)', function () {
151-
var template =
149+
var template =
152150
'<div ng-controller="MyTopController">' +
153151
'<div ng-controller="MyInnerController" ng-if="name">' +
154152
'<div ng-controller="MyBottomController" ng-if="name">' +
@@ -162,7 +160,7 @@ describe('uiScroll', function () {
162160
});
163161

164162
it('should work for "Controller As" syntax (viewport)', function () {
165-
var template =
163+
var template =
166164
'<div ng-controller="MyTopController">' +
167165
'<div ng-controller="MyInnerController as ctrl" ng-if="name">' +
168166
'<div ng-controller="MyBottomController" ng-if="name">' +
@@ -178,7 +176,7 @@ describe('uiScroll', function () {
178176
});
179177

180178
it('should work for "Controller As" syntax (no viewport)', function () {
181-
var template =
179+
var template =
182180
'<div ng-controller="MyTopController">' +
183181
'<div ng-controller="MyInnerController as ctrl" ng-if="name">' +
184182
'<div ng-controller="MyBottomController" ng-if="name">' +
@@ -192,30 +190,67 @@ describe('uiScroll', function () {
192190
});
193191

194192
it('should work for custom directive with "Controller As" syntax (viewport)', function () {
195-
setDir(true);
193+
myApp.directive('myDir1', setDirective({
194+
ctrlAs: 'ctrl',
195+
template:
196+
'<div ui-scroll-viewport style="height:200px" ng-if="ctrl.show">' +
197+
'<div ui-scroll="item in myMultipageDatasource" adapter="ctrl.adapter">' +
198+
'{{$index}}: {{item}}' +
199+
'</div>' +
200+
'</div>'
201+
}));
196202
var template =
197203
'<div ng-controller="MyTopController">' +
198204
'<div ng-controller="MyInnerController" ng-if="name">' +
199205
'<div ng-controller="MyBottomController" ng-if="name">' +
200-
'<my-dir></my-dir>' +
206+
'<my-dir1></my-dir1>' +
201207
'</div>' +
202208
'</div>' +
203209
'</div>';
204210
executeTest(template, 'MyBottomController', 'ctrl');
205211
});
206212

207-
/*it('should work for custom directive with "Controller As" syntax (no viewport)', function () {
208-
setDir(false);
213+
it('should work for custom directive with "Controller As" syntax (no viewport)', function () {
214+
myApp.directive('myDir2', setDirective({
215+
ctrlAs: 'ctrl',
216+
template:
217+
'<div style="height:200px" ng-if="ctrl.show">' +
218+
'<div ui-scroll="item in myMultipageDatasource" adapter="ctrl.adapter">' +
219+
'{{$index}}: {{item}}' +
220+
'</div>' +
221+
'</div>'
222+
}));
209223
var template =
210224
'<div ng-controller="MyTopController">' +
211225
'<div ng-controller="MyInnerController" ng-if="name">' +
212226
'<div ng-controller="MyBottomController" ng-if="name">' +
213-
'<my-dir></my-dir>' +
227+
'<my-dir2></my-dir2>' +
214228
'</div>' +
215229
'</div>' +
216230
'</div>';
217231
executeTest(template, 'MyBottomController', 'ctrl');
218-
});*/
232+
});
233+
234+
it('should work for custom directive with the adapter defined on some external controller', function () {
235+
myApp.directive('myDir3', setDirective({
236+
ctrlAs: 'ctrl2',
237+
template:
238+
'<div style="height:200px" ng-if="ctrl2.show">' +
239+
'<div ui-scroll="item in myMultipageDatasource" adapter="ctrl.adapter">' +
240+
'{{$index}}: {{item}}' +
241+
'</div>' +
242+
'</div>'
243+
}));
244+
var template =
245+
'<div ng-controller="MyTopController">' +
246+
'<div ng-controller="MyInnerController as ctrl" ng-if="name">' +
247+
'<div ng-controller="MyBottomController" ng-if="name">' +
248+
'<my-dir3></my-dir3>' +
249+
'</div>' +
250+
'</div>' +
251+
'</div>';
252+
executeTest(template, 'MyInnerController as ctrl', 'ctrl');
253+
});
219254
});
220255

221256
});

0 commit comments

Comments
 (0)