2024-05-19 21:20:42 +08:00
|
|
|
import { createPinia, setActivePinia } from 'pinia';
|
2024-06-02 15:04:37 +08:00
|
|
|
import { beforeEach, describe, expect, it } from 'vitest';
|
2024-05-19 21:20:42 +08:00
|
|
|
|
2024-07-05 23:15:46 +08:00
|
|
|
import { useCoreAccessStore } from './access';
|
2024-05-19 21:20:42 +08:00
|
|
|
|
2024-07-05 23:15:46 +08:00
|
|
|
describe('useCoreAccessStore', () => {
|
2024-06-02 15:04:37 +08:00
|
|
|
beforeEach(() => {
|
2024-05-19 21:20:42 +08:00
|
|
|
setActivePinia(createPinia());
|
2024-06-02 15:04:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('updates accessMenus state', () => {
|
2024-07-05 23:15:46 +08:00
|
|
|
const store = useCoreAccessStore();
|
2024-06-02 15:04:37 +08:00
|
|
|
expect(store.accessMenus).toEqual([]);
|
|
|
|
store.setAccessMenus([{ name: 'Dashboard', path: '/dashboard' }]);
|
|
|
|
expect(store.accessMenus).toEqual([
|
|
|
|
{ name: 'Dashboard', path: '/dashboard' },
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates userInfo and userRoles state', () => {
|
2024-07-05 23:15:46 +08:00
|
|
|
const store = useCoreAccessStore();
|
2024-06-02 15:04:37 +08:00
|
|
|
expect(store.userInfo).toBeNull();
|
|
|
|
expect(store.userRoles).toEqual([]);
|
|
|
|
|
2024-07-04 22:14:51 +08:00
|
|
|
const userInfo: any = { name: 'John Doe', roles: ['admin'] };
|
2024-06-02 15:04:37 +08:00
|
|
|
store.setUserInfo(userInfo);
|
2024-05-19 21:20:42 +08:00
|
|
|
|
2024-06-02 15:04:37 +08:00
|
|
|
expect(store.userInfo).toEqual(userInfo);
|
|
|
|
expect(store.userRoles).toEqual(['admin']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns correct userInfo', () => {
|
2024-07-05 23:15:46 +08:00
|
|
|
const store = useCoreAccessStore();
|
2024-06-02 15:04:37 +08:00
|
|
|
const userInfo: any = { name: 'Jane Doe', roles: [{ value: 'user' }] };
|
|
|
|
store.setUserInfo(userInfo);
|
2024-07-10 00:50:41 +08:00
|
|
|
expect(store.userInfo).toEqual(userInfo);
|
2024-06-02 15:04:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('updates accessToken state correctly', () => {
|
2024-07-05 23:15:46 +08:00
|
|
|
const store = useCoreAccessStore();
|
2024-06-02 15:04:37 +08:00
|
|
|
expect(store.accessToken).toBeNull(); // 初始状态
|
|
|
|
store.setAccessToken('abc123');
|
|
|
|
expect(store.accessToken).toBe('abc123');
|
|
|
|
});
|
|
|
|
|
|
|
|
// 测试重置用户信息时的行为
|
|
|
|
it('clears userInfo and userRoles when setting null userInfo', () => {
|
2024-07-05 23:15:46 +08:00
|
|
|
const store = useCoreAccessStore();
|
2024-06-02 15:04:37 +08:00
|
|
|
store.setUserInfo({
|
|
|
|
roles: [{ roleName: 'User', value: 'user' }],
|
|
|
|
} as any);
|
|
|
|
expect(store.userInfo).not.toBeNull();
|
|
|
|
expect(store.userRoles.length).toBeGreaterThan(0);
|
|
|
|
|
|
|
|
store.setUserInfo(null as any); // 重置用户信息
|
|
|
|
expect(store.userInfo).toBeNull();
|
|
|
|
expect(store.userRoles).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns the correct accessToken', () => {
|
2024-07-05 23:15:46 +08:00
|
|
|
const store = useCoreAccessStore();
|
2024-06-02 15:04:37 +08:00
|
|
|
store.setAccessToken('xyz789');
|
2024-07-10 00:50:41 +08:00
|
|
|
expect(store.accessToken).toBe('xyz789');
|
2024-06-02 15:04:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// 测试在没有用户角色时返回空数组
|
|
|
|
it('returns an empty array for userRoles if not set', () => {
|
2024-07-05 23:15:46 +08:00
|
|
|
const store = useCoreAccessStore();
|
2024-07-10 00:50:41 +08:00
|
|
|
expect(store.userRoles).toEqual([]);
|
2024-06-02 15:04:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// 测试设置空的访问菜单列表
|
|
|
|
it('handles empty accessMenus correctly', () => {
|
2024-07-05 23:15:46 +08:00
|
|
|
const store = useCoreAccessStore();
|
2024-06-02 15:04:37 +08:00
|
|
|
store.setAccessMenus([]);
|
|
|
|
expect(store.accessMenus).toEqual([]);
|
|
|
|
});
|
2024-05-19 21:20:42 +08:00
|
|
|
|
2024-06-02 15:04:37 +08:00
|
|
|
// 测试设置空的访问路由列表
|
|
|
|
it('handles empty accessRoutes correctly', () => {
|
2024-07-05 23:15:46 +08:00
|
|
|
const store = useCoreAccessStore();
|
2024-06-02 15:04:37 +08:00
|
|
|
store.setAccessRoutes([]);
|
|
|
|
expect(store.accessRoutes).toEqual([]);
|
2024-05-19 21:20:42 +08:00
|
|
|
});
|
|
|
|
});
|