diff --git a/src/observer.ts b/src/observer.ts index f926e742..b2fa63c7 100644 --- a/src/observer.ts +++ b/src/observer.ts @@ -78,6 +78,9 @@ export function createObserver( } export function observeElement(element: Instance) { + if (element.observer && !observerElementsMap.has(element.observer)) { + observerElementsMap.set(element.observer, new Set()); + } observerElementsMap.get(element.observer)?.add(element); element.observer!.observe(element.target!); } diff --git a/test/unit/observer.spec.js b/test/unit/observer.spec.js index 5b8d33cc..0c527bc2 100644 --- a/test/unit/observer.spec.js +++ b/test/unit/observer.spec.js @@ -194,4 +194,20 @@ describe('findObserverElement', () => { expect(instance1).toEqual(entry1); expect(instance2).toEqual(entry2); }); + + test('two subsequent createObserver calls should not produce two observers', () => { + const observer1 = createObserver(); + const observer2 = createObserver(); + expect(observer1).toEqual(observer2); + }); + + test('observing element should add target to observerElementsMap even if there is no such observer key', () => { + const observer = createObserver(); + const entry = { observer, target: target1 }; + observeElement(entry); + unobserveElement(entry, entry.target); + observeElement(entry); + const instance = findObserverElement(observer, entry); + expect(instance).toEqual(entry); + }); });