summaryrefslogtreecommitdiffstats
path: root/cvp/3rd_party/static/testapi-ui/assets/lib/angular-confirm-modal/test/unit/confirmSpec.js
blob: 8434de407434948c7e065db288be4bc73a1f6b61 (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
describe('angular-confirm', function() {

    var $rootScope, $uibModal;

    beforeEach(angular.mock.module('angular-confirm', function ($provide) {

        $provide.decorator('$uibModal', function($delegate) {
            $uibModal = {
                open: jasmine.createSpy('$uibModal.open', function(settings) {
                    return {result: settings};
                })
            };
            return $uibModal;
        });

        $provide.decorator('$confirm', function($delegate) {
            return jasmine.createSpy('$confirm', $delegate);
        });

    }));

    beforeEach(angular.mock.inject(function (_$rootScope_) {
        $rootScope = _$rootScope_;
    }));

    describe('ConfirmModalController', function() {
        var $scope, controller, data = {testVal: 1}, $uibModalInstance;

        beforeEach(angular.mock.inject(function($controller) {
            $scope = $rootScope.$new();
            $uibModalInstance = {
                close: jasmine.createSpy('$uibModalInstance.close'),
                dismiss: jasmine.createSpy('$uibModalInstance.dismiss'),
                result: {
                    then: jasmine.createSpy('$uibModalInstance.result.then')
                }
            };
            controller = $controller('ConfirmModalController', {"$scope": $scope, "$uibModalInstance": $uibModalInstance, "data": data});
        }));

        it("should copy the data, not use it as a reference", function() {
            data.testVal = 2;
            expect($scope.data.testVal).toEqual(1);
        });

        it("should call close when $scope.ok is invoked", function() {
            $scope.ok();
            expect($uibModalInstance.close).toHaveBeenCalled();
        });

        it("should call dismiss when $scope.cancel is invoked", function() {
            $scope.cancel();
            expect($uibModalInstance.dismiss).toHaveBeenCalledWith('cancel');
        });

    });

    describe('$confirm factory', function() {

        var $confirm, $confirmModalDefaults;

        beforeEach(angular.mock.inject(function(_$confirm_, _$confirmModalDefaults_) {
            $confirm = _$confirm_;
            $confirm.and.callThrough();
            $confirmModalDefaults = _$confirmModalDefaults_;
            $uibModal.open.and.callThrough();
        }));

        it("should call $uibModal.open", function() {
            $confirm();
            expect($uibModal.open).toHaveBeenCalled();
        });

        it("should override the defaults with settings passed in", function() {
            var settings = $confirm({}, {"template": "hello"});
            expect(settings.template).toEqual("hello");
        });
		
		it("should not change the defaults", function() {
            var settings = $confirm({}, {"templateUrl": "hello"});
            expect(settings.templateUrl).toEqual("hello");
			expect(settings.template).not.toBeDefined();
			expect($confirmModalDefaults.template).toBeDefined();
			expect($confirmModalDefaults.templateUrl).not.toBeDefined();
        });

        it("should override the default labels with the data passed in", function() {
            var settings = $confirm({title: "Title"});
            var data = settings.resolve.data();
            expect(data.title).toEqual("Title");
            expect(data.ok).toEqual('OK');
        });

        it("should remove template if templateUrl is passed in", function() {
            var settings = $confirm({}, {templateUrl: "abc.txt"});
            expect(settings.template).not.toBeDefined();
        });

    });

    describe('confirm directive', function() {
        var $scope, element, $confirm, data;

        beforeEach(angular.mock.inject(function (_$confirm_, $compile) {
            $confirm = _$confirm_;

            $confirm.and.callFake(function(d) {
                data = d;
                return {then: function() {}}
            });

            $scope = $rootScope.$new();
            $scope.click = jasmine.createSpy('$scope.click');
        }));

        describe('resolve properties in title', function() {
            beforeEach(angular.mock.inject(function($compile) {
                $scope.name = 'Joe';
                element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure, {{name}}?">Delete</button>');
                $compile(element)($scope);
                $scope.$digest();
            }));

            it("should resolve the name to the text property", function() {
                element.triggerHandler('click');
                expect(data.text).toEqual('Are you sure, Joe?');
            });
        });

        describe('without confirmIf', function() {

            beforeEach(angular.mock.inject(function($compile) {
                element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?">Delete</button>');
                $compile(element)($scope);
                $scope.$digest();
            }));

            it("should call confirm on click and not call the function", function() {
                element.triggerHandler('click');
                expect($scope.click).not.toHaveBeenCalled();
                expect($confirm).toHaveBeenCalled();
            });

        });

        describe('with confirmIf option', function() {

            beforeEach(angular.mock.inject(function($compile) {
                element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?" confirm-if="truthy">Delete</button>');
                $compile(element)($scope);
                $scope.$digest();
            }));

            it("should call confirm on click and not call the function", function() {
                $scope.truthy = true;
                $scope.$apply();
                element.triggerHandler('click');
                expect($scope.click).not.toHaveBeenCalled();
                expect($confirm).toHaveBeenCalled();
            });

            it("should call the function", function() {
                $scope.truthy = false;
                $scope.$apply();
                element.triggerHandler('click');
                expect($scope.click).toHaveBeenCalled();
                expect($confirm).not.toHaveBeenCalled();
            });

        });

        describe('with confirmTitle option', function() {
            beforeEach(angular.mock.inject(function($compile) {
                $scope.name = 'Joe';
                element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?" confirm-title="Hello, {{name}}!">Delete</button>');
                $compile(element)($scope);
                $scope.$digest();
            }));

            it("should resolve the confirmTitle to the title property", function() {
                element.triggerHandler('click');
                expect(data.title).toEqual('Hello, Joe!');
            });

        });

        describe('with confirmOk option', function() {
            beforeEach(angular.mock.inject(function($compile) {
                $scope.name = 'Joe';
                element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?" confirm-ok="Okie Dokie, {{name}}!">Delete</button>');
                $compile(element)($scope);
                $scope.$digest();
            }));

            it("should resolve the confirmTitle to the title property", function() {
                element.triggerHandler('click');
                expect(data.ok).toEqual('Okie Dokie, Joe!');
            });
        });

        describe('with confirmCancel option', function() {
            beforeEach(angular.mock.inject(function($compile) {
                $scope.name = 'Joe';
                element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?" confirm-cancel="No Way, {{name}}!">Delete</button>');
                $compile(element)($scope);
                $scope.$digest();
            }));

            it("should resolve the confirmTitle to the title property", function() {
                element.triggerHandler('click');
                expect(data.cancel).toEqual('No Way, Joe!');
            });
        });

        describe('with confirmSettings option', function() {
            beforeEach(angular.mock.inject(function($compile) {
                $scope.settings = {name: 'Joe'};
                element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?" confirm-settings="settings">Delete</button>');
                $compile(element)($scope);
                $scope.$digest();
            }));

            it("should pass the settings to $confirm", function() {
                element.triggerHandler('click');
                expect($confirm).toHaveBeenCalledWith({text: "Are you sure?"}, $scope.settings)
            });
        });

        describe('with confirmSettings option direct entry', function() {
            beforeEach(angular.mock.inject(function($compile) {
                element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?" confirm-settings="{name: \'Joe\'}">Delete</button>');
                $compile(element)($scope);
                $scope.$digest();
            }));

            it("should pass the settings to $confirm", function() {
                element.triggerHandler('click');
                expect($confirm).toHaveBeenCalledWith({text: "Are you sure?"}, {name: "Joe"})
            });
        });

    });

});