myAccessesMenuBuilder.test.ts 2.2 KB

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