Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 7bff29e

Browse files
fix(api): remove circular dependencies
* fxShow and fxHide had 'unused' circular dependencies; which causes issues with the Clojure compiler * add unit tests to show/hide to test for MatchMediObservable usages in templates Fixes #88.
1 parent dde6e87 commit 7bff29e

File tree

4 files changed

+61
-33
lines changed

4 files changed

+61
-33
lines changed

src/lib/flexbox/api/hide.spec.ts

+35-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {Component, OnInit} from '@angular/core';
1+
import {Component, OnInit, Inject} from '@angular/core';
22
import {CommonModule} from '@angular/common';
33
import {ComponentFixture, TestBed } from '@angular/core/testing';
44

55
import {MockMatchMedia} from '../../media-query/mock/mock-match-media';
6-
import {MatchMedia} from '../../media-query/match-media';
6+
import {MatchMedia, MatchMediaObservable} from '../../media-query/match-media';
77
import {BreakPointsProvider} from '../../media-query/providers/break-points-provider';
88
import {BreakPointRegistry} from '../../media-query/breakpoints/break-point-registry';
99
import {FlexLayoutModule} from '../_module';
@@ -100,6 +100,36 @@ describe('show directive', () => {
100100
});
101101

102102
});
103+
104+
it('should support use of the `media` observable in templates ', () => {
105+
fixture = createTestComponent(`
106+
<div [fxHide]="media.isActive('xs')" >
107+
...content
108+
</div>
109+
`);
110+
expectNativeEl(fixture).toHaveCssStyle({ 'display': 'flex' });
111+
112+
activateMediaQuery('xs');
113+
expectNativeEl(fixture).toHaveCssStyle({ 'display': 'none' });
114+
115+
activateMediaQuery('lg');
116+
expectNativeEl(fixture).toHaveCssStyle({ 'display': 'flex' });
117+
});
118+
119+
it('should support use of the `media` observable in adaptive templates ', () => {
120+
fixture = createTestComponent(`
121+
<div fxHide="false" [fxHide.md]="media.isActive('xs')" >
122+
...content
123+
</div>
124+
`);
125+
expectNativeEl(fixture).toHaveCssStyle({ 'display': 'flex' });
126+
127+
activateMediaQuery('xs');
128+
expectNativeEl(fixture).toHaveCssStyle({ 'display': 'flex' });
129+
130+
activateMediaQuery('md');
131+
expectNativeEl(fixture).toHaveCssStyle({ 'display': 'flex' });
132+
});
103133
});
104134

105135

@@ -114,10 +144,12 @@ describe('show directive', () => {
114144
export class TestHideComponent implements OnInit {
115145
isVisible = 0;
116146
menuHidden: boolean = true;
147+
148+
constructor(@Inject(MatchMediaObservable) private media) { }
149+
117150
toggleMenu() {
118151
this.menuHidden = !this.menuHidden;
119152
}
120-
constructor() { }
121153
ngOnInit() { }
122154
}
123155

src/lib/flexbox/api/hide.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {BaseFxDirective} from './base';
1717
import {MediaChange} from '../../media-query/media-change';
1818
import {MediaMonitor} from '../../media-query/media-monitor';
1919

20-
import {ShowDirective} from "./show";
2120
import {LayoutDirective} from './layout';
2221

2322
/**
@@ -65,7 +64,6 @@ export class HideDirective extends BaseFxDirective implements OnInit, OnChanges,
6564
constructor(
6665
monitor : MediaMonitor,
6766
@Optional() @Self() private _layout: LayoutDirective,
68-
@Optional() @Self() private _showDirective : ShowDirective,
6967
protected elRef: ElementRef,
7068
protected renderer: Renderer) {
7169
super(monitor, elRef, renderer);
@@ -79,12 +77,6 @@ export class HideDirective extends BaseFxDirective implements OnInit, OnChanges,
7977
}
8078
}
8179

82-
/**
83-
* Does the current element also use the fxShow API ?
84-
*/
85-
protected get usesShowAPI() {
86-
return !!this._showDirective;
87-
}
8880

8981
// *********************************************
9082
// Lifecycle Methods
@@ -134,9 +126,7 @@ export class HideDirective extends BaseFxDirective implements OnInit, OnChanges,
134126
}
135127

136128
let shouldHide = this._validateTruthy(value);
137-
if ( shouldHide || !this.usesShowAPI ) {
138-
this._applyStyleToElement(this._buildCSS(shouldHide));
139-
}
129+
this._applyStyleToElement(this._buildCSS(shouldHide));
140130
}
141131

142132

src/lib/flexbox/api/show.spec.ts

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {Component, OnInit} from '@angular/core';
1+
import {Component, OnInit, Inject} from '@angular/core';
22
import {CommonModule} from '@angular/common';
33
import {ComponentFixture, TestBed } from '@angular/core/testing';
44

55
import {MockMatchMedia} from '../../media-query/mock/mock-match-media';
6-
import {MatchMedia} from '../../media-query/match-media';
6+
import {MatchMedia, MatchMediaObservable} from '../../media-query/match-media';
77
import {BreakPointsProvider} from '../../media-query/providers/break-points-provider';
88
import {BreakPointRegistry} from '../../media-query/breakpoints/break-point-registry';
99
import {FlexLayoutModule} from '../_module';
@@ -66,6 +66,9 @@ describe('show directive', () => {
6666
</div>
6767
`);
6868
expectNativeEl(fixture).toHaveCssStyle({ 'display': 'none' });
69+
70+
fixture.componentInstance.isVisible = true;
71+
expectNativeEl(fixture).toHaveCssStyle({ 'display': 'flex' });
6972
});
7073

7174
it('should update styles with binding changes', () => {
@@ -105,6 +108,21 @@ describe('show directive', () => {
105108
expectNativeEl(fixture).toHaveCssStyle({ 'display': 'none' });
106109
});
107110

111+
it('should support use of the `media` observable in templates ', () => {
112+
fixture = createTestComponent(`
113+
<div [fxShow]="media.isActive('xs')" >
114+
...content
115+
</div>
116+
`);
117+
expectNativeEl(fixture).toHaveCssStyle({ 'display': 'none' });
118+
119+
activateMediaQuery('xs', true);
120+
expectNativeEl(fixture).toHaveCssStyle({ 'display': 'flex' });
121+
122+
activateMediaQuery('gt-md', true);
123+
expectNativeEl(fixture).toHaveCssStyle({ 'display': 'none' });
124+
});
125+
108126
});
109127
});
110128

@@ -120,10 +138,12 @@ describe('show directive', () => {
120138
export class TestShowComponent implements OnInit {
121139
isVisible = 0;
122140
menuOpen: boolean = true;
141+
142+
constructor(@Inject(MatchMediaObservable) private media) { }
143+
123144
toggleMenu() {
124145
this.menuOpen = !this.menuOpen;
125146
}
126-
constructor() { }
127147
ngOnInit() { }
128148
}
129149

src/lib/flexbox/api/show.ts

+2-16
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import {
88
Renderer,
99
SimpleChanges,
1010
Self,
11-
Optional,
12-
Inject,
13-
forwardRef
11+
Optional
1412
} from '@angular/core';
1513

1614
import {Subscription} from 'rxjs/Subscription';
@@ -19,7 +17,6 @@ import {BaseFxDirective} from './base';
1917
import {MediaChange} from '../../media-query/media-change';
2018
import {MediaMonitor} from '../../media-query/media-monitor';
2119

22-
import {HideDirective} from "./hide";
2320
import {LayoutDirective} from './layout';
2421

2522

@@ -70,7 +67,6 @@ export class ShowDirective extends BaseFxDirective implements OnInit, OnChanges,
7067
constructor(
7168
monitor : MediaMonitor,
7269
@Optional() @Self() private _layout: LayoutDirective,
73-
@Inject(forwardRef(() => HideDirective)) @Optional() @Self() private _hideDirective,
7470
protected elRef: ElementRef,
7571
protected renderer: Renderer)
7672
{
@@ -85,14 +81,6 @@ export class ShowDirective extends BaseFxDirective implements OnInit, OnChanges,
8581
}
8682
}
8783

88-
/**
89-
* Does the current element also use the fxShow API ?
90-
*/
91-
protected get usesHideAPI() {
92-
return !!this._hideDirective;
93-
}
94-
95-
9684
// *********************************************
9785
// Lifecycle Methods
9886
// *********************************************
@@ -138,9 +126,7 @@ export class ShowDirective extends BaseFxDirective implements OnInit, OnChanges,
138126
}
139127

140128
let shouldShow = this._validateTruthy(value);
141-
if ( shouldShow || !this.usesHideAPI ) {
142-
this._applyStyleToElement(this._buildCSS(shouldShow));
143-
}
129+
this._applyStyleToElement(this._buildCSS(shouldShow));
144130
}
145131

146132

0 commit comments

Comments
 (0)