Skip to content

Commit

Permalink
Merge pull request #55 from trurl-master/unobserved-nodes
Browse files Browse the repository at this point in the history
Add tests to not call the callback from unobserved nodes
  • Loading branch information
trurl-master authored Feb 26, 2024
2 parents 9a36419 + 6b6ca39 commit 89011dc
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsdom-testing-mocks",
"version": "1.11.0",
"version": "1.12.0",
"author": "Ivan Galiatin",
"license": "MIT",
"description": "A set of tools for emulating browser behavior in jsdom environment",
Expand Down
68 changes: 68 additions & 0 deletions src/mocks/intersection-observer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { mockIntersectionObserver } from './intersection-observer';

const io = mockIntersectionObserver();

describe('mockIntersectionObserver', () => {
it("don't call unobserved nodes, enterNode", () => {
const node = document.createElement('div');
const callback = runner.fn();

const observer = new IntersectionObserver(callback);

observer.observe(node);

io.enterNode(node);

expect(callback).toHaveBeenCalledTimes(1);

observer.unobserve(node);

io.enterNode(node);

expect(callback).toHaveBeenCalledTimes(1);
});

it('handles multiple nodes correctly, enterNodes', () => {
const node1 = document.createElement('div');
const node2 = document.createElement('div');
const callback = runner.fn();

const observer = new IntersectionObserver(callback);

observer.observe(node1);
observer.observe(node2);

io.enterNodes([node1, node2]);

expect(callback).toHaveBeenCalledTimes(1);

observer.unobserve(node1);
observer.unobserve(node2);

io.enterNodes([node1, node2]);

expect(callback).toHaveBeenCalledTimes(1);
});

it('handles multiple nodes correctly, enterAll', () => {
const node1 = document.createElement('div');
const node2 = document.createElement('div');
const callback = runner.fn();

const observer = new IntersectionObserver(callback);

observer.observe(node1);
observer.observe(node2);

io.enterAll();

expect(callback).toHaveBeenCalledTimes(1);

observer.unobserve(node1);
observer.unobserve(node2);

io.enterAll();

expect(callback).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 89011dc

Please sign in to comment.