websiteListMenuBuilder.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. target: '_self',
  53. type: MENU_LINK_TYPE.EXTERNAL,
  54. active: false,
  55. endOfSubsection: false,
  56. },
  57. ])
  58. })
  59. test('with parents but no website', () => {
  60. organizationProfile.name = 'MyOrganization'
  61. organizationProfile.website = ''
  62. organizationProfile.parents = [
  63. { id: 1, name: 'parent1', website: 'https://parent1.net' },
  64. { id: 2, name: 'parent2', website: 'https://parent2.net' },
  65. { id: 3, name: 'parent3', website: 'https://parent3.net' },
  66. ]
  67. const result = menuBuilder.build() as MenuGroup
  68. expect(result.children).toEqual([
  69. {
  70. label: 'parent1',
  71. icon: undefined,
  72. to: 'https://parent1.net',
  73. target: '_self',
  74. type: MENU_LINK_TYPE.EXTERNAL,
  75. active: false,
  76. endOfSubsection: false,
  77. },
  78. {
  79. label: 'parent2',
  80. icon: undefined,
  81. to: 'https://parent2.net',
  82. target: '_self',
  83. type: MENU_LINK_TYPE.EXTERNAL,
  84. active: false,
  85. endOfSubsection: false,
  86. },
  87. {
  88. label: 'parent3',
  89. icon: undefined,
  90. to: 'https://parent3.net',
  91. target: '_self',
  92. type: MENU_LINK_TYPE.EXTERNAL,
  93. active: false,
  94. endOfSubsection: false,
  95. },
  96. ])
  97. })
  98. test('with parents and website, opentalent is excluded from parents', () => {
  99. organizationProfile.name = 'MyOrganization'
  100. organizationProfile.website = 'https://some-website.com'
  101. organizationProfile.parents = [
  102. { id: 1, name: 'parent1', website: 'https://parent1.net' },
  103. { id: 2, name: 'parent2', website: 'https://parent2.net' },
  104. { id: 3, name: 'parent3', website: 'https://parent3.net' },
  105. ]
  106. WebsiteListMenuBuilder.OPENTALENT_MANAGER_ID = 3
  107. const result = menuBuilder.build() as MenuGroup
  108. expect(result.children).toEqual([
  109. {
  110. label: 'MyOrganization',
  111. icon: undefined,
  112. to: 'https://some-website.com',
  113. target: '_self',
  114. type: MENU_LINK_TYPE.EXTERNAL,
  115. active: false,
  116. endOfSubsection: false,
  117. },
  118. {
  119. label: 'parent1',
  120. icon: undefined,
  121. to: 'https://parent1.net',
  122. target: '_self',
  123. type: MENU_LINK_TYPE.EXTERNAL,
  124. active: false,
  125. endOfSubsection: false,
  126. },
  127. {
  128. label: 'parent2',
  129. icon: undefined,
  130. to: 'https://parent2.net',
  131. target: '_self',
  132. type: MENU_LINK_TYPE.EXTERNAL,
  133. active: false,
  134. endOfSubsection: false,
  135. },
  136. ])
  137. })
  138. })