myFamilyMenuBuilder.test.ts 3.1 KB

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