import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest' import { usePagination } from '#lib/composables' vi.mock('#imports', () => ({ persistedState: vi.fn().mockReturnValue({ localStorage: {}, }), })) describe('usePagination', () => { let scrollIntoViewMock: Mock beforeEach(() => { scrollIntoViewMock = vi.fn() global.document.querySelector = vi.fn().mockReturnValue({ scrollIntoView: scrollIntoViewMock, }) }) it('should scroll to the specified section if the element exists', () => { const { onLoadPage } = usePagination() const selector = '#section-id' onLoadPage(selector, false) expect(global.document.querySelector).toHaveBeenCalledWith(selector) expect(scrollIntoViewMock).toHaveBeenCalledWith({ behavior: 'smooth' }) }) it('should not attempt to scroll if the element does not exist', () => { global.document.querySelector = vi.fn().mockReturnValue(null) const { onLoadPage } = usePagination() const selector = '#non-existent-section' onLoadPage(selector, false) expect(global.document.querySelector).toHaveBeenCalledWith(selector) expect(scrollIntoViewMock).not.toHaveBeenCalled() }) it('should handle the paramInUrl flag without throwing errors', () => { const { onLoadPage } = usePagination() expect(() => { onLoadPage('#section-id', true) }).not.toThrow() // Placeholder for future URL handling logic }) it('should not throw errors when no scrollToSection is provided', () => { const { onLoadPage } = usePagination() expect(() => { onLoadPage('', false) }).not.toThrow() expect(global.document.querySelector).not.toHaveBeenCalled() expect(scrollIntoViewMock).not.toHaveBeenCalled() }) })