communicationMenuBuilder.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 CommunicationMenuBuilder from '~/services/layout/menuBuilder/communicationMenuBuilder'
  6. import type { MenuGroup } 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: CommunicationMenuBuilder
  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 CommunicationMenuBuilder(
  20. runtimeConfig,
  21. ability,
  22. organizationProfile,
  23. accessProfile,
  24. )
  25. })
  26. describe('getMenuName', () => {
  27. test('validate name', () => {
  28. expect(menuBuilder.getMenuName()).toEqual('Communication')
  29. })
  30. })
  31. describe('build', () => {
  32. test('has all items', () => {
  33. ability.can = vi.fn(() => true)
  34. // Should return a MenuGroup
  35. const result = menuBuilder.build() as MenuGroup
  36. expect(result.label).toEqual('communication')
  37. expect(result.icon).toEqual({ name: 'fas fa-comments' })
  38. // @ts-ignore
  39. expect(result.children.length).toEqual(3)
  40. })
  41. test('has no items', () => {
  42. ability.can = vi.fn(() => false)
  43. expect(menuBuilder.build()).toEqual(null)
  44. })
  45. test('has only rights for menu inbox', () => {
  46. ability.can = vi.fn(
  47. (action: string, subject: string) =>
  48. action === 'display' && subject === 'inbox_page',
  49. )
  50. expect(menuBuilder.build()).toEqual({
  51. label: 'inbox',
  52. icon: { name: 'fas fa-inbox' },
  53. to: 'https://mydomain.com/#/messages/list/',
  54. type: MENU_LINK_TYPE.V1,
  55. active: false,
  56. })
  57. })
  58. test('has only rights for menu message_send', () => {
  59. ability.can = vi.fn(
  60. (action: string, subject: string) =>
  61. action === 'display' && subject === 'message_send_page',
  62. )
  63. expect(menuBuilder.build()).toEqual({
  64. label: 'message_send',
  65. icon: { name: 'fas fa-paper-plane' },
  66. to: 'https://mydomain.com/#/messagessends/list/',
  67. type: MENU_LINK_TYPE.V1,
  68. active: false,
  69. })
  70. })
  71. test('has only rights for menu message_templates', () => {
  72. ability.can = vi.fn(
  73. (action: string, subject: string) =>
  74. action === 'display' && subject === 'message_templates_page',
  75. )
  76. expect(menuBuilder.build()).toEqual({
  77. label: 'message_templates',
  78. icon: { name: 'fas fa-edit' },
  79. to: 'https://mydomain.com/#/templates/list/',
  80. type: MENU_LINK_TYPE.V1,
  81. active: false,
  82. })
  83. })
  84. })