diff --git a/package.json b/package.json index f004662..9d81b2a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/mocks/intersection-observer.test.ts b/src/mocks/intersection-observer.test.ts new file mode 100644 index 0000000..eed8bfd --- /dev/null +++ b/src/mocks/intersection-observer.test.ts @@ -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); + }); +});