7
7
*/
8
8
import { Component , OnInit } from '@angular/core' ;
9
9
import { CommonModule } from '@angular/common' ;
10
- import { ComponentFixture , TestBed , async } from '@angular/core/testing' ;
10
+ import { ComponentFixture , TestBed , async , inject } from '@angular/core/testing' ;
11
11
12
12
import { customMatchers } from '../../utils/testing/custom-matchers' ;
13
13
import { makeCreateTestComponent , expectNativeEl } from '../../utils/testing/helpers' ;
@@ -23,10 +23,13 @@ import {MediaQueriesModule} from '../../media-query/_module';
23
23
24
24
describe ( 'class directive' , ( ) => {
25
25
let fixture : ComponentFixture < any > ;
26
- let createTestComponent = makeCreateTestComponent ( ( ) => TestClassComponent ) ;
27
- let activateMediaQuery : Function = ( alias , useOverlaps = false ) : void => {
28
- let matchMedia : MockMatchMedia = fixture . debugElement . injector . get ( MatchMedia ) ;
29
- matchMedia . activate ( alias , useOverlaps ) ;
26
+ let matchMedia : MockMatchMedia ;
27
+ let createTestComponent = ( template : string ) => {
28
+ fixture = makeCreateTestComponent ( ( ) => TestClassComponent ) ( template ) ;
29
+
30
+ inject ( [ MatchMedia ] , ( _matchMedia : MockMatchMedia ) => {
31
+ matchMedia = _matchMedia ;
32
+ } ) ( ) ;
30
33
} ;
31
34
32
35
beforeEach ( ( ) => {
@@ -42,163 +45,139 @@ describe('class directive', () => {
42
45
]
43
46
} ) ;
44
47
} ) ;
45
- afterEach ( ( ) => {
46
- if ( fixture ) {
47
- fixture . debugElement . injector . get ( MatchMedia ) . clearAll ( ) ;
48
- fixture = null ;
49
- }
50
- } ) ;
51
48
52
- [ 'xs' , 'sm' , 'md' , 'lg' ]
53
- . forEach ( mq => {
54
- const selector = `class-${ mq } ` ;
55
- it ( `should apply '${ selector } ' with '${ mq } ' media query` , ( ) => {
56
- fixture = createTestComponent ( `
57
- <div ngClass.${ mq } ="${ selector } ">
58
- </div>
59
- ` ) ;
60
- activateMediaQuery ( mq ) ;
61
- expectNativeEl ( fixture ) . toHaveCssClass ( selector ) ;
62
- } ) ;
63
- } ) ;
49
+ [ 'xs' , 'sm' , 'md' , 'lg' ] . forEach ( mq => {
50
+ const selector = `class-${ mq } ` ;
51
+
52
+ it ( `should apply '${ selector } ' with '${ mq } ' media query` , ( ) => {
53
+ createTestComponent ( `<div ngClass.${ mq } ="${ selector } "></div>` ) ;
54
+ matchMedia . activate ( mq ) ;
55
+ expectNativeEl ( fixture ) . toHaveCssClass ( selector ) ;
56
+ } ) ;
57
+ } ) ;
64
58
65
59
it ( 'should merge `ngClass` values with any `class` values' , ( ) => {
66
- fixture = createTestComponent ( `
67
- <div class="class0" ngClass="class1 class2">
68
- </div>
69
- ` ) ;
60
+ createTestComponent ( `<div class="class0" ngClass="class1 class2"></div>` ) ;
70
61
71
62
expectNativeEl ( fixture ) . toHaveCssClass ( 'class0' ) ;
72
63
expectNativeEl ( fixture ) . toHaveCssClass ( 'class1' ) ;
73
64
expectNativeEl ( fixture ) . toHaveCssClass ( 'class2' ) ;
74
65
} ) ;
75
66
76
67
it ( 'should override base `class` values with responsive values' , ( ) => {
77
- fixture = createTestComponent ( `
78
- <div class="class0"
79
- class.xs="class1 class2"
80
- ngClass.xs="what">
81
- </div>
82
- ` ) ;
68
+ createTestComponent ( `<div class="class0" class.xs="class1 class2" ngClass.xs="what"></div>` ) ;
83
69
84
70
expectNativeEl ( fixture ) . toHaveCssClass ( 'class0' ) ;
85
71
expectNativeEl ( fixture ) . not . toHaveCssClass ( 'class1' ) ;
86
72
expectNativeEl ( fixture ) . not . toHaveCssClass ( 'class2' ) ;
87
73
88
- activateMediaQuery ( 'xs' ) ;
74
+ matchMedia . activate ( 'xs' ) ;
89
75
expectNativeEl ( fixture ) . not . toHaveCssClass ( 'class0' ) ;
90
76
expectNativeEl ( fixture ) . toHaveCssClass ( 'class1' ) ;
91
77
expectNativeEl ( fixture ) . toHaveCssClass ( 'class2' ) ;
92
78
93
- // activateMediaQuery ('lg');
79
+ // matchMedia.activate ('lg');
94
80
// expectNativeEl(fixture).toHaveCssClass('class0');
95
81
// expectNativeEl(fixture).not.toHaveCssClass('class1');
96
82
// expectNativeEl(fixture).not.toHaveCssClass('class2');
97
83
} ) ;
98
84
99
85
it ( 'should keep the raw existing `class` with responsive updates' , ( ) => {
100
- fixture = createTestComponent ( `
86
+ createTestComponent ( `
101
87
<div class="existing-class" ngClass="class1" ngClass.xs="xs-class">
102
88
</div>
103
89
` ) ;
104
90
105
91
expectNativeEl ( fixture ) . toHaveCssClass ( 'existing-class' ) ;
106
92
expectNativeEl ( fixture ) . toHaveCssClass ( 'class1' ) ;
107
93
108
- activateMediaQuery ( 'xs' ) ;
94
+ matchMedia . activate ( 'xs' ) ;
109
95
expectNativeEl ( fixture ) . toHaveCssClass ( 'xs-class' ) ;
110
96
expectNativeEl ( fixture ) . toHaveCssClass ( 'existing-class' ) ;
111
97
expectNativeEl ( fixture ) . not . toHaveCssClass ( 'class1' ) ;
112
98
113
- activateMediaQuery ( 'lg' ) ;
99
+ matchMedia . activate ( 'lg' ) ;
114
100
expectNativeEl ( fixture ) . not . toHaveCssClass ( 'xs-class' ) ;
115
101
expectNativeEl ( fixture ) . toHaveCssClass ( 'existing-class' ) ;
116
102
expectNativeEl ( fixture ) . toHaveCssClass ( 'class1' ) ;
117
103
} ) ;
118
104
119
105
120
106
it ( 'should keep allow removal of class selector' , ( ) => {
121
- fixture = createTestComponent ( `
122
- <div
123
- class="existing-class"
124
- [ngClass.xs]="{'xs-class':true, 'existing-class':false}">
125
- </div>
107
+ createTestComponent ( `
108
+ <div
109
+ class="existing-class"
110
+ [ngClass.xs]="{'xs-class':true, 'existing-class':false}">
111
+ </div>
126
112
` ) ;
127
113
128
114
expectNativeEl ( fixture ) . toHaveCssClass ( 'existing-class' ) ;
129
- activateMediaQuery ( 'xs' ) ;
115
+ matchMedia . activate ( 'xs' ) ;
130
116
expectNativeEl ( fixture ) . not . toHaveCssClass ( 'existing-class' ) ;
131
117
expectNativeEl ( fixture ) . toHaveCssClass ( 'xs-class' ) ;
132
118
133
- activateMediaQuery ( 'lg' ) ;
119
+ matchMedia . activate ( 'lg' ) ;
134
120
expectNativeEl ( fixture ) . toHaveCssClass ( 'existing-class' ) ;
135
121
expectNativeEl ( fixture ) . not . toHaveCssClass ( 'xs-class' ) ;
136
122
} ) ;
137
123
138
124
it ( 'should keep existing ngClass selector' , ( ) => {
139
125
// @see documentation for @angular/core ngClass =http://bit.ly/2mz0LAa
140
- fixture = createTestComponent ( `
141
- <div class="always"
142
- ngClass="existing-class"
143
- ngClass.xs="existing-class xs-class">
144
- </div>
126
+ createTestComponent ( `
127
+ <div class="always"
128
+ ngClass="existing-class"
129
+ ngClass.xs="existing-class xs-class">
130
+ </div>
145
131
` ) ;
146
132
147
133
expectNativeEl ( fixture ) . toHaveCssClass ( 'always' ) ;
148
134
expectNativeEl ( fixture ) . toHaveCssClass ( 'existing-class' ) ;
149
135
150
- activateMediaQuery ( 'xs' ) ;
136
+ matchMedia . activate ( 'xs' ) ;
151
137
expectNativeEl ( fixture ) . toHaveCssClass ( 'always' ) ;
152
138
expectNativeEl ( fixture ) . toHaveCssClass ( 'existing-class' ) ;
153
139
expectNativeEl ( fixture ) . toHaveCssClass ( 'xs-class' ) ;
154
140
155
- activateMediaQuery ( 'lg' ) ;
141
+ matchMedia . activate ( 'lg' ) ;
156
142
expectNativeEl ( fixture ) . toHaveCssClass ( 'always' ) ;
157
143
expectNativeEl ( fixture ) . toHaveCssClass ( 'existing-class' ) ;
158
144
expectNativeEl ( fixture ) . not . toHaveCssClass ( 'xs-class' ) ;
159
145
} ) ;
160
146
161
147
it ( 'should support more than one responsive breakpoint on one element' , ( ) => {
162
- fixture = createTestComponent ( `
163
- <div ngClass.xs="xs-class"
164
- ngClass.md="md-class">
165
- </div>
166
- ` ) ;
167
- activateMediaQuery ( 'xs' ) ;
148
+ createTestComponent ( `<div ngClass.xs="xs-class" ngClass.md="md-class"></div>` ) ;
149
+ matchMedia . activate ( 'xs' ) ;
168
150
expectNativeEl ( fixture ) . toHaveCssClass ( 'xs-class' ) ;
169
151
expectNativeEl ( fixture ) . not . toHaveCssClass ( 'md-class' ) ;
170
- activateMediaQuery ( 'md' ) ;
152
+ matchMedia . activate ( 'md' ) ;
171
153
expectNativeEl ( fixture ) . not . toHaveCssClass ( 'xs-class' ) ;
172
154
expectNativeEl ( fixture ) . toHaveCssClass ( 'md-class' ) ;
173
155
} ) ;
174
156
175
157
it ( 'should work with ngClass object notation' , ( ) => {
176
- fixture = createTestComponent ( `
177
- <div [ngClass]="{'x1': hasX1, 'x3': hasX3}"
178
- [ngClass.xs]="{'x1': hasX1, 'x2': hasX2}">
179
- </div>
158
+ createTestComponent ( `
159
+ <div [ngClass]="{'x1': hasX1, 'x3': hasX3}"
160
+ [ngClass.xs]="{'x1': hasX1, 'x2': hasX2}">
161
+ </div>
180
162
` ) ;
181
163
expectNativeEl ( fixture , { hasX1 : true , hasX2 : true , hasX3 : true } ) . toHaveCssClass ( 'x1' ) ;
182
164
expectNativeEl ( fixture , { hasX1 : true , hasX2 : true , hasX3 : true } ) . not . toHaveCssClass ( 'x2' ) ;
183
165
expectNativeEl ( fixture , { hasX1 : true , hasX2 : true , hasX3 : true } ) . toHaveCssClass ( 'x3' ) ;
184
166
185
- activateMediaQuery ( 'X' ) ;
167
+ matchMedia . activate ( 'X' ) ;
186
168
expectNativeEl ( fixture , { hasX1 : true , hasX2 : false , hasX3 : false } ) . toHaveCssClass ( 'x1' ) ;
187
169
expectNativeEl ( fixture , { hasX1 : true , hasX2 : false , hasX3 : false } ) . not . toHaveCssClass ( 'x2' ) ;
188
170
expectNativeEl ( fixture , { hasX1 : true , hasX2 : false , hasX3 : false } ) . not . toHaveCssClass ( 'x3' ) ;
189
171
190
- activateMediaQuery ( 'md' ) ;
172
+ matchMedia . activate ( 'md' ) ;
191
173
expectNativeEl ( fixture , { hasX1 : true , hasX2 : false , hasX3 : true } ) . toHaveCssClass ( 'x1' ) ;
192
174
expectNativeEl ( fixture , { hasX1 : true , hasX2 : false , hasX3 : true } ) . not . toHaveCssClass ( 'x2' ) ;
193
175
expectNativeEl ( fixture , { hasX1 : true , hasX2 : false , hasX3 : true } ) . toHaveCssClass ( 'x3' ) ;
194
176
} ) ;
195
177
196
178
it ( 'should work with ngClass array notation' , ( ) => {
197
- fixture = createTestComponent ( `
198
- <div [ngClass.xs]="['xs-1', 'xs-2']">
199
- </div>
200
- ` ) ;
201
- activateMediaQuery ( 'xs' ) ;
179
+ createTestComponent ( `<div [ngClass.xs]="['xs-1', 'xs-2']"></div>` ) ;
180
+ matchMedia . activate ( 'xs' ) ;
202
181
expectNativeEl ( fixture ) . toHaveCssClass ( 'xs-1' ) ;
203
182
expectNativeEl ( fixture ) . toHaveCssClass ( 'xs-2' ) ;
204
183
} ) ;
@@ -565,7 +544,7 @@ describe('binding to CSS class list', () => {
565
544
566
545
@Component ( { selector : 'test-cmp' , template : '' } )
567
546
class TestComponent {
568
- condition : boolean = true ;
547
+ condition = true ;
569
548
items : any [ ] ;
570
549
arrExpr : string [ ] = [ 'foo' ] ;
571
550
setExpr : Set < string > = new Set < string > ( ) ;
0 commit comments