UNPKG

3.99 kBJavaScriptView Raw
1import 'angular';
2import 'angular-mocks';
3import compilerModuleName from './compiler-ng';
4
5describe('Compiler Ng', () => {
6
7
8 beforeEach(window.module(
9 compilerModuleName
10 ));
11
12
13 let $rootScope;
14 let $scope;
15 let runDigest;
16 beforeEach(window.inject(_$rootScope_ => {
17 $rootScope = _$rootScope_;
18 runDigest = () => {
19 $rootScope.$apply();
20 };
21
22 $scope = $rootScope.$new();
23 }));
24
25
26 let compiler;
27 beforeEach(window.inject(rgCompiler => {
28 compiler = rgCompiler;
29 }));
30
31
32 it('should compile template', () => {
33 $scope.text = 'Hello World';
34 let compileData = null;
35
36
37 compiler({
38 template: '<div>{{text}}</div>'
39 }).then(data => (compileData = data));
40 runDigest();
41
42
43 compileData.link($scope);
44 runDigest();
45
46 compileData.element[0].should.have.html($scope.text);
47 });
48
49
50 it('should allow pass controller', () => {
51 let compileData = null;
52 const text = 'Test';
53
54
55 compiler({
56 template: '<div>{{text}}</div>',
57 controller: [
58 '$scope',
59 scope => {
60 scope.text = text;
61 }]
62 }).then(data => (compileData = data));
63 runDigest();
64
65
66 compileData.link($scope);
67 runDigest();
68
69 compileData.element[0].should.have.html(text);
70 });
71
72
73 it('should allow use controllerAs syntax', () => {
74 let compileData = null;
75 const text = 'Test';
76
77
78 compiler({
79 template: '<div>{{myCtrl.text}}</div>',
80 controllerAs: 'myCtrl',
81 controller() {
82 this.text = text;
83 }
84 }).then(data => (compileData = data));
85 runDigest();
86
87
88 compileData.link($scope);
89 runDigest();
90
91 compileData.element[0].should.have.html(text);
92 });
93
94
95 it('should allow pass local dependencies', () => {
96 let compileData = null;
97 const text = 'Test';
98
99
100 compiler({
101 template: '<div>{{text}}</div>',
102 locals: {
103 textLocal: text
104 },
105 controller: [
106 '$scope',
107 'textLocal', (scope, textLocal) => {
108 scope.text = textLocal;
109 }]
110 }).then(data => (compileData = data));
111 runDigest();
112
113
114 compileData.link($scope);
115 runDigest();
116
117 compileData.element[0].should.have.html(text);
118 });
119
120
121 it('should allow pass asynchronous dependencies', () => {
122 let compileData = null;
123 const text = 'Test';
124
125
126 compiler({
127 template: '<div>{{text}}</div>',
128 resolve: {
129 textResolve($q) {
130 return $q.when(text);
131 }
132 },
133 controller: [
134 '$scope',
135 'textResolve', (scope, textResolve) => {
136 scope.text = textResolve;
137 }]
138 }).then(data => (compileData = data));
139 runDigest();
140
141
142 compileData.link($scope);
143 runDigest();
144
145 compileData.element[0].should.have.html(text);
146 });
147
148
149 it('should fail if resolve was not satisfied', () => {
150 const onCompileError = sandbox.stub();
151
152 compiler({
153 template: '<div/>',
154 resolve: {
155 rejectedResolve: $q => $q.reject()
156 }
157 }).catch(onCompileError);
158 runDigest();
159
160 onCompileError.should.have.been.called;
161 });
162
163
164 it('should allow use bindToController for resolve and locals', () => {
165 let compileData = null;
166 const text = 'Test';
167
168
169 compiler({
170 template: '<div>{{myCtrl.textResolve}}</div>',
171 resolve: {
172 textResolve($q) {
173 return $q.when(text);
174 }
175 },
176 bindToController: true,
177 controllerAs: 'myCtrl',
178 controller: window.angular.noop
179 }).then(data => (compileData = data));
180 runDigest();
181
182
183 compileData.link($scope);
184 runDigest();
185
186 compileData.element[0].should.have.html(text);
187 });
188
189
190 it('should allow use ngTransclude', () => {
191 let compileData = null;
192 const transcludeFn = sandbox.stub();
193
194
195 compiler({
196 template: '<ng-transclude>',
197 locals: {$transclude: transcludeFn}
198 }).then(data => (compileData = data));
199 runDigest();
200
201
202 compileData.link($scope);
203 runDigest();
204
205 transcludeFn.should.have.been.called;
206 });
207});