11'use strict' ;
22
3- describe ( 'modal' , function ( ) {
3+ describe ( 'modal' , function ( ) {
44
5- var $compile , $templateCache , scope , sandboxEl ;
5+ var bodyEl = $ ( 'body' ) , sandboxEl ;
6+ var $compile , $templateCache , $modal , scope ;
67
78 beforeEach ( module ( 'ngSanitize' ) ) ;
89 beforeEach ( module ( 'mgcrea.ngStrap.modal' ) ) ;
910
10- beforeEach ( inject ( function ( _$rootScope_ , _$compile_ , _$templateCache_ ) {
11+ beforeEach ( inject ( function ( _$rootScope_ , _$compile_ , _$templateCache_ , _$modal_ ) {
1112 scope = _$rootScope_ . $new ( ) ;
12- sandboxEl = $ ( '<div>' ) . attr ( 'id' , 'sandbox' ) . appendTo ( $ ( 'body' ) ) ;
13+ bodyEl . html ( '' ) ;
14+ sandboxEl = $ ( '<div>' ) . attr ( 'id' , 'sandbox' ) . appendTo ( bodyEl ) ;
1315 $compile = _$compile_ ;
1416 $templateCache = _$templateCache_ ;
17+ $modal = _$modal_ ;
1518 } ) ) ;
1619
1720 afterEach ( function ( ) {
@@ -33,6 +36,9 @@ describe('modal', function () {
3336 scope : { items : [ { name : 'foo' , modal : { title : 'Title' , content : 'Hello Modal!' } } ] } ,
3437 element : '<ul><li ng-repeat="item in items"><a title="{{item.modal.title}}" data-content="{{item.modal.content}}" bs-modal>{{item.name}}</a></li></ul>'
3538 } ,
39+ 'markup-ngClick-service' : {
40+ element : '<a ng-click="showModal()">click me</a>'
41+ } ,
3642 'options-placement' : {
3743 element : '<a data-placement="bottom" bs-modal="modal">click me</a>'
3844 } ,
@@ -60,7 +66,7 @@ describe('modal', function () {
6066
6167 // Tests
6268
63- describe ( 'with default template' , function ( ) {
69+ describe ( 'with default template' , function ( ) {
6470
6571 it ( 'should open on click' , function ( ) {
6672 var elm = compileDirective ( 'default' ) ;
@@ -100,10 +106,41 @@ describe('modal', function () {
100106
101107 } ) ;
102108
109+ describe ( 'using service' , function ( ) {
110+
111+ it ( 'should correctly open on next digest' , function ( ) {
112+ var myModal = $modal ( templates [ 'default' ] . scope . modal ) ;
113+ scope . $digest ( ) ;
114+ expect ( bodyEl . children ( '.modal' ) . length ) . toBe ( 1 ) ;
115+ myModal . hide ( ) ;
116+ expect ( bodyEl . children ( '.modal' ) . length ) . toBe ( 0 ) ;
117+ } ) ;
118+
119+ it ( 'should correctly be destroyed' , function ( ) {
120+ var myModal = $modal ( angular . extend ( templates [ 'default' ] . scope . modal ) ) ;
121+ scope . $digest ( ) ;
122+ expect ( bodyEl . children ( '.modal' ) . length ) . toBe ( 1 ) ;
123+ myModal . destroy ( ) ;
124+ expect ( bodyEl . children ( '.modal' ) . length ) . toBe ( 0 ) ;
125+ expect ( bodyEl . children ( ) . length ) . toBe ( 1 ) ;
126+ } ) ;
127+
128+ it ( 'should correctly work with ngClick' , function ( ) {
129+ var elm = compileDirective ( 'markup-ngClick-service' ) ;
130+ var myModal = $modal ( angular . extend ( { show : false } , templates [ 'default' ] . scope . modal ) ) ;
131+ scope . showModal = function ( ) {
132+ myModal . $promise . then ( myModal . show ) ;
133+ } ;
134+ expect ( bodyEl . children ( '.modal' ) . length ) . toBe ( 0 ) ;
135+ angular . element ( elm [ 0 ] ) . triggerHandler ( 'click' ) ;
136+ expect ( bodyEl . children ( '.modal' ) . length ) . toBe ( 1 ) ;
137+ } ) ;
138+
139+ } ) ;
103140
104- describe ( 'options' , function ( ) {
141+ describe ( 'options' , function ( ) {
105142
106- describe ( 'animation' , function ( ) {
143+ describe ( 'animation' , function ( ) {
107144
108145 it ( 'should default to `animation-fade` animation' , function ( ) {
109146 var elm = compileDirective ( 'default' ) ;
@@ -113,7 +150,7 @@ describe('modal', function () {
113150
114151 } ) ;
115152
116- describe ( 'placement' , function ( ) {
153+ describe ( 'placement' , function ( ) {
117154
118155 it ( 'should default to `top` placement' , function ( ) {
119156 var elm = compileDirective ( 'default' ) ;
@@ -135,7 +172,7 @@ describe('modal', function () {
135172
136173 } ) ;
137174
138- describe ( 'html' , function ( ) {
175+ describe ( 'html' , function ( ) {
139176
140177 it ( 'should correctly compile inner content' , function ( ) {
141178 var elm = compileDirective ( 'options-html' ) ;
@@ -146,7 +183,7 @@ describe('modal', function () {
146183
147184 } ) ;
148185
149- describe ( 'template' , function ( ) {
186+ describe ( 'template' , function ( ) {
150187
151188 it ( 'should support custom template' , function ( ) {
152189 $templateCache . put ( 'custom' , '<div class="modal"><div class="modal-inner">foo: {{title}}</div></div>' ) ;
0 commit comments