Skip to content

fix: don't show sanity check messages in tests #8080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/lib/core/common-behaviors/common-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ' +
Expand All @@ -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');

Expand All @@ -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;
}
}
15 changes: 6 additions & 9 deletions src/lib/core/gestures/gesture-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();
}
}

Expand Down