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 { DOCUMENT } from '@angular/common';
|
||||||
import { NgZone } from '@angular/core';
|
import { NgZone } from '@angular/core';
|
||||||
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
|
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', () => {
|
describe('IdleActivityTracker', () => {
|
||||||
let tracker: IdleActivityTracker;
|
let tracker: IdleActivityTracker;
|
||||||
@@ -83,10 +83,14 @@ describe('IdleActivityTracker', () => {
|
|||||||
expect(spy).toHaveBeenCalledTimes(1);
|
expect(spy).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should allow a later listener to prevent default when an activity event fires', () => {
|
[
|
||||||
|
{ 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());
|
const preventDefault = jasmine.createSpy('preventDefault').and.callFake((event: Event) => event.preventDefault());
|
||||||
tracker.start();
|
tracker.start();
|
||||||
doc.addEventListener('mousemove', preventDefault);
|
doc.addEventListener('mousemove', preventDefault, listenerOptions);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const event = new MouseEvent('mousemove', { cancelable: true });
|
const event = new MouseEvent('mousemove', { cancelable: true });
|
||||||
@@ -95,9 +99,21 @@ describe('IdleActivityTracker', () => {
|
|||||||
expect(preventDefault).toHaveBeenCalledTimes(1);
|
expect(preventDefault).toHaveBeenCalledTimes(1);
|
||||||
expect(event.defaultPrevented).toBeTrue();
|
expect(event.defaultPrevented).toBeTrue();
|
||||||
} finally {
|
} finally {
|
||||||
doc.removeEventListener('mousemove', preventDefault);
|
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();
|
||||||
|
|
||||||
|
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()', () => {
|
it('is a no-op when stop() is called without a prior start()', () => {
|
||||||
const spy = jasmine.createSpy('activity');
|
const spy = jasmine.createSpy('activity');
|
||||||
|
|||||||
@@ -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. */
|
/** 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;
|
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()
|
@Injectable()
|
||||||
export class IdleActivityTracker implements OnDestroy {
|
export class IdleActivityTracker implements OnDestroy {
|
||||||
@@ -46,7 +50,9 @@ export class IdleActivityTracker implements OnDestroy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.ngZone.runOutsideAngular(() => {
|
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.document.addEventListener('visibilitychange', this.handleVisibilityChange);
|
||||||
});
|
});
|
||||||
this.isRegistered = true;
|
this.isRegistered = true;
|
||||||
@@ -57,7 +63,9 @@ export class IdleActivityTracker implements OnDestroy {
|
|||||||
return;
|
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.document.removeEventListener('visibilitychange', this.handleVisibilityChange);
|
||||||
this.isRegistered = false;
|
this.isRegistered = false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user