Merge "Add qtip job to pod zte-virtual6"
[releng.git] / utils / test / testapi / 3rd_party / static / testapi-ui / assets / lib / angular-confirm-modal / test / unit / confirmSpec.js
1 describe('angular-confirm', function() {
2
3     var $rootScope, $uibModal;
4
5     beforeEach(angular.mock.module('angular-confirm', function ($provide) {
6
7         $provide.decorator('$uibModal', function($delegate) {
8             $uibModal = {
9                 open: jasmine.createSpy('$uibModal.open', function(settings) {
10                     return {result: settings};
11                 })
12             };
13             return $uibModal;
14         });
15
16         $provide.decorator('$confirm', function($delegate) {
17             return jasmine.createSpy('$confirm', $delegate);
18         });
19
20     }));
21
22     beforeEach(angular.mock.inject(function (_$rootScope_) {
23         $rootScope = _$rootScope_;
24     }));
25
26     describe('ConfirmModalController', function() {
27         var $scope, controller, data = {testVal: 1}, $uibModalInstance;
28
29         beforeEach(angular.mock.inject(function($controller) {
30             $scope = $rootScope.$new();
31             $uibModalInstance = {
32                 close: jasmine.createSpy('$uibModalInstance.close'),
33                 dismiss: jasmine.createSpy('$uibModalInstance.dismiss'),
34                 result: {
35                     then: jasmine.createSpy('$uibModalInstance.result.then')
36                 }
37             };
38             controller = $controller('ConfirmModalController', {"$scope": $scope, "$uibModalInstance": $uibModalInstance, "data": data});
39         }));
40
41         it("should copy the data, not use it as a reference", function() {
42             data.testVal = 2;
43             expect($scope.data.testVal).toEqual(1);
44         });
45
46         it("should call close when $scope.ok is invoked", function() {
47             $scope.ok();
48             expect($uibModalInstance.close).toHaveBeenCalled();
49         });
50
51         it("should call dismiss when $scope.cancel is invoked", function() {
52             $scope.cancel();
53             expect($uibModalInstance.dismiss).toHaveBeenCalledWith('cancel');
54         });
55
56     });
57
58     describe('$confirm factory', function() {
59
60         var $confirm, $confirmModalDefaults;
61
62         beforeEach(angular.mock.inject(function(_$confirm_, _$confirmModalDefaults_) {
63             $confirm = _$confirm_;
64             $confirm.and.callThrough();
65             $confirmModalDefaults = _$confirmModalDefaults_;
66             $uibModal.open.and.callThrough();
67         }));
68
69         it("should call $uibModal.open", function() {
70             $confirm();
71             expect($uibModal.open).toHaveBeenCalled();
72         });
73
74         it("should override the defaults with settings passed in", function() {
75             var settings = $confirm({}, {"template": "hello"});
76             expect(settings.template).toEqual("hello");
77         });
78                 
79                 it("should not change the defaults", function() {
80             var settings = $confirm({}, {"templateUrl": "hello"});
81             expect(settings.templateUrl).toEqual("hello");
82                         expect(settings.template).not.toBeDefined();
83                         expect($confirmModalDefaults.template).toBeDefined();
84                         expect($confirmModalDefaults.templateUrl).not.toBeDefined();
85         });
86
87         it("should override the default labels with the data passed in", function() {
88             var settings = $confirm({title: "Title"});
89             var data = settings.resolve.data();
90             expect(data.title).toEqual("Title");
91             expect(data.ok).toEqual('OK');
92         });
93
94         it("should remove template if templateUrl is passed in", function() {
95             var settings = $confirm({}, {templateUrl: "abc.txt"});
96             expect(settings.template).not.toBeDefined();
97         });
98
99     });
100
101     describe('confirm directive', function() {
102         var $scope, element, $confirm, data;
103
104         beforeEach(angular.mock.inject(function (_$confirm_, $compile) {
105             $confirm = _$confirm_;
106
107             $confirm.and.callFake(function(d) {
108                 data = d;
109                 return {then: function() {}}
110             });
111
112             $scope = $rootScope.$new();
113             $scope.click = jasmine.createSpy('$scope.click');
114         }));
115
116         describe('resolve properties in title', function() {
117             beforeEach(angular.mock.inject(function($compile) {
118                 $scope.name = 'Joe';
119                 element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure, {{name}}?">Delete</button>');
120                 $compile(element)($scope);
121                 $scope.$digest();
122             }));
123
124             it("should resolve the name to the text property", function() {
125                 element.triggerHandler('click');
126                 expect(data.text).toEqual('Are you sure, Joe?');
127             });
128         });
129
130         describe('without confirmIf', function() {
131
132             beforeEach(angular.mock.inject(function($compile) {
133                 element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?">Delete</button>');
134                 $compile(element)($scope);
135                 $scope.$digest();
136             }));
137
138             it("should call confirm on click and not call the function", function() {
139                 element.triggerHandler('click');
140                 expect($scope.click).not.toHaveBeenCalled();
141                 expect($confirm).toHaveBeenCalled();
142             });
143
144         });
145
146         describe('with confirmIf option', function() {
147
148             beforeEach(angular.mock.inject(function($compile) {
149                 element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?" confirm-if="truthy">Delete</button>');
150                 $compile(element)($scope);
151                 $scope.$digest();
152             }));
153
154             it("should call confirm on click and not call the function", function() {
155                 $scope.truthy = true;
156                 $scope.$apply();
157                 element.triggerHandler('click');
158                 expect($scope.click).not.toHaveBeenCalled();
159                 expect($confirm).toHaveBeenCalled();
160             });
161
162             it("should call the function", function() {
163                 $scope.truthy = false;
164                 $scope.$apply();
165                 element.triggerHandler('click');
166                 expect($scope.click).toHaveBeenCalled();
167                 expect($confirm).not.toHaveBeenCalled();
168             });
169
170         });
171
172         describe('with confirmTitle option', function() {
173             beforeEach(angular.mock.inject(function($compile) {
174                 $scope.name = 'Joe';
175                 element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?" confirm-title="Hello, {{name}}!">Delete</button>');
176                 $compile(element)($scope);
177                 $scope.$digest();
178             }));
179
180             it("should resolve the confirmTitle to the title property", function() {
181                 element.triggerHandler('click');
182                 expect(data.title).toEqual('Hello, Joe!');
183             });
184
185         });
186
187         describe('with confirmOk option', function() {
188             beforeEach(angular.mock.inject(function($compile) {
189                 $scope.name = 'Joe';
190                 element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?" confirm-ok="Okie Dokie, {{name}}!">Delete</button>');
191                 $compile(element)($scope);
192                 $scope.$digest();
193             }));
194
195             it("should resolve the confirmTitle to the title property", function() {
196                 element.triggerHandler('click');
197                 expect(data.ok).toEqual('Okie Dokie, Joe!');
198             });
199         });
200
201         describe('with confirmCancel option', function() {
202             beforeEach(angular.mock.inject(function($compile) {
203                 $scope.name = 'Joe';
204                 element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?" confirm-cancel="No Way, {{name}}!">Delete</button>');
205                 $compile(element)($scope);
206                 $scope.$digest();
207             }));
208
209             it("should resolve the confirmTitle to the title property", function() {
210                 element.triggerHandler('click');
211                 expect(data.cancel).toEqual('No Way, Joe!');
212             });
213         });
214
215         describe('with confirmSettings option', function() {
216             beforeEach(angular.mock.inject(function($compile) {
217                 $scope.settings = {name: 'Joe'};
218                 element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?" confirm-settings="settings">Delete</button>');
219                 $compile(element)($scope);
220                 $scope.$digest();
221             }));
222
223             it("should pass the settings to $confirm", function() {
224                 element.triggerHandler('click');
225                 expect($confirm).toHaveBeenCalledWith({text: "Are you sure?"}, $scope.settings)
226             });
227         });
228
229         describe('with confirmSettings option direct entry', function() {
230             beforeEach(angular.mock.inject(function($compile) {
231                 element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?" confirm-settings="{name: \'Joe\'}">Delete</button>');
232                 $compile(element)($scope);
233                 $scope.$digest();
234             }));
235
236             it("should pass the settings to $confirm", function() {
237                 element.triggerHandler('click');
238                 expect($confirm).toHaveBeenCalledWith({text: "Are you sure?"}, {name: "Joe"})
239             });
240         });
241
242     });
243
244 });