// @ts-nocheck
import { describe, it, expect, jest } from '@jest/globals';
import { render, screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import { StaticLink } from './static-link';
// Note: jsdom's window.location is non-configurable, so we cannot use
// jest.spyOn or Object.defineProperty to mock the entire location object.
// However, window.location.href IS writable in jsdom, but navigation
// (setting href to a different page) is not implemented — see skipped tests below.
describe('StaticLink', () => {
beforeEach(() => {
// Reset location to a known state before each test
window.location.href = '/';
});
it('should render children', () => {
render(关于我们);
expect(screen.getByText('关于我们')).toBeInTheDocument();
});
it('should render with correct href', () => {
render(ERP);
const link = screen.getByText('ERP');
expect(link).toHaveAttribute('href', '/products/erp');
});
// ── 导航行为测试(jsdom 限制跳过) ──────────────────────────────
//
// 以下测试涉及 window.location.href 赋值导航,在 jsdom 中不可用:
// Error: Not implemented: navigation (except hash changes)
//
// 后期补全方案(任选其一):
// A. 升级至 @playwright/test 做 E2E 验证(推荐 — 可真实模拟浏览器导航)
// B. 在 jest 中 mock window.location(需升级 jsdom 或使用 jest-environment-jsdom 29+)
// C. 将 StaticLink 的导航逻辑抽取为独立函数,单独测试该函数
it.skip('should navigate to internal link on click', () => {
render(关于);
fireEvent.click(screen.getByText('关于'));
expect(window.location.href).toBe('/about');
});
it.skip('should handle hash link (same page scroll)', () => {
// hash 导航中 scrollIntoView 部分可在 jsdom 中测试,但需先 mock
// Element.prototype.scrollIntoView = jest.fn()
render(Hash Link);
fireEvent.click(screen.getByText('Hash Link'));
// 期望:window.location.href 被设置为 '/about#section'
// 但 jsdom 导航未实现,此断言无法通过
});
it('should add noopener noreferrer for external links', () => {
render(External);
const link = screen.getByText('External');
expect(link).toHaveAttribute('rel', 'noopener noreferrer');
});
it('should call custom onClick handler', () => {
const handleClick = jest.fn<() => void>();
render(Click);
fireEvent.click(screen.getByText('Click'));
expect(handleClick).toHaveBeenCalled();
});
it('should render with custom className', () => {
render(Home);
const link = screen.getByText('Home');
expect(link.className).toContain('custom-link');
});
it('should handle mailto links', () => {
render(Email);
const link = screen.getByText('Email');
expect(link).toHaveAttribute('href', 'mailto:test@test.com');
expect(link).toHaveAttribute('rel', 'noopener noreferrer');
});
});