Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to not call the callback from unobserved nodes #55

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
Loading