Skip to content

Commit

Permalink
Merge pull request #707 from capricorn86/659-fix-publish-of-mutation-…
Browse files Browse the repository at this point in the history
…observer-fix

#659@trivial: Adds unit tests for MutationObserver.disconnect().
  • Loading branch information
capricorn86 committed Jan 30, 2023
2 parents 9bc81a2 + 80e4f43 commit 62085eb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Expand Up @@ -57,6 +57,7 @@ export default class MutationObserver {
public disconnect(): void {
if (this.target) {
(<Node>this.target)._unobserve(this.listener);
this.target = null;
}
}

Expand Down
19 changes: 19 additions & 0 deletions packages/happy-dom/test/mutation-observer/MutationObserver.test.ts
Expand Up @@ -213,4 +213,23 @@ describe('MutationObserver', () => {
]);
});
});

describe('disconnect()', () => {
it('Disconnects the observer.', () => {
let records = [];
const div = document.createElement('div');
const observer = new MutationObserver((mutationRecords) => {
records = mutationRecords;
});
observer.observe(div, { attributes: true });
observer.disconnect();
div.setAttribute('attr', 'value');
expect(records).toEqual([]);
});

it('Ignores if triggered when it is not observing.', () => {
const observer = new MutationObserver(() => {});
expect(() => observer.disconnect()).not.toThrow();
});
});
});

0 comments on commit 62085eb

Please sign in to comment.