myFamilyMenuBuilder.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import {describe, expect, test} from 'vitest'
  2. import {RuntimeConfig} from "@nuxt/schema";
  3. import {AnyAbility} from "@casl/ability/dist/types";
  4. import {AccessProfile, organizationState} from "~/types/interfaces";
  5. import MyFamilyMenuBuilder from "~/services/layout/menuBuilder/myFamilyMenuBuilder";
  6. import {GENDER} from "~/types/enum/enums";
  7. import {MENU_LINK_TYPE} from "~/types/enum/layout";
  8. import {MenuGroup} from "~/types/layout";
  9. let runtimeConfig: RuntimeConfig
  10. let ability: AnyAbility
  11. let organizationProfile: organizationState
  12. let accessProfile: AccessProfile
  13. let menuBuilder: MyFamilyMenuBuilder
  14. beforeEach(()=> {
  15. runtimeConfig = vi.fn() as any as RuntimeConfig
  16. ability = vi.fn() as any as AnyAbility
  17. organizationProfile = vi.fn() as any as organizationState
  18. accessProfile = vi.fn() as any as AccessProfile
  19. runtimeConfig.baseUrlAdminLegacy = 'https://mydomain.com/'
  20. menuBuilder = new MyFamilyMenuBuilder(runtimeConfig, ability, organizationProfile, accessProfile)
  21. })
  22. describe('getMenuName', () => {
  23. test('validate name', () => {
  24. expect(menuBuilder.getMenuName()).toEqual("MyFamily")
  25. })
  26. })
  27. describe('build', () => {
  28. test('no item by default', () => {
  29. ability.can = vi.fn(() => true)
  30. expect(menuBuilder.build()).toEqual(null)
  31. })
  32. test('with family accesses', () => {
  33. ability.can = vi.fn(() => true)
  34. organizationProfile.id = 100
  35. accessProfile.id = 1
  36. accessProfile.familyAccesses = [
  37. { id: 1, name: 'Bob', givenName: 'Dupont', gender: GENDER.MISTER, avatarId: 1 },
  38. { id: 2, name: 'Séraphin', givenName: 'Dupuis', gender: GENDER.MISTER, avatarId: 2 },
  39. { id: 3, name: 'Lilou', givenName: 'Dubois', gender: GENDER.MISS, avatarId: 3 },
  40. ]
  41. accessProfile.originalAccess = {
  42. id: 4,
  43. name: 'Tony',
  44. givenName: 'Soprano',
  45. gender: GENDER.MISTER,
  46. avatarId: 4,
  47. isSuperAdminAccess: false,
  48. organization: { id: 100, name: 'my_organization' }
  49. }
  50. const result = menuBuilder.build() as MenuGroup
  51. expect(result.children).toEqual([
  52. { label: 'Dupont Bob', icon: { avatarId: 1, avatarByDefault: '/images/default/men-1.png'}, to: 'https://mydomain.com/#/switch_user/100/1/1', type: MENU_LINK_TYPE.V1, active: false },
  53. { label: 'Dupuis Séraphin', icon: { avatarId: 2, avatarByDefault: '/images/default/men-1.png'}, to: 'https://mydomain.com/#/switch_user/100/1/2', type: MENU_LINK_TYPE.V1, active: false },
  54. { label: 'Dubois Lilou', icon: { avatarId: 3, avatarByDefault: '/images/default/women-1.png'}, to: 'https://mydomain.com/#/switch_user/100/1/3', type: MENU_LINK_TYPE.V1, active: false },
  55. { label: 'Soprano Tony', icon: undefined, to: 'https://mydomain.com/#/switch_user/100/4/exit', type: MENU_LINK_TYPE.V1, active: false }
  56. ])
  57. })
  58. })