|
| 1 | +/** |
| 2 | + * Group 3 tests: navigation buttons, year granularity, layout persistence |
| 3 | + * (保存布局), and the PNG / PDF export toolbar buttons. |
| 4 | + * |
| 5 | + * Conventions match the other interaction tests: innerWidth=1280 → |
| 6 | + * columnWidth 60, rowHeight 40. |
| 7 | + */ |
| 8 | +import React from 'react'; |
| 9 | +import { render, fireEvent, act } from '@testing-library/react'; |
| 10 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 11 | +import { GanttView, type GanttTask, type GanttLayout } from './GanttView'; |
| 12 | + |
| 13 | +beforeEach(() => { |
| 14 | + Object.defineProperty(window, 'innerWidth', { value: 1280, configurable: true }); |
| 15 | + window.localStorage.clear(); |
| 16 | +}); |
| 17 | + |
| 18 | +function makeTask(id: string, start: string, end: string, extra: Partial<GanttTask> = {}): GanttTask { |
| 19 | + return { id, title: `Task ${id}`, start: new Date(start), end: new Date(end), progress: 0, ...extra }; |
| 20 | +} |
| 21 | + |
| 22 | +const TASKS = () => [ |
| 23 | + makeTask('a', '2024-06-03T00:00:00.000Z', '2024-06-13T00:00:00.000Z', { progress: 50 }), |
| 24 | + makeTask('b', '2024-06-17T00:00:00.000Z', '2024-06-21T00:00:00.000Z'), |
| 25 | +]; |
| 26 | + |
| 27 | +function renderView(props: Partial<React.ComponentProps<typeof GanttView>> = {}) { |
| 28 | + return render( |
| 29 | + <div style={{ width: 1280, height: 600 }}> |
| 30 | + <GanttView |
| 31 | + tasks={TASKS()} |
| 32 | + startDate={new Date('2024-06-01T00:00:00.000Z')} |
| 33 | + endDate={new Date('2024-12-30T00:00:00.000Z')} |
| 34 | + {...props} |
| 35 | + /> |
| 36 | + </div> |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +describe('GanttView navigation buttons (导航)', () => { |
| 41 | + it('renders jump-to-today / this-week / this-month controls', () => { |
| 42 | + const { getByTestId } = renderView(); |
| 43 | + expect(getByTestId('gantt-jump-today')).toBeTruthy(); |
| 44 | + expect(getByTestId('gantt-jump-week')).toBeTruthy(); |
| 45 | + expect(getByTestId('gantt-jump-month')).toBeTruthy(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('this-week / this-month scroll the timeline horizontally', () => { |
| 49 | + const { getByTestId } = renderView(); |
| 50 | + const timeline = getByTestId('gantt-timeline') as HTMLElement; |
| 51 | + // jsdom has no layout, so scrollLeft is a plain settable number; assert the |
| 52 | + // handlers run without throwing and leave a finite scrollLeft. |
| 53 | + act(() => { fireEvent.click(getByTestId('gantt-jump-week')); }); |
| 54 | + expect(Number.isFinite(timeline.scrollLeft)).toBe(true); |
| 55 | + act(() => { fireEvent.click(getByTestId('gantt-jump-month')); }); |
| 56 | + expect(Number.isFinite(timeline.scrollLeft)).toBe(true); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +describe('GanttView year granularity (年刻度)', () => { |
| 61 | + it('exposes a year view-mode button and switches to it', () => { |
| 62 | + const { getByTestId } = renderView(); |
| 63 | + const yearBtn = getByTestId('gantt-view-mode-year'); |
| 64 | + expect(yearBtn).toBeTruthy(); |
| 65 | + act(() => { fireEvent.click(yearBtn); }); |
| 66 | + expect(yearBtn.getAttribute('aria-pressed')).toBe('true'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('seeds the year granularity from the viewMode prop', () => { |
| 70 | + const { getByTestId } = renderView({ viewMode: 'year' }); |
| 71 | + expect(getByTestId('gantt-view-mode-year').getAttribute('aria-pressed')).toBe('true'); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe('GanttView export buttons (导出 PNG / PDF)', () => { |
| 76 | + it('renders both the PNG and PDF export buttons', () => { |
| 77 | + const { getByTestId } = renderView(); |
| 78 | + expect(getByTestId('gantt-export-png')).toBeTruthy(); |
| 79 | + expect(getByTestId('gantt-export-pdf')).toBeTruthy(); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe('GanttView save layout (保存布局)', () => { |
| 84 | + it('hides the save-layout button without persistLayoutKey/onLayoutChange', () => { |
| 85 | + const { queryByTestId } = renderView(); |
| 86 | + expect(queryByTestId('gantt-save-layout')).toBeNull(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('shows the save-layout button when onLayoutChange is set', () => { |
| 90 | + const { getByTestId } = renderView({ onLayoutChange: () => {} }); |
| 91 | + expect(getByTestId('gantt-save-layout')).toBeTruthy(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('persists the current layout to localStorage under the key', () => { |
| 95 | + const { getByTestId } = renderView({ persistLayoutKey: 'proj1' }); |
| 96 | + act(() => { fireEvent.click(getByTestId('gantt-view-mode-month')); }); |
| 97 | + act(() => { fireEvent.click(getByTestId('gantt-save-layout')); }); |
| 98 | + const raw = window.localStorage.getItem('gantt-layout:proj1'); |
| 99 | + expect(raw).toBeTruthy(); |
| 100 | + const saved = JSON.parse(raw!) as GanttLayout; |
| 101 | + expect(saved.viewMode).toBe('month'); |
| 102 | + expect(saved.taskListCollapsed).toBe(false); |
| 103 | + }); |
| 104 | + |
| 105 | + it('calls onLayoutChange with the snapshot on save', () => { |
| 106 | + const onLayoutChange = vi.fn(); |
| 107 | + const { getByTestId } = renderView({ onLayoutChange }); |
| 108 | + act(() => { fireEvent.click(getByTestId('gantt-view-mode-quarter')); }); |
| 109 | + act(() => { fireEvent.click(getByTestId('gantt-save-layout')); }); |
| 110 | + expect(onLayoutChange).toHaveBeenCalledTimes(1); |
| 111 | + expect(onLayoutChange.mock.calls[0][0].viewMode).toBe('quarter'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('restores a persisted granularity on mount', () => { |
| 115 | + window.localStorage.setItem( |
| 116 | + 'gantt-layout:proj2', |
| 117 | + JSON.stringify({ viewMode: 'month', columnWidth: null, taskListCollapsed: false } satisfies GanttLayout) |
| 118 | + ); |
| 119 | + const { getByTestId } = renderView({ persistLayoutKey: 'proj2' }); |
| 120 | + expect(getByTestId('gantt-view-mode-month').getAttribute('aria-pressed')).toBe('true'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('lets the viewMode prop win over a persisted granularity', () => { |
| 124 | + window.localStorage.setItem( |
| 125 | + 'gantt-layout:proj3', |
| 126 | + JSON.stringify({ viewMode: 'month', columnWidth: null, taskListCollapsed: false } satisfies GanttLayout) |
| 127 | + ); |
| 128 | + const { getByTestId } = renderView({ persistLayoutKey: 'proj3', viewMode: 'week' }); |
| 129 | + expect(getByTestId('gantt-view-mode-week').getAttribute('aria-pressed')).toBe('true'); |
| 130 | + expect(getByTestId('gantt-view-mode-month').getAttribute('aria-pressed')).toBe('false'); |
| 131 | + }); |
| 132 | + |
| 133 | + it('ignores malformed persisted layout JSON', () => { |
| 134 | + window.localStorage.setItem('gantt-layout:proj4', '{not valid json'); |
| 135 | + const { getByTestId } = renderView({ persistLayoutKey: 'proj4' }); |
| 136 | + // Falls back to the default 'day' granularity without throwing. |
| 137 | + expect(getByTestId('gantt-view-mode-day').getAttribute('aria-pressed')).toBe('true'); |
| 138 | + }); |
| 139 | +}); |
0 commit comments