test: refs #7058 improve
This commit is contained in:
parent
2bfc6606f0
commit
710532bc4e
|
@ -33,13 +33,18 @@ const pinnedModules = computed(() => {
|
||||||
const search = ref(null);
|
const search = ref(null);
|
||||||
|
|
||||||
const filteredItems = computed(() => {
|
const filteredItems = computed(() => {
|
||||||
|
console.error('filterItems');
|
||||||
|
return filterItems();
|
||||||
|
});
|
||||||
|
function filterItems() {
|
||||||
|
console.error('filterItems');
|
||||||
if (!search.value) return items.value;
|
if (!search.value) return items.value;
|
||||||
const normalizedSearch = normalize(search.value);
|
const normalizedSearch = normalize(search.value);
|
||||||
return items.value.filter((item) => {
|
return items.value.filter((item) => {
|
||||||
const locale = normalize(t(item.title));
|
const locale = normalize(t(item.title));
|
||||||
return locale.includes(normalizedSearch);
|
return locale.includes(normalizedSearch);
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
const filteredPinnedModules = computed(() => {
|
const filteredPinnedModules = computed(() => {
|
||||||
if (!search.value) return pinnedModules.value;
|
if (!search.value) return pinnedModules.value;
|
||||||
|
@ -103,33 +108,38 @@ function addChildren(module, route, parent) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRoutes() {
|
function getRoutes() {
|
||||||
if (props.source === 'main') {
|
const handleRoutes = {
|
||||||
const modules = Object.assign([], navigation.getModules().value);
|
main: getMainRoutes,
|
||||||
|
card: getCardRoutes,
|
||||||
|
};
|
||||||
|
console.log(props.source);
|
||||||
|
handleRoutes[props.source]();
|
||||||
|
}
|
||||||
|
function getMainRoutes() {
|
||||||
|
const modules = Object.assign([], navigation.getModules().value);
|
||||||
|
|
||||||
for (const item of modules) {
|
for (const item of modules) {
|
||||||
const moduleDef = routes.find(
|
const moduleDef = routes.find(
|
||||||
(route) => toLowerCamel(route.name) === item.module
|
(route) => toLowerCamel(route.name) === item.module
|
||||||
);
|
|
||||||
if (!moduleDef) continue;
|
|
||||||
item.children = [];
|
|
||||||
|
|
||||||
addChildren(item.module, moduleDef, item.children);
|
|
||||||
}
|
|
||||||
|
|
||||||
items.value = modules;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (props.source === 'card') {
|
|
||||||
const currentRoute = route.matched[1];
|
|
||||||
const currentModule = toLowerCamel(currentRoute.name);
|
|
||||||
let moduleDef = routes.find(
|
|
||||||
(route) => toLowerCamel(route.name) === currentModule
|
|
||||||
);
|
);
|
||||||
|
if (!moduleDef) continue;
|
||||||
|
item.children = [];
|
||||||
|
|
||||||
if (!moduleDef) return;
|
addChildren(item.module, moduleDef, item.children);
|
||||||
if (!moduleDef?.menus) moduleDef = betaGetRoutes();
|
|
||||||
addChildren(currentModule, moduleDef, items.value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
items.value = modules;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCardRoutes() {
|
||||||
|
console.log('getCardRoutes');
|
||||||
|
const currentRoute = route.matched[1];
|
||||||
|
const currentModule = toLowerCamel(currentRoute.name);
|
||||||
|
let moduleDef = routes.find((route) => toLowerCamel(route.name) === currentModule);
|
||||||
|
|
||||||
|
if (!moduleDef) return;
|
||||||
|
if (!moduleDef?.menus) moduleDef = betaGetRoutes();
|
||||||
|
addChildren(currentModule, moduleDef, items.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
function betaGetRoutes() {
|
function betaGetRoutes() {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { vi, describe, expect, it, beforeAll } from 'vitest';
|
import { vi, describe, expect, it, beforeAll, beforeEach } from 'vitest';
|
||||||
import { createWrapper, axios } from 'app/test/vitest/helper';
|
import { createWrapper, axios } from 'app/test/vitest/helper';
|
||||||
import Leftmenu from 'components/LeftMenu.vue';
|
import Leftmenu from 'components/LeftMenu.vue';
|
||||||
|
|
||||||
|
@ -44,6 +44,13 @@ vi.mock('src/router/modules', () => ({
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}));
|
}));
|
||||||
|
function mount(source) {
|
||||||
|
return createWrapper(Leftmenu, {
|
||||||
|
propsData: {
|
||||||
|
source,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
describe('Leftmenu', () => {
|
describe('Leftmenu', () => {
|
||||||
let vm;
|
let vm;
|
||||||
|
@ -52,12 +59,8 @@ describe('Leftmenu', () => {
|
||||||
vi.spyOn(axios, 'get').mockResolvedValue({
|
vi.spyOn(axios, 'get').mockResolvedValue({
|
||||||
data: [],
|
data: [],
|
||||||
});
|
});
|
||||||
|
vm = mount('main').vm;
|
||||||
vm = createWrapper(Leftmenu, {
|
vi.spyOn(vm, 'getCardRoutes');
|
||||||
propsData: {
|
|
||||||
source: 'main',
|
|
||||||
},
|
|
||||||
}).vm;
|
|
||||||
|
|
||||||
navigation = useNavigationStore();
|
navigation = useNavigationStore();
|
||||||
navigation.fetchPinned = vi.fn().mockReturnValue(Promise.resolve(true));
|
navigation.fetchPinned = vi.fn().mockReturnValue(Promise.resolve(true));
|
||||||
|
@ -72,23 +75,95 @@ describe('Leftmenu', () => {
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
describe.only(' its card', () => {
|
||||||
|
it('getRoutes', () => {
|
||||||
|
vm.props.source = 'card';
|
||||||
|
|
||||||
it('should return a proper formated object with two child items', async () => {
|
vm.getRoutes();
|
||||||
const expectedMenuItem = [
|
expect(useNavigationStore().getModules).toHaveBeenCalled();
|
||||||
{
|
});
|
||||||
children: null,
|
});
|
||||||
name: 'CustomerList',
|
describe(' its main', () => {
|
||||||
title: 'globals.pageTitles.list',
|
beforeAll(() => {
|
||||||
icon: 'view_list',
|
vm = mount('main').vm;
|
||||||
},
|
});
|
||||||
{
|
|
||||||
children: null,
|
it('should return a proper formated object with two child items', async () => {
|
||||||
name: 'CustomerCreate',
|
const expectedMenuItem = [
|
||||||
title: 'globals.pageTitles.createCustomer',
|
{
|
||||||
icon: 'vn:addperson',
|
children: null,
|
||||||
},
|
name: 'CustomerList',
|
||||||
];
|
title: 'globals.pageTitles.list',
|
||||||
const firstMenuItem = vm.items[0];
|
icon: 'view_list',
|
||||||
expect(firstMenuItem.children).toEqual(expect.arrayContaining(expectedMenuItem));
|
},
|
||||||
|
{
|
||||||
|
children: null,
|
||||||
|
name: 'CustomerCreate',
|
||||||
|
title: 'globals.pageTitles.createCustomer',
|
||||||
|
icon: 'vn:addperson',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
const firstMenuItem = vm.items[0];
|
||||||
|
expect(firstMenuItem.children).toEqual(
|
||||||
|
expect.arrayContaining(expectedMenuItem)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should initialize with default props', () => {
|
||||||
|
expect(vm.source).toBe('main');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should filter items based on search input', async () => {
|
||||||
|
vm.search = 'Rou';
|
||||||
|
await vm.$nextTick();
|
||||||
|
// expect(vm.filterItems).toHaveBeenCalled();
|
||||||
|
expect(vm.filterItems()).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return pinned items', () => {
|
||||||
|
vm.items = [
|
||||||
|
{ name: 'Item 1', isPinned: false },
|
||||||
|
{ name: 'Item 2', isPinned: true },
|
||||||
|
];
|
||||||
|
expect(vm.pinnedModules).toEqual(
|
||||||
|
new Map([['Item 2', { name: 'Item 2', isPinned: true }]])
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should find matches in routes', () => {
|
||||||
|
const search = 'child1';
|
||||||
|
const item = {
|
||||||
|
children: [
|
||||||
|
{ name: 'child1', children: [] },
|
||||||
|
{ name: 'child2', children: [] },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
const matches = vm.findMatches(search, item);
|
||||||
|
expect(matches).toEqual([{ name: 'child1', children: [] }]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.skip('should add children to navigation', () => {
|
||||||
|
const module = 'module1';
|
||||||
|
const route = {
|
||||||
|
meta: { menu: 'child1' },
|
||||||
|
children: [
|
||||||
|
{ name: 'child1', children: [] },
|
||||||
|
{ name: 'child2', children: [] },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
const parent = 'parent1';
|
||||||
|
vm.addChildren(module, route, parent);
|
||||||
|
expect(useNavigationStore().addMenuItem).toHaveBeenCalledWith(
|
||||||
|
module,
|
||||||
|
{ name: 'child1', children: [] },
|
||||||
|
parent
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should get routes for main source', () => {
|
||||||
|
vm.props.source = 'main';
|
||||||
|
vm.getRoutes();
|
||||||
|
expect(useNavigationStore().getModules).toHaveBeenCalled();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue