1 describe('angular-confirm', function() {
3 var $rootScope, $uibModal;
5 beforeEach(angular.mock.module('angular-confirm', function ($provide) {
7 $provide.decorator('$uibModal', function($delegate) {
9 open: jasmine.createSpy('$uibModal.open', function(settings) {
10 return {result: settings};
16 $provide.decorator('$confirm', function($delegate) {
17 return jasmine.createSpy('$confirm', $delegate);
22 beforeEach(angular.mock.inject(function (_$rootScope_) {
23 $rootScope = _$rootScope_;
26 describe('ConfirmModalController', function() {
27 var $scope, controller, data = {testVal: 1}, $uibModalInstance;
29 beforeEach(angular.mock.inject(function($controller) {
30 $scope = $rootScope.$new();
32 close: jasmine.createSpy('$uibModalInstance.close'),
33 dismiss: jasmine.createSpy('$uibModalInstance.dismiss'),
35 then: jasmine.createSpy('$uibModalInstance.result.then')
38 controller = $controller('ConfirmModalController', {"$scope": $scope, "$uibModalInstance": $uibModalInstance, "data": data});
41 it("should copy the data, not use it as a reference", function() {
43 expect($scope.data.testVal).toEqual(1);
46 it("should call close when $scope.ok is invoked", function() {
48 expect($uibModalInstance.close).toHaveBeenCalled();
51 it("should call dismiss when $scope.cancel is invoked", function() {
53 expect($uibModalInstance.dismiss).toHaveBeenCalledWith('cancel');
58 describe('$confirm factory', function() {
60 var $confirm, $confirmModalDefaults;
62 beforeEach(angular.mock.inject(function(_$confirm_, _$confirmModalDefaults_) {
63 $confirm = _$confirm_;
64 $confirm.and.callThrough();
65 $confirmModalDefaults = _$confirmModalDefaults_;
66 $uibModal.open.and.callThrough();
69 it("should call $uibModal.open", function() {
71 expect($uibModal.open).toHaveBeenCalled();
74 it("should override the defaults with settings passed in", function() {
75 var settings = $confirm({}, {"template": "hello"});
76 expect(settings.template).toEqual("hello");
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();
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');
94 it("should remove template if templateUrl is passed in", function() {
95 var settings = $confirm({}, {templateUrl: "abc.txt"});
96 expect(settings.template).not.toBeDefined();
101 describe('confirm directive', function() {
102 var $scope, element, $confirm, data;
104 beforeEach(angular.mock.inject(function (_$confirm_, $compile) {
105 $confirm = _$confirm_;
107 $confirm.and.callFake(function(d) {
109 return {then: function() {}}
112 $scope = $rootScope.$new();
113 $scope.click = jasmine.createSpy('$scope.click');
116 describe('resolve properties in title', function() {
117 beforeEach(angular.mock.inject(function($compile) {
119 element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure, {{name}}?">Delete</button>');
120 $compile(element)($scope);
124 it("should resolve the name to the text property", function() {
125 element.triggerHandler('click');
126 expect(data.text).toEqual('Are you sure, Joe?');
130 describe('without confirmIf', function() {
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);
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();
146 describe('with confirmIf option', function() {
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);
154 it("should call confirm on click and not call the function", function() {
155 $scope.truthy = true;
157 element.triggerHandler('click');
158 expect($scope.click).not.toHaveBeenCalled();
159 expect($confirm).toHaveBeenCalled();
162 it("should call the function", function() {
163 $scope.truthy = false;
165 element.triggerHandler('click');
166 expect($scope.click).toHaveBeenCalled();
167 expect($confirm).not.toHaveBeenCalled();
172 describe('with confirmTitle option', function() {
173 beforeEach(angular.mock.inject(function($compile) {
175 element = angular.element('<button type="button" ng-click="click()" confirm="Are you sure?" confirm-title="Hello, {{name}}!">Delete</button>');
176 $compile(element)($scope);
180 it("should resolve the confirmTitle to the title property", function() {
181 element.triggerHandler('click');
182 expect(data.title).toEqual('Hello, Joe!');
187 describe('with confirmOk option', function() {
188 beforeEach(angular.mock.inject(function($compile) {
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);
195 it("should resolve the confirmTitle to the title property", function() {
196 element.triggerHandler('click');
197 expect(data.ok).toEqual('Okie Dokie, Joe!');
201 describe('with confirmCancel option', function() {
202 beforeEach(angular.mock.inject(function($compile) {
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);
209 it("should resolve the confirmTitle to the title property", function() {
210 element.triggerHandler('click');
211 expect(data.cancel).toEqual('No Way, Joe!');
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);
223 it("should pass the settings to $confirm", function() {
224 element.triggerHandler('click');
225 expect($confirm).toHaveBeenCalledWith({text: "Are you sure?"}, $scope.settings)
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);
236 it("should pass the settings to $confirm", function() {
237 element.triggerHandler('click');
238 expect($confirm).toHaveBeenCalledWith({text: "Are you sure?"}, {name: "Joe"})