|
|
@@ -0,0 +1,156 @@
|
|
|
+import { describe, it, expect } from 'vitest';
|
|
|
+import {
|
|
|
+ ApiResponse,
|
|
|
+ Pagination,
|
|
|
+ PaginatedResponse,
|
|
|
+ QueryParams,
|
|
|
+ EnableStatus,
|
|
|
+ DeleteStatus,
|
|
|
+ EnableStatusNameMap,
|
|
|
+ DeleteStatusNameMap,
|
|
|
+ AuthContextType,
|
|
|
+ GlobalConfig
|
|
|
+} from '../../src/index.js';
|
|
|
+
|
|
|
+describe('共享类型', () => {
|
|
|
+ describe('ApiResponse', () => {
|
|
|
+ it('应该创建成功的 ApiResponse', () => {
|
|
|
+ const response: ApiResponse<string> = {
|
|
|
+ success: true,
|
|
|
+ data: 'test data',
|
|
|
+ message: 'Operation successful'
|
|
|
+ };
|
|
|
+
|
|
|
+ expect(response.success).toBe(true);
|
|
|
+ expect(response.data).toBe('test data');
|
|
|
+ expect(response.message).toBe('Operation successful');
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该创建错误的 ApiResponse', () => {
|
|
|
+ const response: ApiResponse = {
|
|
|
+ success: false,
|
|
|
+ error: 'Something went wrong'
|
|
|
+ };
|
|
|
+
|
|
|
+ expect(response.success).toBe(false);
|
|
|
+ expect(response.error).toBe('Something went wrong');
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('分页', () => {
|
|
|
+ it('应该创建分页对象', () => {
|
|
|
+ const pagination: Pagination = {
|
|
|
+ page: 1,
|
|
|
+ pageSize: 10,
|
|
|
+ total: 100,
|
|
|
+ totalPages: 10
|
|
|
+ };
|
|
|
+
|
|
|
+ expect(pagination.page).toBe(1);
|
|
|
+ expect(pagination.pageSize).toBe(10);
|
|
|
+ expect(pagination.total).toBe(100);
|
|
|
+ expect(pagination.totalPages).toBe(10);
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('分页响应', () => {
|
|
|
+ it('应该创建分页响应', () => {
|
|
|
+ const response: PaginatedResponse<string> = {
|
|
|
+ success: true,
|
|
|
+ data: ['item1', 'item2'],
|
|
|
+ pagination: {
|
|
|
+ page: 1,
|
|
|
+ pageSize: 10,
|
|
|
+ total: 20,
|
|
|
+ totalPages: 2
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ expect(response.success).toBe(true);
|
|
|
+ expect(response.data).toEqual(['item1', 'item2']);
|
|
|
+ expect(response.pagination.page).toBe(1);
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('查询参数', () => {
|
|
|
+ it('应该创建带分页的查询参数', () => {
|
|
|
+ const params: QueryParams = {
|
|
|
+ page: 1,
|
|
|
+ pageSize: 10,
|
|
|
+ sortBy: 'name',
|
|
|
+ sortOrder: 'ASC'
|
|
|
+ };
|
|
|
+
|
|
|
+ expect(params.page).toBe(1);
|
|
|
+ expect(params.pageSize).toBe(10);
|
|
|
+ expect(params.sortBy).toBe('name');
|
|
|
+ expect(params.sortOrder).toBe('ASC');
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该创建带额外过滤器的查询参数', () => {
|
|
|
+ const params: QueryParams = {
|
|
|
+ page: 1,
|
|
|
+ status: 'active',
|
|
|
+ category: 'tech'
|
|
|
+ };
|
|
|
+
|
|
|
+ expect(params.page).toBe(1);
|
|
|
+ expect(params.status).toBe('active');
|
|
|
+ expect(params.category).toBe('tech');
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('启用状态', () => {
|
|
|
+ it('应该有正确的枚举值', () => {
|
|
|
+ expect(EnableStatus.DISABLED).toBe(0);
|
|
|
+ expect(EnableStatus.ENABLED).toBe(1);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该有正确的名称映射', () => {
|
|
|
+ expect(EnableStatusNameMap[EnableStatus.DISABLED]).toBe('禁用');
|
|
|
+ expect(EnableStatusNameMap[EnableStatus.ENABLED]).toBe('启用');
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('删除状态', () => {
|
|
|
+ it('应该有正确的枚举值', () => {
|
|
|
+ expect(DeleteStatus.NOT_DELETED).toBe(0);
|
|
|
+ expect(DeleteStatus.DELETED).toBe(1);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('应该有正确的名称映射', () => {
|
|
|
+ expect(DeleteStatusNameMap[DeleteStatus.NOT_DELETED]).toBe('未删除');
|
|
|
+ expect(DeleteStatusNameMap[DeleteStatus.DELETED]).toBe('已删除');
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('认证上下文类型', () => {
|
|
|
+ it('应该定义认证上下文类型结构', () => {
|
|
|
+ const authContext: AuthContextType<{ id: string; name: string }> = {
|
|
|
+ user: { id: '1', name: 'Test User' },
|
|
|
+ token: 'test-token',
|
|
|
+ login: async () => {},
|
|
|
+ logout: async () => {},
|
|
|
+ isAuthenticated: true,
|
|
|
+ isLoading: false
|
|
|
+ };
|
|
|
+
|
|
|
+ expect(authContext.user?.id).toBe('1');
|
|
|
+ expect(authContext.token).toBe('test-token');
|
|
|
+ expect(authContext.isAuthenticated).toBe(true);
|
|
|
+ expect(authContext.isLoading).toBe(false);
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('全局配置', () => {
|
|
|
+ it('应该定义全局配置结构', () => {
|
|
|
+ const config: GlobalConfig = {
|
|
|
+ OSS_BASE_URL: 'https://oss.example.com',
|
|
|
+ APP_NAME: 'Test App'
|
|
|
+ };
|
|
|
+
|
|
|
+ expect(config.OSS_BASE_URL).toBe('https://oss.example.com');
|
|
|
+ expect(config.APP_NAME).toBe('Test App');
|
|
|
+ });
|
|
|
+ });
|
|
|
+});
|