|
|
@@ -5,10 +5,44 @@ import { vi } from 'vitest';
|
|
|
import { toast } from 'sonner';
|
|
|
import { BatchSpecCreator } from '../../src/components/BatchSpecCreator';
|
|
|
|
|
|
+// 辅助函数:等待父商品数据加载完成
|
|
|
+const waitForParentGoodsLoaded = async () => {
|
|
|
+ // 等待加载状态消失
|
|
|
+ await waitFor(() => {
|
|
|
+ expect(screen.queryByText('正在加载父商品信息...')).not.toBeInTheDocument();
|
|
|
+ });
|
|
|
+ // 等待父商品信息显示
|
|
|
+ await waitFor(() => {
|
|
|
+ expect(screen.getByDisplayValue('1')).toBeInTheDocument(); // 父商品ID
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
// Mock the goodsClientManager
|
|
|
vi.mock('../../src/api/goodsClient', () => ({
|
|
|
goodsClientManager: {
|
|
|
get: vi.fn(() => ({
|
|
|
+ ':id': {
|
|
|
+ $get: vi.fn(({ param }: { param: { id: number } }) => {
|
|
|
+ // 模拟获取父商品详情API
|
|
|
+ return Promise.resolve({
|
|
|
+ status: 200,
|
|
|
+ json: () => Promise.resolve({
|
|
|
+ id: param.id,
|
|
|
+ name: '测试父商品',
|
|
|
+ categoryId1: 1,
|
|
|
+ categoryId2: 2,
|
|
|
+ categoryId3: 3,
|
|
|
+ goodsType: 1,
|
|
|
+ supplierId: 1,
|
|
|
+ merchantId: 1,
|
|
|
+ price: 100,
|
|
|
+ costPrice: 80,
|
|
|
+ stock: 100,
|
|
|
+ state: 1
|
|
|
+ })
|
|
|
+ });
|
|
|
+ })
|
|
|
+ },
|
|
|
index: {
|
|
|
$post: vi.fn(() => Promise.resolve({
|
|
|
status: 201,
|
|
|
@@ -31,6 +65,11 @@ const queryClient = new QueryClient({
|
|
|
defaultOptions: {
|
|
|
queries: {
|
|
|
retry: false,
|
|
|
+ staleTime: 0,
|
|
|
+ cacheTime: 0,
|
|
|
+ refetchOnWindowFocus: false,
|
|
|
+ refetchOnMount: true,
|
|
|
+ refetchOnReconnect: false,
|
|
|
},
|
|
|
mutations: {
|
|
|
retry: false,
|
|
|
@@ -57,7 +96,7 @@ describe('BatchSpecCreator', () => {
|
|
|
vi.clearAllMocks();
|
|
|
});
|
|
|
|
|
|
- it('应该正确渲染组件', () => {
|
|
|
+ it('应该正确渲染组件', async () => {
|
|
|
render(
|
|
|
<Wrapper>
|
|
|
<BatchSpecCreator {...defaultProps} />
|
|
|
@@ -68,17 +107,31 @@ describe('BatchSpecCreator', () => {
|
|
|
expect(screen.getByText('为父商品 "父商品" 批量创建多个子商品规格')).toBeInTheDocument();
|
|
|
expect(screen.getByText('父商品信息')).toBeInTheDocument();
|
|
|
expect(screen.getByText('规格列表')).toBeInTheDocument();
|
|
|
- expect(screen.getByDisplayValue('1')).toBeInTheDocument(); // 父商品ID
|
|
|
+
|
|
|
+ // 等待父商品数据加载完成
|
|
|
+ await waitFor(() => {
|
|
|
+ expect(screen.getByDisplayValue('1')).toBeInTheDocument(); // 父商品ID
|
|
|
+ });
|
|
|
+
|
|
|
expect(screen.getByDisplayValue('父商品')).toBeInTheDocument(); // 父商品名称
|
|
|
+ expect(screen.getByDisplayValue('1')).toBeInTheDocument(); // 一级分类ID
|
|
|
+ expect(screen.getByDisplayValue('2')).toBeInTheDocument(); // 二级分类ID
|
|
|
+ expect(screen.getByDisplayValue('3')).toBeInTheDocument(); // 三级分类ID
|
|
|
+ expect(screen.getByDisplayValue('实物产品')).toBeInTheDocument(); // 商品类型
|
|
|
});
|
|
|
|
|
|
- it('应该显示初始的2个规格行', () => {
|
|
|
+ it('应该显示初始的2个规格行', async () => {
|
|
|
render(
|
|
|
<Wrapper>
|
|
|
<BatchSpecCreator {...defaultProps} />
|
|
|
</Wrapper>
|
|
|
);
|
|
|
|
|
|
+ // 等待父商品数据加载完成
|
|
|
+ await waitFor(() => {
|
|
|
+ expect(screen.getByDisplayValue('1')).toBeInTheDocument(); // 父商品ID
|
|
|
+ });
|
|
|
+
|
|
|
const nameInputs = screen.getAllByPlaceholderText('例如:红色、64GB、大号');
|
|
|
expect(nameInputs).toHaveLength(2);
|
|
|
|
|
|
@@ -89,13 +142,15 @@ describe('BatchSpecCreator', () => {
|
|
|
expect(stockInputs).toHaveLength(2);
|
|
|
});
|
|
|
|
|
|
- it('应该添加新的规格行', () => {
|
|
|
+ it('应该添加新的规格行', async () => {
|
|
|
render(
|
|
|
<Wrapper>
|
|
|
<BatchSpecCreator {...defaultProps} />
|
|
|
</Wrapper>
|
|
|
);
|
|
|
|
|
|
+ await waitForParentGoodsLoaded();
|
|
|
+
|
|
|
const addButton = screen.getByText('添加规格');
|
|
|
fireEvent.click(addButton);
|
|
|
|