admin2iosMenuBuilder.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { describe, test, it, expect } from 'vitest'
  2. import {RuntimeConfig} from "@nuxt/schema";
  3. import {AnyAbility} from "@casl/ability/dist/types";
  4. import {AccessProfile, organizationState} from "~/types/interfaces";
  5. import Admin2iosMenuBuilder from "~/services/layout/menuBuilder/admin2iosMenuBuilder";
  6. import {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(runtimeConfig, ability, organizationProfile, accessProfile)
  20. })
  21. describe('getMenuName', () => {
  22. test('validate name', () => {
  23. expect(menuBuilder.getMenuName()).toEqual("Admin2ios")
  24. })
  25. })
  26. describe('build', () => {
  27. test('has all items', () => {
  28. ability.can = vi.fn(() => true)
  29. // Should return a MenuGroup
  30. const result = menuBuilder.build() as MenuGroup
  31. expect(result.label).toEqual('admin2ios')
  32. expect(result.icon).toEqual({name: 'fas fa-sitemap'})
  33. // @ts-ignore
  34. expect(result.children.length).toEqual(7)
  35. })
  36. test('has no items', () => {
  37. ability.can = vi.fn(() => false)
  38. expect(menuBuilder.build()).toEqual(null)
  39. })
  40. test('has only rights for menu all_accesses', () => {
  41. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'all_accesses_page')
  42. expect(menuBuilder.build()).toEqual(
  43. {label: 'all_accesses', icon: {name: 'fas fa-users'}, to: 'https://mydomain.com/#/all_accesses/list/', type: MENU_LINK_TYPE.V1, active: false}
  44. )
  45. })
  46. test('has only rights for menu all_organizations', () => {
  47. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'all_organizations_page')
  48. expect(menuBuilder.build()).toEqual(
  49. {label: 'all_organizations', icon: {name: 'fas fa-building'}, to: 'https://mydomain.com/#/organization_params/list/', type: MENU_LINK_TYPE.V1, active: false}
  50. )
  51. })
  52. test('has only rights for menu tips', () => {
  53. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'tips_page')
  54. expect(menuBuilder.build()).toEqual(
  55. {label: 'tips', icon: {name: 'fas fa-info-circle'}, to: 'https://mydomain.com/#/tips/list/', type: MENU_LINK_TYPE.V1, active: false}
  56. )
  57. })
  58. test('has only rights for menu dgv', () => {
  59. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'dgv_page')
  60. expect(menuBuilder.build()).toEqual(
  61. {label: 'dgv', icon: {name: 'fas fa-house-damage'}, to: 'https://mydomain.com/#/admin2ios/dgv', type: MENU_LINK_TYPE.V1, active: false}
  62. )
  63. })
  64. test('has only rights for menu cmf_cotisation', () => {
  65. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'cmf_cotisation_page')
  66. expect(menuBuilder.build()).toEqual(
  67. {label: 'cmf_cotisation', icon: {name: 'fas fa-info-circle'}, to: 'https://mydomain.com/#/admin2ios/cotisationcmf', type: MENU_LINK_TYPE.V1, active: false}
  68. )
  69. })
  70. test('has only rights for menu right_menu', () => {
  71. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'right_page')
  72. expect(menuBuilder.build()).toEqual(
  73. {label: 'right_menu', icon: {name: 'fas fa-balance-scale-right'}, to: 'https://mydomain.com/#/admin2ios/right', type: MENU_LINK_TYPE.V1, active: false}
  74. )
  75. })
  76. test('has only rights for menu tree_menu', () => {
  77. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'tree_page')
  78. expect(menuBuilder.build()).toEqual(
  79. {label: 'tree_menu', icon: {name: 'fas fa-sitemap'}, to: 'https://mydomain.com/#/admin2ios/tree', type: MENU_LINK_TYPE.V1, active: false}
  80. )
  81. })
  82. })