websiteListMenuBuilder.test.ts 4.1 KB

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