mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2026-09-09 18:03:21 +00:00
AAE-49328 Form console errors (#12108)
* fix: form console errors * fix: copilot feedback
This commit is contained in:
@@ -18,7 +18,7 @@
|
||||
import { DOCUMENT } from '@angular/common';
|
||||
import { NgZone } from '@angular/core';
|
||||
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
|
||||
import { ACTIVITY_THROTTLE_MS, IdleActivityTracker } from './idle-activity-tracker';
|
||||
import { ACTIVITY_EVENTS, ACTIVITY_THROTTLE_MS, IdleActivityTracker } from './idle-activity-tracker';
|
||||
|
||||
describe('IdleActivityTracker', () => {
|
||||
let tracker: IdleActivityTracker;
|
||||
@@ -83,20 +83,36 @@ describe('IdleActivityTracker', () => {
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should allow a later listener to prevent default when an activity event fires', () => {
|
||||
const preventDefault = jasmine.createSpy('preventDefault').and.callFake((event: Event) => event.preventDefault());
|
||||
[
|
||||
{ phase: 'bubbling', listenerOptions: { capture: false, passive: false } },
|
||||
{ phase: 'capturing', listenerOptions: { capture: true, passive: false } }
|
||||
].forEach(({ phase, listenerOptions }) => {
|
||||
it(`should allow a later active ${phase} listener to prevent default when an activity event fires`, () => {
|
||||
const preventDefault = jasmine.createSpy('preventDefault').and.callFake((event: Event) => event.preventDefault());
|
||||
tracker.start();
|
||||
doc.addEventListener('mousemove', preventDefault, listenerOptions);
|
||||
|
||||
try {
|
||||
const event = new MouseEvent('mousemove', { cancelable: true });
|
||||
doc.dispatchEvent(event);
|
||||
|
||||
expect(preventDefault).toHaveBeenCalledTimes(1);
|
||||
expect(event.defaultPrevented).toBeTrue();
|
||||
} finally {
|
||||
doc.removeEventListener('mousemove', preventDefault, listenerOptions);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should keep scroll-sensitive activity listeners passive when tracking starts', () => {
|
||||
const addEventListener = spyOn(doc, 'addEventListener').and.callThrough();
|
||||
|
||||
tracker.start();
|
||||
doc.addEventListener('mousemove', preventDefault);
|
||||
|
||||
try {
|
||||
const event = new MouseEvent('mousemove', { cancelable: true });
|
||||
doc.dispatchEvent(event);
|
||||
|
||||
expect(preventDefault).toHaveBeenCalledTimes(1);
|
||||
expect(event.defaultPrevented).toBeTrue();
|
||||
} finally {
|
||||
doc.removeEventListener('mousemove', preventDefault);
|
||||
}
|
||||
expect(addEventListener).toHaveBeenCalledTimes(ACTIVITY_EVENTS.length + 1);
|
||||
expect(addEventListener).toHaveBeenCalledWith('mousemove', jasmine.any(Function), { capture: true, passive: false });
|
||||
expect(addEventListener).toHaveBeenCalledWith('touchstart', jasmine.any(Function), { capture: true, passive: true });
|
||||
expect(addEventListener).toHaveBeenCalledWith('wheel', jasmine.any(Function), { capture: true, passive: true });
|
||||
});
|
||||
|
||||
it('is a no-op when stop() is called without a prior start()', () => {
|
||||
|
||||
@@ -25,7 +25,11 @@ export const ACTIVITY_EVENTS = ['click', 'keydown', 'mousedown', 'mousemove', 'p
|
||||
/** High-frequency activity events (e.g. mousemove, scroll) are throttled to avoid rescheduling the idle timer on every DOM event. */
|
||||
export const ACTIVITY_THROTTLE_MS = 1000;
|
||||
|
||||
const ACTIVITY_LISTENER_OPTIONS: AddEventListenerOptions = { capture: true, passive: true };
|
||||
const ACTIVE_ACTIVITY_LISTENER_OPTIONS: AddEventListenerOptions = { capture: true, passive: false };
|
||||
const PASSIVE_ACTIVITY_LISTENER_OPTIONS: AddEventListenerOptions = { capture: true, passive: true };
|
||||
|
||||
const getActivityListenerOptions = (eventName: (typeof ACTIVITY_EVENTS)[number]): AddEventListenerOptions =>
|
||||
eventName === 'mousemove' ? ACTIVE_ACTIVITY_LISTENER_OPTIONS : PASSIVE_ACTIVITY_LISTENER_OPTIONS;
|
||||
|
||||
@Injectable()
|
||||
export class IdleActivityTracker implements OnDestroy {
|
||||
@@ -46,7 +50,9 @@ export class IdleActivityTracker implements OnDestroy {
|
||||
}
|
||||
|
||||
this.ngZone.runOutsideAngular(() => {
|
||||
ACTIVITY_EVENTS.forEach((eventName) => this.document.addEventListener(eventName, this.handleActivity, ACTIVITY_LISTENER_OPTIONS));
|
||||
ACTIVITY_EVENTS.forEach((eventName) =>
|
||||
this.document.addEventListener(eventName, this.handleActivity, getActivityListenerOptions(eventName))
|
||||
);
|
||||
this.document.addEventListener('visibilitychange', this.handleVisibilityChange);
|
||||
});
|
||||
this.isRegistered = true;
|
||||
@@ -57,7 +63,9 @@ export class IdleActivityTracker implements OnDestroy {
|
||||
return;
|
||||
}
|
||||
|
||||
ACTIVITY_EVENTS.forEach((eventName) => this.document.removeEventListener(eventName, this.handleActivity, ACTIVITY_LISTENER_OPTIONS));
|
||||
ACTIVITY_EVENTS.forEach((eventName) =>
|
||||
this.document.removeEventListener(eventName, this.handleActivity, getActivityListenerOptions(eventName))
|
||||
);
|
||||
this.document.removeEventListener('visibilitychange', this.handleVisibilityChange);
|
||||
this.isRegistered = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user