myAccessesMenuBuilder.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { describe, test, expect, 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 MyAccessesMenuBuilder from '~/services/layout/menuBuilder/myAccessesMenuBuilder'
  7. import type { MenuGroup } from '~/types/layout'
  8. import { MENU_LINK_TYPE } from '~/types/enum/layout'
  9. let runtimeConfig: RuntimeConfig
  10. let ability: AnyAbility
  11. let organizationProfile: organizationState
  12. let accessProfile: AccessProfile
  13. let menuBuilder: MyAccessesMenuBuilder
  14. let router: Router
  15. beforeEach(() => {
  16. runtimeConfig = vi.fn() as any as RuntimeConfig
  17. ability = vi.fn() as any as AnyAbility
  18. organizationProfile = vi.fn() as any as organizationState
  19. accessProfile = vi.fn() as any as AccessProfile
  20. // @ts-ignore
  21. router = vi.fn() as Router
  22. runtimeConfig.baseUrlAdminLegacy = 'https://mydomain.com/'
  23. menuBuilder = new MyAccessesMenuBuilder(
  24. runtimeConfig,
  25. ability,
  26. organizationProfile,
  27. accessProfile,
  28. router,
  29. )
  30. })
  31. describe('getMenuName', () => {
  32. test('validate name', () => {
  33. expect(menuBuilder.getMenuName()).toEqual('MyAccesses')
  34. })
  35. })
  36. describe('build', () => {
  37. test('default : has no items, even with rights', () => {
  38. ability.can = vi.fn(() => true)
  39. expect(menuBuilder.build()).toEqual(null)
  40. })
  41. test('with enabled multi accesses', () => {
  42. accessProfile.multiAccesses = [
  43. { id: 1, name: 'Bob' },
  44. { id: 2, name: 'Séraphin' },
  45. { id: 3, name: 'Lilou' },
  46. ]
  47. const result = menuBuilder.build() as MenuGroup
  48. expect(result.label).toEqual('multiAccesses')
  49. expect(result.icon).toEqual({ name: 'fas fa-building' })
  50. expect(result.children).toEqual([
  51. {
  52. label: 'Bob',
  53. icon: undefined,
  54. to: 'https://mydomain.com/#/switch/1',
  55. target: '_self',
  56. type: MENU_LINK_TYPE.V1,
  57. active: false,
  58. },
  59. {
  60. label: 'Séraphin',
  61. icon: undefined,
  62. to: 'https://mydomain.com/#/switch/2',
  63. target: '_self',
  64. type: MENU_LINK_TYPE.V1,
  65. active: false,
  66. },
  67. {
  68. label: 'Lilou',
  69. icon: undefined,
  70. to: 'https://mydomain.com/#/switch/3',
  71. target: '_self',
  72. type: MENU_LINK_TYPE.V1,
  73. active: false,
  74. },
  75. ])
  76. })
  77. })