myFamilyMenuBuilder.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { describe, expect, test, vi, beforeEach } from 'vitest'
  2. import type { RuntimeConfig } from '@nuxt/schema'
  3. //@ts-expect-error problème de typage sans conséquence
  4. import type { AnyAbility } from '@casl/ability/dist/types'
  5. import type { Router } from 'vue-router'
  6. import type { AccessProfile, organizationState } from '~/types/interfaces'
  7. import MyFamilyMenuBuilder from '~/services/layout/menuBuilder/myFamilyMenuBuilder'
  8. import { GENDER } from '~/types/enum/enums'
  9. import { MENU_LINK_TYPE } from '~/types/enum/layout'
  10. import type { MenuGroup } from '~/types/layout'
  11. let runtimeConfig: RuntimeConfig
  12. let ability: AnyAbility
  13. let organizationProfile: organizationState
  14. let accessProfile: AccessProfile
  15. let menuBuilder: MyFamilyMenuBuilder
  16. let router: Router
  17. beforeEach(() => {
  18. runtimeConfig = vi.fn() as any as RuntimeConfig
  19. ability = vi.fn() as any as AnyAbility
  20. organizationProfile = vi.fn() as any as organizationState
  21. accessProfile = vi.fn() as any as AccessProfile
  22. // @ts-ignore
  23. router = vi.fn() as Router
  24. runtimeConfig.baseUrlAdminLegacy = 'https://mydomain.com/'
  25. menuBuilder = new MyFamilyMenuBuilder(
  26. runtimeConfig,
  27. ability,
  28. organizationProfile,
  29. accessProfile,
  30. router,
  31. )
  32. })
  33. describe('getMenuName', () => {
  34. test('validate name', () => {
  35. expect(menuBuilder.getMenuName()).toEqual('MyFamily')
  36. })
  37. })
  38. describe('build', () => {
  39. test('no item by default', () => {
  40. ability.can = vi.fn(() => true)
  41. expect(menuBuilder.build()).toEqual(null)
  42. })
  43. test('with family accesses', () => {
  44. ability.can = vi.fn(() => true)
  45. organizationProfile.id = 100
  46. accessProfile.id = 1
  47. accessProfile.familyAccesses = [
  48. {
  49. id: 1,
  50. name: 'Bob',
  51. givenName: 'Dupont',
  52. gender: GENDER.MISTER,
  53. avatarId: 1,
  54. },
  55. {
  56. id: 2,
  57. name: 'Séraphin',
  58. givenName: 'Dupuis',
  59. gender: GENDER.MISTER,
  60. avatarId: 2,
  61. },
  62. {
  63. id: 3,
  64. name: 'Lilou',
  65. givenName: 'Dubois',
  66. gender: GENDER.MISS,
  67. avatarId: 3,
  68. },
  69. ]
  70. accessProfile.originalAccess = {
  71. id: 4,
  72. name: 'Tony',
  73. givenName: 'Soprano',
  74. gender: GENDER.MISTER,
  75. avatarId: 4,
  76. isSuperAdminAccess: false,
  77. organization: { id: 100, name: 'my_organization' },
  78. }
  79. const result = menuBuilder.build() as MenuGroup
  80. expect(result.children).toEqual([
  81. {
  82. label: 'Dupont Bob',
  83. icon: { avatarId: 1, avatarByDefault: '/images/default/men-1.png' },
  84. to: 'https://mydomain.com/#/switch_user/100/1/1',
  85. target: '_self',
  86. type: MENU_LINK_TYPE.V1,
  87. active: false,
  88. endOfSubsection: false,
  89. },
  90. {
  91. label: 'Dupuis Séraphin',
  92. icon: { avatarId: 2, avatarByDefault: '/images/default/men-1.png' },
  93. to: 'https://mydomain.com/#/switch_user/100/1/2',
  94. target: '_self',
  95. type: MENU_LINK_TYPE.V1,
  96. active: false,
  97. endOfSubsection: false,
  98. },
  99. {
  100. label: 'Dubois Lilou',
  101. icon: { avatarId: 3, avatarByDefault: '/images/default/women-1.png' },
  102. to: 'https://mydomain.com/#/switch_user/100/1/3',
  103. target: '_self',
  104. type: MENU_LINK_TYPE.V1,
  105. active: false,
  106. endOfSubsection: false,
  107. },
  108. {
  109. label: 'Soprano Tony',
  110. icon: undefined,
  111. to: 'https://mydomain.com/#/switch_user/100/4/exit',
  112. target: '_self',
  113. type: MENU_LINK_TYPE.V1,
  114. active: false,
  115. endOfSubsection: false,
  116. },
  117. ])
  118. })
  119. })