admin2iosMenuBuilder.test.ts 4.5 KB

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