PositionTabs.test.tsx
2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { describe, it, expect, vi } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import { PositionTabs } from '../components/PositionTabs';
import { createMockPositionTab, MOCK_FIXTURES } from '../mocks/data';
describe('岗位 Tab 测试', () => {
it('默认选中第一个岗位', () => {
const tabs = [createMockPositionTab(), createMockPositionTab({ tab_id: 'tab-002' })];
render(<PositionTabs tabs={tabs} currentIndex={0} onChange={vi.fn()} />);
const buttons = screen.getAllByRole('button');
expect(buttons[0]).toHaveClass('bg-primary-container');
expect(buttons[0]).toHaveClass('text-on-primary');
});
it('标题区与 Tab 区域的上下节奏应保持一致', () => {
const tabs = [createMockPositionTab(), createMockPositionTab({ tab_id: 'tab-002' })];
render(<PositionTabs tabs={tabs} currentIndex={0} onChange={vi.fn()} />);
const titleBlock = screen.getByTestId('position-tabs-title');
const tabsBlock = screen.getByTestId('position-tabs-list');
expect(titleBlock.className).toContain('mb-stack-md');
expect(titleBlock.className).toContain('tablet:mb-stack-lg');
expect(tabsBlock.className).toContain('mb-0');
});
it('切换岗位应调用 onChange', () => {
const handleChange = vi.fn();
const tabs = [createMockPositionTab(), createMockPositionTab({ tab_id: 'tab-002' })];
render(<PositionTabs tabs={tabs} currentIndex={0} onChange={handleChange} />);
const buttons = screen.getAllByRole('button');
fireEvent.click(buttons[1]!);
expect(handleChange).toHaveBeenCalledWith(1);
});
it('无岗位时不应展示', () => {
render(<PositionTabs tabs={[]} currentIndex={0} onChange={vi.fn()} />);
expect(screen.queryByRole('button')).not.toBeInTheDocument();
});
it('Tab 过多时应可横向滚动', () => {
const tabs = MOCK_FIXTURES.manyPositionTabs.tabs;
render(<PositionTabs tabs={tabs} currentIndex={0} onChange={vi.fn()} />);
const buttons = screen.getAllByRole('button');
expect(buttons.length).toBe(10);
});
it('切换岗位内容应同步', () => {
const tabs = [
createMockPositionTab({ tab_id: 'tab-001', position_name: '岗位一' }),
createMockPositionTab({ tab_id: 'tab-002', position_name: '岗位二' }),
];
const { rerender } = render(<PositionTabs tabs={tabs} currentIndex={0} onChange={vi.fn()} />);
expect(screen.getByText('岗位一')).toBeInTheDocument();
rerender(<PositionTabs tabs={tabs} currentIndex={1} onChange={vi.fn()} />);
const buttons = screen.getAllByRole('button');
expect(buttons[1]).toHaveClass('bg-primary-container');
});
});