#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 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;
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();
});
});
});
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.