Skip to content

Commit c164394

Browse files
committed
tests scaffolding refactoring: added option to catch the errors
1 parent 64eca71 commit c164394

File tree

1 file changed

+103
-92
lines changed

1 file changed

+103
-92
lines changed

test/scaffolding.js

Lines changed: 103 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,121 @@
1-
function createHtml (settings) {
2-
var viewportStyle = ' style="height:' + (settings.viewportHeight || 200) + 'px"';
3-
var itemStyle = settings.itemHeight ? ' style="height:' + settings.itemHeight + 'px"' : '';
1+
function createHtml(settings) {
2+
var viewportStyle = ' style="height:' + (settings.viewportHeight || 200) + 'px"';
3+
var itemStyle = settings.itemHeight ? ' style="height:' + settings.itemHeight + 'px"' : '';
44
var bufferSize = settings.bufferSize ? ' buffer-size="' + settings.bufferSize + '"' : '';
5-
var padding = settings.padding ? ' padding="' + settings.padding + '"' : '';
6-
var isLoading = settings.isLoading ? ' is-loading="' + settings.isLoading + '"' : '';
7-
var topVisible = settings.topVisible ? ' top-visible="' + settings.topVisible + '"' : '';
8-
var disabled = settings.disabled ? ' disabled="' + settings.disabled + '"' : '';
9-
var adapter = settings.adapter ? ' adapter="' + settings.adapter + '"' : '';
10-
var template = settings.template ? settings.template : '{{$index}}: {{item}}';
11-
return '<div ui-scroll-viewport' + viewportStyle + '>' +
12-
(settings.wrapper ? settings.wrapper.start : '') +
13-
'<div ui-scroll="item in ' + settings.datasource + '"' +
14-
adapter +
15-
itemStyle + bufferSize + padding + isLoading + topVisible + disabled + '>' +
16-
template +
17-
'</div>' +
18-
(settings.wrapper ? settings.wrapper.end : '') +
19-
'</div>';
5+
var padding = settings.padding ? ' padding="' + settings.padding + '"' : '';
6+
var isLoading = settings.isLoading ? ' is-loading="' + settings.isLoading + '"' : '';
7+
var topVisible = settings.topVisible ? ' top-visible="' + settings.topVisible + '"' : '';
8+
var disabled = settings.disabled ? ' disabled="' + settings.disabled + '"' : '';
9+
var adapter = settings.adapter ? ' adapter="' + settings.adapter + '"' : '';
10+
var template = settings.template ? settings.template : '{{$index}}: {{item}}';
11+
var extra = settings.extra || '';
12+
return '<div ui-scroll-viewport' + viewportStyle + '>' +
13+
(settings.wrapper ? settings.wrapper.start : '') +
14+
'<div ui-scroll="item in ' + settings.datasource + '"' +
15+
adapter +
16+
itemStyle + bufferSize + padding + isLoading + topVisible + disabled + extra + '>' +
17+
template +
18+
'</div>' +
19+
(settings.wrapper ? settings.wrapper.end : '') +
20+
'</div>';
2021
}
2122

2223
function createGridHtml (settings) {
23-
var viewportStyle = ' style="height:' + (settings.viewportHeight || 200) + 'px"';
24-
var columns = ['col0', 'col1', 'col2', 'col3'];
25-
26-
var html =
27-
'<table ui-scroll-viewport ' + viewportStyle + ' >' +
28-
'<thead style="display:block">' +
29-
'<tr>';
30-
columns.forEach(col => { html +=
31-
'<th ui-scroll-th class="' + col + '">' + col + '</th>';
32-
}); html +=
33-
'</tr>' +
34-
'</thead>' +
35-
'<tbody class="grid">' +
36-
'<tr ui-scroll="item in ' + settings.datasource + '" adapter="adapter">';
37-
if(settings.rowTemplate) {
38-
html += settings.rowTemplate;
39-
} else {
40-
columns.forEach(col => { html +=
41-
'<td ui-scroll-td class="' + col + '">{{item.' + col + '}}</td>';
42-
});
43-
} html +=
44-
'</tr>' +
45-
'</tbody>' +
46-
'</table>';
47-
return html;
24+
var viewportStyle = ' style="height:' + (settings.viewportHeight || 200) + 'px"';
25+
var columns = ['col0', 'col1', 'col2', 'col3'];
26+
27+
var html =
28+
'<table ui-scroll-viewport ' + viewportStyle + ' >' +
29+
'<thead style="display:block">' +
30+
'<tr>';
31+
columns.forEach(col => { html +=
32+
'<th ui-scroll-th class="' + col + '">' + col + '</th>';
33+
}); html +=
34+
'</tr>' +
35+
'</thead>' +
36+
'<tbody class="grid">' +
37+
'<tr ui-scroll="item in ' + settings.datasource + '" adapter="adapter">';
38+
if(settings.rowTemplate) {
39+
html += settings.rowTemplate;
40+
} else {
41+
columns.forEach(col => { html +=
42+
'<td ui-scroll-td class="' + col + '">{{item.' + col + '}}</td>';
43+
});
44+
} html +=
45+
'</tr>' +
46+
'</tbody>' +
47+
'</table>';
48+
return html;
4849
}
4950

50-
function finalize (scroller, options, scope, $timeout) {
51-
scroller.remove();
51+
function finalize(scroller, options = {}, scope, $timeout) {
52+
scroller.remove();
5253

53-
if (options && typeof options.cleanupTest === 'function') {
54-
options.cleanupTest(scroller, scope, $timeout);
55-
}
54+
if (typeof options.cleanupTest === 'function') {
55+
options.cleanupTest(scroller, scope, $timeout);
56+
}
5657
}
5758

58-
function runTest (scrollSettings, run, options) {
59-
inject(function ($rootScope, $compile, $window, $timeout) {
60-
var scroller = angular.element(createHtml(scrollSettings));
61-
var scope = $rootScope.$new();
62-
63-
angular.element(document).find('body').append(scroller);
64-
65-
if(options && options.scope) {
66-
angular.extend(scope, options.scope);
67-
}
68-
69-
$compile(scroller)(scope);
70-
71-
scope.$apply();
72-
$timeout.flush();
73-
74-
try {
75-
run(scroller, scope, $timeout);
76-
}
77-
finally {
78-
finalize(scroller, options, scope, $timeout);
79-
}
80-
81-
});
59+
function runTest(scrollSettings, run, options = {}) {
60+
inject(function($rootScope, $compile, $window, $timeout) {
61+
var scroller = angular.element(createHtml(scrollSettings));
62+
var scope = $rootScope.$new();
63+
64+
angular.element(document).find('body').append(scroller);
65+
66+
if (options.scope) {
67+
angular.extend(scope, options.scope);
68+
}
69+
70+
var compile = function() {
71+
$compile(scroller)(scope);
72+
scope.$apply();
73+
$timeout.flush();
74+
};
75+
76+
if (typeof options.catch === 'function') {
77+
try {
78+
compile();
79+
} catch (error) {
80+
options.catch(error);
81+
}
82+
} else {
83+
compile();
84+
}
85+
86+
if(typeof run === 'function') {
87+
try {
88+
run(scroller, scope, $timeout);
89+
} finally {
90+
finalize(scroller, options, scope, $timeout);
91+
}
92+
}
93+
});
8294
}
8395

84-
function runGridTest (scrollSettings, run, options) {
85-
inject(function ($rootScope, $compile, $window, $timeout) {
86-
var scroller = angular.element(createGridHtml(scrollSettings));
87-
var scope = $rootScope.$new();
96+
function runGridTest(scrollSettings, run, options = {}) {
97+
inject(function($rootScope, $compile, $window, $timeout) {
98+
var scroller = angular.element(createGridHtml(scrollSettings));
99+
var scope = $rootScope.$new();
88100

89-
angular.element(document).find('body').append(scroller);
90-
var head = angular.element(scroller.children()[0]);
91-
var body = angular.element(scroller.children()[1]);
101+
angular.element(document).find('body').append(scroller);
102+
var head = angular.element(scroller.children()[0]);
103+
var body = angular.element(scroller.children()[1]);
92104

93-
if(options && options.scope) {
94-
angular.extend(scope, options.scope);
95-
}
105+
if (options.scope) {
106+
angular.extend(scope, options.scope);
107+
}
96108

97-
$compile(scroller)(scope);
109+
$compile(scroller)(scope);
98110

99-
scope.$apply();
100-
$timeout.flush();
111+
scope.$apply();
112+
$timeout.flush();
101113

102-
try {
103-
run(head, body, scope, $timeout);
104-
} finally {
105-
finalize(scroller, options, scope, $timeout);
106-
}
114+
try {
115+
run(head, body, scope, $timeout);
116+
} finally {
117+
finalize(scroller, options, scope, $timeout);
118+
}
107119

108-
}
109-
);
120+
});
110121
}

0 commit comments

Comments
 (0)