|
|
@@ -12,96 +12,86 @@ vi.mock('sonner', () => ({
|
|
|
}
|
|
|
}));
|
|
|
|
|
|
-vi.mock('@d8d/shared-ui-components/components/ui/button', () => ({
|
|
|
- Button: ({ children, ...props }: any) => (
|
|
|
- <button {...props}>{children}</button>
|
|
|
- )
|
|
|
-}));
|
|
|
-
|
|
|
-vi.mock('@d8d/shared-ui-components/components/ui/card', () => ({
|
|
|
- Card: ({ children }: any) => <div>{children}</div>,
|
|
|
- CardContent: ({ children }: any) => <div>{children}</div>,
|
|
|
- CardDescription: ({ children }: any) => <div>{children}</div>,
|
|
|
- CardHeader: ({ children }: any) => <div>{children}</div>,
|
|
|
- CardTitle: ({ children }: any) => <div>{children}</div>
|
|
|
-}));
|
|
|
-
|
|
|
-vi.mock('@d8d/shared-ui-components/components/ui/badge', () => ({
|
|
|
- Badge: ({ children, variant }: any) => (
|
|
|
- <span data-variant={variant}>{children}</span>
|
|
|
- )
|
|
|
-}));
|
|
|
-
|
|
|
-vi.mock('@d8d/shared-ui-components/components/ui/separator', () => ({
|
|
|
- Separator: () => <hr />
|
|
|
-}));
|
|
|
-
|
|
|
-vi.mock('@d8d/shared-ui-components/components/ui/tabs', () => ({
|
|
|
- Tabs: ({ children, value, onValueChange }: any) => (
|
|
|
- <div data-value={value}>
|
|
|
- {React.Children.map(children, child =>
|
|
|
- React.cloneElement(child, { value, onValueChange })
|
|
|
- )}
|
|
|
- </div>
|
|
|
- ),
|
|
|
- TabsContent: ({ children, value }: any) => (
|
|
|
- <div data-tab-content={value}>{children}</div>
|
|
|
- ),
|
|
|
- TabsList: ({ children }: any) => <div>{children}</div>,
|
|
|
- TabsTrigger: ({ children, value, disabled }: any) => (
|
|
|
- <button data-tab-trigger={value} disabled={disabled}>
|
|
|
- {children}
|
|
|
- </button>
|
|
|
- )
|
|
|
-}));
|
|
|
-
|
|
|
-vi.mock('@d8d/shared-ui-components/components/ui/dialog', () => ({
|
|
|
- Dialog: ({ children, open, onOpenChange }: any) => (
|
|
|
- open ? <div>{children}</div> : null
|
|
|
- ),
|
|
|
- DialogContent: ({ children }: any) => <div>{children}</div>,
|
|
|
- DialogDescription: ({ children }: any) => <div>{children}</div>,
|
|
|
- DialogFooter: ({ children }: any) => <div>{children}</div>,
|
|
|
- DialogHeader: ({ children }: any) => <div>{children}</div>,
|
|
|
- DialogTitle: ({ children }: any) => <div>{children}</div>
|
|
|
-}));
|
|
|
-
|
|
|
-vi.mock('@d8d/shared-ui-components/components/ui/table', () => ({
|
|
|
- Table: ({ children }: any) => <table>{children}</table>,
|
|
|
- TableBody: ({ children }: any) => <tbody>{children}</tbody>,
|
|
|
- TableCell: ({ children }: any) => <td>{children}</td>,
|
|
|
- TableHead: ({ children }: any) => <thead>{children}</thead>,
|
|
|
- TableHeader: ({ children }: any) => <tr>{children}</tr>,
|
|
|
- TableRow: ({ children }: any) => <tr>{children}</tr>
|
|
|
-}));
|
|
|
+// Mock lucide-react icons used by the component
|
|
|
+vi.mock('lucide-react', async () => {
|
|
|
+ const actual = await vi.importActual('lucide-react');
|
|
|
+ return {
|
|
|
+ ...actual,
|
|
|
+ Layers: () => <span data-testid="layers-icon">📚</span>,
|
|
|
+ Package: () => <span data-testid="package-icon">📦</span>,
|
|
|
+ Plus: () => <span data-testid="plus-icon">➕</span>,
|
|
|
+ Edit: () => <span data-testid="edit-icon">✏️</span>
|
|
|
+ };
|
|
|
+});
|
|
|
+
|
|
|
+// Mock axios to prevent actual network requests
|
|
|
+vi.mock('axios', async () => {
|
|
|
+ const actual = await vi.importActual('axios');
|
|
|
+ return {
|
|
|
+ ...actual,
|
|
|
+ request: vi.fn(() => Promise.resolve({
|
|
|
+ status: 200,
|
|
|
+ data: { data: [], total: 0 },
|
|
|
+ headers: {},
|
|
|
+ config: {},
|
|
|
+ statusText: 'OK'
|
|
|
+ }))
|
|
|
+ };
|
|
|
+});
|
|
|
+
|
|
|
+// 完整的mock响应对象
|
|
|
+const createMockResponse = (status: number, data?: any) => ({
|
|
|
+ status,
|
|
|
+ ok: status >= 200 && status < 300,
|
|
|
+ body: null,
|
|
|
+ bodyUsed: false,
|
|
|
+ statusText: status === 200 ? 'OK' : status === 201 ? 'Created' : status === 204 ? 'No Content' : 'Error',
|
|
|
+ headers: new Headers(),
|
|
|
+ url: '',
|
|
|
+ redirected: false,
|
|
|
+ type: 'basic' as ResponseType,
|
|
|
+ json: async () => data || {},
|
|
|
+ text: async () => '',
|
|
|
+ blob: async () => new Blob(),
|
|
|
+ arrayBuffer: async () => new ArrayBuffer(0),
|
|
|
+ formData: async () => new FormData(),
|
|
|
+ clone: function() { return this; }
|
|
|
+});
|
|
|
|
|
|
// Mock API client
|
|
|
-vi.mock('../src/api/goodsClient', () => ({
|
|
|
- goodsClientManager: {
|
|
|
- get: vi.fn(() => ({
|
|
|
- index: {
|
|
|
- $get: vi.fn(),
|
|
|
- $post: vi.fn()
|
|
|
+vi.mock('../src/api/goodsClient', () => {
|
|
|
+ const mockGoodsClient = {
|
|
|
+ index: {
|
|
|
+ $get: vi.fn(() => Promise.resolve(createMockResponse(200, { data: [], total: 0 }))),
|
|
|
+ $post: vi.fn(() => Promise.resolve(createMockResponse(201))),
|
|
|
+ },
|
|
|
+ ':id': {
|
|
|
+ children: {
|
|
|
+ $get: vi.fn(() => Promise.resolve(createMockResponse(200, { data: [], total: 0 }))),
|
|
|
},
|
|
|
- ':id': {
|
|
|
- children: {
|
|
|
- $get: vi.fn()
|
|
|
- },
|
|
|
- setAsParent: {
|
|
|
- $post: vi.fn()
|
|
|
- },
|
|
|
- parent: {
|
|
|
- $delete: vi.fn()
|
|
|
- },
|
|
|
- $delete: vi.fn(),
|
|
|
- $get: vi.fn()
|
|
|
+ setAsParent: {
|
|
|
+ $post: vi.fn(() => Promise.resolve(createMockResponse(200))),
|
|
|
},
|
|
|
- batchCreateChildren: {
|
|
|
- $post: vi.fn()
|
|
|
- }
|
|
|
- }))
|
|
|
- }
|
|
|
-}));
|
|
|
+ parent: {
|
|
|
+ $delete: vi.fn(() => Promise.resolve(createMockResponse(200))),
|
|
|
+ },
|
|
|
+ $delete: vi.fn(() => Promise.resolve(createMockResponse(200, { success: true }))),
|
|
|
+ $get: vi.fn(() => Promise.resolve(createMockResponse(200, { id: 123, spuId: 0, tenantId: 1 }))),
|
|
|
+ },
|
|
|
+ batchCreateChildren: {
|
|
|
+ $post: vi.fn(() => Promise.resolve(createMockResponse(200))),
|
|
|
+ },
|
|
|
+ };
|
|
|
+
|
|
|
+ const mockGoodsClientManager = {
|
|
|
+ get: vi.fn(() => mockGoodsClient),
|
|
|
+ };
|
|
|
+
|
|
|
+ return {
|
|
|
+ goodsClientManager: mockGoodsClientManager,
|
|
|
+ goodsClient: mockGoodsClient,
|
|
|
+ };
|
|
|
+});
|
|
|
|
|
|
import { GoodsParentChildPanel } from '../../src/components/GoodsParentChildPanel';
|
|
|
|
|
|
@@ -140,7 +130,8 @@ describe('GoodsParentChildPanel', () => {
|
|
|
|
|
|
expect(screen.getByText('父子商品管理')).toBeInTheDocument();
|
|
|
expect(screen.getByText('创建商品时配置父子关系')).toBeInTheDocument();
|
|
|
- expect(screen.getByText('普通商品')).toBeInTheDocument();
|
|
|
+ // 默认spuId为0,所以显示父商品状态
|
|
|
+ expect(screen.getByText('父商品')).toBeInTheDocument();
|
|
|
});
|
|
|
|
|
|
it('应该正确渲染编辑模式', () => {
|