test: refs #7058 improve

This commit is contained in:
Javier Segarra 2024-12-31 09:49:29 +01:00
parent 2bfc6606f0
commit 710532bc4e
2 changed files with 133 additions and 48 deletions

View File

@ -33,13 +33,18 @@ const pinnedModules = computed(() => {
const search = ref(null);
const filteredItems = computed(() => {
console.error('filterItems');
return filterItems();
});
function filterItems() {
console.error('filterItems');
if (!search.value) return items.value;
const normalizedSearch = normalize(search.value);
return items.value.filter((item) => {
const locale = normalize(t(item.title));
return locale.includes(normalizedSearch);
});
});
}
const filteredPinnedModules = computed(() => {
if (!search.value) return pinnedModules.value;
@ -103,7 +108,14 @@ function addChildren(module, route, parent) {
}
function getRoutes() {
if (props.source === 'main') {
const handleRoutes = {
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) {
@ -119,18 +131,16 @@ function getRoutes() {
items.value = modules;
}
if (props.source === 'card') {
function getCardRoutes() {
console.log('getCardRoutes');
const currentRoute = route.matched[1];
const currentModule = toLowerCamel(currentRoute.name);
let moduleDef = routes.find(
(route) => toLowerCamel(route.name) === currentModule
);
let moduleDef = routes.find((route) => toLowerCamel(route.name) === currentModule);
if (!moduleDef) return;
if (!moduleDef?.menus) moduleDef = betaGetRoutes();
addChildren(currentModule, moduleDef, items.value);
}
}
function betaGetRoutes() {
let menuRoute;

View File

@ -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 Leftmenu from 'components/LeftMenu.vue';
@ -44,6 +44,13 @@ vi.mock('src/router/modules', () => ({
},
],
}));
function mount(source) {
return createWrapper(Leftmenu, {
propsData: {
source,
},
});
}
describe('Leftmenu', () => {
let vm;
@ -52,12 +59,8 @@ describe('Leftmenu', () => {
vi.spyOn(axios, 'get').mockResolvedValue({
data: [],
});
vm = createWrapper(Leftmenu, {
propsData: {
source: 'main',
},
}).vm;
vm = mount('main').vm;
vi.spyOn(vm, 'getCardRoutes');
navigation = useNavigationStore();
navigation.fetchPinned = vi.fn().mockReturnValue(Promise.resolve(true));
@ -72,6 +75,18 @@ describe('Leftmenu', () => {
],
});
});
describe.only(' its card', () => {
it('getRoutes', () => {
vm.props.source = 'card';
vm.getRoutes();
expect(useNavigationStore().getModules).toHaveBeenCalled();
});
});
describe(' its main', () => {
beforeAll(() => {
vm = mount('main').vm;
});
it('should return a proper formated object with two child items', async () => {
const expectedMenuItem = [
@ -89,6 +104,66 @@ describe('Leftmenu', () => {
},
];
const firstMenuItem = vm.items[0];
expect(firstMenuItem.children).toEqual(expect.arrayContaining(expectedMenuItem));
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();
});
});
});