agendaMenuBuilder.test.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 AgendaMenuBuilder from '~/services/layout/menuBuilder/agendaMenuBuilder'
  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: AgendaMenuBuilder
  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 AgendaMenuBuilder(
  24. runtimeConfig,
  25. ability,
  26. organizationProfile,
  27. accessProfile,
  28. router,
  29. )
  30. })
  31. describe('getMenuName', () => {
  32. test('validate name', () => {
  33. expect(menuBuilder.getMenuName()).toEqual('Agenda')
  34. })
  35. })
  36. describe('build', () => {
  37. test('has all items', () => {
  38. ability.can = vi.fn(() => true)
  39. // Should return a MenuGroup
  40. const result = menuBuilder.build() as MenuGroup
  41. expect(result.label).toEqual('schedule')
  42. expect(result.icon).toEqual({ name: 'fas fa-calendar-alt' })
  43. // @ts-ignore
  44. expect(result.children.length).toEqual(2)
  45. })
  46. test('has no items', () => {
  47. ability.can = vi.fn(() => false)
  48. expect(menuBuilder.build()).toEqual(null)
  49. })
  50. test('has only rights for menu schedule', () => {
  51. ability.can = vi.fn(
  52. (action: string, subject: string) =>
  53. action === 'display' && subject === 'agenda_page',
  54. )
  55. expect(menuBuilder.build()).toEqual({
  56. label: 'schedule',
  57. icon: { name: 'fas fa-calendar-alt' },
  58. to: 'https://mydomain.com/#/calendar',
  59. target: '_self',
  60. type: MENU_LINK_TYPE.V1,
  61. active: false,
  62. })
  63. })
  64. test('has only rights for menu attendances', () => {
  65. ability.can = vi.fn(
  66. (action: string, subject: string) =>
  67. action === 'display' && subject === 'attendance_page',
  68. )
  69. expect(menuBuilder.build()).toEqual({
  70. label: 'attendances',
  71. icon: { name: 'fas fa-calendar-check' },
  72. to: 'https://mydomain.com/#/attendances/list/',
  73. target: '_self',
  74. type: MENU_LINK_TYPE.V1,
  75. active: false,
  76. })
  77. })
  78. })