|
@@ -2,6 +2,7 @@ import React from 'react';
|
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
import { render, screen, fireEvent, waitFor, within } from '@testing-library/react';
|
|
import { render, screen, fireEvent, waitFor, within } from '@testing-library/react';
|
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
|
|
|
+import type { Hono } from 'hono';
|
|
|
import { GoodsManagement } from '../../src/components/GoodsManagement';
|
|
import { GoodsManagement } from '../../src/components/GoodsManagement';
|
|
|
import { goodsClient, goodsClientManager } from '../../src/api/goodsClient';
|
|
import { goodsClient, goodsClientManager } from '../../src/api/goodsClient';
|
|
|
|
|
|
|
@@ -24,16 +25,20 @@ const createMockResponse = (status: number, data?: any) => ({
|
|
|
clone: function() { return this; }
|
|
clone: function() { return this; }
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
-// Mock API client
|
|
|
|
|
-vi.mock('../../src/api/goodsClient', () => {
|
|
|
|
|
- const mockGoodsClient = {
|
|
|
|
|
|
|
+// 创建模拟的rpcClient函数(根据API模拟规范)
|
|
|
|
|
+// 符合测试策略文档的API模拟规范:统一模拟@d8d/shared-ui-components/utils/hc中的rpcClient函数
|
|
|
|
|
+const mockRpcClient = vi.hoisted(() => vi.fn((aptBaseUrl: string) => {
|
|
|
|
|
+ // 根据页面组件实际调用的RPC路径定义模拟端点
|
|
|
|
|
+ // 符合规范:支持Hono风格的$get、$post、$put、$delete方法
|
|
|
|
|
+ return {
|
|
|
index: {
|
|
index: {
|
|
|
$get: vi.fn(() => Promise.resolve(createMockResponse(200))),
|
|
$get: vi.fn(() => Promise.resolve(createMockResponse(200))),
|
|
|
$post: vi.fn(() => Promise.resolve(createMockResponse(201))),
|
|
$post: vi.fn(() => Promise.resolve(createMockResponse(201))),
|
|
|
},
|
|
},
|
|
|
':id': {
|
|
':id': {
|
|
|
|
|
+ $get: vi.fn(() => Promise.resolve(createMockResponse(200, { id: 1, spuId: 0, tenantId: 1 }))),
|
|
|
$put: vi.fn(() => Promise.resolve(createMockResponse(200))),
|
|
$put: vi.fn(() => Promise.resolve(createMockResponse(200))),
|
|
|
- $delete: vi.fn(() => Promise.resolve(createMockResponse(204))),
|
|
|
|
|
|
|
+ $delete: vi.fn(() => Promise.resolve(createMockResponse(204))), // 商品删除API,支持子商品删除
|
|
|
// 故事006.002新增的父子商品管理API
|
|
// 故事006.002新增的父子商品管理API
|
|
|
children: {
|
|
children: {
|
|
|
$get: vi.fn(() => Promise.resolve(createMockResponse(200, { data: [], total: 0 }))),
|
|
$get: vi.fn(() => Promise.resolve(createMockResponse(200, { data: [], total: 0 }))),
|
|
@@ -50,14 +55,28 @@ vi.mock('../../src/api/goodsClient', () => {
|
|
|
$post: vi.fn(() => Promise.resolve(createMockResponse(200))),
|
|
$post: vi.fn(() => Promise.resolve(createMockResponse(200))),
|
|
|
},
|
|
},
|
|
|
};
|
|
};
|
|
|
|
|
+}));
|
|
|
|
|
+
|
|
|
|
|
+// 模拟共享UI组件包中的rpcClient函数(统一模拟点)
|
|
|
|
|
+// 核心API模拟规范:统一拦截所有API调用,支持跨UI包集成测试
|
|
|
|
|
+vi.mock('@d8d/shared-ui-components/utils/hc', () => ({
|
|
|
|
|
+ rpcClient: mockRpcClient
|
|
|
|
|
+}));
|
|
|
|
|
+
|
|
|
|
|
+// Mock API client(保持向后兼容性,但实际使用上面的统一模拟)
|
|
|
|
|
+// 符合API模拟规范:统一模拟rpcClient函数,客户端管理器使用模拟的rpcClient
|
|
|
|
|
+vi.mock('../../src/api/goodsClient', () => {
|
|
|
|
|
+ // 获取模拟的客户端实例(通过mockRpcClient创建)
|
|
|
|
|
+ // 符合测试策略文档的规范:直接通过模拟的rpcClient函数创建客户端
|
|
|
|
|
+ const mockClient = mockRpcClient('/');
|
|
|
|
|
|
|
|
const mockGoodsClientManager = {
|
|
const mockGoodsClientManager = {
|
|
|
- get: vi.fn(() => mockGoodsClient),
|
|
|
|
|
|
|
+ get: vi.fn(() => mockClient),
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
return {
|
|
|
goodsClientManager: mockGoodsClientManager,
|
|
goodsClientManager: mockGoodsClientManager,
|
|
|
- goodsClient: mockGoodsClient,
|
|
|
|
|
|
|
+ goodsClient: mockClient,
|
|
|
};
|
|
};
|
|
|
});
|
|
});
|
|
|
|
|
|