// @vitest-environment jsdom
import { cleanup, fireEvent, render, screen } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { useBrowserPathname } from './use-browser-pathname.js';
function Harness({ link = Automation }: { link?: React.ReactNode }) {
const pathname = useBrowserPathname();
return (
<>
{link}
{pathname}
>
);
}
afterEach(() => {
cleanup();
window.history.replaceState(null, '', '/');
vi.restoreAllMocks();
});
describe('useBrowserPathname', () => {
it('pushes eligible same-origin links and updates pathname without reloading', () => {
window.history.replaceState(null, '', '/fleet');
const pushState = vi.spyOn(window.history, 'pushState');
const scrollTo = vi.spyOn(window, 'scrollTo').mockImplementation(() => undefined);
render();
const link = screen.getByRole('link', { name: 'Automation' });
const accepted = fireEvent.click(link);
expect(accepted).toBe(false);
expect(pushState).toHaveBeenCalledWith(null, '', '/automation');
expect(screen.getByRole('main').textContent).toBe('/automation');
expect(document.activeElement).toBe(screen.getByRole('main'));
expect(scrollTo).toHaveBeenCalledWith({ top: 0, left: 0, behavior: 'auto' });
});
it('tracks browser history traversal and restores the page start', () => {
window.history.replaceState(null, '', '/automation');
const scrollTo = vi.spyOn(window, 'scrollTo').mockImplementation(() => undefined);
render();
window.history.replaceState(null, '', '/fleet');
fireEvent(window, new PopStateEvent('popstate'));
expect(screen.getByRole('main').textContent).toBe('/fleet');
expect(document.activeElement).toBe(screen.getByRole('main'));
expect(scrollTo).toHaveBeenCalledWith({ top: 0, left: 0, behavior: 'auto' });
});
it.each([
[
'target window',
Excluded
,
{},
],
[
'download',
Excluded
,
{},
],
['external origin', Excluded, {}],
['hash-only navigation', Excluded, {}],
['middle click', Excluded, { button: 1 }],
['meta click', Excluded, { metaKey: true }],
['control click', Excluded, { ctrlKey: true }],
['shift click', Excluded, { shiftKey: true }],
['alt click', Excluded, { altKey: true }],
])('leaves %s to the browser', (_label, link, eventInit) => {
window.history.replaceState(null, '', '/fleet');
const pushState = vi.spyOn(window.history, 'pushState');
render();
let hookPrevented = true;
const suppressBrowserNavigation = (event: MouseEvent) => {
hookPrevented = event.defaultPrevented;
event.preventDefault();
};
document.addEventListener('click', suppressBrowserNavigation, { once: true });
const event = new MouseEvent('click', { bubbles: true, cancelable: true, ...eventInit });
screen.getByRole('link', { name: 'Excluded' }).dispatchEvent(event);
expect(hookPrevented).toBe(false);
expect(pushState).not.toHaveBeenCalled();
expect(screen.getByRole('main').textContent).toBe('/fleet');
});
});