Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState, useEffect, useRef, useMemo } from 'react';
import { Card, Alert } from 'antd';
import * as echarts from 'echarts';
import { format, subMonths, subYears } from 'date-fns';
import {
useQueueChartData,
QueueChartParams,
Expand Down Expand Up @@ -45,8 +46,8 @@ const QueueOverview: React.FC = () => {
// 计算实际的日期范围
const getActualDateRange = (): { begin_date: string; end_date: string } => {
const now = new Date();
const endDate = now.toISOString().split('T')[0];
const endDate = format(now, 'yyyy-MM-dd');

if (dateRange === 'custom' && customDateRange) {
return {
begin_date: customDateRange.start,
Expand All @@ -57,32 +58,32 @@ const QueueOverview: React.FC = () => {
let beginDate: Date;
switch (dateRange) {
case '1M':
beginDate = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate());
beginDate = subMonths(now, 1);
break;
case '3M':
beginDate = new Date(now.getFullYear(), now.getMonth() - 3, now.getDate());
beginDate = subMonths(now, 3);
break;
case '6M':
beginDate = new Date(now.getFullYear(), now.getMonth() - 6, now.getDate());
beginDate = subMonths(now, 6);
break;
case '1Y':
beginDate = new Date(now.getFullYear() - 1, now.getMonth(), now.getDate());
beginDate = subYears(now, 1);
break;
case '3Y':
beginDate = new Date(now.getFullYear() - 3, now.getMonth(), now.getDate());
beginDate = subYears(now, 3);
break;
case '5Y':
beginDate = new Date(now.getFullYear() - 5, now.getMonth(), now.getDate());
beginDate = subYears(now, 5);
break;
case 'Since 2000':
beginDate = new Date(2000, 0, 1);
break;
default:
beginDate = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate());
beginDate = subMonths(now, 1);
}

return {
begin_date: beginDate.toISOString().split('T')[0],
begin_date: format(beginDate, 'yyyy-MM-dd'),
end_date: endDate,
};
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import React from 'react';
import { fireEvent, render, screen } from '@testing-library/react';
import EcosystemQueue from './EcosystemEvaluationMonitor/QueueTab/QueueOverview';
import TpcQueue from './SelectionEvaluationTPCMonitor/QueueTab/QueueOverview';
import { useQueueChartData } from '@modules/system-admin/hooks/useQueueChartApi';
import { useTpcQueueChartData } from '@modules/system-admin/hooks/useTpcQueueChartApi';

jest.mock('@modules/system-admin/hooks/useQueueChartApi', () => ({
useQueueChartData: jest.fn(),
}));
jest.mock('@modules/system-admin/hooks/useTpcQueueChartApi', () => ({
useTpcQueueChartData: jest.fn(),
}));
jest.mock('echarts', () => ({}));
jest.mock('antd', () => ({
Card: ({ title, children }) => (
<section>
{title}
{children}
</section>
),
Alert: () => null,
}));
jest.mock('@common/components/DateRangePicker', () => ({
__esModule: true,
default: ({ onChange }) => (
<div>
{['1M', '3M', '6M', '1Y', 'Since 2000'].map((range) => (
<button key={range} onClick={() => onChange(range)}>
{range}
</button>
))}
<button
onClick={() =>
onChange('custom', { start: '2025-01-02', end: '2025-02-03' })
}
>
custom
</button>
</div>
),
}));

describe.each([
{ name: 'ecosystem', Component: EcosystemQueue, query: useQueueChartData },
{ name: 'TPC', Component: TpcQueue, query: useTpcQueueChartData },
])('$name queue calendar range', ({ Component, query }) => {
beforeEach(() => {
jest.useFakeTimers();
jest.clearAllMocks();
(query as jest.Mock).mockReturnValue({
data: [],
isLoading: true,
error: null,
});
});
afterEach(() => jest.useRealTimers());

function expectDates(begin: string, end: string) {
expect(query).toHaveBeenLastCalledWith(
expect.objectContaining({ begin_date: begin, end_date: end })
);
}

it.each([4, 23])('keeps local calendar dates at hour %s', (hour) => {
jest.setSystemTime(new Date(2026, 2, 15, hour));
render(<Component />);
expectDates('2026-02-15', '2026-03-15');
});

it('clamps a one-month range at the end of February', () => {
jest.setSystemTime(new Date(2026, 2, 31, 12));
render(<Component />);
expectDates('2026-02-28', '2026-03-31');
});

it('clamps a six-month range instead of overflowing into May', () => {
jest.setSystemTime(new Date(2026, 9, 31, 12));
render(<Component />);
fireEvent.click(screen.getByRole('button', { name: '6M' }));
expectDates('2026-04-30', '2026-10-31');
});

it('clamps leap day when selecting the previous year', () => {
jest.setSystemTime(new Date(2024, 1, 29, 12));
render(<Component />);
fireEvent.click(screen.getByRole('button', { name: '1Y' }));
expectDates('2023-02-28', '2024-02-29');
});

it('keeps the fixed history start date', () => {
jest.setSystemTime(new Date(2026, 2, 15, 12));
render(<Component />);
fireEvent.click(screen.getByRole('button', { name: 'Since 2000' }));
expectDates('2000-01-01', '2026-03-15');
});

it('preserves explicit custom dates', () => {
jest.setSystemTime(new Date(2026, 2, 15, 12));
render(<Component />);
fireEvent.click(screen.getByRole('button', { name: 'custom' }));
expectDates('2025-01-02', '2025-02-03');
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState, useEffect, useRef, useMemo } from 'react';
import { Card, Alert } from 'antd';
import * as echarts from 'echarts';
import { format, subMonths, subYears } from 'date-fns';
import {
useTpcQueueChartData,
TpcQueueChartParams,
Expand Down Expand Up @@ -38,8 +39,8 @@ const QueueOverview: React.FC = () => {
// 计算实际的日期范围
const getActualDateRange = (): { begin_date: string; end_date: string } => {
const now = new Date();
const endDate = now.toISOString().split('T')[0];
const endDate = format(now, 'yyyy-MM-dd');

if (dateRange === 'custom' && customDateRange) {
return {
begin_date: customDateRange.start,
Expand All @@ -50,32 +51,32 @@ const QueueOverview: React.FC = () => {
let beginDate: Date;
switch (dateRange) {
case '1M':
beginDate = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate());
beginDate = subMonths(now, 1);
break;
case '3M':
beginDate = new Date(now.getFullYear(), now.getMonth() - 3, now.getDate());
beginDate = subMonths(now, 3);
break;
case '6M':
beginDate = new Date(now.getFullYear(), now.getMonth() - 6, now.getDate());
beginDate = subMonths(now, 6);
break;
case '1Y':
beginDate = new Date(now.getFullYear() - 1, now.getMonth(), now.getDate());
beginDate = subYears(now, 1);
break;
case '3Y':
beginDate = new Date(now.getFullYear() - 3, now.getMonth(), now.getDate());
beginDate = subYears(now, 3);
break;
case '5Y':
beginDate = new Date(now.getFullYear() - 5, now.getMonth(), now.getDate());
beginDate = subYears(now, 5);
break;
case 'Since 2000':
beginDate = new Date(2000, 0, 1);
break;
default:
beginDate = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate());
beginDate = subMonths(now, 1);
}

return {
begin_date: beginDate.toISOString().split('T')[0],
begin_date: format(beginDate, 'yyyy-MM-dd'),
end_date: endDate,
};
};
Expand Down Expand Up @@ -243,7 +244,13 @@ const QueueOverview: React.FC = () => {
return (
<Card
title={
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
}}
>
<span>TPC队列趋势分析</span>
<CommonDateRangePicker
value={dateRange}
Expand All @@ -269,7 +276,13 @@ const QueueOverview: React.FC = () => {
return (
<Card
title={
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
}}
>
<span>TPC队列趋势分析</span>
<CommonDateRangePicker
value={dateRange}
Expand Down