parametersMenuBuilder.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { describe, test, expect, beforeEach, vi } 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 ParametersMenuBuilder from '~/services/layout/menuBuilder/parametersMenuBuilder'
  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: ParametersMenuBuilder
  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 ParametersMenuBuilder(
  24. runtimeConfig,
  25. ability,
  26. organizationProfile,
  27. accessProfile,
  28. router,
  29. )
  30. })
  31. describe('getMenuName', () => {
  32. test('validate name', () => {
  33. expect(menuBuilder.getMenuName()).toEqual('Parameters')
  34. })
  35. })
  36. describe('build', () => {
  37. test('has all items', () => {
  38. ability.can = vi.fn(() => true)
  39. // @ts-ignore
  40. organizationProfile.isSchool = vi.fn(() => true)
  41. organizationProfile.hasModule = vi.fn((name) => name === 'Sms')
  42. // Should return a MenuGroup
  43. const result = menuBuilder.build() as MenuGroup
  44. expect(result.label).toEqual('parameters')
  45. expect(result.icon).toEqual(undefined)
  46. // @ts-ignore
  47. expect(result.children.length).toEqual(11)
  48. })
  49. test('has no items', () => {
  50. ability.can = vi.fn(() => false)
  51. expect(menuBuilder.build()).toEqual(null)
  52. })
  53. test('has only rights for menu general_params', () => {
  54. ability.can = vi.fn(
  55. (action: string, subject: string) =>
  56. action === 'display' && subject === 'parameters_page',
  57. )
  58. // @ts-ignore
  59. expect(menuBuilder.build().children[0]).toEqual({
  60. label: 'general_params',
  61. icon: { name: 'fas fa-cogs' },
  62. to: 'https://mydomain.com/#/parameters',
  63. type: MENU_LINK_TYPE.V1,
  64. active: false,
  65. })
  66. })
  67. test('has only rights for menu communication_params', () => {
  68. ability.can = vi.fn(
  69. (action: string, subject: string) =>
  70. action === 'display' && subject === 'parameters_communication_page',
  71. )
  72. // @ts-ignore
  73. expect(menuBuilder.build().children[0]).toEqual({
  74. label: 'communication_params',
  75. icon: { name: 'fas fa-comments' },
  76. to: 'https://mydomain.com/#/parameters/communication',
  77. type: MENU_LINK_TYPE.V1,
  78. active: false,
  79. })
  80. })
  81. test('has only rights for menu students_params', () => {
  82. ability.can = vi.fn(
  83. (action: string, subject: string) =>
  84. action === 'display' && subject === 'parameters_student_page',
  85. )
  86. // @ts-ignore
  87. expect(menuBuilder.build().children[0]).toEqual({
  88. label: 'students_params',
  89. icon: { name: 'fas fa-users' },
  90. to: 'https://mydomain.com/#/parameters/student',
  91. type: MENU_LINK_TYPE.V1,
  92. active: false,
  93. })
  94. })
  95. test('has only rights for menu education_params', () => {
  96. ability.can = vi.fn(
  97. (action: string, subject: string) =>
  98. action === 'display' && subject === 'parameters_education_page',
  99. )
  100. // @ts-ignore
  101. expect(menuBuilder.build().children[0]).toEqual({
  102. label: 'education_params',
  103. icon: { name: 'fas fa-graduation-cap' },
  104. to: 'https://mydomain.com/#/parameters/education',
  105. type: MENU_LINK_TYPE.V1,
  106. active: false,
  107. })
  108. })
  109. test('has only rights for menu bills_params', () => {
  110. ability.can = vi.fn(
  111. (action: string, subject: string) =>
  112. action === 'display' && subject === 'parameters_bills_page',
  113. )
  114. // @ts-ignore
  115. expect(menuBuilder.build().children[0]).toEqual({
  116. label: 'bills_params',
  117. icon: { name: 'fas fa-euro-sign' },
  118. to: 'https://mydomain.com/#/parameters/billing',
  119. type: MENU_LINK_TYPE.V1,
  120. active: false,
  121. })
  122. })
  123. test('has only rights for menu secure_params', () => {
  124. ability.can = vi.fn(
  125. (action: string, subject: string) =>
  126. action === 'display' && subject === 'parameters_secure_page',
  127. )
  128. // @ts-ignore
  129. expect(menuBuilder.build().children[0]).toEqual({
  130. label: 'secure_params',
  131. icon: { name: 'fas fa-lock' },
  132. to: 'https://mydomain.com/#/parameters/secure',
  133. type: MENU_LINK_TYPE.V1,
  134. active: false,
  135. })
  136. })
  137. })