2026-06-21 22:11:31 +02:00
|
|
|
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
|
|
|
import { renderHook, act } from '@testing-library/react';
|
|
|
|
|
import { useHashTab } from '@/hooks/useHashTab';
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
window.location.hash = '';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
window.location.hash = '';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('useHashTab', () => {
|
|
|
|
|
it('returns the default id when hash is empty', () => {
|
|
|
|
|
const { result } = renderHook(() => useHashTab('schedule'));
|
|
|
|
|
expect(result.current[0]).toBe('schedule');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('reads the hash on mount', () => {
|
|
|
|
|
window.location.hash = 'simulations';
|
|
|
|
|
const { result } = renderHook(() => useHashTab('schedule'));
|
|
|
|
|
expect(result.current[0]).toBe('simulations');
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-21 22:20:51 +02:00
|
|
|
it('navigate() updates activeId and sets hash via replaceState (no history entry)', () => {
|
2026-06-21 22:11:31 +02:00
|
|
|
const { result } = renderHook(() => useHashTab('schedule'));
|
|
|
|
|
act(() => {
|
|
|
|
|
result.current[1]('description');
|
|
|
|
|
});
|
|
|
|
|
expect(result.current[0]).toBe('description');
|
|
|
|
|
expect(window.location.hash).toBe('#description');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('falls back to defaultId when hash becomes empty', () => {
|
|
|
|
|
window.location.hash = 'simulations';
|
|
|
|
|
const { result } = renderHook(() => useHashTab('schedule'));
|
|
|
|
|
act(() => {
|
|
|
|
|
window.location.hash = '';
|
|
|
|
|
window.dispatchEvent(new HashChangeEvent('hashchange'));
|
|
|
|
|
});
|
|
|
|
|
expect(result.current[0]).toBe('schedule');
|
|
|
|
|
});
|
|
|
|
|
});
|