perf: Improve the use of store in the app
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
import { createPinia, setActivePinia } from 'pinia';
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
|
||||
import { useAccessStore } from './access';
|
||||
import { useCoreAccessStore } from './access';
|
||||
|
||||
describe('useAccessStore', () => {
|
||||
describe('useCoreAccessStore', () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia());
|
||||
});
|
||||
|
||||
it('updates accessMenus state', () => {
|
||||
const store = useAccessStore();
|
||||
const store = useCoreAccessStore();
|
||||
expect(store.accessMenus).toEqual([]);
|
||||
store.setAccessMenus([{ name: 'Dashboard', path: '/dashboard' }]);
|
||||
expect(store.accessMenus).toEqual([
|
||||
@@ -18,7 +18,7 @@ describe('useAccessStore', () => {
|
||||
});
|
||||
|
||||
it('updates userInfo and userRoles state', () => {
|
||||
const store = useAccessStore();
|
||||
const store = useCoreAccessStore();
|
||||
expect(store.userInfo).toBeNull();
|
||||
expect(store.userRoles).toEqual([]);
|
||||
|
||||
@@ -30,14 +30,14 @@ describe('useAccessStore', () => {
|
||||
});
|
||||
|
||||
it('returns correct userInfo', () => {
|
||||
const store = useAccessStore();
|
||||
const store = useCoreAccessStore();
|
||||
const userInfo: any = { name: 'Jane Doe', roles: [{ value: 'user' }] };
|
||||
store.setUserInfo(userInfo);
|
||||
expect(store.getUserInfo).toEqual(userInfo);
|
||||
});
|
||||
|
||||
it('updates accessToken state correctly', () => {
|
||||
const store = useAccessStore();
|
||||
const store = useCoreAccessStore();
|
||||
expect(store.accessToken).toBeNull(); // 初始状态
|
||||
store.setAccessToken('abc123');
|
||||
expect(store.accessToken).toBe('abc123');
|
||||
@@ -45,7 +45,7 @@ describe('useAccessStore', () => {
|
||||
|
||||
// 测试重置用户信息时的行为
|
||||
it('clears userInfo and userRoles when setting null userInfo', () => {
|
||||
const store = useAccessStore();
|
||||
const store = useCoreAccessStore();
|
||||
store.setUserInfo({
|
||||
roles: [{ roleName: 'User', value: 'user' }],
|
||||
} as any);
|
||||
@@ -58,27 +58,27 @@ describe('useAccessStore', () => {
|
||||
});
|
||||
|
||||
it('returns the correct accessToken', () => {
|
||||
const store = useAccessStore();
|
||||
const store = useCoreAccessStore();
|
||||
store.setAccessToken('xyz789');
|
||||
expect(store.getAccessToken).toBe('xyz789');
|
||||
});
|
||||
|
||||
// 测试在没有用户角色时返回空数组
|
||||
it('returns an empty array for userRoles if not set', () => {
|
||||
const store = useAccessStore();
|
||||
const store = useCoreAccessStore();
|
||||
expect(store.getUserRoles).toEqual([]);
|
||||
});
|
||||
|
||||
// 测试设置空的访问菜单列表
|
||||
it('handles empty accessMenus correctly', () => {
|
||||
const store = useAccessStore();
|
||||
const store = useCoreAccessStore();
|
||||
store.setAccessMenus([]);
|
||||
expect(store.accessMenus).toEqual([]);
|
||||
});
|
||||
|
||||
// 测试设置空的访问路由列表
|
||||
it('handles empty accessRoutes correctly', () => {
|
||||
const store = useAccessStore();
|
||||
const store = useCoreAccessStore();
|
||||
store.setAccessRoutes([]);
|
||||
expect(store.accessRoutes).toEqual([]);
|
||||
});
|
||||
|
@@ -58,7 +58,7 @@ interface AccessState {
|
||||
/**
|
||||
* @zh_CN 访问权限相关
|
||||
*/
|
||||
const useAccessStore = defineStore('access', {
|
||||
const useCoreAccessStore = defineStore('core-access', {
|
||||
actions: {
|
||||
setAccessMenus(menus: MenuRecordRaw[]) {
|
||||
this.accessMenus = menus;
|
||||
@@ -72,7 +72,7 @@ const useAccessStore = defineStore('access', {
|
||||
setRefreshToken(token: AccessToken) {
|
||||
this.refreshToken = token;
|
||||
},
|
||||
setUserInfo(userInfo: BasicUserInfo) {
|
||||
setUserInfo(userInfo: BasicUserInfo | null) {
|
||||
// 设置用户信息
|
||||
this.userInfo = userInfo;
|
||||
// 设置角色信息
|
||||
@@ -105,7 +105,7 @@ const useAccessStore = defineStore('access', {
|
||||
},
|
||||
persist: {
|
||||
// 持久化
|
||||
paths: ['accessToken', 'refreshToken', 'userRoles', 'userInfo'],
|
||||
paths: ['accessToken', 'refreshToken'],
|
||||
},
|
||||
state: (): AccessState => ({
|
||||
accessMenus: [],
|
||||
@@ -120,7 +120,7 @@ const useAccessStore = defineStore('access', {
|
||||
// 解决热更新问题
|
||||
const hot = import.meta.hot;
|
||||
if (hot) {
|
||||
hot.accept(acceptHMRUpdate(useAccessStore, hot));
|
||||
hot.accept(acceptHMRUpdate(useCoreAccessStore, hot));
|
||||
}
|
||||
|
||||
export { useAccessStore };
|
||||
export { useCoreAccessStore };
|
||||
|
@@ -3,9 +3,9 @@ import { createRouter, createWebHistory } from 'vue-router';
|
||||
import { createPinia, setActivePinia } from 'pinia';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { useTabbarStore } from './tabbar';
|
||||
import { useCoreTabbarStore } from './tabbar';
|
||||
|
||||
describe('useAccessStore', () => {
|
||||
describe('useCoreAccessStore', () => {
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
routes: [],
|
||||
@@ -18,7 +18,7 @@ describe('useAccessStore', () => {
|
||||
});
|
||||
|
||||
it('adds a new tab', () => {
|
||||
const store = useTabbarStore();
|
||||
const store = useCoreTabbarStore();
|
||||
const tab: any = {
|
||||
fullPath: '/home',
|
||||
meta: {},
|
||||
@@ -31,7 +31,7 @@ describe('useAccessStore', () => {
|
||||
});
|
||||
|
||||
it('adds a new tab if it does not exist', () => {
|
||||
const store = useTabbarStore();
|
||||
const store = useCoreTabbarStore();
|
||||
const newTab: any = {
|
||||
fullPath: '/new',
|
||||
meta: {},
|
||||
@@ -43,7 +43,7 @@ describe('useAccessStore', () => {
|
||||
});
|
||||
|
||||
it('updates an existing tab instead of adding a new one', () => {
|
||||
const store = useTabbarStore();
|
||||
const store = useCoreTabbarStore();
|
||||
const initialTab: any = {
|
||||
fullPath: '/existing',
|
||||
meta: {},
|
||||
@@ -59,7 +59,7 @@ describe('useAccessStore', () => {
|
||||
});
|
||||
|
||||
it('closes all tabs', async () => {
|
||||
const store = useTabbarStore();
|
||||
const store = useCoreTabbarStore();
|
||||
store.tabs = [
|
||||
{ fullPath: '/home', meta: {}, name: 'Home', path: '/home' },
|
||||
] as any;
|
||||
@@ -72,7 +72,7 @@ describe('useAccessStore', () => {
|
||||
});
|
||||
|
||||
it('returns all tabs including affix tabs', () => {
|
||||
const store = useTabbarStore();
|
||||
const store = useCoreTabbarStore();
|
||||
store.tabs = [
|
||||
{ fullPath: '/home', meta: {}, name: 'Home', path: '/home' },
|
||||
] as any;
|
||||
@@ -86,7 +86,7 @@ describe('useAccessStore', () => {
|
||||
});
|
||||
|
||||
it('closes a non-affix tab', () => {
|
||||
const store = useTabbarStore();
|
||||
const store = useCoreTabbarStore();
|
||||
const tab: any = {
|
||||
fullPath: '/closable',
|
||||
meta: {},
|
||||
@@ -99,7 +99,7 @@ describe('useAccessStore', () => {
|
||||
});
|
||||
|
||||
it('does not close an affix tab', () => {
|
||||
const store = useTabbarStore();
|
||||
const store = useCoreTabbarStore();
|
||||
const affixTab: any = {
|
||||
fullPath: '/affix',
|
||||
meta: { affixTab: true },
|
||||
@@ -112,14 +112,14 @@ describe('useAccessStore', () => {
|
||||
});
|
||||
|
||||
it('returns all cache tabs', () => {
|
||||
const store = useTabbarStore();
|
||||
const store = useCoreTabbarStore();
|
||||
store.cacheTabs.add('Home');
|
||||
store.cacheTabs.add('About');
|
||||
expect(store.getCacheTabs).toEqual(['Home', 'About']);
|
||||
});
|
||||
|
||||
it('returns all tabs, including affix tabs', () => {
|
||||
const store = useTabbarStore();
|
||||
const store = useCoreTabbarStore();
|
||||
const normalTab: any = {
|
||||
fullPath: '/normal',
|
||||
meta: {},
|
||||
@@ -139,7 +139,7 @@ describe('useAccessStore', () => {
|
||||
});
|
||||
|
||||
it('navigates to a specific tab', async () => {
|
||||
const store = useTabbarStore();
|
||||
const store = useCoreTabbarStore();
|
||||
const tab: any = { meta: {}, name: 'Dashboard', path: '/dashboard' };
|
||||
|
||||
await store._goToTab(tab, router);
|
||||
@@ -152,7 +152,7 @@ describe('useAccessStore', () => {
|
||||
});
|
||||
|
||||
it('closes multiple tabs by paths', async () => {
|
||||
const store = useTabbarStore();
|
||||
const store = useCoreTabbarStore();
|
||||
store.addTab({
|
||||
fullPath: '/home',
|
||||
meta: {},
|
||||
@@ -179,7 +179,7 @@ describe('useAccessStore', () => {
|
||||
});
|
||||
|
||||
it('closes all tabs to the left of the specified tab', async () => {
|
||||
const store = useTabbarStore();
|
||||
const store = useCoreTabbarStore();
|
||||
store.addTab({
|
||||
fullPath: '/home',
|
||||
meta: {},
|
||||
@@ -207,7 +207,7 @@ describe('useAccessStore', () => {
|
||||
});
|
||||
|
||||
it('closes all tabs except the specified tab', async () => {
|
||||
const store = useTabbarStore();
|
||||
const store = useCoreTabbarStore();
|
||||
store.addTab({
|
||||
fullPath: '/home',
|
||||
meta: {},
|
||||
@@ -235,7 +235,7 @@ describe('useAccessStore', () => {
|
||||
});
|
||||
|
||||
it('closes all tabs to the right of the specified tab', async () => {
|
||||
const store = useTabbarStore();
|
||||
const store = useCoreTabbarStore();
|
||||
const targetTab: any = {
|
||||
fullPath: '/home',
|
||||
meta: {},
|
||||
@@ -263,7 +263,7 @@ describe('useAccessStore', () => {
|
||||
});
|
||||
|
||||
it('closes the tab with the specified key', async () => {
|
||||
const store = useTabbarStore();
|
||||
const store = useCoreTabbarStore();
|
||||
const keyToClose = '/about';
|
||||
store.addTab({
|
||||
fullPath: '/home',
|
||||
@@ -293,7 +293,7 @@ describe('useAccessStore', () => {
|
||||
});
|
||||
|
||||
it('refreshes the current tab', async () => {
|
||||
const store = useTabbarStore();
|
||||
const store = useCoreTabbarStore();
|
||||
const currentTab: any = {
|
||||
fullPath: '/dashboard',
|
||||
meta: { name: 'Dashboard' },
|
||||
@@ -302,7 +302,7 @@ describe('useAccessStore', () => {
|
||||
};
|
||||
router.currentRoute.value = currentTab;
|
||||
|
||||
await store.refreshTab(router);
|
||||
await store.refresh(router);
|
||||
|
||||
expect(store.excludeCacheTabs.has('Dashboard')).toBe(false);
|
||||
expect(store.renderRouteView).toBe(true);
|
||||
|
@@ -62,7 +62,7 @@ interface TabsState {
|
||||
/**
|
||||
* @zh_CN 访问权限相关
|
||||
*/
|
||||
const useTabbarStore = defineStore('tabbar', {
|
||||
const useCoreTabbarStore = defineStore('core-tabbar', {
|
||||
actions: {
|
||||
/**
|
||||
* Close tabs in bulk
|
||||
@@ -290,7 +290,7 @@ const useTabbarStore = defineStore('tabbar', {
|
||||
/**
|
||||
* 刷新标签页
|
||||
*/
|
||||
async refreshTab(router: Router) {
|
||||
async refresh(router: Router) {
|
||||
const { currentRoute } = router;
|
||||
const { name } = currentRoute.value;
|
||||
|
||||
@@ -395,7 +395,7 @@ const useTabbarStore = defineStore('tabbar', {
|
||||
// 解决热更新问题
|
||||
const hot = import.meta.hot;
|
||||
if (hot) {
|
||||
hot.accept(acceptHMRUpdate(useTabbarStore, hot));
|
||||
hot.accept(acceptHMRUpdate(useCoreTabbarStore, hot));
|
||||
}
|
||||
|
||||
export { useTabbarStore };
|
||||
export { useCoreTabbarStore };
|
||||
|
@@ -39,10 +39,12 @@
|
||||
"@vue/shared": "^3.4.31",
|
||||
"clsx": "2.1.1",
|
||||
"defu": "^6.1.4",
|
||||
"lodash.clonedeep": "^4.5.0",
|
||||
"nprogress": "^0.2.0",
|
||||
"tailwind-merge": "^2.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/lodash.clonedeep": "^4.5.9",
|
||||
"@types/nprogress": "^0.2.3"
|
||||
}
|
||||
}
|
||||
|
@@ -10,3 +10,4 @@ export * from './tree';
|
||||
export * from './unique';
|
||||
export * from './update-css-variables';
|
||||
export * from './window';
|
||||
export { default as cloneDepp } from 'lodash.clonedeep';
|
||||
|
Reference in New Issue
Block a user