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

Commit af92cb0

Browse files
authored
fix(mock-match-media): ensure overlapping breakpoints are activated (#1265)
1 parent f47da38 commit af92cb0

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/lib/core/match-media/mock/mock-match-media.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,17 @@ describe('mock-match-media', () => {
255255

256256
subscription.unsubscribe();
257257
});
258+
259+
it('activates overlapping breakpoints correct', () => {
260+
mediaController.activate('xs', true);
261+
expect(mediaController
262+
.isActive('screen and (min-width: 0px) and (max-width: 599.9px)'))
263+
.toBe(true);
264+
expect(mediaController
265+
.isActive('screen and (min-width: 600px) and (max-width: 959.9px)'))
266+
.toBe(false);
267+
expect(mediaController
268+
.isActive('screen and (max-width: 599.9px)'))
269+
.toBe(true);
270+
});
258271
});

src/lib/core/match-media/mock/mock-match-media.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,54 +72,58 @@ export class MockMatchMedia extends MatchMedia {
7272
// Simulate activation of overlapping lt-<XXX> ranges
7373
switch (alias) {
7474
case 'lg' :
75-
this._activateByAlias('lt-xl');
75+
this._activateByAlias(['lt-xl']);
7676
break;
7777
case 'md' :
78-
this._activateByAlias('lt-xl, lt-lg');
78+
this._activateByAlias(['lt-xl', 'lt-lg']);
7979
break;
8080
case 'sm' :
81-
this._activateByAlias('lt-xl, lt-lg, lt-md');
81+
this._activateByAlias(['lt-xl', 'lt-lg', 'lt-md']);
8282
break;
8383
case 'xs' :
84-
this._activateByAlias('lt-xl, lt-lg, lt-md, lt-sm');
84+
this._activateByAlias(['lt-xl', 'lt-lg', 'lt-md', 'lt-sm']);
8585
break;
8686
}
8787

8888
// Simulate activation of overlapping gt-<xxxx> mediaQuery ranges
8989
switch (alias) {
9090
case 'xl' :
91-
this._activateByAlias('gt-lg, gt-md, gt-sm, gt-xs');
91+
this._activateByAlias(['gt-lg', 'gt-md', 'gt-sm', 'gt-xs']);
9292
break;
9393
case 'lg' :
94-
this._activateByAlias('gt-md, gt-sm, gt-xs');
94+
this._activateByAlias(['gt-md', 'gt-sm', 'gt-xs']);
9595
break;
9696
case 'md' :
97-
this._activateByAlias('gt-sm, gt-xs');
97+
this._activateByAlias(['gt-sm', 'gt-xs']);
9898
break;
9999
case 'sm' :
100-
this._activateByAlias('gt-xs');
100+
this._activateByAlias(['gt-xs']);
101101
break;
102102
}
103103
}
104+
104105
// Activate last since the responsiveActivation is watching *this* mediaQuery
105106
return this._activateByQuery(mediaQuery);
106107
}
107108

108109
/**
109110
*
110111
*/
111-
private _activateByAlias(aliases: string) {
112+
private _activateByAlias(aliases: string[]) {
112113
const activate = (alias: string) => {
113114
const bp = this._breakpoints.findByAlias(alias);
114115
this._activateByQuery(bp ? bp.mediaQuery : alias);
115116
};
116-
aliases.split(',').forEach(alias => activate(alias.trim()));
117+
aliases.forEach(activate);
117118
}
118119

119120
/**
120121
*
121122
*/
122123
private _activateByQuery(mediaQuery: string) {
124+
if (!this.registry.has(mediaQuery) && this.autoRegisterQueries) {
125+
this._registerMediaQuery(mediaQuery);
126+
}
123127
const mql: MockMediaQueryList = this.registry.get(mediaQuery) as MockMediaQueryList;
124128

125129
if (mql && !this.isActive(mediaQuery)) {

0 commit comments

Comments
 (0)