@@ -5,6 +5,7 @@ describe('uiCodemirror', function () {
55
66 // declare these up here to be global to all tests
77 var scope , $compile , $timeout , uiConfig ;
8+ var codemirrorDefaults = window . CodeMirror . defaults ;
89
910 beforeEach ( function ( ) {
1011 module ( 'ui.codemirror' ) ;
@@ -104,11 +105,13 @@ describe('uiCodemirror', function () {
104105
105106 beforeEach ( function ( ) {
106107 var _constructor = window . CodeMirror ;
107- spyOn ( window , ' CodeMirror') . andCallFake ( function ( ) {
108+ window . CodeMirror = jasmine . createSpy ( 'window. CodeMirror') . andCallFake ( function ( ) {
108109 codemirror = _constructor . apply ( this , arguments ) ;
109110 spies ( codemirror ) ;
110111 return codemirror ;
111112 } ) ;
113+
114+ window . CodeMirror . defaults = codemirrorDefaults ;
112115 } ) ;
113116
114117
@@ -142,60 +145,57 @@ describe('uiCodemirror', function () {
142145 } ) ;
143146
144147
145- describe ( 'setOptions ' , function ( ) {
148+ describe ( 'options ' , function ( ) {
146149
147150 spies = function ( codemirror ) {
148- spyOn ( codemirror , 'setOption' ) . andCallThrough ( ) ;
151+ codemirror . _setOption = codemirror . _setOption || codemirror . setOption ;
152+ codemirror . setOption = jasmine . createSpy ( 'codemirror.setOption' ) . andCallFake ( function ( ) {
153+ codemirror . _setOption . apply ( this , arguments ) ;
154+ } ) ;
149155 } ;
150156
151157 it ( 'should not be called' , function ( ) {
152158 $compile ( '<div ui-codemirror></div>' ) ( scope ) ;
159+ expect ( window . CodeMirror ) . toHaveBeenCalledWith ( jasmine . any ( Function ) , codemirrorDefaults ) ;
153160 expect ( codemirror . setOption ) . not . toHaveBeenCalled ( ) ;
154161 } ) ;
155162
156- it ( 'should include the passed options' , function ( ) {
163+ it ( 'should include the passed options (attribute directive) ' , function ( ) {
157164 $compile ( '<div ui-codemirror="{oof: \'baar\'}"></div>' ) ( scope ) ;
158165
159- expect ( codemirror . setOption ) . toHaveBeenCalled ( ) ;
160- expect ( codemirror . setOption . calls . length ) . toEqual ( 1 ) ;
161- expect ( codemirror . setOption ) . toHaveBeenCalledWith ( 'oof' , 'baar' ) ;
166+ expect ( window . CodeMirror ) . toHaveBeenCalledWith ( jasmine . any ( Function ) , angular . extend ( codemirrorDefaults , { oof : 'baar' } ) ) ;
167+ expect ( codemirror . setOption ) . not . toHaveBeenCalled ( ) ;
168+ } ) ;
162169
170+ it ( 'should include the passed options (element directive)' , function ( ) {
163171 $compile ( '<ui-codemirror ui-codemirror-opts="{oof: \'baar\'}"></ui-codemirror>' ) ( scope ) ;
164172
165- expect ( codemirror . setOption ) . toHaveBeenCalled ( ) ;
166- expect ( codemirror . setOption . calls . length ) . toEqual ( 1 ) ;
167- expect ( codemirror . setOption ) . toHaveBeenCalledWith ( 'oof' , 'baar' ) ;
173+ expect ( window . CodeMirror ) . toHaveBeenCalledWith ( jasmine . any ( Function ) , angular . extend ( codemirrorDefaults , { oof : 'baar' } ) ) ;
174+ expect ( codemirror . setOption ) . not . toHaveBeenCalled ( ) ;
168175 } ) ;
169176
170177 it ( 'should include the default options' , function ( ) {
171- $compile ( '<div ui-codemirror>' ) ( scope ) ;
172178 uiConfig . codemirror = { bar : 'baz' } ;
173179 $compile ( '<div ui-codemirror></div>' ) ( scope ) ;
174180
175- expect ( codemirror . setOption ) . toHaveBeenCalled ( ) ;
176- expect ( codemirror . setOption . calls . length ) . toEqual ( 1 ) ;
177- expect ( codemirror . setOption ) . toHaveBeenCalledWith ( 'bar' , 'baz' ) ;
181+ expect ( window . CodeMirror ) . toHaveBeenCalledWith ( jasmine . any ( Function ) , angular . extend ( codemirrorDefaults , { bar : 'baz' } ) ) ;
182+ expect ( codemirror . setOption ) . not . toHaveBeenCalled ( ) ;
178183 } ) ;
179184
180185 it ( 'should extent the default options' , function ( ) {
181- $compile ( '<div ui-codemirror>' ) ( scope ) ;
182186 uiConfig . codemirror = { bar : 'baz' } ;
183187 $compile ( '<div ui-codemirror="{oof: \'baar\'}"></div>' ) ( scope ) ;
184188
185- expect ( codemirror . setOption ) . toHaveBeenCalled ( ) ;
186- expect ( codemirror . setOption . calls . length ) . toEqual ( 2 ) ;
187- expect ( codemirror . setOption ) . toHaveBeenCalledWith ( 'oof' , 'baar' ) ;
188- expect ( codemirror . setOption ) . toHaveBeenCalledWith ( 'bar' , 'baz' ) ;
189+ expect ( window . CodeMirror ) . toHaveBeenCalledWith ( jasmine . any ( Function ) , angular . extend ( codemirrorDefaults , { oof : 'baar' , bar : 'baz' } ) ) ;
190+ expect ( codemirror . setOption ) . not . toHaveBeenCalled ( ) ;
189191 } ) ;
190192
191193 it ( 'should impact codemirror' , function ( ) {
192- $compile ( '<div ui-codemirror>' ) ( scope ) ;
193194 uiConfig . codemirror = { } ;
194195 $compile ( '<div ui-codemirror="{theme: \'baar\'}"></div>' ) ( scope ) ;
195- expect ( codemirror . setOption ) . toHaveBeenCalled ( ) ;
196- expect ( codemirror . setOption . calls . length ) . toEqual ( 1 ) ;
197- expect ( codemirror . setOption ) . toHaveBeenCalledWith ( 'theme' , 'baar' ) ;
198196
197+ expect ( window . CodeMirror ) . toHaveBeenCalledWith ( jasmine . any ( Function ) , angular . extend ( codemirrorDefaults , { theme : 'baar' } ) ) ;
198+ expect ( codemirror . setOption ) . not . toHaveBeenCalled ( ) ;
199199
200200 expect ( codemirror . getOption ( 'theme' ) ) . toEqual ( 'baar' ) ;
201201 } ) ;
@@ -283,8 +283,7 @@ describe('uiCodemirror', function () {
283283 } ) ;
284284
285285 it ( 'should runs the onLoad callback' , function ( ) {
286- scope . codemirrorLoaded = angular . noop ;
287- spyOn ( scope , 'codemirrorLoaded' ) ;
286+ scope . codemirrorLoaded = jasmine . createSpy ( 'scope.codemirrorLoaded' ) ;
288287
289288 $compile ( '<div ui-codemirror="{onLoad: codemirrorLoaded}"></div>' ) ( scope ) ;
290289
@@ -293,8 +292,8 @@ describe('uiCodemirror', function () {
293292 } ) ;
294293
295294 it ( 'responds to the $broadcast event "CodeMirror"' , function ( ) {
296- var broadcast = { callback : angular . noop } ;
297- spyOn ( broadcast , ' callback') ;
295+ var broadcast = { } ;
296+ broadcast . callback = jasmine . createSpy ( 'broadcast. callback') ;
298297
299298 $compile ( '<div ui-codemirror></div>' ) ( scope ) ;
300299 scope . $broadcast ( 'CodeMirror' , broadcast . callback ) ;
@@ -305,13 +304,11 @@ describe('uiCodemirror', function () {
305304
306305
307306 it ( 'should watch the options' , function ( ) {
308- spyOn ( scope , '$watch' ) . andCallThrough ( ) ;
309307
310308 scope . cmOption = { readOnly : true } ;
311309 $compile ( '<div ui-codemirror="cmOption"></div>' ) ( scope ) ;
310+ scope . $digest ( ) ;
312311
313- expect ( scope . $watch . callCount ) . toEqual ( 1 ) ; // the uiCodemirror option
314- expect ( scope . $watch ) . toHaveBeenCalledWith ( 'cmOption' , jasmine . any ( Function ) , true ) ;
315312 expect ( codemirror . getOption ( 'readOnly' ) ) . toBeTruthy ( ) ;
316313
317314 scope . cmOption . readOnly = false ;
@@ -320,14 +317,12 @@ describe('uiCodemirror', function () {
320317 } ) ;
321318
322319 it ( 'should watch the options (object property)' , function ( ) {
323- spyOn ( scope , '$watch' ) . andCallThrough ( ) ;
324320
325321 scope . cm = { } ;
326322 scope . cm . option = { readOnly : true } ;
327323 $compile ( '<div ui-codemirror="cm.option"></div>' ) ( scope ) ;
324+ scope . $digest ( ) ;
328325
329- expect ( scope . $watch . callCount ) . toEqual ( 1 ) ; // the uiCodemirror option
330- expect ( scope . $watch ) . toHaveBeenCalledWith ( 'cm.option' , jasmine . any ( Function ) , true ) ;
331326 expect ( codemirror . getOption ( 'readOnly' ) ) . toBeTruthy ( ) ;
332327
333328 scope . cm . option . readOnly = false ;
0 commit comments