websiteListMenuBuilder.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { describe, test, expect, vi, beforeEach } from 'vitest'
  2. import type { RuntimeConfig } from '@nuxt/schema'
  3. //@ts-expect-error false error
  4. import type { AnyAbility } from '@casl/ability/dist/types'
  5. import type { Router } from 'vue-router'
  6. import type { AccessProfile, organizationState } from '~/types/interfaces'
  7. import WebsiteListMenuBuilder from '~/services/layout/menuBuilder/websiteListMenuBuilder'
  8. import type { MenuGroup } from '~/types/layout'
  9. import { MENU_LINK_TYPE } from '~/types/enum/layout'
  10. let runtimeConfig: RuntimeConfig
  11. let ability: AnyAbility
  12. let organizationProfile: organizationState
  13. let accessProfile: AccessProfile
  14. let menuBuilder: WebsiteListMenuBuilder
  15. let router: Router
  16. beforeEach(() => {
  17. runtimeConfig = vi.fn() as any as RuntimeConfig
  18. ability = vi.fn() as any as AnyAbility
  19. organizationProfile = {} as any as organizationState
  20. accessProfile = vi.fn() as any as AccessProfile
  21. // @ts-ignore
  22. router = vi.fn() as Router
  23. runtimeConfig.baseUrlAdminLegacy = 'https://mydomain.com/'
  24. menuBuilder = new WebsiteListMenuBuilder(
  25. runtimeConfig,
  26. ability,
  27. organizationProfile,
  28. accessProfile,
  29. router,
  30. )
  31. })
  32. describe('getMenuName', () => {
  33. test('validate name', () => {
  34. expect(menuBuilder.getMenuName()).toEqual('WebsiteList')
  35. })
  36. })
  37. describe('build', () => {
  38. test('without website and parents', () => {
  39. organizationProfile.website = null
  40. organizationProfile.parents = []
  41. expect(menuBuilder.build()).toEqual(null)
  42. })
  43. test('with website but no parents', () => {
  44. organizationProfile.name = 'MyOrganization'
  45. organizationProfile.website = 'https://some-website.com'
  46. organizationProfile.parents = []
  47. const result = menuBuilder.build() as MenuGroup
  48. expect(result.children).toEqual([
  49. {
  50. label: 'MyOrganization',
  51. icon: undefined,
  52. to: 'https://some-website.com',
  53. target: '_self',
  54. type: MENU_LINK_TYPE.EXTERNAL,
  55. active: false,
  56. endOfSubsection: false,
  57. },
  58. ])
  59. })
  60. test('with parents but no website', () => {
  61. organizationProfile.name = 'MyOrganization'
  62. organizationProfile.website = ''
  63. organizationProfile.parents = [
  64. { id: 1, name: 'parent1', website: 'https://parent1.net' },
  65. { id: 2, name: 'parent2', website: 'https://parent2.net' },
  66. { id: 3, name: 'parent3', website: 'https://parent3.net' },
  67. ]
  68. const result = menuBuilder.build() as MenuGroup
  69. expect(result.children).toEqual([
  70. {
  71. label: 'parent1',
  72. icon: undefined,
  73. to: 'https://parent1.net',
  74. target: '_self',
  75. type: MENU_LINK_TYPE.EXTERNAL,
  76. active: false,
  77. endOfSubsection: false,
  78. },
  79. {
  80. label: 'parent2',
  81. icon: undefined,
  82. to: 'https://parent2.net',
  83. target: '_self',
  84. type: MENU_LINK_TYPE.EXTERNAL,
  85. active: false,
  86. endOfSubsection: false,
  87. },
  88. {
  89. label: 'parent3',
  90. icon: undefined,
  91. to: 'https://parent3.net',
  92. target: '_self',
  93. type: MENU_LINK_TYPE.EXTERNAL,
  94. active: false,
  95. endOfSubsection: false,
  96. },
  97. ])
  98. })
  99. test('with parents and website, opentalent is excluded from parents', () => {
  100. organizationProfile.name = 'MyOrganization'
  101. organizationProfile.website = 'https://some-website.com'
  102. organizationProfile.parents = [
  103. { id: 1, name: 'parent1', website: 'https://parent1.net' },
  104. { id: 2, name: 'parent2', website: 'https://parent2.net' },
  105. { id: 3, name: 'parent3', website: 'https://parent3.net' },
  106. ]
  107. WebsiteListMenuBuilder.OPENTALENT_MANAGER_ID = 3
  108. const result = menuBuilder.build() as MenuGroup
  109. expect(result.children).toEqual([
  110. {
  111. label: 'MyOrganization',
  112. icon: undefined,
  113. to: 'https://some-website.com',
  114. target: '_self',
  115. type: MENU_LINK_TYPE.EXTERNAL,
  116. active: false,
  117. endOfSubsection: false,
  118. },
  119. {
  120. label: 'parent1',
  121. icon: undefined,
  122. to: 'https://parent1.net',
  123. target: '_self',
  124. type: MENU_LINK_TYPE.EXTERNAL,
  125. active: false,
  126. endOfSubsection: false,
  127. },
  128. {
  129. label: 'parent2',
  130. icon: undefined,
  131. to: 'https://parent2.net',
  132. target: '_self',
  133. type: MENU_LINK_TYPE.EXTERNAL,
  134. active: false,
  135. endOfSubsection: false,
  136. },
  137. ])
  138. })
  139. })