forked from jirikavi/AngularJS-Toaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
directiveTemplateSpec.js
213 lines (165 loc) · 6.85 KB
/
directiveTemplateSpec.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* global describe global it global beforeEach global angular global inject global expect */
'use strict';
describe('directiveTemplate', function () {
var toaster, scope, $compile;
beforeEach(function () {
createDirectives();
// load dependencies
module('testApp');
module('toaster');
// inject the toaster service
inject(function (_toaster_, _$rootScope_, _$compile_) {
toaster = _toaster_;
scope = _$rootScope_;
$compile = _$compile_;
});
});
it('should load and render the referenced directive template text', function () {
var container = compileContainer();
pop({ type: 'info', body: 'bind-template-only', bodyOutputType: 'directive' });
expect(container[0].innerText).toBe('here is some great new text! It was brought in via directive!');
});
it('should bind directiveData to the directive template', function () {
var container = compileContainer();
pop({ type: 'info', body: 'bind-template-with-data', bodyOutputType: 'directive', directiveData: { name: 'Bob' } });
expect(container[0].innerText).toBe('Hello Bob');
});
it('should parse type string directiveData to an object', function () {
var container = compileContainer();
pop({ type: 'info', body: 'bind-template-with-data', bodyOutputType: 'directive', directiveData: '{ "name": "Bob" }' });
expect(container[0].innerText).toBe('Hello Bob');
});
it('should render type number directiveData', function () {
var container = compileContainer();
pop({ type: 'info', body: 'bind-template-with-numeric-data', bodyOutputType: 'directive', directiveData: 2 });
expect(container[0].innerText).toBe('1 + 1 = 2');
});
it('should bind Attribute-restricted templates', function () {
var container = compileContainer();
pop({ type: 'info', body: 'bind-template-only', bodyOutputType: 'directive', directiveData: { name: 'Bob' } });
expect(container[0].innerText).toBe('here is some great new text! It was brought in via directive!');
});
it('should bind unrestricted templates', function () {
var container = compileContainer();
pop({ type: 'info', body: 'unrestricted-template', bodyOutputType: 'directive' });
expect(container[0].innerText).toBe('Unrestricted Template');
});
it('should not bind Element-only-restricted templates', function () {
var hasError = false;
var container = compileContainer();
try {
pop({ type: 'info', body: 'element-template', bodyOutputType: 'directive' });
} catch(e) {
var message = 'Directives must be usable as attributes. ' +
'Add "A" to the restrict option (or remove the option entirely). Occurred for directive element-template.';
expect(e.message).toBe(message);
hasError = true;
}
expect(hasError).toBe(true);
});
it('should not bind Class-only-restricted templates', function () {
var hasError = false;
var container = compileContainer();
try {
pop({ type: 'info', body: 'class-template', bodyOutputType: 'directive' });
} catch(e) {
var message = 'Directives must be usable as attributes. ' +
'Add "A" to the restrict option (or remove the option entirely). Occurred for directive class-template.';
expect(e.message).toBe(message);
hasError = true;
}
expect(hasError).toBe(true);
});
it('should throw an error if directiveName argument is not passed via body', function () {
var container = compileContainer();
var hasError = false;
expect(container[0].innerText).toBe('');
try {
pop({ type: 'info', bodyOutputType: 'directive' });
} catch (e) {
expect(e.message).toBe('A valid directive name must be provided via the toast body argument when using bodyOutputType: directive');
hasError = true;
}
expect(container[0].innerText).toBe('');
expect(hasError).toBe(true);
});
it('should throw an error if directiveName argument is an empty string', function () {
var container = compileContainer();
var hasError = false;
expect(container[0].innerText).toBe('');
try {
pop({ type: 'info', body: '', bodyOutputType: 'directive' });
} catch (e) {
expect(e.message).toBe('A valid directive name must be provided via the toast body argument when using bodyOutputType: directive');
hasError = true;
}
expect(container[0].innerText).toBe('');
expect(hasError).toBe(true);
});
it('should throw an error if the directive could not be found', function () {
var hasError = false;
compileContainer();
try {
pop({ type: 'info', body: 'non-existent-directive', bodyOutputType: 'directive' });
} catch (e) {
var message = 'non-existent-directive could not be found. ' +
'The name should appear as it exists in the markup,' +
' not camelCased as it would appear in the directive declaration,' +
' e.g. directive-name not directiveName.'
expect(e.message).toBe(message);
hasError = true;
}
expect(hasError).toBe(true);
});
it('should throw an error if the directive uses isolate scope', function () {
var hasError = false;
compileContainer();
try {
pop({ type: 'info', body: 'isolate-scope', bodyOutputType: 'directive' });
} catch (e) {
var message = 'Cannot use a directive with an isolated scope.' +
' The scope must be either true or falsy (e.g. false/null/undefined). Occurred for directive isolate-scope.';
expect(e.message).toBe(message)
hasError = true;
}
expect(hasError).toBe(true);
})
function compileContainer() {
var element = angular.element('<toaster-container></toaster-container>');
$compile(element)(scope);
scope.$digest();
return element;
}
function pop(params) {
toaster.pop(params);
// force new toast to be rendered
scope.$digest();
}
function createDirectives() {
angular.module('testApp', [])
.directive('bindTemplateOnly', function () {
return {
restrict: 'A',
template: 'here is some great new text! <span style="color:orange">It was brought in via directive!</span>'
}
})
.directive('bindTemplateWithData', function () {
return { template: 'Hello {{directiveData.name}}' }
})
.directive('bindTemplateWithNumericData', function () {
return { template: '1 + 1 = {{directiveData}}' }
})
.directive('elementTemplate', function () {
return { restrict: 'E', template: 'Element Template' }
})
.directive('classTemplate', function () {
return { restrict: 'C', template: 'Class Template' }
})
.directive('unrestrictedTemplate', function () {
return { template: 'Unrestricted Template' }
})
.directive('isolateScope', function () {
return { template: 'isolate scope template', scope: {}}
});
}
})