import { describe, test, expect, beforeEach, vi } from 'vitest' import type { RuntimeConfig } from '@nuxt/schema' import type { AnyAbility } from '@casl/ability/dist/types' import type { Router } from 'vue-router' import type { AccessProfile, organizationState } from '~/types/interfaces' import AccessMenuBuilder from '~/services/layout/menuBuilder/accessMenuBuilder' import type { MenuGroup } from '~/types/layout' import { MENU_LINK_TYPE } from '~/types/enum/layout' let runtimeConfig: RuntimeConfig let ability: AnyAbility let organizationProfile: organizationState let accessProfile: AccessProfile let menuBuilder: AccessMenuBuilder let router: Router beforeEach(() => { runtimeConfig = vi.fn() as any as RuntimeConfig ability = vi.fn() as any as AnyAbility organizationProfile = vi.fn() as any as organizationState accessProfile = vi.fn() as any as AccessProfile // @ts-ignore router = vi.fn() as Router runtimeConfig.baseUrlAdminLegacy = 'https://mydomain.com/' menuBuilder = new AccessMenuBuilder( runtimeConfig, ability, organizationProfile, accessProfile, router, ) }) describe('getMenuName', () => { test('validate name', () => { expect(menuBuilder.getMenuName()).toEqual('Access') }) }) describe('build', () => { test('has all items', () => { // @ts-ignore organizationProfile.isSchool = true ability.can = vi.fn(() => true) // Should return a MenuGroup const result = menuBuilder.build() as MenuGroup expect(result.label).toEqual('address_book') expect(result.icon).toEqual({ name: 'fas fa-address-book' }) // @ts-ignore expect(result.children.length).toEqual(6) }) test('has no items', () => { ability.can = vi.fn(() => false) expect(menuBuilder.build()).toEqual(null) }) test('has only rights for menu person', () => { ability.can = vi.fn( (action: string, subject: string) => action === 'display' && subject === 'accesses_page', ) // @ts-ignore organizationProfile.isSchool = true expect(menuBuilder.build()).toEqual({ label: 'person', icon: { name: 'fas fa-user' }, to: 'https://mydomain.com/#/students/list/', type: MENU_LINK_TYPE.V1, active: false, }) // @ts-ignore organizationProfile.isSchool = false expect(menuBuilder.build()).toEqual({ label: 'person', icon: { name: 'fas fa-user' }, to: 'https://mydomain.com/#/adherent/list/', type: MENU_LINK_TYPE.V1, active: false, }) }) test('has only rights for menu family_view', () => { ability.can = vi.fn( (action: string, subject: string) => action === 'display' && subject === 'student_registration_page', ) expect(menuBuilder.build()).toEqual({ label: 'family_view', icon: { name: 'fas fa-users' }, to: 'https://mydomain.com/#/student_registration/new', type: MENU_LINK_TYPE.V1, active: false, }) }) test('has only rights for menu education_student_next_year', () => { ability.can = vi.fn( (action: string, subject: string) => action === 'display' && subject === 'education_student_next_year_page', ) expect(menuBuilder.build()).toEqual({ label: 'education_student_next_year', icon: { name: 'fas fa-list-alt' }, to: 'https://mydomain.com/#/education_student_next_year/list/', type: MENU_LINK_TYPE.V1, active: false, }) }) test('has only rights for menu commissions_page', () => { ability.can = vi.fn( (action: string, subject: string) => action === 'display' && subject === 'commissions_page', ) expect(menuBuilder.build()).toEqual({ label: 'commissions', icon: { name: 'fas fa-street-view' }, to: 'https://mydomain.com/#/commissions/list/', type: MENU_LINK_TYPE.V1, active: false, }) }) test('has only rights for menu network_children_page', () => { ability.can = vi.fn( (action: string, subject: string) => action === 'display' && subject === 'network_children_page', ) expect(menuBuilder.build()).toEqual({ label: 'network', icon: { name: 'fas fa-sitemap' }, to: 'https://mydomain.com/#/networks/list/', type: MENU_LINK_TYPE.V1, active: false, }) }) test('has only rights for menu network_parents_page', () => { ability.can = vi.fn( (action: string, subject: string) => action === 'display' && subject === 'network_parents_page', ) expect(menuBuilder.build()).toEqual({ label: 'my_network', icon: { name: 'fas fa-sitemap' }, to: 'https://mydomain.com/#/network_artist_schools/list/', type: MENU_LINK_TYPE.V1, active: false, }) }) })