From 9850e60e291d841cb82aff576e5a7301cd07ca0c Mon Sep 17 00:00:00 2001 From: Jeremy Elbourn Date: Fri, 27 Oct 2017 09:10:18 -0700 Subject: [PATCH] fix: don't show sanity check messages in tests --- .../core/common-behaviors/common-module.ts | 34 +++++++++++++++---- src/lib/core/gestures/gesture-config.ts | 15 ++++---- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/lib/core/common-behaviors/common-module.ts b/src/lib/core/common-behaviors/common-module.ts index 991ee6594fa8..48a77e4da9da 100644 --- a/src/lib/core/common-behaviors/common-module.ts +++ b/src/lib/core/common-behaviors/common-module.ts @@ -31,18 +31,31 @@ export class MatCommonModule { /** Whether we've done the global sanity checks (e.g. a theme is loaded, there is a doctype). */ private _hasDoneGlobalChecks = false; + /** Whether we've already checked for HammerJs availability. */ + private _hasCheckedHammer = false; + /** Reference to the global `document` object. */ private _document = typeof document === 'object' && document ? document : null; - constructor(@Optional() @Inject(MATERIAL_SANITY_CHECKS) sanityChecksEnabled: boolean) { - if (sanityChecksEnabled && !this._hasDoneGlobalChecks && isDevMode()) { - this._checkDoctype(); - this._checkTheme(); + constructor(@Optional() @Inject(MATERIAL_SANITY_CHECKS) private _sanityChecksEnabled: boolean) { + if (this._areChecksEnabled() && !this._hasDoneGlobalChecks) { + this._checkDoctypeIsDefined(); + this._checkThemeIsPresent(); this._hasDoneGlobalChecks = true; } } - private _checkDoctype(): void { + /** Whether any sanity checks are enabled */ + private _areChecksEnabled(): boolean { + return this._sanityChecksEnabled && isDevMode() && !this._isTestEnv(); + } + + /** Whether the code is running in tests. */ + private _isTestEnv() { + return window['__karma__'] || window['jasmine']; + } + + private _checkDoctypeIsDefined(): void { if (this._document && !this._document.doctype) { console.warn( 'Current document does not have a doctype. This may cause ' + @@ -51,7 +64,7 @@ export class MatCommonModule { } } - private _checkTheme(): void { + private _checkThemeIsPresent(): void { if (this._document && typeof getComputedStyle === 'function') { const testElement = this._document.createElement('div'); @@ -74,4 +87,13 @@ export class MatCommonModule { this._document.body.removeChild(testElement); } } + + /** Checks whether HammerJS is available. */ + _checkHammerIsAvailable(): void { + if (this._areChecksEnabled() && !this._hasCheckedHammer && !window['Hammer']) { + console.warn( + 'Could not find HammerJS. Certain Angular Material components may not work correctly.'); + } + this._hasCheckedHammer = true; + } } diff --git a/src/lib/core/gestures/gesture-config.ts b/src/lib/core/gestures/gesture-config.ts index 3172f71d835f..2299daede94f 100644 --- a/src/lib/core/gestures/gesture-config.ts +++ b/src/lib/core/gestures/gesture-config.ts @@ -6,9 +6,10 @@ * found in the LICENSE file at https://angular.io/license */ -import {Injectable, isDevMode} from '@angular/core'; +import {Injectable, Optional} from '@angular/core'; import {HammerGestureConfig} from '@angular/platform-browser'; -import {HammerStatic, HammerInstance, Recognizer, RecognizerStatic} from './gesture-annotations'; +import {MatCommonModule} from '../common-behaviors/common-module'; +import {HammerInstance, HammerStatic, Recognizer, RecognizerStatic} from './gesture-annotations'; /* Adjusts configuration of our gesture library, Hammer. */ @Injectable() @@ -25,14 +26,10 @@ export class GestureConfig extends HammerGestureConfig { 'slideleft' ] : []; - constructor() { + constructor(@Optional() commonModule?: MatCommonModule) { super(); - - if (!this._hammer && isDevMode()) { - console.warn( - 'Could not find HammerJS. Certain Angular Material ' + - 'components may not work correctly.' - ); + if (commonModule) { + commonModule._checkHammerIsAvailable(); } }