1+ describe ( 'angular-confirm' , function ( ) {
2+
3+ var $rootScope , $modal ;
4+
5+ beforeEach ( angular . mock . module ( 'angular-confirm' , function ( $provide ) {
6+
7+ $provide . decorator ( '$modal' , function ( $delegate ) {
8+ $modal = {
9+ open : jasmine . createSpy ( '$modal.open' , function ( settings ) {
10+ return { result : settings } ;
11+ } )
12+ } ;
13+ return $modal ;
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 } , $modalInstance ;
28+
29+ beforeEach ( angular . mock . inject ( function ( $controller ) {
30+ $scope = $rootScope . $new ( ) ;
31+ $modalInstance = {
32+ close : jasmine . createSpy ( 'modalInstance.close' ) ,
33+ dismiss : jasmine . createSpy ( 'modalInstance.dismiss' ) ,
34+ result : {
35+ then : jasmine . createSpy ( 'modalInstance.result.then' )
36+ }
37+ } ;
38+ controller = $controller ( 'ConfirmModalController' , { "$scope" : $scope , "$modalInstance" : $modalInstance , "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 ( $modalInstance . close ) . toHaveBeenCalled ( ) ;
49+ } ) ;
50+
51+ it ( "should call dismiss when $scope.cancel is invoked" , function ( ) {
52+ $scope . cancel ( ) ;
53+ expect ( $modalInstance . 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+ $modal . open . and . callThrough ( ) ;
67+ } ) ) ;
68+
69+ it ( "should call $modal.open" , function ( ) {
70+ $confirm ( ) ;
71+ expect ( $modal . 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 override the default labels with the data passed in" , function ( ) {
80+ var settings = $confirm ( { title : "Title" } ) ;
81+ var data = settings . resolve . data ( ) ;
82+ expect ( data . title ) . toEqual ( "Title" ) ;
83+ expect ( data . ok ) . toEqual ( 'OK' ) ;
84+ } ) ;
85+
86+ it ( "should remove template if templateUrl is passed in" , function ( ) {
87+ var settings = $confirm ( { } , { templateUrl : "abc.txt" } ) ;
88+ expect ( settings . template ) . not . toBeDefined ( ) ;
89+ } ) ;
90+
91+ } ) ;
92+
93+ describe ( 'confirm directive' , function ( ) {
94+ var $scope , element , $confirm , data ;
95+
96+ beforeEach ( angular . mock . inject ( function ( _$confirm_ , $compile ) {
97+ $confirm = _$confirm_ ;
98+
99+ $confirm . and . callFake ( function ( d ) {
100+ data = d ;
101+ return { then : function ( ) { } }
102+ } ) ;
103+
104+ $scope = $rootScope . $new ( ) ;
105+ $scope . click = jasmine . createSpy ( '$scope.click' ) ;
106+ } ) ) ;
107+
108+ describe ( 'resolve properties in title' , function ( ) {
109+ beforeEach ( angular . mock . inject ( function ( $compile ) {
110+ $scope . name = 'Joe' ;
111+ element = angular . element ( '<button type="button" ng-click="click()" confirm="Are you sure, {{name}}?">Delete</button>' ) ;
112+ $compile ( element ) ( $scope ) ;
113+ $scope . $digest ( ) ;
114+ } ) ) ;
115+
116+ it ( "should resolve the name to the text property" , function ( ) {
117+ element . triggerHandler ( 'click' ) ;
118+ expect ( data . text ) . toEqual ( 'Are you sure, Joe?' ) ;
119+ } ) ;
120+ } ) ;
121+
122+ describe ( 'without confirmIf' , function ( ) {
123+
124+ beforeEach ( angular . mock . inject ( function ( $compile ) {
125+ element = angular . element ( '<button type="button" ng-click="click()" confirm="Are you sure?">Delete</button>' ) ;
126+ $compile ( element ) ( $scope ) ;
127+ $scope . $digest ( ) ;
128+ } ) ) ;
129+
130+ it ( "should call confirm on click and not call the function" , function ( ) {
131+ element . triggerHandler ( 'click' ) ;
132+ expect ( $scope . click ) . not . toHaveBeenCalled ( ) ;
133+ expect ( $confirm ) . toHaveBeenCalled ( ) ;
134+ } ) ;
135+
136+ } ) ;
137+
138+ describe ( 'with confirmIf option' , function ( ) {
139+
140+ beforeEach ( angular . mock . inject ( function ( $compile ) {
141+ element = angular . element ( '<button type="button" ng-click="click()" confirm="Are you sure?" confirm-if="truthy">Delete</button>' ) ;
142+ $compile ( element ) ( $scope ) ;
143+ $scope . $digest ( ) ;
144+ } ) ) ;
145+
146+ it ( "should call confirm on click and not call the function" , function ( ) {
147+ $scope . truthy = true ;
148+ $scope . $apply ( ) ;
149+ element . triggerHandler ( 'click' ) ;
150+ expect ( $scope . click ) . not . toHaveBeenCalled ( ) ;
151+ expect ( $confirm ) . toHaveBeenCalled ( ) ;
152+ } ) ;
153+
154+ it ( "should call the function" , function ( ) {
155+ $scope . truthy = false ;
156+ $scope . $apply ( ) ;
157+ element . triggerHandler ( 'click' ) ;
158+ expect ( $scope . click ) . toHaveBeenCalled ( ) ;
159+ expect ( $confirm ) . not . toHaveBeenCalled ( ) ;
160+ } ) ;
161+
162+ } ) ;
163+
164+ describe ( 'with confirmTitle option' , function ( ) {
165+ beforeEach ( angular . mock . inject ( function ( $compile ) {
166+ element = angular . element ( '<button type="button" ng-click="click()" confirm="Are you sure?" confirm-title="Hello!">Delete</button>' ) ;
167+ $compile ( element ) ( $scope ) ;
168+ $scope . $digest ( ) ;
169+ } ) ) ;
170+
171+ it ( "should resolve the confirmTitle to the title property" , function ( ) {
172+ element . triggerHandler ( 'click' ) ;
173+ expect ( data . title ) . toEqual ( 'Hello!' ) ;
174+ } ) ;
175+ } ) ;
176+
177+ describe ( 'with confirmOk option' , function ( ) {
178+ beforeEach ( angular . mock . inject ( function ( $compile ) {
179+ element = angular . element ( '<button type="button" ng-click="click()" confirm="Are you sure?" confirm-ok="Okie Dokie!">Delete</button>' ) ;
180+ $compile ( element ) ( $scope ) ;
181+ $scope . $digest ( ) ;
182+ } ) ) ;
183+
184+ it ( "should resolve the confirmTitle to the title property" , function ( ) {
185+ element . triggerHandler ( 'click' ) ;
186+ expect ( data . ok ) . toEqual ( 'Okie Dokie!' ) ;
187+ } ) ;
188+ } ) ;
189+
190+ describe ( 'with confirmCancel option' , function ( ) {
191+ beforeEach ( angular . mock . inject ( function ( $compile ) {
192+ element = angular . element ( '<button type="button" ng-click="click()" confirm="Are you sure?" confirm-cancel="No Way!">Delete</button>' ) ;
193+ $compile ( element ) ( $scope ) ;
194+ $scope . $digest ( ) ;
195+ } ) ) ;
196+
197+ it ( "should resolve the confirmTitle to the title property" , function ( ) {
198+ element . triggerHandler ( 'click' ) ;
199+ expect ( data . cancel ) . toEqual ( 'No Way!' ) ;
200+ } ) ;
201+ } ) ;
202+
203+ } ) ;
204+
205+ } ) ;
0 commit comments