parametersMenuBuilder.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 ParametersMenuBuilder from "~/services/layout/menuBuilder/parametersMenuBuilder";
  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: ParametersMenuBuilder
  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 ParametersMenuBuilder(runtimeConfig, ability, organizationProfile, accessProfile)
  20. })
  21. describe('getMenuName', () => {
  22. test('validate name', () => {
  23. expect(menuBuilder.getMenuName()).toEqual("Parameters")
  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('parameters')
  32. expect(result.icon).toEqual(undefined)
  33. // @ts-ignore
  34. expect(result.children.length).toEqual(6)
  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 general_params', () => {
  41. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'parameters_page')
  42. // @ts-ignore
  43. expect(menuBuilder.build().children[0]).toEqual(
  44. {label: 'general_params', icon: {name: 'fas fa-cogs'}, to: 'https://mydomain.com/parameters', type: MENU_LINK_TYPE.V1, active: false}
  45. )
  46. })
  47. test('has only rights for menu communication_params', () => {
  48. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'parameters_communication_page')
  49. // @ts-ignore
  50. expect(menuBuilder.build().children[0]).toEqual(
  51. {label: 'communication_params', icon: {name: 'fas fa-comments'}, to: 'https://mydomain.com/parameters/communication', type: MENU_LINK_TYPE.V1, active: false}
  52. )
  53. })
  54. test('has only rights for menu students_params', () => {
  55. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'parameters_student_page')
  56. // @ts-ignore
  57. expect(menuBuilder.build().children[0]).toEqual(
  58. {label: 'students_params', icon: {name: 'fas fa-users'}, to: 'https://mydomain.com/parameters/student', type: MENU_LINK_TYPE.V1, active: false}
  59. )
  60. })
  61. test('has only rights for menu education_params', () => {
  62. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'parameters_education_page')
  63. // @ts-ignore
  64. expect(menuBuilder.build().children[0]).toEqual(
  65. {label: 'education_params', icon: {name: 'fas fa-graduation-cap'}, to: 'https://mydomain.com/parameters/education', type: MENU_LINK_TYPE.V1, active: false}
  66. )
  67. })
  68. test('has only rights for menu bills_params', () => {
  69. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'parameters_bills_page')
  70. // @ts-ignore
  71. expect(menuBuilder.build().children[0]).toEqual(
  72. {label: 'bills_params', icon: {name: 'fas fa-euro-sign'}, to: 'https://mydomain.com/parameters/billing', type: MENU_LINK_TYPE.V1, active: false}
  73. )
  74. })
  75. test('has only rights for menu secure_params', () => {
  76. ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'parameters_secure_page')
  77. // @ts-ignore
  78. expect(menuBuilder.build().children[0]).toEqual(
  79. {label: 'secure_params', icon: {name: 'fas fa-lock'}, to: 'https://mydomain.com/parameters/secure', type: MENU_LINK_TYPE.V1, active: false}
  80. )
  81. })
  82. })