chore: replace all cn comments of fe to en version by volc api (#320)
This commit is contained in:
@@ -32,37 +32,37 @@ describe('Project Calc Permission', () => {
|
||||
spaceType: SpaceType.Personal,
|
||||
};
|
||||
|
||||
// 个人空间应该有查看权限
|
||||
// Personal space should have viewing permission
|
||||
expect(calcPermission(EProjectPermission.View, params)).toBe(true);
|
||||
|
||||
// 个人空间应该有编辑信息权限
|
||||
// Personal space should have permission to edit information
|
||||
expect(calcPermission(EProjectPermission.EDIT_INFO, params)).toBe(true);
|
||||
|
||||
// 个人空间应该有删除权限
|
||||
// Personal space should have deletion permission
|
||||
expect(calcPermission(EProjectPermission.DELETE, params)).toBe(true);
|
||||
|
||||
// 个人空间应该有发布权限
|
||||
// Personal space should have publishing permission
|
||||
expect(calcPermission(EProjectPermission.PUBLISH, params)).toBe(true);
|
||||
|
||||
// 个人空间应该有创建资源权限
|
||||
// Personal space should have permission to create resources
|
||||
expect(calcPermission(EProjectPermission.CREATE_RESOURCE, params)).toBe(
|
||||
true,
|
||||
);
|
||||
|
||||
// 个人空间应该有复制资源权限
|
||||
// Personal space should have permission to copy resources
|
||||
expect(calcPermission(EProjectPermission.COPY_RESOURCE, params)).toBe(
|
||||
true,
|
||||
);
|
||||
|
||||
// 个人空间应该有复制项目权限
|
||||
// Personal space should have permission to copy items
|
||||
expect(calcPermission(EProjectPermission.COPY, params)).toBe(true);
|
||||
|
||||
// 个人空间应该有测试运行插件权限
|
||||
// Personal space should have test run plug-in permissions
|
||||
expect(calcPermission(EProjectPermission.TEST_RUN_PLUGIN, params)).toBe(
|
||||
true,
|
||||
);
|
||||
|
||||
// 个人空间应该有测试运行工作流权限
|
||||
// Personal space should have test run workflow permissions
|
||||
expect(calcPermission(EProjectPermission.TEST_RUN_WORKFLOW, params)).toBe(
|
||||
true,
|
||||
);
|
||||
@@ -75,12 +75,12 @@ describe('Project Calc Permission', () => {
|
||||
spaceType: SpaceType.Personal,
|
||||
};
|
||||
|
||||
// 个人空间不应该有添加协作者权限
|
||||
// Personal Spaces should not have Add Collaborators permissions
|
||||
expect(calcPermission(EProjectPermission.ADD_COLLABORATOR, params)).toBe(
|
||||
false,
|
||||
);
|
||||
|
||||
// 个人空间不应该有删除协作者权限
|
||||
// Personal space should not have permission to delete collaborators
|
||||
expect(
|
||||
calcPermission(EProjectPermission.DELETE_COLLABORATOR, params),
|
||||
).toBe(false);
|
||||
@@ -95,47 +95,47 @@ describe('Project Calc Permission', () => {
|
||||
spaceType: SpaceType.Team,
|
||||
};
|
||||
|
||||
// 项目所有者应该有查看权限
|
||||
// The project owner should have viewing rights
|
||||
expect(calcPermission(EProjectPermission.View, params)).toBe(true);
|
||||
|
||||
// 项目所有者应该有编辑信息权限
|
||||
// The project owner should have permission to edit the information
|
||||
expect(calcPermission(EProjectPermission.EDIT_INFO, params)).toBe(true);
|
||||
|
||||
// 项目所有者应该有删除权限
|
||||
// The project owner should have delete permissions
|
||||
expect(calcPermission(EProjectPermission.DELETE, params)).toBe(true);
|
||||
|
||||
// 项目所有者应该有发布权限
|
||||
// The project owner should have publishing rights
|
||||
expect(calcPermission(EProjectPermission.PUBLISH, params)).toBe(true);
|
||||
|
||||
// 项目所有者应该有创建资源权限
|
||||
// The project owner should have the Create Resource permission
|
||||
expect(calcPermission(EProjectPermission.CREATE_RESOURCE, params)).toBe(
|
||||
true,
|
||||
);
|
||||
|
||||
// 项目所有者应该有复制资源权限
|
||||
// The project owner should have permission to copy the resource
|
||||
expect(calcPermission(EProjectPermission.COPY_RESOURCE, params)).toBe(
|
||||
true,
|
||||
);
|
||||
|
||||
// 项目所有者应该有复制项目权限
|
||||
// The project owner should have permission to copy the project
|
||||
expect(calcPermission(EProjectPermission.COPY, params)).toBe(true);
|
||||
|
||||
// 项目所有者应该有测试运行插件权限
|
||||
// The project owner should have test run plug-in permissions
|
||||
expect(calcPermission(EProjectPermission.TEST_RUN_PLUGIN, params)).toBe(
|
||||
true,
|
||||
);
|
||||
|
||||
// 项目所有者应该有测试运行工作流权限
|
||||
// The project owner should have test run workflow permissions
|
||||
expect(calcPermission(EProjectPermission.TEST_RUN_WORKFLOW, params)).toBe(
|
||||
true,
|
||||
);
|
||||
|
||||
// 项目所有者应该有添加协作者权限
|
||||
// The project owner should have permission to add collaborators
|
||||
expect(calcPermission(EProjectPermission.ADD_COLLABORATOR, params)).toBe(
|
||||
true,
|
||||
);
|
||||
|
||||
// 项目所有者应该有删除协作者权限
|
||||
// The project owner should have the Delete Collaborator permission
|
||||
expect(
|
||||
calcPermission(EProjectPermission.DELETE_COLLABORATOR, params),
|
||||
).toBe(true);
|
||||
@@ -148,47 +148,47 @@ describe('Project Calc Permission', () => {
|
||||
spaceType: SpaceType.Team,
|
||||
};
|
||||
|
||||
// 项目编辑者应该有查看权限
|
||||
// Project editors should have viewing rights
|
||||
expect(calcPermission(EProjectPermission.View, params)).toBe(true);
|
||||
|
||||
// 项目编辑者应该有编辑信息权限
|
||||
// Project editors should have permission to edit information
|
||||
expect(calcPermission(EProjectPermission.EDIT_INFO, params)).toBe(true);
|
||||
|
||||
// 项目编辑者应该有创建资源权限
|
||||
// Project editors should have the Create Resource permission
|
||||
expect(calcPermission(EProjectPermission.CREATE_RESOURCE, params)).toBe(
|
||||
true,
|
||||
);
|
||||
|
||||
// 项目编辑者应该有复制资源权限
|
||||
// Project editors should have permission to copy resources
|
||||
expect(calcPermission(EProjectPermission.COPY_RESOURCE, params)).toBe(
|
||||
true,
|
||||
);
|
||||
|
||||
// 项目编辑者应该有复制项目权限
|
||||
// The project editor should have permission to copy the project
|
||||
expect(calcPermission(EProjectPermission.COPY, params)).toBe(true);
|
||||
|
||||
// 项目编辑者应该有测试运行插件权限
|
||||
// Project editors should have test run plug-in permissions
|
||||
expect(calcPermission(EProjectPermission.TEST_RUN_PLUGIN, params)).toBe(
|
||||
true,
|
||||
);
|
||||
|
||||
// 项目编辑者应该有测试运行工作流权限
|
||||
// The project editor should have test run workflow permissions
|
||||
expect(calcPermission(EProjectPermission.TEST_RUN_WORKFLOW, params)).toBe(
|
||||
true,
|
||||
);
|
||||
|
||||
// 项目编辑者应该有添加协作者权限
|
||||
// Project editors should have Add Collaborators permission
|
||||
expect(calcPermission(EProjectPermission.ADD_COLLABORATOR, params)).toBe(
|
||||
true,
|
||||
);
|
||||
|
||||
// 项目编辑者不应该有删除权限
|
||||
// Project editors should not have delete permissions
|
||||
expect(calcPermission(EProjectPermission.DELETE, params)).toBe(false);
|
||||
|
||||
// 项目编辑者不应该有发布权限
|
||||
// Project editors should not have permission to publish
|
||||
expect(calcPermission(EProjectPermission.PUBLISH, params)).toBe(false);
|
||||
|
||||
// 项目编辑者不应该有删除协作者权限
|
||||
// Project editors should not have permission to delete collaborators
|
||||
expect(
|
||||
calcPermission(EProjectPermission.DELETE_COLLABORATOR, params),
|
||||
).toBe(false);
|
||||
@@ -203,47 +203,47 @@ describe('Project Calc Permission', () => {
|
||||
spaceType: SpaceType.Team,
|
||||
};
|
||||
|
||||
// 空间成员应该有查看权限
|
||||
// Space members should have viewing rights
|
||||
expect(calcPermission(EProjectPermission.View, params)).toBe(true);
|
||||
|
||||
// 空间成员应该有复制项目权限
|
||||
// Space members should have permission to copy items
|
||||
expect(calcPermission(EProjectPermission.COPY, params)).toBe(true);
|
||||
|
||||
// 空间成员应该有测试运行工作流权限
|
||||
// Space members should have test run workflow permissions
|
||||
expect(calcPermission(EProjectPermission.TEST_RUN_WORKFLOW, params)).toBe(
|
||||
true,
|
||||
);
|
||||
|
||||
// 空间成员不应该有编辑信息权限
|
||||
// Space members should not have permission to edit information
|
||||
expect(calcPermission(EProjectPermission.EDIT_INFO, params)).toBe(false);
|
||||
|
||||
// 空间成员不应该有删除权限
|
||||
// Space members should not have delete permissions
|
||||
expect(calcPermission(EProjectPermission.DELETE, params)).toBe(false);
|
||||
|
||||
// 空间成员不应该有发布权限
|
||||
// Space members should not have publishing privileges
|
||||
expect(calcPermission(EProjectPermission.PUBLISH, params)).toBe(false);
|
||||
|
||||
// 空间成员不应该有创建资源权限
|
||||
// Space members should not have permission to create resources
|
||||
expect(calcPermission(EProjectPermission.CREATE_RESOURCE, params)).toBe(
|
||||
false,
|
||||
);
|
||||
|
||||
// 空间成员不应该有复制资源权限
|
||||
// Space members should not have permission to copy resources
|
||||
expect(calcPermission(EProjectPermission.COPY_RESOURCE, params)).toBe(
|
||||
false,
|
||||
);
|
||||
|
||||
// 空间成员不应该有测试运行插件权限
|
||||
// Space members should not have test run plug-in permissions
|
||||
expect(calcPermission(EProjectPermission.TEST_RUN_PLUGIN, params)).toBe(
|
||||
false,
|
||||
);
|
||||
|
||||
// 空间成员不应该有添加协作者权限
|
||||
// Space members should not have Add Collaborator permissions
|
||||
expect(calcPermission(EProjectPermission.ADD_COLLABORATOR, params)).toBe(
|
||||
false,
|
||||
);
|
||||
|
||||
// 空间成员不应该有删除协作者权限
|
||||
// Space members should not have permission to delete collaborators
|
||||
expect(
|
||||
calcPermission(EProjectPermission.DELETE_COLLABORATOR, params),
|
||||
).toBe(false);
|
||||
@@ -256,13 +256,13 @@ describe('Project Calc Permission', () => {
|
||||
spaceType: SpaceType.Team,
|
||||
};
|
||||
|
||||
// 空间所有者应该有查看权限
|
||||
// Space owners should have viewing rights
|
||||
expect(calcPermission(EProjectPermission.View, params)).toBe(true);
|
||||
|
||||
// 空间所有者应该有复制项目权限
|
||||
// The space owner should have permission to copy items
|
||||
expect(calcPermission(EProjectPermission.COPY, params)).toBe(true);
|
||||
|
||||
// 空间所有者应该有测试运行工作流权限
|
||||
// Space owners should have test run workflow permissions
|
||||
expect(calcPermission(EProjectPermission.TEST_RUN_WORKFLOW, params)).toBe(
|
||||
true,
|
||||
);
|
||||
@@ -275,13 +275,13 @@ describe('Project Calc Permission', () => {
|
||||
spaceType: SpaceType.Team,
|
||||
};
|
||||
|
||||
// 空间管理员应该有查看权限
|
||||
// The space administrator should have viewing rights
|
||||
expect(calcPermission(EProjectPermission.View, params)).toBe(true);
|
||||
|
||||
// 空间管理员应该有复制项目权限
|
||||
// The space administrator should have permission to copy items
|
||||
expect(calcPermission(EProjectPermission.COPY, params)).toBe(true);
|
||||
|
||||
// 空间管理员应该有测试运行工作流权限
|
||||
// The space administrator should have test run workflow permissions
|
||||
expect(calcPermission(EProjectPermission.TEST_RUN_WORKFLOW, params)).toBe(
|
||||
true,
|
||||
);
|
||||
@@ -294,7 +294,7 @@ describe('Project Calc Permission', () => {
|
||||
spaceType: SpaceType.Team,
|
||||
};
|
||||
|
||||
// 默认角色不应该有任何权限
|
||||
// The default role should not have any permissions
|
||||
expect(calcPermission(EProjectPermission.View, params)).toBe(false);
|
||||
expect(calcPermission(EProjectPermission.EDIT_INFO, params)).toBe(false);
|
||||
expect(calcPermission(EProjectPermission.DELETE, params)).toBe(false);
|
||||
@@ -329,7 +329,7 @@ describe('Project Calc Permission', () => {
|
||||
spaceType: SpaceType.Team,
|
||||
};
|
||||
|
||||
// 应该有项目编辑者的所有权限
|
||||
// Should have all the permissions of the project editor
|
||||
expect(calcPermission(EProjectPermission.View, params)).toBe(true);
|
||||
expect(calcPermission(EProjectPermission.EDIT_INFO, params)).toBe(true);
|
||||
expect(calcPermission(EProjectPermission.CREATE_RESOURCE, params)).toBe(
|
||||
@@ -349,7 +349,7 @@ describe('Project Calc Permission', () => {
|
||||
true,
|
||||
);
|
||||
|
||||
// 不应该有项目编辑者没有的权限
|
||||
// There should be no permissions that the project editor does not have
|
||||
expect(calcPermission(EProjectPermission.DELETE, params)).toBe(false);
|
||||
expect(calcPermission(EProjectPermission.PUBLISH, params)).toBe(false);
|
||||
expect(
|
||||
@@ -364,7 +364,7 @@ describe('Project Calc Permission', () => {
|
||||
spaceType: SpaceType.Team,
|
||||
};
|
||||
|
||||
// 没有角色不应该有任何权限
|
||||
// No role should have no permissions
|
||||
expect(calcPermission(EProjectPermission.View, params)).toBe(false);
|
||||
expect(calcPermission(EProjectPermission.EDIT_INFO, params)).toBe(false);
|
||||
expect(calcPermission(EProjectPermission.DELETE, params)).toBe(false);
|
||||
|
||||
@@ -24,28 +24,28 @@ import {
|
||||
describe('Project Constants', () => {
|
||||
describe('ProjectRoleType', () => {
|
||||
it('应该定义所有必要的角色类型', () => {
|
||||
// 验证所有角色类型都已定义
|
||||
// Verify that all role types are defined
|
||||
expect(ProjectRoleType.Owner).toBeDefined();
|
||||
expect(ProjectRoleType.Editor).toBeDefined();
|
||||
|
||||
// 验证角色类型的值
|
||||
// Validate the value of the role type
|
||||
expect(ProjectRoleType.Owner).toBe('owner');
|
||||
expect(ProjectRoleType.Editor).toBe('editor');
|
||||
});
|
||||
|
||||
it('应该包含正确数量的角色类型', () => {
|
||||
// 验证角色类型的数量
|
||||
// Number of validation role types
|
||||
const roleTypeCount = Object.keys(ProjectRoleType).filter(key =>
|
||||
isNaN(Number(key)),
|
||||
).length;
|
||||
|
||||
expect(roleTypeCount).toBe(2); // Owner 和 Editor
|
||||
expect(roleTypeCount).toBe(2); // Owner and Editor
|
||||
});
|
||||
});
|
||||
|
||||
describe('EProjectPermission', () => {
|
||||
it('应该定义所有必要的权限点', () => {
|
||||
// 验证所有权限点都已定义
|
||||
// Verify that all permission spots are defined
|
||||
expect(EProjectPermission.View).toBeDefined();
|
||||
expect(EProjectPermission.EDIT_INFO).toBeDefined();
|
||||
expect(EProjectPermission.DELETE).toBeDefined();
|
||||
@@ -60,17 +60,17 @@ describe('Project Constants', () => {
|
||||
});
|
||||
|
||||
it('应该为每个权限点分配唯一的值', () => {
|
||||
// 创建一个集合来存储所有权限点的值
|
||||
// Create a collection to store the values of all permission spots
|
||||
const permissionValues = new Set();
|
||||
|
||||
// 获取所有权限点的值
|
||||
// Get values for all permission spots
|
||||
Object.values(EProjectPermission)
|
||||
.filter(value => typeof value === 'number')
|
||||
.forEach(value => {
|
||||
permissionValues.add(value);
|
||||
});
|
||||
|
||||
// 验证权限点的数量与唯一值的数量相同
|
||||
// The number of validation permission spots is the same as the number of unique values
|
||||
const numericKeys = Object.keys(EProjectPermission).filter(
|
||||
key => !isNaN(Number(key)),
|
||||
).length;
|
||||
@@ -79,12 +79,12 @@ describe('Project Constants', () => {
|
||||
});
|
||||
|
||||
it('应该包含正确数量的权限点', () => {
|
||||
// 验证权限点的数量
|
||||
// Number of permission spots verified
|
||||
const permissionCount = Object.keys(EProjectPermission).filter(key =>
|
||||
isNaN(Number(key)),
|
||||
).length;
|
||||
|
||||
expect(permissionCount).toBe(12); // 11个权限点
|
||||
expect(permissionCount).toBe(12); // 11 permission spots
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -143,20 +143,20 @@ describe('Project Auth Store', () => {
|
||||
const projectId = 'test-project-1';
|
||||
const roles = [ProjectRoleType.Owner];
|
||||
|
||||
// 设置初始数据
|
||||
// Set initial data
|
||||
await act(() => {
|
||||
result.current.setRoles(projectId, roles);
|
||||
result.current.setIsReady(projectId, true);
|
||||
});
|
||||
|
||||
// 验证数据已设置
|
||||
// Verify that the data is set
|
||||
expect(result.current.roles[projectId]).toEqual(roles);
|
||||
expect(result.current.isReady[projectId]).toBe(true);
|
||||
|
||||
// 销毁数据
|
||||
// Destroy data
|
||||
result.current.destory(projectId);
|
||||
|
||||
// 验证数据已清除
|
||||
// Verify that the data has been cleared
|
||||
expect(result.current.roles[projectId]).toEqual([]);
|
||||
expect(result.current.isReady[projectId]).toBe(false);
|
||||
});
|
||||
@@ -171,16 +171,16 @@ describe('Project Auth Store', () => {
|
||||
const roles1 = [ProjectRoleType.Owner];
|
||||
const roles2 = [ProjectRoleType.Editor];
|
||||
|
||||
// 设置初始数据
|
||||
// Set initial data
|
||||
result.current.setRoles(projectId1, roles1);
|
||||
result.current.setRoles(projectId2, roles2);
|
||||
result.current.setIsReady(projectId1, true);
|
||||
result.current.setIsReady(projectId2, true);
|
||||
|
||||
// 销毁项目1的数据
|
||||
// Destruction of data for item 1
|
||||
result.current.destory(projectId1);
|
||||
|
||||
// 验证项目1的数据已清除,项目2的数据保持不变
|
||||
// Verify that the data for item 1 has been cleared and that the data for item 2 remains unchanged
|
||||
expect(result.current.roles[projectId1]).toEqual([]);
|
||||
expect(result.current.isReady[projectId1]).toBe(false);
|
||||
expect(result.current.roles[projectId2]).toEqual(roles2);
|
||||
|
||||
@@ -17,15 +17,15 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
|
||||
// 模拟 React 的 useEffect
|
||||
// The useEffect of React
|
||||
const cleanupFns = new Map();
|
||||
vi.mock('react', () => ({
|
||||
useEffect: vi.fn((fn, deps) => {
|
||||
// 执行 effect 函数并获取清理函数
|
||||
// Execute the effect function and get the cleanup function
|
||||
const cleanup = fn();
|
||||
// 存储清理函数,以便在 unmount 时调用
|
||||
// Store the cleanup function to call when unmounted
|
||||
cleanupFns.set(fn, cleanup);
|
||||
// 返回清理函数
|
||||
// Return cleanup function
|
||||
return cleanup;
|
||||
}),
|
||||
}));
|
||||
@@ -33,7 +33,7 @@ vi.mock('react', () => ({
|
||||
import { useDestoryProject } from '../../src/project/use-destory-project';
|
||||
import { useProjectAuthStore } from '../../src/project/store';
|
||||
|
||||
// 模拟 useProjectAuthStore
|
||||
// emulation useProjectAuthStore
|
||||
vi.mock('../../src/project/store', () => {
|
||||
const destorySpy = vi.fn();
|
||||
return {
|
||||
@@ -41,19 +41,19 @@ vi.mock('../../src/project/store', () => {
|
||||
};
|
||||
});
|
||||
|
||||
// 创建一个包装函数,确保在 unmount 时调用清理函数
|
||||
// Create a wrapper function to ensure that the cleanup function is called when unmounted
|
||||
function renderHookWithCleanup(callback, options = {}) {
|
||||
const result = renderHook(callback, options);
|
||||
const originalUnmount = result.unmount;
|
||||
|
||||
result.unmount = () => {
|
||||
// 调用所有清理函数
|
||||
// Call all cleanup functions
|
||||
cleanupFns.forEach(cleanup => {
|
||||
if (typeof cleanup === 'function') {
|
||||
cleanup();
|
||||
}
|
||||
});
|
||||
// 调用原始的 unmount
|
||||
// Call the original unmount
|
||||
originalUnmount();
|
||||
};
|
||||
|
||||
@@ -70,21 +70,21 @@ describe('useDestoryProject', () => {
|
||||
const projectId = 'test-project-id';
|
||||
const destorySpy = vi.fn();
|
||||
|
||||
// 模拟 useProjectAuthStore 返回 destorySpy
|
||||
// Emulate useProjectAuthStore returns destorySpy
|
||||
(useProjectAuthStore as any).mockReturnValue(destorySpy);
|
||||
|
||||
// 渲染 hook
|
||||
// Render hook
|
||||
const { unmount } = renderHookWithCleanup(() =>
|
||||
useDestoryProject(projectId),
|
||||
);
|
||||
|
||||
// 验证初始状态下 destory 未被调用
|
||||
// Verify that destory is not called in the initial state
|
||||
expect(destorySpy).not.toHaveBeenCalled();
|
||||
|
||||
// 卸载组件
|
||||
// uninstall components
|
||||
unmount();
|
||||
|
||||
// 验证 destory 被调用,且参数正确
|
||||
// Verify that destory is called and the parameters are correct
|
||||
expect(destorySpy).toHaveBeenCalledTimes(1);
|
||||
expect(destorySpy).toHaveBeenCalledWith(projectId);
|
||||
});
|
||||
@@ -93,18 +93,18 @@ describe('useDestoryProject', () => {
|
||||
const projectId1 = 'test-project-id-1';
|
||||
const destorySpy = vi.fn();
|
||||
|
||||
// 模拟 useProjectAuthStore 返回 destorySpy
|
||||
// Emulate useProjectAuthStore returns destorySpy
|
||||
(useProjectAuthStore as any).mockReturnValue(destorySpy);
|
||||
|
||||
// 渲染 hook
|
||||
// Render hook
|
||||
const { unmount } = renderHookWithCleanup(() =>
|
||||
useDestoryProject(projectId1),
|
||||
);
|
||||
|
||||
// 卸载组件
|
||||
// uninstall components
|
||||
unmount();
|
||||
|
||||
// 验证 destory 被调用,且参数为 projectId1
|
||||
// Verify that destory is called with the parameter projectId1.
|
||||
expect(destorySpy).toHaveBeenCalledTimes(1);
|
||||
expect(destorySpy).toHaveBeenCalledWith(projectId1);
|
||||
});
|
||||
@@ -113,22 +113,22 @@ describe('useDestoryProject', () => {
|
||||
const projectId2 = 'test-project-id-2';
|
||||
const destorySpy = vi.fn();
|
||||
|
||||
// 清除之前的所有模拟和清理函数
|
||||
// Clear all previous simulation and cleanup functions
|
||||
vi.clearAllMocks();
|
||||
cleanupFns.clear();
|
||||
|
||||
// 模拟 useProjectAuthStore 返回 destorySpy
|
||||
// Emulate useProjectAuthStore returns destorySpy
|
||||
(useProjectAuthStore as any).mockReturnValue(destorySpy);
|
||||
|
||||
// 渲染 hook
|
||||
// Render hook
|
||||
const { unmount } = renderHookWithCleanup(() =>
|
||||
useDestoryProject(projectId2),
|
||||
);
|
||||
|
||||
// 卸载组件
|
||||
// uninstall components
|
||||
unmount();
|
||||
|
||||
// 验证 destory 被调用,且参数为 projectId2
|
||||
// Verify that destory is called with the parameter projectId2.
|
||||
expect(destorySpy).toHaveBeenCalledTimes(1);
|
||||
expect(destorySpy).toHaveBeenCalledWith(projectId2);
|
||||
});
|
||||
|
||||
@@ -29,7 +29,7 @@ import {
|
||||
} from '../../src/project/constants';
|
||||
import { calcPermission } from '../../src/project/calc-permission';
|
||||
|
||||
// 模拟依赖
|
||||
// simulated dependency
|
||||
vi.mock('@coze-arch/foundation-sdk', () => ({
|
||||
useSpace: vi.fn(),
|
||||
}));
|
||||
@@ -54,94 +54,94 @@ describe('useProjectAuth', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
|
||||
// 模拟 useSpace 返回空间信息
|
||||
// Simulating useSpace returns spatial information
|
||||
(useSpace as any).mockReturnValue({
|
||||
space_type: SpaceType.Team,
|
||||
});
|
||||
|
||||
// 模拟 useSpaceRole 返回空间角色
|
||||
// Simulate useSpaceRole Return Space Role
|
||||
(useSpaceRole as any).mockReturnValue([SpaceRoleType.Member]);
|
||||
|
||||
// 模拟 useProjectRole 返回项目角色
|
||||
// Simulate useProjectRole Return project role
|
||||
(useProjectRole as any).mockReturnValue([ProjectRoleType.Editor]);
|
||||
|
||||
// 模拟 calcPermission 返回权限结果
|
||||
// Simulate calcPermission return permission result
|
||||
(calcPermission as any).mockReturnValue(true);
|
||||
});
|
||||
|
||||
it('应该调用 calcPermission 并返回正确的权限结果', () => {
|
||||
// 渲染 hook
|
||||
// Render hook
|
||||
const { result } = renderHook(() =>
|
||||
useProjectAuth(permissionKey, projectId, spaceId),
|
||||
);
|
||||
|
||||
// 验证 useSpace 被调用
|
||||
// Verify that useSpace is called
|
||||
expect(useSpace).toHaveBeenCalledWith(spaceId);
|
||||
|
||||
// 验证 useSpaceRole 被调用
|
||||
// Verify useSpaceRole is called
|
||||
expect(useSpaceRole).toHaveBeenCalledWith(spaceId);
|
||||
|
||||
// 验证 useProjectRole 被调用
|
||||
// Verify useProjectRole is called
|
||||
expect(useProjectRole).toHaveBeenCalledWith(projectId);
|
||||
|
||||
// 验证 calcPermission 被调用,且参数正确
|
||||
// Verify that calcPermission is called and the parameters are correct
|
||||
expect(calcPermission).toHaveBeenCalledWith(permissionKey, {
|
||||
projectRoles: [ProjectRoleType.Editor],
|
||||
spaceRoles: [SpaceRoleType.Member],
|
||||
spaceType: SpaceType.Team,
|
||||
});
|
||||
|
||||
// 验证返回值
|
||||
// Validate the return value
|
||||
expect(result.current).toBe(true);
|
||||
});
|
||||
|
||||
it('应该在 calcPermission 返回 false 时返回 false', () => {
|
||||
// 模拟 calcPermission 返回 false
|
||||
// simulated calcPermission returns false
|
||||
(calcPermission as any).mockReturnValue(false);
|
||||
|
||||
// 渲染 hook
|
||||
// Render hook
|
||||
const { result } = renderHook(() =>
|
||||
useProjectAuth(permissionKey, projectId, spaceId),
|
||||
);
|
||||
|
||||
// 验证返回值
|
||||
// Validate the return value
|
||||
expect(result.current).toBe(false);
|
||||
});
|
||||
|
||||
it('应该在空间类型不存在时抛出错误', () => {
|
||||
// 模拟 useSpace 返回没有 space_type 的对象
|
||||
// Mock useSpace returns objects without space_type
|
||||
(useSpace as any).mockReturnValue({});
|
||||
|
||||
// 使用 vi.spyOn 监听 console.error 以防止测试输出错误信息
|
||||
// Use vi.spyOn to listen to console.error to prevent test output error messages
|
||||
vi.spyOn(console, 'error').mockImplementation(() => {
|
||||
// 空实现,防止错误输出
|
||||
// Empty implementation to prevent error output
|
||||
});
|
||||
|
||||
// 验证抛出错误
|
||||
// validation throws error
|
||||
expect(() => {
|
||||
const { result } = renderHook(() =>
|
||||
useProjectAuth(permissionKey, projectId, spaceId),
|
||||
);
|
||||
// 强制访问 result.current 触发错误
|
||||
// Force access result.current trigger error
|
||||
console.log(result.current);
|
||||
}).toThrow('useSpaceAuth must be used after space list has been pulled.');
|
||||
});
|
||||
|
||||
it('应该在空间为 null 时抛出错误', () => {
|
||||
// 模拟 useSpace 返回 null
|
||||
// Simulate useSpace returns null
|
||||
(useSpace as any).mockReturnValue(null);
|
||||
|
||||
// 使用 vi.spyOn 监听 console.error 以防止测试输出错误信息
|
||||
// Use vi.spyOn to listen to console.error to prevent test output error messages
|
||||
vi.spyOn(console, 'error').mockImplementation(() => {
|
||||
// 空实现,防止错误输出
|
||||
// Empty implementation to prevent error output
|
||||
});
|
||||
|
||||
// 验证抛出错误
|
||||
// validation throws error
|
||||
expect(() => {
|
||||
const { result } = renderHook(() =>
|
||||
useProjectAuth(permissionKey, projectId, spaceId),
|
||||
);
|
||||
// 强制访问 result.current 触发错误
|
||||
// Force access result.current trigger error
|
||||
console.log(result.current);
|
||||
}).toThrow('useSpaceAuth must be used after space list has been pulled.');
|
||||
});
|
||||
|
||||
@@ -21,7 +21,7 @@ import { useProjectRole } from '../../src/project/use-project-role';
|
||||
import { useProjectAuthStore } from '../../src/project/store';
|
||||
import { ProjectRoleType } from '../../src/project/constants';
|
||||
|
||||
// 模拟依赖
|
||||
// simulated dependency
|
||||
vi.mock('../../src/project/store', () => ({
|
||||
useProjectAuthStore: vi.fn(),
|
||||
}));
|
||||
@@ -36,38 +36,38 @@ describe('useProjectRole', () => {
|
||||
it('应该返回正确的项目角色', () => {
|
||||
const expectedRoles = [ProjectRoleType.Owner];
|
||||
|
||||
// 模拟 useProjectAuthStore 返回项目角色和 ready 状态
|
||||
// Mock useProjectAuthStore returns project role and ready state
|
||||
(useProjectAuthStore as any).mockReturnValue({
|
||||
isReady: true,
|
||||
role: expectedRoles,
|
||||
});
|
||||
|
||||
// 渲染 hook
|
||||
// Render hook
|
||||
const { result } = renderHook(() => useProjectRole(projectId));
|
||||
|
||||
// 验证 useProjectAuthStore 被调用
|
||||
// Verify useProjectAuthStore is called
|
||||
expect(useProjectAuthStore).toHaveBeenCalled();
|
||||
|
||||
// 验证返回值
|
||||
// Validate the return value
|
||||
expect(result.current).toEqual(expectedRoles);
|
||||
});
|
||||
|
||||
it('应该在项目未准备好时抛出错误', () => {
|
||||
// 模拟 useProjectAuthStore 返回未准备好的状态
|
||||
// Simulate useProjectAuthStore returns an unprepared state
|
||||
(useProjectAuthStore as any).mockReturnValue({
|
||||
isReady: false,
|
||||
role: [],
|
||||
});
|
||||
|
||||
// 使用 vi.spyOn 监听 console.error 以防止测试输出错误信息
|
||||
// Use vi.spyOn to listen to console.error to prevent test output error messages
|
||||
vi.spyOn(console, 'error').mockImplementation(() => {
|
||||
// 空实现,防止错误输出
|
||||
// Empty implementation to prevent error output
|
||||
});
|
||||
|
||||
// 验证抛出错误
|
||||
// validation throws error
|
||||
expect(() => {
|
||||
const { result } = renderHook(() => useProjectRole(projectId));
|
||||
// 强制访问 result.current 触发错误
|
||||
// Force access result.current trigger error
|
||||
console.log(result.current);
|
||||
}).toThrow(
|
||||
'useProjectAuth must be used after useInitProjectRole has been completed.',
|
||||
@@ -75,32 +75,32 @@ describe('useProjectRole', () => {
|
||||
});
|
||||
|
||||
it('应该在角色为 undefined 时返回空数组', () => {
|
||||
// 模拟 useProjectAuthStore 返回 undefined 角色
|
||||
// Emulate useProjectAuthStore returns undefined role
|
||||
(useProjectAuthStore as any).mockReturnValue({
|
||||
isReady: true,
|
||||
role: undefined,
|
||||
});
|
||||
|
||||
// 渲染 hook
|
||||
// Render hook
|
||||
const { result } = renderHook(() => useProjectRole(projectId));
|
||||
|
||||
// 验证返回值为空数组
|
||||
// Verify that the return value is an empty array
|
||||
expect(result.current).toEqual([]);
|
||||
});
|
||||
|
||||
it('应该处理多种角色类型', () => {
|
||||
const expectedRoles = [ProjectRoleType.Owner, ProjectRoleType.Editor];
|
||||
|
||||
// 模拟 useProjectAuthStore 返回多种角色
|
||||
// Emulate useProjectAuthStore returns multiple roles
|
||||
(useProjectAuthStore as any).mockReturnValue({
|
||||
isReady: true,
|
||||
role: expectedRoles,
|
||||
});
|
||||
|
||||
// 渲染 hook
|
||||
// Render hook
|
||||
const { result } = renderHook(() => useProjectRole(projectId));
|
||||
|
||||
// 验证返回值
|
||||
// Validate the return value
|
||||
expect(result.current).toEqual(expectedRoles);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user