Repository: exadel-inc/esl
Pull request #1886 (2023-08-23 20:33:02)

fix(esl-event-listener): `ESLResizeObserverTarget.for` handles no target cases silently (warning + null result)
Closes: #1885

codeclimate[bot]: Code Climate has analyzed commit 6637d1d7 and detected **0 issues** on this pull request.

The test coverage on the diff in this pull request is **100.0%** (**50%** is the threshold).

This pull request will bring the total coverage in the repository to **49.5%** (**0.0%** change).

View more on [Code Climate](https://codeclimate.com/github/exadel-inc/esl/pull/1886).


Patch:
diff --git a/src/modules/esl-event-listener/core/targets/resize.adapter.ts b/src/modules/esl-event-listener/core/targets/resize.adapter.ts
index 3afcfc8c2..436c63331 100644
--- a/src/modules/esl-event-listener/core/targets/resize.adapter.ts
+++ b/src/modules/esl-event-listener/core/targets/resize.adapter.ts
@@ -1,4 +1,5 @@
 import {SyntheticEventTarget} from '../../../esl-utils/dom/events/target';
+import {isElement} from '../../../esl-utils/dom/api';
 import {resolveDomTarget} from '../../../esl-utils/abstract/dom-target';
 import {ESLElementResizeEvent} from './resize.adapter.event';
 
@@ -29,16 +30,20 @@ export class ESLResizeObserverTarget extends SyntheticEventTarget {
   }
 
   /** Creates {@link ESLResizeObserverTarget} instance for the {@link ESLDomElementTarget} */
-  public static for(target: ESLDomElementTarget): ESLResizeObserverTarget {
-    return new ESLResizeObserverTarget(target);
+  public static for(target: ESLDomElementTarget): ESLResizeObserverTarget;
+  public static for(target: ESLDomElementTarget): ESLResizeObserverTarget | null {
+    const $target = resolveDomTarget(target);
+    if (isElement($target)) return new ESLResizeObserverTarget($target);
+    // Error handling
+    console.warn('[ESL]: ESLResizeObserverTarget can not observe %o', target);
+    return null;
   }
 
   /**
    * Creates {@link ESLResizeObserverTarget} for the {@link ESLDomElementTarget}.
    * Note the {@link ESLResizeObserverTarget} instances are singletons relatively to the {@link Element}
    */
-  protected constructor(target: ESLDomElementTarget) {
-    target = resolveDomTarget(target);
+  protected constructor(target: Element) {
     const instance = ESLResizeObserverTarget.mapping.get(target);
     if (instance) return instance;
 


Test patch:
diff --git a/src/modules/esl-event-listener/test/targets/resize.test.ts b/src/modules/esl-event-listener/test/targets/resize.test.ts
index ae3599b0f..b91499b4c 100644
--- a/src/modules/esl-event-listener/test/targets/resize.test.ts
+++ b/src/modules/esl-event-listener/test/targets/resize.test.ts
@@ -16,6 +16,32 @@ describe('ESLEventUtils: ResizeObserver EventTarget adapter', () => {
     );
   });
 
+  describe('ESLResizeObserverTarget do not throws error on incorrect input (silent processing)', () => {
+    const consoleSpy = jest.spyOn(console, 'warn');
+    beforeEach(() => consoleSpy.mockReset().mockImplementation(() => void 0));
+    afterAll(() => consoleSpy.mockRestore());
+
+    test('ESLResizeObserverTarget.for(undefined) returns null without error', () => {
+      expect(ESLResizeObserverTarget.for(undefined as any)).toBeNull();
+      expect(consoleSpy).toBeCalled();
+    });
+
+    test('ESLResizeObserverTarget.for(null) returns null without error', () => {
+      expect(ESLResizeObserverTarget.for(null as any)).toBeNull();
+      expect(consoleSpy).toBeCalled();
+    });
+
+    test('ESLResizeObserverTarget.for(123) returns null without error', () => {
+      expect(ESLResizeObserverTarget.for(123 as any)).toBeNull();
+      expect(consoleSpy).toBeCalled();
+    });
+
+    test('ESLResizeObserverTarget.for({}) returns null without error', () => {
+      expect(ESLResizeObserverTarget.for({} as any)).toBeNull();
+      expect(consoleSpy).toBeCalled();
+    });
+  });
+
   describe('ESLResizeObserverTarget livecycle', () => {
     describe('Element as a taget', () => {
       const mock = getLastResizeObserverMock();