admin2iosMenuBuilder.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 Admin2iosMenuBuilder from '~/services/layout/menuBuilder/admin2iosMenuBuilder'
  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: Admin2iosMenuBuilder
  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 Admin2iosMenuBuilder(
  24. runtimeConfig,
  25. ability,
  26. organizationProfile,
  27. accessProfile,
  28. router,
  29. )
  30. })
  31. describe('getMenuName', () => {
  32. test('validate name', () => {
  33. expect(menuBuilder.getMenuName()).toEqual('Admin2ios')
  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('admin2ios')
  42. expect(result.icon).toEqual({ name: 'fas fa-sitemap' })
  43. // @ts-ignore
  44. expect(result.children.length).toEqual(7)
  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 all_accesses', () => {
  51. ability.can = vi.fn(
  52. (action: string, subject: string) =>
  53. action === 'display' && subject === 'all_accesses_page',
  54. )
  55. expect(menuBuilder.build()).toEqual({
  56. label: 'all_accesses',
  57. icon: { name: 'fas fa-users' },
  58. to: 'https://mydomain.com/#/all_accesses/list/',
  59. target: '_self',
  60. type: MENU_LINK_TYPE.V1,
  61. active: false,
  62. })
  63. })
  64. test('has only rights for menu all_organizations', () => {
  65. ability.can = vi.fn(
  66. (action: string, subject: string) =>
  67. action === 'display' && subject === 'all_organizations_page',
  68. )
  69. expect(menuBuilder.build()).toEqual({
  70. label: 'all_organizations',
  71. icon: { name: 'fas fa-building' },
  72. to: 'https://mydomain.com/#/organization_params/list/',
  73. target: '_self',
  74. type: MENU_LINK_TYPE.V1,
  75. active: false,
  76. })
  77. })
  78. test('has only rights for menu tips', () => {
  79. ability.can = vi.fn(
  80. (action: string, subject: string) =>
  81. action === 'display' && subject === 'tips_page',
  82. )
  83. expect(menuBuilder.build()).toEqual({
  84. label: 'tips',
  85. icon: { name: 'fas fa-info-circle' },
  86. to: 'https://mydomain.com/#/tips/list/',
  87. target: '_self',
  88. type: MENU_LINK_TYPE.V1,
  89. active: false,
  90. })
  91. })
  92. test('has only rights for menu dgv', () => {
  93. ability.can = vi.fn(
  94. (action: string, subject: string) =>
  95. action === 'display' && subject === 'dgv_page',
  96. )
  97. expect(menuBuilder.build()).toEqual({
  98. label: 'dgv',
  99. icon: { name: 'fas fa-house-damage' },
  100. to: 'https://mydomain.com/#/admin2ios/dgv',
  101. target: '_self',
  102. type: MENU_LINK_TYPE.V1,
  103. active: false,
  104. })
  105. })
  106. test('has only rights for menu cmf_cotisation', () => {
  107. ability.can = vi.fn(
  108. (action: string, subject: string) =>
  109. action === 'display' && subject === 'cmf_cotisation_page',
  110. )
  111. expect(menuBuilder.build()).toEqual({
  112. label: 'cmf_cotisation',
  113. icon: { name: 'fas fa-info-circle' },
  114. to: 'https://mydomain.com/#/admin2ios/cotisationcmf',
  115. target: '_self',
  116. type: MENU_LINK_TYPE.V1,
  117. active: false,
  118. })
  119. })
  120. test('has only rights for menu right_menu', () => {
  121. ability.can = vi.fn(
  122. (action: string, subject: string) =>
  123. action === 'display' && subject === 'right_page',
  124. )
  125. expect(menuBuilder.build()).toEqual({
  126. label: 'right_menu',
  127. icon: { name: 'fas fa-balance-scale-right' },
  128. to: 'https://mydomain.com/#/admin2ios/right',
  129. target: '_self',
  130. type: MENU_LINK_TYPE.V1,
  131. active: false,
  132. })
  133. })
  134. test('has only rights for menu tree_menu', () => {
  135. ability.can = vi.fn(
  136. (action: string, subject: string) =>
  137. action === 'display' && subject === 'tree_page',
  138. )
  139. expect(menuBuilder.build()).toEqual({
  140. label: 'tree_menu',
  141. icon: { name: 'fas fa-sitemap' },
  142. to: 'https://mydomain.com/#/admin2ios/tree',
  143. target: '_self',
  144. type: MENU_LINK_TYPE.V1,
  145. active: false,
  146. })
  147. })
  148. })