1
1
describe ( 'mdSubheader' , function ( ) {
2
- var $mdStickyMock ,
3
- basicHtml = '<md-subheader>Hello world!</md-header>' ,
4
- pageScope , element , controller ;
5
-
2
+ var BASIC_SUBHEADER = '<md-subheader>Hello world!</md-subheader>' ;
3
+ var pageScope , element , controller , contentElement ;
6
4
var $rootScope , $timeout , $exceptionHandler ;
7
5
8
- beforeEach ( module ( 'material.components.subheader' , function ( $provide ) {
9
- $mdStickyMock = function ( ) {
10
- $mdStickyMock . args = Array . prototype . slice . call ( arguments ) ;
11
- } ;
12
- $provide . value ( '$mdSticky' , $mdStickyMock ) ;
13
- } ) ) ;
6
+ beforeEach ( module ( 'material.components.subheader' ) ) ;
14
7
15
- beforeEach ( inject ( function ( _$rootScope_ , _$timeout_ , _$exceptionHandler_ ) {
16
- $rootScope = _$rootScope_ ;
17
- $timeout = _$timeout_ ;
18
- $exceptionHandler = _$exceptionHandler_ ;
8
+ beforeEach ( inject ( function ( $injector ) {
9
+ $rootScope = $injector . get ( '$rootScope' ) ;
10
+ $timeout = $injector . get ( '$timeout' ) ;
11
+ $exceptionHandler = $injector . get ( '$exceptionHandler' ) ;
19
12
} ) ) ;
20
13
21
14
22
15
it ( 'should have `._md` class indicator' , inject ( function ( ) {
23
- build ( '<div><md-subheader>Hello {{ to }}!</md-subheader></div>' ) ;
24
- pageScope . to = 'world' ;
25
- pageScope . $digest ( ) ;
16
+ build ( BASIC_SUBHEADER ) ;
26
17
27
- expect ( element . children ( ) . hasClass ( '_md' ) ) . toBe ( true ) ;
18
+ expect ( element . hasClass ( '_md' ) ) . toBe ( true ) ;
28
19
} ) ) ;
29
20
30
21
31
22
it ( 'preserves content' , function ( ) {
32
- build ( '<div><md-subheader>Hello {{ to }}!</md-subheader></div>' ) ;
23
+ build (
24
+ '<div>' +
25
+ '<md-subheader>Hello {{ to }}!</md-subheader>' +
26
+ '</div>'
27
+ ) ;
28
+
33
29
pageScope . to = 'world' ;
34
30
pageScope . $digest ( ) ;
35
31
@@ -39,23 +35,22 @@ describe('mdSubheader', function() {
39
35
} ) ;
40
36
41
37
it ( 'implements $mdSticky' , function ( ) {
42
- build ( basicHtml ) ;
38
+ build ( BASIC_SUBHEADER ) ;
43
39
44
- expect ( $mdStickyMock . args [ 0 ] ) . toBe ( pageScope ) ;
40
+ var cloneScope = element . scope ( ) ;
41
+
42
+ expect ( cloneScope ) . toBe ( pageScope ) ;
45
43
} ) ;
46
44
47
45
it ( 'applies the theme to the header and clone' , function ( ) {
48
- build ( '<div md-theme="somethingElse">' + basicHtml + '</div>' ) ;
49
-
50
- // Grab the real element
51
- var element = $mdStickyMock . args [ 1 ] ;
46
+ build ( '<div md-theme="somethingElse">' + BASIC_SUBHEADER + '</div>' ) ;
52
47
53
48
// The subheader now wraps the clone in a DIV in case of ng-if usage, so we have to search for
54
49
// the proper element.
55
- var clone = angular . element ( $mdStickyMock . args [ 2 ] [ 0 ] . querySelector ( '.md-subheader' ) ) ;
50
+ var clone = getCloneElement ( ) ;
56
51
57
- expect ( element . hasClass ( 'md-somethingElse-theme' ) ) . toBe ( true ) ;
58
- expect ( clone . hasClass ( 'md-somethingElse-theme' ) ) . toBe ( true ) ;
52
+ expect ( getSubheader ( ) . classList ) . toContain ( 'md-somethingElse-theme' ) ;
53
+ expect ( getSubheader ( clone ) . classList ) . toContain ( 'md-somethingElse-theme' ) ;
59
54
} ) ;
60
55
61
56
it ( 'applies the proper scope to the clone' , function ( ) {
@@ -64,11 +59,10 @@ describe('mdSubheader', function() {
64
59
pageScope . to = 'world' ;
65
60
pageScope . $apply ( ) ;
66
61
67
- var element = $mdStickyMock . args [ 1 ] ;
68
- var clone = $mdStickyMock . args [ 2 ] ;
62
+ var clone = getCloneElement ( ) ;
69
63
70
- expect ( element . text ( ) . trim ( ) ) . toEqual ( 'Hello world!' ) ;
71
- expect ( clone . text ( ) . trim ( ) ) . toEqual ( 'Hello world!' ) ;
64
+ expect ( getSubheader ( ) . textContent . trim ( ) ) . toEqual ( 'Hello world!' ) ;
65
+ expect ( getSubheader ( clone ) . textContent . trim ( ) ) . toEqual ( 'Hello world!' ) ;
72
66
} ) ;
73
67
74
68
it ( 'supports ng-if' , function ( ) {
@@ -78,6 +72,39 @@ describe('mdSubheader', function() {
78
72
expect ( element [ 0 ] . querySelectorAll ( '.md-subheader' ) . length ) . toEqual ( 1 ) ;
79
73
} ) ;
80
74
75
+ it ( 'should support ng-if inside of stickyClone' , function ( ) {
76
+ build (
77
+ '<div>' +
78
+ '<md-subheader>' +
79
+ 'Foo' +
80
+ '<span ng-if="isBar">Bar</span>' +
81
+ '</md-subheader>' +
82
+ '</div>'
83
+ ) ;
84
+
85
+ var clone = getCloneElement ( ) [ 0 ] ;
86
+
87
+ expect ( clone . textContent . trim ( ) ) . toBe ( 'Foo' ) ;
88
+
89
+ pageScope . $apply ( 'isBar = true' ) ;
90
+
91
+ expect ( clone . textContent . trim ( ) ) . toBe ( 'FooBar' ) ;
92
+ } ) ;
93
+
94
+ it ( 'should work with a ng-if directive inside of the stickyClone' , function ( ) {
95
+ build (
96
+ '<div>' +
97
+ '<md-subheader>' +
98
+ '<span ng-repeat="item in [0, 1, 2, 3]">{{ item }}</span>' +
99
+ '</md-subheader>' +
100
+ '</div>'
101
+ ) ;
102
+
103
+ var cloneContent = getCloneElement ( ) [ 0 ] . querySelector ( '._md-subheader-content' ) ;
104
+
105
+ expect ( cloneContent . children . length ) . toBe ( 4 ) ;
106
+ } ) ;
107
+
81
108
it ( 'supports ng-repeat' , function ( ) {
82
109
build ( '<div><md-subheader ng-repeat="i in [1,2,3]">Test {{i}}</md-subheader></div>' ) ;
83
110
@@ -86,13 +113,38 @@ describe('mdSubheader', function() {
86
113
} ) ;
87
114
88
115
function build ( template ) {
89
- inject ( function ( $compile ) {
116
+ inject ( function ( $compile , $timeout ) {
90
117
pageScope = $rootScope . $new ( ) ;
91
- element = $compile ( template ) ( pageScope ) ;
118
+
119
+ contentElement = $compile ( '<md-content>' + template + '</md-content>' ) ( pageScope ) ;
120
+
121
+ // Flush the timeout, which prepends the sticky clone to the md-content.
122
+ $timeout . flush ( ) ;
123
+
124
+ // When the contentElement only has only one children then the current
125
+ // browser supports sticky elements natively.
126
+ if ( contentElement . children ( ) . length === 1 ) {
127
+ element = getCloneElement ( ) ;
128
+ } else {
129
+ // When the browser doesn't support sticky elements natively we will have a sticky clone.
130
+ // The sticky clone element will be always prepended, which means that we have to use the child
131
+ // at the second position.
132
+ element = contentElement . children ( ) . eq ( 1 ) ;
133
+ }
134
+
92
135
controller = element . controller ( 'mdSubheader' ) ;
93
136
94
137
pageScope . $apply ( ) ;
95
138
$timeout . flush ( ) ;
96
139
} ) ;
97
140
}
141
+
142
+ function getSubheader ( el ) {
143
+ return ( el || element ) [ 0 ] . querySelector ( '.md-subheader' ) ;
144
+ }
145
+
146
+ function getCloneElement ( ) {
147
+ // The clone element will be always prepended, which means that we have to get the child at index zero.
148
+ return contentElement . children ( ) . eq ( 0 ) ;
149
+ }
98
150
} ) ;
0 commit comments