1
- /*global beforeEach, afterEach, describe, it, inject, expect, module, spyOn, CodeMirror*/
1
+ /*global beforeEach, afterEach, describe, it, inject, expect, module, spyOn, CodeMirror, angular */
2
2
describe ( 'uiCodemirror' , function ( ) {
3
3
'use strict' ;
4
4
5
- var scope ;
5
+ var scope , $compile ;
6
+ beforeEach ( function ( ) {
7
+ // throw some garbage in the codemirror cfg to be sure it's getting thru to the directive
8
+ angular . module ( 'ui.config' ) . value ( 'ui.config' , { codemirror : { bar : 'baz' } } ) ;
9
+ } ) ;
6
10
beforeEach ( module ( 'ui' ) ) ;
7
- beforeEach ( inject ( function ( $rootScope ) {
8
- scope = $rootScope . $new ( ) ;
11
+ beforeEach ( inject ( function ( _$rootScope_ , _$compile_ ) {
12
+ scope = _$rootScope_ . $new ( ) ;
13
+ $compile = _$compile_ ;
9
14
} ) ) ;
10
- afterEach ( function ( ) {
11
- scope . $destroy ( ) ;
15
+
16
+ afterEach ( function ( ) {
17
+ angular . module ( 'ui.config' ) . value ( 'ui.config' , { } ) ; // cleanup
12
18
} ) ;
19
+
13
20
describe ( 'compiling this directive' , function ( ) {
14
21
it ( 'should throw an error if used against a non-textarea' , function ( ) {
15
- inject ( function ( $compile ) {
16
- function compile ( ) {
17
- $compile ( '<div ui-codemirror ng-model="foo"></div>' ) ( scope ) ;
18
- }
19
-
20
- expect ( compile ) . toThrow ( ) ;
21
- } ) ;
22
+ function compile ( ) {
23
+ $compile ( '<div ui-codemirror ng-model="foo"></div>' ) ( scope ) ;
24
+ }
25
+ expect ( compile ) . toThrow ( ) ;
22
26
} ) ;
23
- it ( 'should not throw an error when used against a textarea' , function ( ) {
24
- inject ( function ( $compile ) {
25
- function compile ( ) {
26
- $compile ( '<textarea ui-codemirror ng-model="foo"></textarea>' ) ( scope ) ;
27
- }
28
27
29
- expect ( compile ) . not . toThrow ( ) ;
30
- } ) ;
28
+ it ( 'should not throw an error when used against a textarea' , function ( ) {
29
+ function compile ( ) {
30
+ $compile ( '<textarea ui-codemirror ng-model="foo"></textarea>' ) ( scope ) ;
31
+ }
32
+ expect ( compile ) . not . toThrow ( ) ;
31
33
} ) ;
32
- it ( 'should throw an error when no ngModel attribute defined' , function ( ) {
33
- inject ( function ( $compile ) {
34
- function compile ( ) {
35
- $compile ( '<textarea ui-codemirror></textarea>' ) ( scope ) ;
36
- }
37
34
38
- expect ( compile ) . toThrow ( ) ;
39
- } ) ;
35
+ it ( 'should throw an error when no ngModel attribute defined' , function ( ) {
36
+ function compile ( ) {
37
+ $compile ( '<textarea ui-codemirror></textarea>' ) ( scope ) ;
38
+ }
39
+ expect ( compile ) . toThrow ( ) ;
40
40
} ) ;
41
+
41
42
it ( 'should watch the uiCodemirror attribute' , function ( ) {
42
- inject ( function ( $compile ) {
43
- spyOn ( scope , '$watch' ) ;
44
- $compile ( '<textarea ui-codemirror ng-model="foo"></textarea>' ) ( scope ) ;
45
- expect ( scope . $watch ) . toHaveBeenCalled ( ) ;
46
- } ) ;
43
+ spyOn ( scope , '$watch' ) ;
44
+ $compile ( '<textarea ui-codemirror ng-model="foo"></textarea>' ) ( scope ) ;
45
+ expect ( scope . $watch ) . toHaveBeenCalled ( ) ;
46
+ } ) ;
47
+
48
+ it ( 'should include the passed options' , function ( ) {
49
+ spyOn ( CodeMirror , 'fromTextArea' ) ;
50
+ $compile ( '<textarea ui-codemirror="{foo: \'bar\'}" ng-model="foo"></textarea>' ) ( scope ) ;
51
+ expect ( CodeMirror . fromTextArea . mostRecentCall . args [ 1 ] . foo ) . toEqual ( 'bar' ) ;
52
+ } ) ;
53
+
54
+ it ( 'should include the default options' , function ( ) {
55
+ spyOn ( CodeMirror , 'fromTextArea' ) ;
56
+ $compile ( '<textarea ui-codemirror ng-model="foo"></textarea>' ) ( scope ) ;
57
+ expect ( CodeMirror . fromTextArea . mostRecentCall . args [ 1 ] . bar ) . toEqual ( 'baz' ) ;
47
58
} ) ;
48
59
} ) ;
60
+
49
61
describe ( 'when the model changes' , function ( ) {
50
62
it ( 'should update the IDE' , function ( ) {
51
- inject ( function ( $compile ) {
52
- var element = $compile ( '<textarea ui-codemirror ng-model="foo"></textarea>' ) ( scope ) ;
53
- scope . foo = 'bar' ;
54
- scope . $apply ( ) ;
55
- expect ( element . siblings ( ) . text ( ) . trim ( ) ) . toBe ( scope . foo ) ;
56
- } ) ;
63
+ var element = $compile ( '<textarea ui-codemirror ng-model="foo"></textarea>' ) ( scope ) ;
64
+ scope . foo = 'bar' ;
65
+ scope . $apply ( ) ;
66
+ expect ( element . siblings ( ) . text ( ) . trim ( ) ) . toBe ( scope . foo ) ;
57
67
} ) ;
58
68
} ) ;
69
+
59
70
describe ( 'when the IDE changes' , function ( ) {
60
71
it ( 'should update the model' , function ( ) {
61
- inject ( function ( $compile ) {
62
- var codemirror ,
63
- fromTextArea = CodeMirror . fromTextArea ;
64
- spyOn ( CodeMirror , 'fromTextArea' ) . andCallFake ( function ( ) {
65
- codemirror = fromTextArea . apply ( this , arguments ) ;
66
- return codemirror ;
67
- } ) ;
68
- $compile ( '<textarea ui-codemirror ng-model="foo"></textarea>' ) ( scope ) ;
69
- scope . foo = 'bar' ;
70
- scope . $apply ( ) ;
71
- var value = 'baz' ;
72
- codemirror . setValue ( value ) ;
73
- expect ( scope . foo ) . toBe ( value ) ;
72
+ var codemirror ,
73
+ fromTextArea = CodeMirror . fromTextArea ;
74
+ spyOn ( CodeMirror , 'fromTextArea' ) . andCallFake ( function ( ) {
75
+ codemirror = fromTextArea . apply ( this , arguments ) ;
76
+ return codemirror ;
74
77
} ) ;
78
+ $compile ( '<textarea ui-codemirror ng-model="foo"></textarea>' ) ( scope ) ;
79
+ scope . foo = 'bar' ;
80
+ scope . $apply ( ) ;
81
+ var value = 'baz' ;
82
+ codemirror . setValue ( value ) ;
83
+ expect ( scope . foo ) . toBe ( value ) ;
75
84
} ) ;
76
85
} ) ;
86
+
77
87
describe ( 'when the model is undefined/null' , function ( ) {
78
88
it ( 'should update the IDE with an empty string' , function ( ) {
79
- inject ( function ( $compile ) {
80
- var element = $compile ( '<textarea ui-codemirror ng-model="foo"></textarea>' ) ( scope ) ;
81
- scope . $apply ( ) ;
82
- expect ( scope . foo ) . toBe ( undefined ) ;
83
- expect ( element . siblings ( ) . text ( ) . trim ( ) ) . toBe ( '' ) ;
84
- scope . foo = null ;
85
- scope . $apply ( ) ;
86
- expect ( scope . foo ) . toBe ( null ) ;
87
- expect ( element . siblings ( ) . text ( ) . trim ( ) ) . toBe ( '' ) ;
88
- } ) ;
89
+ var element = $compile ( '<textarea ui-codemirror ng-model="foo"></textarea>' ) ( scope ) ;
90
+ scope . $apply ( ) ;
91
+ expect ( scope . foo ) . toBe ( undefined ) ;
92
+ expect ( element . siblings ( ) . text ( ) . trim ( ) ) . toBe ( '' ) ;
93
+ scope . foo = null ;
94
+ scope . $apply ( ) ;
95
+ expect ( scope . foo ) . toBe ( null ) ;
96
+ expect ( element . siblings ( ) . text ( ) . trim ( ) ) . toBe ( '' ) ;
89
97
} ) ;
90
98
} ) ;
91
- describe ( 'when the model is an object or an array' , function ( ) {
92
- it ( 'should throw an error' , function ( ) {
93
- inject ( function ( $compile ) {
94
- function compileWithObject ( ) {
95
- $compile ( '<textarea ui-codemirror ng-model="foo"></textarea>' ) ( scope ) ;
96
- scope . foo = { } ;
97
- scope . $apply ( ) ;
98
- }
99
- function compileWithArray ( ) {
100
- $compile ( '<textarea ui-codemirror ng-model="foo"></textarea>' ) ( scope ) ;
101
- scope . foo = [ ] ;
102
- scope . $apply ( ) ;
103
- }
104
- expect ( compileWithObject ) . toThrow ( ) ;
105
- expect ( compileWithArray ) . toThrow ( ) ;
106
- } ) ;
99
+
100
+ describe ( 'when the model is an object or an array' , function ( ) {
101
+ it ( 'should throw an error' , function ( ) {
102
+ function compileWithObject ( ) {
103
+ $compile ( '<textarea ui-codemirror ng-model="foo"></textarea>' ) ( scope ) ;
104
+ scope . foo = { } ;
105
+ scope . $apply ( ) ;
106
+ }
107
+
108
+ function compileWithArray ( ) {
109
+ $compile ( '<textarea ui-codemirror ng-model="foo"></textarea>' ) ( scope ) ;
110
+ scope . foo = [ ] ;
111
+ scope . $apply ( ) ;
112
+ }
113
+
114
+ expect ( compileWithObject ) . toThrow ( ) ;
115
+ expect ( compileWithArray ) . toThrow ( ) ;
107
116
} ) ;
108
117
} ) ;
109
118
} ) ;
0 commit comments