Skip to content

Commit d17f9d2

Browse files
jelbournandrewseguin
authored andcommitted
fix: don't show sanity check messages in tests (#8080)
1 parent 2fa679b commit d17f9d2

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

src/lib/core/common-behaviors/common-module.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,31 @@ export class MatCommonModule {
3131
/** Whether we've done the global sanity checks (e.g. a theme is loaded, there is a doctype). */
3232
private _hasDoneGlobalChecks = false;
3333

34+
/** Whether we've already checked for HammerJs availability. */
35+
private _hasCheckedHammer = false;
36+
3437
/** Reference to the global `document` object. */
3538
private _document = typeof document === 'object' && document ? document : null;
3639

37-
constructor(@Optional() @Inject(MATERIAL_SANITY_CHECKS) sanityChecksEnabled: boolean) {
38-
if (sanityChecksEnabled && !this._hasDoneGlobalChecks && isDevMode()) {
39-
this._checkDoctype();
40-
this._checkTheme();
40+
constructor(@Optional() @Inject(MATERIAL_SANITY_CHECKS) private _sanityChecksEnabled: boolean) {
41+
if (this._areChecksEnabled() && !this._hasDoneGlobalChecks) {
42+
this._checkDoctypeIsDefined();
43+
this._checkThemeIsPresent();
4144
this._hasDoneGlobalChecks = true;
4245
}
4346
}
4447

45-
private _checkDoctype(): void {
48+
/** Whether any sanity checks are enabled */
49+
private _areChecksEnabled(): boolean {
50+
return this._sanityChecksEnabled && isDevMode() && !this._isTestEnv();
51+
}
52+
53+
/** Whether the code is running in tests. */
54+
private _isTestEnv() {
55+
return window['__karma__'] || window['jasmine'];
56+
}
57+
58+
private _checkDoctypeIsDefined(): void {
4659
if (this._document && !this._document.doctype) {
4760
console.warn(
4861
'Current document does not have a doctype. This may cause ' +
@@ -51,7 +64,7 @@ export class MatCommonModule {
5164
}
5265
}
5366

54-
private _checkTheme(): void {
67+
private _checkThemeIsPresent(): void {
5568
if (this._document && typeof getComputedStyle === 'function') {
5669
const testElement = this._document.createElement('div');
5770

@@ -74,4 +87,13 @@ export class MatCommonModule {
7487
this._document.body.removeChild(testElement);
7588
}
7689
}
90+
91+
/** Checks whether HammerJS is available. */
92+
_checkHammerIsAvailable(): void {
93+
if (this._areChecksEnabled() && !this._hasCheckedHammer && !window['Hammer']) {
94+
console.warn(
95+
'Could not find HammerJS. Certain Angular Material components may not work correctly.');
96+
}
97+
this._hasCheckedHammer = true;
98+
}
7799
}

src/lib/core/gestures/gesture-config.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Injectable, isDevMode} from '@angular/core';
9+
import {Injectable, Optional} from '@angular/core';
1010
import {HammerGestureConfig} from '@angular/platform-browser';
11-
import {HammerStatic, HammerInstance, Recognizer, RecognizerStatic} from './gesture-annotations';
11+
import {MatCommonModule} from '../common-behaviors/common-module';
12+
import {HammerInstance, HammerStatic, Recognizer, RecognizerStatic} from './gesture-annotations';
1213

1314
/* Adjusts configuration of our gesture library, Hammer. */
1415
@Injectable()
@@ -25,14 +26,10 @@ export class GestureConfig extends HammerGestureConfig {
2526
'slideleft'
2627
] : [];
2728

28-
constructor() {
29+
constructor(@Optional() commonModule?: MatCommonModule) {
2930
super();
30-
31-
if (!this._hammer && isDevMode()) {
32-
console.warn(
33-
'Could not find HammerJS. Certain Angular Material ' +
34-
'components may not work correctly.'
35-
);
31+
if (commonModule) {
32+
commonModule._checkHammerIsAvailable();
3633
}
3734
}
3835

0 commit comments

Comments
 (0)