#7058 LeftMenu vitest #1153

Merged
jsegarra merged 26 commits from 7058_leftMenu_vitest into dev 2025-02-06 21:45:16 +00:00
2 changed files with 133 additions and 48 deletions
Showing only changes of commit 710532bc4e - Show all commits

View File

@ -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();
});
jsegarra marked this conversation as resolved Outdated

Solo se usa 1 vez

Solo se usa 1 vez

Lo sé, era la manera mas simple de probar la lógica de esa función.
Esto no debería haber subido así porque no haría falta poner las llaves y el return

Lo sé, era la manera mas simple de probar la lógica de esa función. Esto no debería haber subido así porque no haría falta poner las llaves y el return

Si no hay manera de poder testearlo vale, pero si se puede mejor poner dentro de computed, si se vuelve muy complicado quizá sería mejor testearlo con test unitario con cypress

Si no hay manera de poder testearlo vale, pero si se puede mejor poner dentro de computed, si se vuelve muy complicado quizá sería mejor testearlo con test unitario con cypress

Yo haría el test en un unitario con cypress. Queda feo crear una fn que solo vas a usar 1 vez y es simple.

Yo haría el test en un unitario con cypress. Queda feo crear una fn que solo vas a usar 1 vez y es simple.

He vuelto a probar y me ha funcionado dejandolo dentro del computed.
No se que pasó

He vuelto a probar y me ha funcionado dejandolo dentro del computed. No se que pasó
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() {

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 { 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();
});
}); });
}); });
jsegarra marked this conversation as resolved Outdated

Poner otro nombre, está repetido.

Poner otro nombre, está repetido.

oh vaya

oh vaya

should not call getMethodA when method is undefined por ej.

should not call getMethodA when method is undefined por ej.