@@ -5,6 +5,7 @@ describe('uiCodemirror', function () {
5
5
6
6
// declare these up here to be global to all tests
7
7
var scope , $compile , $timeout , uiConfig ;
8
+ var codemirrorDefaults = window . CodeMirror . defaults ;
8
9
9
10
beforeEach ( function ( ) {
10
11
module ( 'ui.codemirror' ) ;
@@ -104,11 +105,13 @@ describe('uiCodemirror', function () {
104
105
105
106
beforeEach ( function ( ) {
106
107
var _constructor = window . CodeMirror ;
107
- spyOn ( window , ' CodeMirror') . andCallFake ( function ( ) {
108
+ window . CodeMirror = jasmine . createSpy ( 'window. CodeMirror') . andCallFake ( function ( ) {
108
109
codemirror = _constructor . apply ( this , arguments ) ;
109
110
spies ( codemirror ) ;
110
111
return codemirror ;
111
112
} ) ;
113
+
114
+ window . CodeMirror . defaults = codemirrorDefaults ;
112
115
} ) ;
113
116
114
117
@@ -142,60 +145,57 @@ describe('uiCodemirror', function () {
142
145
} ) ;
143
146
144
147
145
- describe ( 'setOptions ' , function ( ) {
148
+ describe ( 'options ' , function ( ) {
146
149
147
150
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
+ } ) ;
149
155
} ;
150
156
151
157
it ( 'should not be called' , function ( ) {
152
158
$compile ( '<div ui-codemirror></div>' ) ( scope ) ;
159
+ expect ( window . CodeMirror ) . toHaveBeenCalledWith ( jasmine . any ( Function ) , codemirrorDefaults ) ;
153
160
expect ( codemirror . setOption ) . not . toHaveBeenCalled ( ) ;
154
161
} ) ;
155
162
156
- it ( 'should include the passed options' , function ( ) {
163
+ it ( 'should include the passed options (attribute directive) ' , function ( ) {
157
164
$compile ( '<div ui-codemirror="{oof: \'baar\'}"></div>' ) ( scope ) ;
158
165
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
+ } ) ;
162
169
170
+ it ( 'should include the passed options (element directive)' , function ( ) {
163
171
$compile ( '<ui-codemirror ui-codemirror-opts="{oof: \'baar\'}"></ui-codemirror>' ) ( scope ) ;
164
172
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 ( ) ;
168
175
} ) ;
169
176
170
177
it ( 'should include the default options' , function ( ) {
171
- $compile ( '<div ui-codemirror>' ) ( scope ) ;
172
178
uiConfig . codemirror = { bar : 'baz' } ;
173
179
$compile ( '<div ui-codemirror></div>' ) ( scope ) ;
174
180
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 ( ) ;
178
183
} ) ;
179
184
180
185
it ( 'should extent the default options' , function ( ) {
181
- $compile ( '<div ui-codemirror>' ) ( scope ) ;
182
186
uiConfig . codemirror = { bar : 'baz' } ;
183
187
$compile ( '<div ui-codemirror="{oof: \'baar\'}"></div>' ) ( scope ) ;
184
188
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 ( ) ;
189
191
} ) ;
190
192
191
193
it ( 'should impact codemirror' , function ( ) {
192
- $compile ( '<div ui-codemirror>' ) ( scope ) ;
193
194
uiConfig . codemirror = { } ;
194
195
$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' ) ;
198
196
197
+ expect ( window . CodeMirror ) . toHaveBeenCalledWith ( jasmine . any ( Function ) , angular . extend ( codemirrorDefaults , { theme : 'baar' } ) ) ;
198
+ expect ( codemirror . setOption ) . not . toHaveBeenCalled ( ) ;
199
199
200
200
expect ( codemirror . getOption ( 'theme' ) ) . toEqual ( 'baar' ) ;
201
201
} ) ;
@@ -283,8 +283,7 @@ describe('uiCodemirror', function () {
283
283
} ) ;
284
284
285
285
it ( 'should runs the onLoad callback' , function ( ) {
286
- scope . codemirrorLoaded = angular . noop ;
287
- spyOn ( scope , 'codemirrorLoaded' ) ;
286
+ scope . codemirrorLoaded = jasmine . createSpy ( 'scope.codemirrorLoaded' ) ;
288
287
289
288
$compile ( '<div ui-codemirror="{onLoad: codemirrorLoaded}"></div>' ) ( scope ) ;
290
289
@@ -293,8 +292,8 @@ describe('uiCodemirror', function () {
293
292
} ) ;
294
293
295
294
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') ;
298
297
299
298
$compile ( '<div ui-codemirror></div>' ) ( scope ) ;
300
299
scope . $broadcast ( 'CodeMirror' , broadcast . callback ) ;
@@ -305,13 +304,11 @@ describe('uiCodemirror', function () {
305
304
306
305
307
306
it ( 'should watch the options' , function ( ) {
308
- spyOn ( scope , '$watch' ) . andCallThrough ( ) ;
309
307
310
308
scope . cmOption = { readOnly : true } ;
311
309
$compile ( '<div ui-codemirror="cmOption"></div>' ) ( scope ) ;
310
+ scope . $digest ( ) ;
312
311
313
- expect ( scope . $watch . callCount ) . toEqual ( 1 ) ; // the uiCodemirror option
314
- expect ( scope . $watch ) . toHaveBeenCalledWith ( 'cmOption' , jasmine . any ( Function ) , true ) ;
315
312
expect ( codemirror . getOption ( 'readOnly' ) ) . toBeTruthy ( ) ;
316
313
317
314
scope . cmOption . readOnly = false ;
@@ -320,14 +317,12 @@ describe('uiCodemirror', function () {
320
317
} ) ;
321
318
322
319
it ( 'should watch the options (object property)' , function ( ) {
323
- spyOn ( scope , '$watch' ) . andCallThrough ( ) ;
324
320
325
321
scope . cm = { } ;
326
322
scope . cm . option = { readOnly : true } ;
327
323
$compile ( '<div ui-codemirror="cm.option"></div>' ) ( scope ) ;
324
+ scope . $digest ( ) ;
328
325
329
- expect ( scope . $watch . callCount ) . toEqual ( 1 ) ; // the uiCodemirror option
330
- expect ( scope . $watch ) . toHaveBeenCalledWith ( 'cm.option' , jasmine . any ( Function ) , true ) ;
331
326
expect ( codemirror . getOption ( 'readOnly' ) ) . toBeTruthy ( ) ;
332
327
333
328
scope . cm . option . readOnly = false ;
0 commit comments