myFamilyMenuBuilder.test.ts 3.2 KB

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