feat(platform-browser): do not throw error when Hammer.js not loaded (#22257)
closes #16992 PR Close #22257
This commit is contained in:
parent
7c45db3a19
commit
991300b86c
@ -6,7 +6,7 @@
|
|||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {Inject, Injectable, InjectionToken} from '@angular/core';
|
import {Inject, Injectable, InjectionToken, ɵConsole as Console} from '@angular/core';
|
||||||
|
|
||||||
import {DOCUMENT} from '../dom_tokens';
|
import {DOCUMENT} from '../dom_tokens';
|
||||||
|
|
||||||
@ -99,7 +99,8 @@ export class HammerGestureConfig {
|
|||||||
export class HammerGesturesPlugin extends EventManagerPlugin {
|
export class HammerGesturesPlugin extends EventManagerPlugin {
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(DOCUMENT) doc: any,
|
@Inject(DOCUMENT) doc: any,
|
||||||
@Inject(HAMMER_GESTURE_CONFIG) private _config: HammerGestureConfig) {
|
@Inject(HAMMER_GESTURE_CONFIG) private _config: HammerGestureConfig,
|
||||||
|
private console: Console) {
|
||||||
super(doc);
|
super(doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,7 +110,8 @@ export class HammerGesturesPlugin extends EventManagerPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!(window as any).Hammer) {
|
if (!(window as any).Hammer) {
|
||||||
throw new Error(`Hammer.js is not loaded, can not bind ${eventName} event`);
|
this.console.warn(`Hammer.js is not loaded, can not bind '${eventName}' event.`);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -10,14 +10,27 @@ import {HammerGestureConfig, HammerGesturesPlugin} from '@angular/platform-brows
|
|||||||
|
|
||||||
{
|
{
|
||||||
describe('HammerGesturesPlugin', () => {
|
describe('HammerGesturesPlugin', () => {
|
||||||
|
let plugin: HammerGesturesPlugin;
|
||||||
|
let mockConsole: any;
|
||||||
if (isNode) return;
|
if (isNode) return;
|
||||||
|
|
||||||
it('should implement addGlobalEventListener', () => {
|
beforeEach(() => {
|
||||||
const plugin = new HammerGesturesPlugin(document, new HammerGestureConfig());
|
mockConsole = {warn: () => {}};
|
||||||
|
plugin = new HammerGesturesPlugin(document, new HammerGestureConfig(), mockConsole);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should implement addGlobalEventListener', () => {
|
||||||
spyOn(plugin, 'addEventListener').and.callFake(() => {});
|
spyOn(plugin, 'addEventListener').and.callFake(() => {});
|
||||||
|
|
||||||
expect(() => plugin.addGlobalEventListener('document', 'swipe', () => {})).not.toThrowError();
|
expect(() => plugin.addGlobalEventListener('document', 'swipe', () => {})).not.toThrowError();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('shoud warn user and do nothing when Hammer.js not loaeded', () => {
|
||||||
|
spyOn(mockConsole, 'warn');
|
||||||
|
|
||||||
|
expect(plugin.supports('swipe')).toBe(false);
|
||||||
|
expect(mockConsole.warn)
|
||||||
|
.toHaveBeenCalledWith(`Hammer.js is not loaded, can not bind 'swipe' event.`);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user