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 WebsiteListMenuBuilder from '~/services/layout/menuBuilder/websiteListMenuBuilder' 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: WebsiteListMenuBuilder let router: Router beforeEach(() => { runtimeConfig = vi.fn() as any as RuntimeConfig ability = vi.fn() as any as AnyAbility organizationProfile = {} 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 WebsiteListMenuBuilder( runtimeConfig, ability, organizationProfile, accessProfile, router, ) }) describe('getMenuName', () => { test('validate name', () => { expect(menuBuilder.getMenuName()).toEqual('WebsiteList') }) }) describe('build', () => { test('without website and parents', () => { organizationProfile.website = null organizationProfile.parents = [] expect(menuBuilder.build()).toEqual(null) }) test('with website but no parents', () => { organizationProfile.name = 'MyOrganization' organizationProfile.website = 'https://some-website.com' organizationProfile.parents = [] const result = menuBuilder.build() as MenuGroup expect(result.children).toEqual([ { label: 'MyOrganization', icon: undefined, to: 'https://some-website.com', target: '_self', type: MENU_LINK_TYPE.EXTERNAL, active: false, endOfSubsection: false, }, ]) }) test('with parents but no website', () => { organizationProfile.name = 'MyOrganization' organizationProfile.website = '' organizationProfile.parents = [ { id: 1, name: 'parent1', website: 'https://parent1.net' }, { id: 2, name: 'parent2', website: 'https://parent2.net' }, { id: 3, name: 'parent3', website: 'https://parent3.net' }, ] const result = menuBuilder.build() as MenuGroup expect(result.children).toEqual([ { label: 'parent1', icon: undefined, to: 'https://parent1.net', target: '_self', type: MENU_LINK_TYPE.EXTERNAL, active: false, endOfSubsection: false, }, { label: 'parent2', icon: undefined, to: 'https://parent2.net', target: '_self', type: MENU_LINK_TYPE.EXTERNAL, active: false, endOfSubsection: false, }, { label: 'parent3', icon: undefined, to: 'https://parent3.net', target: '_self', type: MENU_LINK_TYPE.EXTERNAL, active: false, endOfSubsection: false, }, ]) }) test('with parents and website, opentalent is excluded from parents', () => { organizationProfile.name = 'MyOrganization' organizationProfile.website = 'https://some-website.com' organizationProfile.parents = [ { id: 1, name: 'parent1', website: 'https://parent1.net' }, { id: 2, name: 'parent2', website: 'https://parent2.net' }, { id: 3, name: 'parent3', website: 'https://parent3.net' }, ] WebsiteListMenuBuilder.OPENTALENT_MANAGER_ID = 3 const result = menuBuilder.build() as MenuGroup expect(result.children).toEqual([ { label: 'MyOrganization', icon: undefined, to: 'https://some-website.com', target: '_self', type: MENU_LINK_TYPE.EXTERNAL, active: false, endOfSubsection: false, }, { label: 'parent1', icon: undefined, to: 'https://parent1.net', target: '_self', type: MENU_LINK_TYPE.EXTERNAL, active: false, endOfSubsection: false, }, { label: 'parent2', icon: undefined, to: 'https://parent2.net', target: '_self', type: MENU_LINK_TYPE.EXTERNAL, active: false, endOfSubsection: false, }, ]) }) })