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
@@ -0,0 +1,50 @@
jest.mock('next-i18next', () => ({
useTranslation: () => ({ t: (key: string) => key, i18n: { language: 'zh' } }),
}));
jest.mock('axios', () => ({
__esModule: true,
default: jest.fn(),
}));
jest.mock('./DescriptionContent', () => ({
__esModule: true,
default: () => null,
}));
jest.mock('./ResponsesViewer', () => ({
__esModule: true,
default: () => null,
}));
jest.mock('./ParamsTableWithForm', () => ({
__esModule: true,
default: jest.fn(() => null),
}));

import { render } from '@testing-library/react';
import axios from 'axios';
import EndpointTab from './EndpointTab';
import ParamsTableWithForm from './ParamsTableWithForm';

const endpoint = {
id: 'ep-1',
method: 'GET',
path: '/api/v2/metrics/community',
summary: 'Community metrics',
description: '',
parameters: [],
responses: {},
};

describe('EndpointTab', () => {
it('passes the selected data source origin to the test form', () => {
// 数据源选择器选中的 origin 是测试请求的目标——此前 ParamsTableWithForm
// 只收到相对路径,axios 解析到页面 origin 而非选中的数据源。
render(<EndpointTab endpoint={endpoint} />);
expect(ParamsTableWithForm).toHaveBeenCalledWith(
expect.objectContaining({
baseUrl: 'https://oss-compass.isrc.ac.cn',
path: '/api/v2/metrics/community',
method: 'GET',
}),
expect.anything()
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const EndpointTab = ({ endpoint }) => {
key={endpoint?.id}
method={endpoint.method}
path={endpoint.path}
baseUrl={baseUrl}
params={endpoint.parameters.map((p) => ({
...p,
key: p.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ const constructUrl = (path: string, values: any) => {
const ParamsTableWithForm = ({
method,
path,
baseUrl,
params,
}: {
method: string;
path: string;
baseUrl?: string;
params: ApiParameter[];
}) => {
const { t, i18n } = useTranslation();
Expand Down Expand Up @@ -134,7 +136,6 @@ const ParamsTableWithForm = ({
};

const handleTest = async (values: any) => {
console.log(values);
setLoading(true);
setTestResult(null);

Expand All @@ -153,21 +154,15 @@ const ParamsTableWithForm = ({
param.type
);

// 添加调试日志,显示参数处理前后的变化
if (originalValue !== processedValues[param.name]) {
console.log(`参数 ${param.name} (${param.type}) 处理: `, {
原始值: originalValue,
处理后: processedValues[param.name],
类型: typeof processedValues[param.name],
});
}
}
});

try {
// 数据源选择器选中的 origin 才是测试请求的目标——此前用的是相对路径,
// axios 解析到页面 origin 而非选中的数据源。
const config = {
method: method.toLowerCase(),
url: constructUrl(path, processedValues), // 使用处理过的 processedValues
url: constructUrl((baseUrl || '') + path, processedValues),
data: method === 'POST' ? processedValues : null, // 使用处理过的 processedValues
params: method === 'GET' ? processedValues : null, // 使用处理过的 processedValues
};
Expand Down