Skip to content

Commit 991300b

Browse files
trotylvicb
authored andcommitted
feat(platform-browser): do not throw error when Hammer.js not loaded (#22257)
closes #16992 PR Close #22257
1 parent 7c45db3 commit 991300b

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

packages/platform-browser/src/dom/events/hammer_gestures.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Inject, Injectable, InjectionToken} from '@angular/core';
9+
import {Inject, Injectable, InjectionToken, ɵConsole as Console} from '@angular/core';
1010

1111
import {DOCUMENT} from '../dom_tokens';
1212

@@ -99,7 +99,8 @@ export class HammerGestureConfig {
9999
export class HammerGesturesPlugin extends EventManagerPlugin {
100100
constructor(
101101
@Inject(DOCUMENT) doc: any,
102-
@Inject(HAMMER_GESTURE_CONFIG) private _config: HammerGestureConfig) {
102+
@Inject(HAMMER_GESTURE_CONFIG) private _config: HammerGestureConfig,
103+
private console: Console) {
103104
super(doc);
104105
}
105106

@@ -109,7 +110,8 @@ export class HammerGesturesPlugin extends EventManagerPlugin {
109110
}
110111

111112
if (!(window as any).Hammer) {
112-
throw new Error(`Hammer.js is not loaded, can not bind ${eventName} event`);
113+
this.console.warn(`Hammer.js is not loaded, can not bind '${eventName}' event.`);
114+
return false;
113115
}
114116

115117
return true;

packages/platform-browser/test/dom/events/hammer_gestures_spec.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,27 @@ import {HammerGestureConfig, HammerGesturesPlugin} from '@angular/platform-brows
1010

1111
{
1212
describe('HammerGesturesPlugin', () => {
13+
let plugin: HammerGesturesPlugin;
14+
let mockConsole: any;
1315
if (isNode) return;
1416

15-
it('should implement addGlobalEventListener', () => {
16-
const plugin = new HammerGesturesPlugin(document, new HammerGestureConfig());
17+
beforeEach(() => {
18+
mockConsole = {warn: () => {}};
19+
plugin = new HammerGesturesPlugin(document, new HammerGestureConfig(), mockConsole);
20+
});
1721

22+
it('should implement addGlobalEventListener', () => {
1823
spyOn(plugin, 'addEventListener').and.callFake(() => {});
1924

2025
expect(() => plugin.addGlobalEventListener('document', 'swipe', () => {})).not.toThrowError();
2126
});
27+
28+
it('shoud warn user and do nothing when Hammer.js not loaeded', () => {
29+
spyOn(mockConsole, 'warn');
30+
31+
expect(plugin.supports('swipe')).toBe(false);
32+
expect(mockConsole.warn)
33+
.toHaveBeenCalledWith(`Hammer.js is not loaded, can not bind 'swipe' event.`);
34+
});
2235
});
2336
}

0 commit comments

Comments
 (0)