accessMenuBuilder.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 AccessMenuBuilder from "~/services/layout/menuBuilder/accessMenuBuilder";
  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: AccessMenuBuilder
  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 AccessMenuBuilder(runtimeConfig, ability, organizationProfile, accessProfile)
  20. })
  21. describe('getMenuName', () => {
  22. test('validate name', () => {
  23. expect(menuBuilder.getMenuName()).toEqual("Access")
  24. })
  25. })
  26. describe('build', () => {
  27. test('has all items', () => {
  28. // @ts-ignore
  29. organizationProfile.isSchool = true
  30. ability.can = vi.fn(() => true)
  31. // Should return a MenuGroup
  32. const result = menuBuilder.build() as MenuGroup
  33. expect(result.label).toEqual('address_book')
  34. expect(result.icon).toEqual({name: 'fas fa-address-book'})
  35. // @ts-ignore
  36. expect(result.children.length).toEqual(6)
  37. })
  38. test('has no items', () => {
  39. ability.can = vi.fn(() => false)
  40. expect(menuBuilder.build()).toEqual(null)
  41. })
  42. test('has only rights for menu person', () => {
  43. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'accesses_page')
  44. // @ts-ignore
  45. organizationProfile.isSchool = true
  46. expect(menuBuilder.build()).toEqual(
  47. {label: 'person', icon: {name: 'fas fa-user'}, to: 'https://mydomain.com/#/students/list/', type: MENU_LINK_TYPE.V1, active: false}
  48. )
  49. // @ts-ignore
  50. organizationProfile.isSchool = false
  51. expect(menuBuilder.build()).toEqual(
  52. {label: 'person', icon: {name: 'fas fa-user'}, to: 'https://mydomain.com/#/adherent/list/', type: MENU_LINK_TYPE.V1, active: false}
  53. )
  54. })
  55. test('has only rights for menu family_view', () => {
  56. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'student_registration_page')
  57. expect(menuBuilder.build()).toEqual(
  58. {label: 'family_view', icon: {name: 'fas fa-users'}, to: 'https://mydomain.com/#/student_registration/new', type: MENU_LINK_TYPE.V1, active: false}
  59. )
  60. })
  61. test('has only rights for menu education_student_next_year', () => {
  62. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'education_student_next_year_page')
  63. expect(menuBuilder.build()).toEqual(
  64. {label: 'education_student_next_year', icon: {name: 'fas fa-list-alt'}, to: 'https://mydomain.com/#/education_student_next_year/list/', type: MENU_LINK_TYPE.V1, active: false}
  65. )
  66. })
  67. test('has only rights for menu commissions_page', () => {
  68. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'commissions_page')
  69. expect(menuBuilder.build()).toEqual(
  70. {label: 'commissions', icon: {name: 'fas fa-street-view'}, to: 'https://mydomain.com/#/commissions/list/', type: MENU_LINK_TYPE.V1, active: false}
  71. )
  72. })
  73. test('has only rights for menu network_children_page', () => {
  74. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'network_children_page')
  75. expect(menuBuilder.build()).toEqual(
  76. {label: 'network', icon: {name: 'fas fa-sitemap'}, to: 'https://mydomain.com/#/networks/list/', type: MENU_LINK_TYPE.V1, active: false}
  77. )
  78. })
  79. test('has only rights for menu network_parents_page', () => {
  80. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'network_parents_page')
  81. expect(menuBuilder.build()).toEqual(
  82. {label: 'my_network', icon: {name: 'fas fa-sitemap'}, to: 'https://mydomain.com/#/network_artist_schools/list/', type: MENU_LINK_TYPE.V1, active: false}
  83. )
  84. })
  85. })