websiteListMenuBuilder.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { describe, test, expect, vi, beforeEach } 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 WebsiteListMenuBuilder from '~/services/layout/menuBuilder/websiteListMenuBuilder'
  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: WebsiteListMenuBuilder
  14. let router: Router
  15. beforeEach(() => {
  16. runtimeConfig = vi.fn() as any as RuntimeConfig
  17. ability = vi.fn() as any as AnyAbility
  18. organizationProfile = {} 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 WebsiteListMenuBuilder(
  24. runtimeConfig,
  25. ability,
  26. organizationProfile,
  27. accessProfile,
  28. router,
  29. )
  30. })
  31. describe('getMenuName', () => {
  32. test('validate name', () => {
  33. expect(menuBuilder.getMenuName()).toEqual('WebsiteList')
  34. })
  35. })
  36. describe('build', () => {
  37. test('without website and parents', () => {
  38. organizationProfile.website = null
  39. organizationProfile.parents = []
  40. expect(menuBuilder.build()).toEqual(null)
  41. })
  42. test('with website but no parents', () => {
  43. organizationProfile.name = 'MyOrganization'
  44. organizationProfile.website = 'https://some-website.com'
  45. organizationProfile.parents = []
  46. const result = menuBuilder.build() as MenuGroup
  47. expect(result.children).toEqual([
  48. {
  49. label: 'MyOrganization',
  50. icon: undefined,
  51. to: 'https://some-website.com',
  52. type: MENU_LINK_TYPE.EXTERNAL,
  53. active: false,
  54. },
  55. ])
  56. })
  57. test('with parents but no website', () => {
  58. organizationProfile.name = 'MyOrganization'
  59. organizationProfile.website = ''
  60. organizationProfile.parents = [
  61. { id: 1, name: 'parent1', website: 'https://parent1.net' },
  62. { id: 2, name: 'parent2', website: 'https://parent2.net' },
  63. { id: 3, name: 'parent3', website: 'https://parent3.net' },
  64. ]
  65. const result = menuBuilder.build() as MenuGroup
  66. expect(result.children).toEqual([
  67. {
  68. label: 'parent1',
  69. icon: undefined,
  70. to: 'https://parent1.net',
  71. type: MENU_LINK_TYPE.EXTERNAL,
  72. active: false,
  73. },
  74. {
  75. label: 'parent2',
  76. icon: undefined,
  77. to: 'https://parent2.net',
  78. type: MENU_LINK_TYPE.EXTERNAL,
  79. active: false,
  80. },
  81. {
  82. label: 'parent3',
  83. icon: undefined,
  84. to: 'https://parent3.net',
  85. type: MENU_LINK_TYPE.EXTERNAL,
  86. active: false,
  87. },
  88. ])
  89. })
  90. test('with parents and website, opentalent is excluded from parents', () => {
  91. organizationProfile.name = 'MyOrganization'
  92. organizationProfile.website = 'https://some-website.com'
  93. organizationProfile.parents = [
  94. { id: 1, name: 'parent1', website: 'https://parent1.net' },
  95. { id: 2, name: 'parent2', website: 'https://parent2.net' },
  96. { id: 3, name: 'parent3', website: 'https://parent3.net' },
  97. ]
  98. runtimeConfig.OPENTALENT_MANAGER_ID = 3
  99. const result = menuBuilder.build() as MenuGroup
  100. expect(result.children).toEqual([
  101. {
  102. label: 'MyOrganization',
  103. icon: undefined,
  104. to: 'https://some-website.com',
  105. type: MENU_LINK_TYPE.EXTERNAL,
  106. active: false,
  107. },
  108. {
  109. label: 'parent1',
  110. icon: undefined,
  111. to: 'https://parent1.net',
  112. type: MENU_LINK_TYPE.EXTERNAL,
  113. active: false,
  114. },
  115. {
  116. label: 'parent2',
  117. icon: undefined,
  118. to: 'https://parent2.net',
  119. type: MENU_LINK_TYPE.EXTERNAL,
  120. active: false,
  121. },
  122. ])
  123. })
  124. })