7
7
*/
8
8
import { Component , OnInit } from '@angular/core' ;
9
9
import { CommonModule } from '@angular/common' ;
10
- import { TestBed , ComponentFixture } from '@angular/core/testing' ;
10
+ import { TestBed , ComponentFixture , async } from '@angular/core/testing' ;
11
11
12
12
import { BreakPointsProvider } from '../../media-query/breakpoints/break-points' ;
13
13
import { BreakPointRegistry } from '../../media-query/breakpoints/break-point-registry' ;
@@ -54,7 +54,10 @@ describe('layout-gap directive', () => {
54
54
fixture1 . detectChanges ( ) ;
55
55
56
56
const nodes = queryFor ( fixture1 , 'span' ) ;
57
- expect ( nodes [ 1 ] . nativeElement ) . toHaveCssStyle ( { [ marginKey ] : margin } ) ;
57
+ const styles = { [ marginKey ] : margin } ;
58
+
59
+ expect ( nodes [ 0 ] . nativeElement ) . toHaveCssStyle ( styles ) ;
60
+ expect ( nodes [ 1 ] . nativeElement ) . not . toHaveCssStyle ( styles ) ;
58
61
}
59
62
60
63
describe ( 'with static features' , ( ) => {
@@ -65,9 +68,7 @@ describe('layout-gap directive', () => {
65
68
<div fxFlex></div>
66
69
</div>
67
70
` ;
68
- expectDomForQuery ( template , "[fxFlex]" ) . not . toHaveCssStyle ( {
69
- 'margin-left' : '13px;' ,
70
- } ) ;
71
+ expectDomForQuery ( template , "[fxFlex]" ) . not . toHaveCssStyle ( { 'margin-right' : '13px;' } ) ;
71
72
} ) ;
72
73
73
74
it ( 'should add gap styles to all children except the 1st child' , ( ) => {
@@ -83,9 +84,10 @@ describe('layout-gap directive', () => {
83
84
84
85
let nodes = queryFor ( fixture , "[fxFlex]" ) ;
85
86
expect ( nodes . length ) . toEqual ( 3 ) ;
86
- expect ( nodes [ 0 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-left' : '13px' } ) ;
87
- expect ( nodes [ 1 ] . nativeElement ) . toHaveCssStyle ( { 'margin-left' : '13px' } ) ;
88
- expect ( nodes [ 2 ] . nativeElement ) . toHaveCssStyle ( { 'margin-left' : '13px' } ) ;
87
+ expect ( nodes [ 0 ] . nativeElement ) . toHaveCssStyle ( { 'margin-right' : '13px' } ) ;
88
+ expect ( nodes [ 1 ] . nativeElement ) . toHaveCssStyle ( { 'margin-right' : '13px' } ) ;
89
+ expect ( nodes [ 2 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-right' : '13px' } ) ;
90
+ expect ( nodes [ 2 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-right' : '0px' } ) ;
89
91
} ) ;
90
92
91
93
it ( 'should add gap styles to dynamics rows EXCEPT first' , ( ) => {
@@ -100,15 +102,15 @@ describe('layout-gap directive', () => {
100
102
101
103
let nodes = queryFor ( fixture , "[fxFlex]" ) ;
102
104
expect ( nodes . length ) . toEqual ( 4 ) ;
103
- expect ( nodes [ 0 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-left' : '13px' } ) ;
104
- expect ( nodes [ 1 ] . nativeElement ) . toHaveCssStyle ( { 'margin-left' : '13px' } ) ;
105
- expect ( nodes [ 2 ] . nativeElement ) . toHaveCssStyle ( { 'margin-left' : '13px' } ) ;
106
- expect ( nodes [ 3 ] . nativeElement ) . toHaveCssStyle ( { 'margin-left' : '13px' } ) ;
105
+ expect ( nodes [ 0 ] . nativeElement ) . toHaveCssStyle ( { 'margin-right' : '13px' } ) ;
106
+ expect ( nodes [ 1 ] . nativeElement ) . toHaveCssStyle ( { 'margin-right' : '13px' } ) ;
107
+ expect ( nodes [ 2 ] . nativeElement ) . toHaveCssStyle ( { 'margin-right' : '13px' } ) ;
108
+ expect ( nodes [ 3 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-right' : '13px' } ) ;
109
+ expect ( nodes [ 3 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-right' : '0px' } ) ;
107
110
} ) ;
108
111
109
- it ( 'should add update gap styles when row items are removed' , ( ) => {
110
- let nodes ,
111
- template = `
112
+ it ( 'should add update gap styles when row items are removed' , async ( ( ) => {
113
+ let template = `
112
114
<div fxLayoutAlign="center center" fxLayoutGap="13px">
113
115
<div fxFlex *ngFor="let row of rows"></div>
114
116
</div>
@@ -117,22 +119,29 @@ describe('layout-gap directive', () => {
117
119
fixture . componentInstance . direction = "row" ;
118
120
fixture . detectChanges ( ) ;
119
121
120
- nodes = queryFor ( fixture , "[fxFlex]" ) ;
122
+ let nodes = queryFor ( fixture , "[fxFlex]" ) ;
121
123
expect ( nodes . length ) . toEqual ( 4 ) ;
122
124
123
- fixture . componentInstance . rows . shift ( ) ;
125
+ fixture . componentInstance . rows = new Array ( 3 ) ;
124
126
fixture . detectChanges ( ) ;
125
127
126
- nodes = queryFor ( fixture , "[fxFlex]" ) ;
127
- expect ( nodes . length ) . toEqual ( 3 ) ;
128
+ setTimeout ( ( ) => {
129
+ // Since the layoutGap directive detects the *ngFor changes by using a MutationObserver, the
130
+ // browser will take up some time, to actually announce the changes to the directive.
131
+ // (Kudos to @DevVersion)
128
132
129
- expect ( nodes [ 0 ] . nativeElement ) . toHaveCssStyle ( { 'margin-left' : '0px' } ) ;
130
- expect ( nodes [ 1 ] . nativeElement ) . toHaveCssStyle ( { 'margin-left' : '13px' } ) ;
131
- expect ( nodes [ 2 ] . nativeElement ) . toHaveCssStyle ( { 'margin-left' : '13px' } ) ;
132
- } ) ;
133
+ nodes = queryFor ( fixture , "[fxFlex]" ) ;
134
+ expect ( nodes . length ) . toEqual ( 3 ) ;
135
+
136
+ expect ( nodes [ 0 ] . nativeElement ) . toHaveCssStyle ( { 'margin-right' : '13px' } ) ;
137
+ expect ( nodes [ 1 ] . nativeElement ) . toHaveCssStyle ( { 'margin-right' : '13px' } ) ;
138
+ expect ( nodes [ 2 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-right' : '13px' } ) ;
139
+ } ) ;
140
+
141
+ } ) ) ;
133
142
134
143
it ( 'should apply margin-top for column layouts' , ( ) => {
135
- verifyCorrectMargin ( 'column' , 'margin-top ' ) ;
144
+ verifyCorrectMargin ( 'column' , 'margin-bottom ' ) ;
136
145
} ) ;
137
146
138
147
it ( 'should apply margin-right for row-reverse layouts' , ( ) => {
@@ -158,26 +167,26 @@ describe('layout-gap directive', () => {
158
167
fixture . detectChanges ( ) ;
159
168
let nodes = queryFor ( fixture , 'span' ) ;
160
169
161
- expect ( nodes [ 1 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-left ' : '8px' } ) ;
162
- expect ( nodes [ 1 ] . nativeElement ) . toHaveCssStyle ( { 'margin-top ' : '8px' } ) ;
170
+ expect ( nodes [ 0 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-right ' : '8px' } ) ;
171
+ expect ( nodes [ 0 ] . nativeElement ) . toHaveCssStyle ( { 'margin-bottom ' : '8px' } ) ;
163
172
164
173
165
174
// layout = column-reverse, use margin-bottom
166
175
instance . direction = "column-reverse" ;
167
176
fixture . detectChanges ( ) ;
168
177
nodes = queryFor ( fixture , 'span' ) ;
169
178
170
- expect ( nodes [ 1 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-top ' : '8px' } ) ;
171
- expect ( nodes [ 1 ] . nativeElement ) . toHaveCssStyle ( { 'margin-bottom' : '8px' } ) ;
179
+ expect ( nodes [ 0 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-right ' : '8px' } ) ;
180
+ expect ( nodes [ 0 ] . nativeElement ) . toHaveCssStyle ( { 'margin-bottom' : '8px' } ) ;
172
181
173
182
174
183
// layout = row-reverse, use margin-right
175
184
instance . direction = "row-reverse" ;
176
185
fixture . detectChanges ( ) ;
177
186
nodes = queryFor ( fixture , 'span' ) ;
178
187
179
- expect ( nodes [ 1 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-bottom' : '8px' } ) ;
180
- expect ( nodes [ 1 ] . nativeElement ) . toHaveCssStyle ( { 'margin-right' : '8px' } ) ;
188
+ expect ( nodes [ 0 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-bottom' : '8px' } ) ;
189
+ expect ( nodes [ 0 ] . nativeElement ) . toHaveCssStyle ( { 'margin-right' : '8px' } ) ;
181
190
} ) ;
182
191
183
192
it ( 'should recognize hidden elements when applying gaps' , ( ) => {
@@ -196,10 +205,50 @@ describe('layout-gap directive', () => {
196
205
let nodes = queryFor ( fixture , '[fxFlex]' ) ;
197
206
198
207
expect ( nodes . length ) . toEqual ( 3 ) ;
199
- expect ( nodes [ 0 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-left' : '0px' } ) ;
200
- expect ( nodes [ 1 ] . nativeElement ) . toHaveCssStyle ( { 'margin-left' : '0px' } ) ;
201
- expect ( nodes [ 2 ] . nativeElement ) . toHaveCssStyle ( { 'margin-left' : '16px' } ) ;
208
+ expect ( nodes [ 0 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-right' : '0px' } ) ;
209
+ expect ( nodes [ 0 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-right' : '16px' } ) ;
210
+ expect ( nodes [ 1 ] . nativeElement ) . toHaveCssStyle ( { 'margin-right' : '16px' } ) ;
211
+ expect ( nodes [ 2 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-right' : '16px' } ) ;
212
+
213
+ } ) ;
202
214
215
+ it ( 'should adjust gaps based on layout-wrap presence' , ( ) => {
216
+ let styles = [ '.col1 { display:none !important;' ] ;
217
+ let template = `
218
+ <div class="container"
219
+ [fxLayout]="direction"
220
+ [fxLayoutGap]="gap"
221
+ fxLayoutWrap>
222
+ <div fxFlex class="col1">Div 1</div>
223
+ <div fxFlex class="col2">Div 2</div>
224
+ <div fxFlex class="col3">Div 2</div>
225
+ <div fxFlex class="col4">Div 3</div>
226
+ </div>
227
+ ` ;
228
+ fixture = createTestComponent ( template , styles ) ;
229
+ fixture . componentInstance . gap = '16px' ;
230
+ fixture . componentInstance . direction = 'row' ;
231
+ fixture . detectChanges ( ) ;
232
+
233
+ let nodes = queryFor ( fixture , '[fxFlex]' ) ;
234
+
235
+ expect ( nodes . length ) . toEqual ( 4 ) ;
236
+ expect ( nodes [ 0 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-right' : '16px' } ) ;
237
+ expect ( nodes [ 1 ] . nativeElement ) . toHaveCssStyle ( { 'margin-right' : '16px' } ) ;
238
+ expect ( nodes [ 2 ] . nativeElement ) . toHaveCssStyle ( { 'margin-right' : '16px' } ) ;
239
+ expect ( nodes [ 3 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-right' : '16px' } ) ;
240
+
241
+ fixture . componentInstance . gap = '8px' ;
242
+ fixture . componentInstance . direction = 'column' ;
243
+ fixture . detectChanges ( ) ;
244
+
245
+ nodes = queryFor ( fixture , '[fxFlex]' ) ;
246
+
247
+ expect ( nodes . length ) . toEqual ( 4 ) ;
248
+ expect ( nodes [ 0 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-bottom' : '8px' } ) ;
249
+ expect ( nodes [ 1 ] . nativeElement ) . toHaveCssStyle ( { 'margin-bottom' : '8px' } ) ;
250
+ expect ( nodes [ 2 ] . nativeElement ) . toHaveCssStyle ( { 'margin-bottom' : '8px' } ) ;
251
+ expect ( nodes [ 3 ] . nativeElement ) . not . toHaveCssStyle ( { 'margin-bottom' : '8px' } ) ;
203
252
} ) ;
204
253
} ) ;
205
254
0 commit comments