import { describe, expect, test, 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 MyFamilyMenuBuilder from '~/services/layout/menuBuilder/myFamilyMenuBuilder' import { GENDER } from '~/types/enum/enums' import { MENU_LINK_TYPE } from '~/types/enum/layout' import type { MenuGroup } from '~/types/layout' let runtimeConfig: RuntimeConfig let ability: AnyAbility let organizationProfile: organizationState let accessProfile: AccessProfile let menuBuilder: MyFamilyMenuBuilder 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 MyFamilyMenuBuilder( runtimeConfig, ability, organizationProfile, accessProfile, router, ) }) describe('getMenuName', () => { test('validate name', () => { expect(menuBuilder.getMenuName()).toEqual('MyFamily') }) }) describe('build', () => { test('no item by default', () => { ability.can = vi.fn(() => true) expect(menuBuilder.build()).toEqual(null) }) test('with family accesses', () => { ability.can = vi.fn(() => true) organizationProfile.id = 100 accessProfile.id = 1 accessProfile.familyAccesses = [ { id: 1, name: 'Bob', givenName: 'Dupont', gender: GENDER.MISTER, avatarId: 1, }, { id: 2, name: 'Séraphin', givenName: 'Dupuis', gender: GENDER.MISTER, avatarId: 2, }, { id: 3, name: 'Lilou', givenName: 'Dubois', gender: GENDER.MISS, avatarId: 3, }, ] accessProfile.originalAccess = { id: 4, name: 'Tony', givenName: 'Soprano', gender: GENDER.MISTER, avatarId: 4, isSuperAdminAccess: false, organization: { id: 100, name: 'my_organization' }, } const result = menuBuilder.build() as MenuGroup expect(result.children).toEqual([ { label: 'Dupont Bob', icon: { avatarId: 1, avatarByDefault: '/images/default/men-1.png' }, to: 'https://mydomain.com/#/switch_user/100/1/1', target: '_self', type: MENU_LINK_TYPE.V1, active: false, }, { label: 'Dupuis Séraphin', icon: { avatarId: 2, avatarByDefault: '/images/default/men-1.png' }, to: 'https://mydomain.com/#/switch_user/100/1/2', target: '_self', type: MENU_LINK_TYPE.V1, active: false, }, { label: 'Dubois Lilou', icon: { avatarId: 3, avatarByDefault: '/images/default/women-1.png' }, to: 'https://mydomain.com/#/switch_user/100/1/3', target: '_self', type: MENU_LINK_TYPE.V1, active: false, }, { label: 'Soprano Tony', icon: undefined, to: 'https://mydomain.com/#/switch_user/100/4/exit', target: '_self', type: MENU_LINK_TYPE.V1, active: false, }, ]) }) })