From 710532bc4e79626283c8c93b2172a31849acdb76 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 31 Dec 2024 09:49:29 +0100 Subject: [PATCH] test: refs #7058 improve --- src/components/LeftMenu.vue | 58 +++++----- src/components/__tests__/Leftmenu.spec.js | 123 +++++++++++++++++----- 2 files changed, 133 insertions(+), 48 deletions(-) diff --git a/src/components/LeftMenu.vue b/src/components/LeftMenu.vue index 7a882e56c..0040b1352 100644 --- a/src/components/LeftMenu.vue +++ b/src/components/LeftMenu.vue @@ -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,33 +108,38 @@ function addChildren(module, route, parent) { } function getRoutes() { - if (props.source === 'main') { - const modules = Object.assign([], navigation.getModules().value); + 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) { - const moduleDef = routes.find( - (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 + for (const item of modules) { + const moduleDef = routes.find( + (route) => toLowerCamel(route.name) === item.module ); + if (!moduleDef) continue; + item.children = []; - if (!moduleDef) return; - if (!moduleDef?.menus) moduleDef = betaGetRoutes(); - addChildren(currentModule, moduleDef, items.value); + addChildren(item.module, moduleDef, item.children); } + + 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() { diff --git a/src/components/__tests__/Leftmenu.spec.js b/src/components/__tests__/Leftmenu.spec.js index 10d9d66fb..54f6d3554 100644 --- a/src/components/__tests__/Leftmenu.spec.js +++ b/src/components/__tests__/Leftmenu.spec.js @@ -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,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 () => { - const expectedMenuItem = [ - { - children: null, - name: 'CustomerList', - title: 'globals.pageTitles.list', - icon: 'view_list', - }, - { - children: null, - name: 'CustomerCreate', - title: 'globals.pageTitles.createCustomer', - icon: 'vn:addperson', - }, - ]; - const firstMenuItem = vm.items[0]; - expect(firstMenuItem.children).toEqual(expect.arrayContaining(expectedMenuItem)); + 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 = [ + { + children: null, + name: 'CustomerList', + title: 'globals.pageTitles.list', + icon: 'view_list', + }, + { + 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(); + }); }); });