|
@@ -1,212 +0,0 @@
|
|
|
-/**
|
|
|
|
|
- * areaClient 测试mock工具
|
|
|
|
|
- * 用于在测试中模拟areaClient API调用
|
|
|
|
|
- */
|
|
|
|
|
-
|
|
|
|
|
-import { vi } from 'vitest';
|
|
|
|
|
-import { mockAllAreas, filterAreas, getPaginatedData, type AreaTestData } from './mockAreaData';
|
|
|
|
|
-
|
|
|
|
|
-// 完整的mock响应对象 - 按照用户UI包规范
|
|
|
|
|
-export 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的areaClient
|
|
|
|
|
-export const createMockAreaClient = (customData?: AreaTestData[]) => {
|
|
|
|
|
- const areas = customData || mockAllAreas;
|
|
|
|
|
-
|
|
|
|
|
- // 模拟 $get 方法
|
|
|
|
|
- const mockGet = vi.fn(async (options?: { query?: any }) => {
|
|
|
|
|
- const { query = {} } = options || {};
|
|
|
|
|
- const { page = 1, pageSize = 10, filters = '{}', sortBy = 'id', sortOrder = 'ASC' } = query;
|
|
|
|
|
-
|
|
|
|
|
- // 解析filters
|
|
|
|
|
- let parsedFilters = {};
|
|
|
|
|
- try {
|
|
|
|
|
- parsedFilters = JSON.parse(filters);
|
|
|
|
|
- } catch {
|
|
|
|
|
- parsedFilters = {};
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 筛选数据
|
|
|
|
|
- let filteredAreas = [...areas];
|
|
|
|
|
-
|
|
|
|
|
- // 应用筛选条件
|
|
|
|
|
- if (parsedFilters) {
|
|
|
|
|
- filteredAreas = filterAreas(areas, parsedFilters);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 排序
|
|
|
|
|
- filteredAreas.sort((a, b) => {
|
|
|
|
|
- const aValue = a[sortBy as keyof AreaTestData];
|
|
|
|
|
- const bValue = b[sortBy as keyof AreaTestData];
|
|
|
|
|
-
|
|
|
|
|
- if (typeof aValue === 'number' && typeof bValue === 'number') {
|
|
|
|
|
- return sortOrder === 'ASC' ? aValue - bValue : bValue - aValue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (typeof aValue === 'string' && typeof bValue === 'string') {
|
|
|
|
|
- return sortOrder === 'ASC'
|
|
|
|
|
- ? aValue.localeCompare(bValue)
|
|
|
|
|
- : bValue.localeCompare(aValue);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return 0;
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- // 分页
|
|
|
|
|
- const paginated = getPaginatedData(filteredAreas, page, pageSize);
|
|
|
|
|
-
|
|
|
|
|
- return Promise.resolve(createMockResponse(200, paginated));
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- // 模拟 $post 方法
|
|
|
|
|
- const mockPost = vi.fn(async (options?: { json?: any }) => {
|
|
|
|
|
- const { json = {} } = options || {};
|
|
|
|
|
- const newId = Math.max(...areas.map(a => a.id)) + 1;
|
|
|
|
|
- const newArea: AreaTestData = {
|
|
|
|
|
- id: newId,
|
|
|
|
|
- name: json.name || '新地区',
|
|
|
|
|
- level: json.level || 1,
|
|
|
|
|
- parentId: json.parentId,
|
|
|
|
|
- isDisabled: json.isDisabled || 0,
|
|
|
|
|
- sort: json.sort || 0,
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- areas.push(newArea);
|
|
|
|
|
-
|
|
|
|
|
- return Promise.resolve(createMockResponse(201, newArea));
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- // 模拟 $put 方法
|
|
|
|
|
- const mockPut = vi.fn(async (options?: { param?: any; json?: any }) => {
|
|
|
|
|
- const { param = {}, json = {} } = options || {};
|
|
|
|
|
- const id = param.id ? parseInt(param.id) : 0;
|
|
|
|
|
-
|
|
|
|
|
- const index = areas.findIndex(area => area.id === id);
|
|
|
|
|
- if (index === -1) {
|
|
|
|
|
- return Promise.resolve(createMockResponse(404, { error: '地区不存在' }));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- areas[index] = { ...areas[index], ...json };
|
|
|
|
|
-
|
|
|
|
|
- return Promise.resolve(createMockResponse(200, areas[index]));
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- // 模拟 $delete 方法
|
|
|
|
|
- const mockDelete = vi.fn(async (options?: { param?: any }) => {
|
|
|
|
|
- const { param = {} } = options || {};
|
|
|
|
|
- const id = param.id ? parseInt(param.id) : 0;
|
|
|
|
|
-
|
|
|
|
|
- const index = areas.findIndex(area => area.id === id);
|
|
|
|
|
- if (index === -1) {
|
|
|
|
|
- return Promise.resolve(createMockResponse(404, { error: '地区不存在' }));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- areas.splice(index, 1);
|
|
|
|
|
-
|
|
|
|
|
- return Promise.resolve(createMockResponse(204));
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- return {
|
|
|
|
|
- index: {
|
|
|
|
|
- $get: mockGet,
|
|
|
|
|
- $post: mockPost,
|
|
|
|
|
- },
|
|
|
|
|
- ':id': {
|
|
|
|
|
- $put: mockPut,
|
|
|
|
|
- $delete: mockDelete,
|
|
|
|
|
- },
|
|
|
|
|
- };
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
-// 创建mock的areaClientManager
|
|
|
|
|
-export const createMockAreaClientManager = (customData?: AreaTestData[]) => {
|
|
|
|
|
- const mockClient = createMockAreaClient(customData);
|
|
|
|
|
-
|
|
|
|
|
- return {
|
|
|
|
|
- getInstance: vi.fn(() => ({
|
|
|
|
|
- get: vi.fn(() => mockClient),
|
|
|
|
|
- reset: vi.fn(),
|
|
|
|
|
- })),
|
|
|
|
|
- get: vi.fn(() => mockClient),
|
|
|
|
|
- reset: vi.fn(),
|
|
|
|
|
- };
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
-// 完整的mock配置(用于vi.mock)
|
|
|
|
|
-export const createAreaClientMock = (customData?: AreaTestData[]) => {
|
|
|
|
|
- const mockClient = createMockAreaClient(customData);
|
|
|
|
|
- const mockManager = createMockAreaClientManager(customData);
|
|
|
|
|
-
|
|
|
|
|
- return {
|
|
|
|
|
- areaClient: mockClient,
|
|
|
|
|
- areaClientManager: mockManager,
|
|
|
|
|
- };
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
-// 预设的mock配置(快速使用)
|
|
|
|
|
-export const presetMocks = {
|
|
|
|
|
- // 基本成功mock
|
|
|
|
|
- success: () => createAreaClientMock(),
|
|
|
|
|
-
|
|
|
|
|
- // 空数据mock
|
|
|
|
|
- empty: () => createAreaClientMock([]),
|
|
|
|
|
-
|
|
|
|
|
- // 错误mock
|
|
|
|
|
- error: (errorType: 'network' | 'server' | 'notFound' = 'server') => {
|
|
|
|
|
- const mockClient = {
|
|
|
|
|
- index: {
|
|
|
|
|
- $get: vi.fn(() => {
|
|
|
|
|
- if (errorType === 'network') {
|
|
|
|
|
- return Promise.reject(new Error('Network Error'));
|
|
|
|
|
- }
|
|
|
|
|
- return Promise.resolve(createMockResponse(500, { error: '服务器错误' }));
|
|
|
|
|
- }),
|
|
|
|
|
- $post: vi.fn(() => Promise.resolve(createMockResponse(500, { error: '创建失败' }))),
|
|
|
|
|
- },
|
|
|
|
|
- ':id': {
|
|
|
|
|
- $put: vi.fn(() => {
|
|
|
|
|
- if (errorType === 'notFound') {
|
|
|
|
|
- return Promise.resolve(createMockResponse(404, { error: '地区不存在' }));
|
|
|
|
|
- }
|
|
|
|
|
- return Promise.resolve(createMockResponse(500, { error: '更新失败' }));
|
|
|
|
|
- }),
|
|
|
|
|
- $delete: vi.fn(() => {
|
|
|
|
|
- if (errorType === 'notFound') {
|
|
|
|
|
- return Promise.resolve(createMockResponse(404, { error: '地区不存在' }));
|
|
|
|
|
- }
|
|
|
|
|
- return Promise.resolve(createMockResponse(500, { error: '删除失败' }));
|
|
|
|
|
- }),
|
|
|
|
|
- },
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- const mockManager = {
|
|
|
|
|
- getInstance: vi.fn(() => ({
|
|
|
|
|
- get: vi.fn(() => mockClient),
|
|
|
|
|
- reset: vi.fn(),
|
|
|
|
|
- })),
|
|
|
|
|
- get: vi.fn(() => mockClient),
|
|
|
|
|
- reset: vi.fn(),
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- return {
|
|
|
|
|
- areaClient: mockClient,
|
|
|
|
|
- areaClientManager: mockManager,
|
|
|
|
|
- };
|
|
|
|
|
- },
|
|
|
|
|
-};
|
|
|