import { describe, test, expect, vi, beforeEach } 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 CommunicationMenuBuilder from '~/services/layout/menuBuilder/communicationMenuBuilder' 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: CommunicationMenuBuilder 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 CommunicationMenuBuilder( runtimeConfig, ability, organizationProfile, accessProfile, router, ) }) describe('getMenuName', () => { test('validate name', () => { expect(menuBuilder.getMenuName()).toEqual('Communication') }) }) describe('build', () => { test('has all items', () => { ability.can = vi.fn(() => true) // Should return a MenuGroup const result = menuBuilder.build() as MenuGroup expect(result.label).toEqual('communication') expect(result.icon).toEqual({ name: 'fas fa-comments' }) // @ts-ignore expect(result.children.length).toEqual(3) }) test('has no items', () => { ability.can = vi.fn(() => false) expect(menuBuilder.build()).toEqual(null) }) test('has only rights for menu inbox', () => { ability.can = vi.fn( (action: string, subject: string) => action === 'display' && subject === 'inbox_page', ) expect(menuBuilder.build()).toEqual({ label: 'inbox', icon: { name: 'fas fa-inbox' }, to: 'https://mydomain.com/#/messages/list/', type: MENU_LINK_TYPE.V1, active: false, }) }) test('has only rights for menu message_send', () => { ability.can = vi.fn( (action: string, subject: string) => action === 'display' && subject === 'message_send_page', ) expect(menuBuilder.build()).toEqual({ label: 'message_send', icon: { name: 'fas fa-paper-plane' }, to: 'https://mydomain.com/#/messagessends/list/', type: MENU_LINK_TYPE.V1, active: false, }) }) test('has only rights for menu message_templates', () => { ability.can = vi.fn( (action: string, subject: string) => action === 'display' && subject === 'message_templates_page', ) expect(menuBuilder.build()).toEqual({ label: 'message_templates', icon: { name: 'fas fa-edit' }, to: 'https://mydomain.com/#/templates/list/', type: MENU_LINK_TYPE.V1, active: false, }) }) })