forked from dwmkerr/angular-modal-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.spec.js
220 lines (156 loc) · 5.87 KB
/
controller.spec.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
214
215
216
217
218
219
220
describe('controller', () => {
var ModalService = null;
var $httpBackend = null;
var $timeout = null;
angular.module('controllertests', ['angularModalService'])
.controller('CloseController', ($scope, close) => {
$scope.close = close;
})
.controller('InputsController', ($scope, input1, input2, close) => {
$scope.input1 = input1;
$scope.input2 = input2;
$scope.close = close;
})
// Important: 'controllerAs' functions are bound to 'this', this is very
// important, we CANNOT change the below to an arrow function.
.controller('ControllerAsController', function() {
var vm = this;
vm.character = "Fry";
vm.checkValidity = () => {
return vm.ExampleForm.$valid;
}
})
.controller('ElementController', ($scope, $element) => {
$scope.getElement = () => { return $element; };
});
beforeEach(() => {
angular.mock.module('controllertests');
inject((_ModalService_, $injector) => {
ModalService = _ModalService_;
$httpBackend = $injector.get('$httpBackend');
$timeout = $injector.get('$timeout');
$httpBackend.when('GET', 'some/controllertemplate.html').respond("<div id='controllertemplate'>controller template</div>");
$httpBackend.when('GET', 'some/formtemplate.html').respond(
"<form name='formCtrl.ExampleForm'><input type='text' name='exampleInput'></form>"
);
});
});
afterEach(() => {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('should inject the close function into the controller', () => {
$httpBackend.expectGET('some/controllertemplate.html');
ModalService.showModal({
controller: "CloseController",
templateUrl: "some/controllertemplate.html"
}).then((modal) => {
// The controller we've created should put the close function on
// the scope, this is how we test it's been passed.
expect(modal.scope.close).not.toBeUndefined();
});
$httpBackend.flush();
});
it('should inject inputs to the controller', () => {
$httpBackend.expectGET('some/controllertemplate.html');
ModalService.showModal({
controller: "InputsController",
templateUrl: "some/controllertemplate.html",
inputs: {
input1: 15,
input2: "hi"
}
}).then((modal) => {
// The controller sets the inputs on the scope.
expect(modal.scope.input1).toBe(15);
expect(modal.scope.input2).toBe("hi");
});
$httpBackend.flush();
});
it('should add a controller to the scope if controllerAs is used', () => {
$httpBackend.expectGET('some/controllertemplate.html');
ModalService.showModal({
controller: 'ControllerAsController',
controllerAs: 'futurama',
templateUrl: 'some/controllertemplate.html'
}).then((modal) => {
// The controller should be on the scope.
expect(modal.scope.futurama).not.toBeNull();
// Fields defined on the controller instance should be on the
// controller on the scope.
expect(modal.scope.futurama.character).toBe('Fry');
});
$httpBackend.flush();
});
it('should add a controller to the scope if the controller is inlined', () => {
$httpBackend.expectGET('some/controllertemplate.html');
ModalService.showModal({
controller: ($scope) => {
$scope.character = "Fry";
},
templateUrl: 'some/controllertemplate.html'
}).then((modal) => {
expect(modal.scope).not.toBeNull();
expect(modal.scope.character).toBe('Fry');
});
$httpBackend.flush();
});
it('should add a controller to the scope if the controller is inlined with controllerAs', () => {
$httpBackend.expectGET('some/controllertemplate.html');
ModalService.showModal({
controller: function() {
this.character = "Fry";
},
controllerAs: 'futurama',
templateUrl: 'some/controllertemplate.html'
}).then((modal) => {
// The controller should be on the scope.
expect(modal.scope.futurama).not.toBeNull();
// Fields defined on the controller instance should be on the
// controller on the scope.
expect(modal.scope.futurama.character).toBe('Fry');
});
$httpBackend.flush();
});
it('should add a controller to the scope if the controller is inlined with controllerAs and also annotated', () => {
$httpBackend.expectGET('some/controllertemplate.html');
ModalService.showModal({
controller: ['$http', function ($http) {
expect($http).not.toBeNull();
this.character = "Fry";
}],
controllerAs: 'futurama',
templateUrl: 'some/controllertemplate.html'
}).then((modal) => {
// The controller should be on the scope.
expect(modal.scope.futurama).not.toBeNull();
// Fields defined on the controller instance should be on the
// controller on the scope.
expect(modal.scope.futurama.character).toBe('Fry');
});
$httpBackend.flush();
});
it('should inject the modal element into the controller', () => {
$httpBackend.expectGET('some/controllertemplate.html');
ModalService.showModal({
controller: 'ElementController',
templateUrl: 'some/controllertemplate.html'
}).then((modal) => {
// The controller should be on the scope.
expect(modal.scope.getElement()).not.toBeUndefined();
});
$httpBackend.flush();
});
it('should correct process form with controllerAs.form syntax', () => {
$httpBackend.expectGET('some/formtemplate.html');
ModalService.showModal({
controller: 'ControllerAsController',
controllerAs: 'formCtrl',
templateUrl: 'some/formtemplate.html'
}).then((modal) => {
expect(modal.scope.formCtrl.ExampleForm).not.toBeUndefined();
expect(modal.scope.formCtrl.checkValidity()).toBe(true);
});
$httpBackend.flush();
});
});