accountMenuBuilder.test.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import { describe, expect, test, beforeEach, vi } from 'vitest'
  2. import type { RuntimeConfig } from '@nuxt/schema'
  3. import type { AnyAbility } from '@casl/ability/dist/types'
  4. import type { AccessProfile, organizationState } from '~/types/interfaces'
  5. import AccountMenuBuilder from '~/services/layout/menuBuilder/accountMenuBuilder'
  6. import type { MenuGroup } from '~/types/layout'
  7. import { MENU_LINK_TYPE } from '~/types/enum/layout'
  8. import { GENDER } from '~/types/enum/enums'
  9. let runtimeConfig: RuntimeConfig
  10. let ability: AnyAbility
  11. let organizationProfile: organizationState
  12. let accessProfile: AccessProfile
  13. let menuBuilder: AccountMenuBuilder
  14. beforeEach(() => {
  15. runtimeConfig = vi.fn() as any as RuntimeConfig
  16. ability = vi.fn() as any as AnyAbility
  17. organizationProfile = vi.fn() as any as organizationState
  18. accessProfile = vi.fn() as any as AccessProfile
  19. runtimeConfig.baseUrlAdminLegacy = 'https://mydomain.com/'
  20. accessProfile.id = 123
  21. menuBuilder = new AccountMenuBuilder(
  22. runtimeConfig,
  23. ability,
  24. organizationProfile,
  25. accessProfile,
  26. )
  27. })
  28. describe('getMenuName', () => {
  29. test('validate name', () => {
  30. expect(menuBuilder.getMenuName()).toEqual('Account')
  31. })
  32. })
  33. describe('build', () => {
  34. test('has all items (mister)', () => {
  35. ability.can = vi.fn(() => true)
  36. // Should return a MenuGroup
  37. const result = menuBuilder.build() as MenuGroup
  38. expect(result.label).toEqual('my_account')
  39. // @ts-ignore
  40. expect(result.children.length).toEqual(14)
  41. // @ts-ignore
  42. expect(result.actions.length).toEqual(1)
  43. // Has the logout action
  44. // @ts-ignore
  45. expect(result.actions).toEqual([
  46. {
  47. label: 'logout',
  48. icon: undefined,
  49. to: 'https://mydomain.com/#/logout',
  50. type: MENU_LINK_TYPE.V1,
  51. active: false,
  52. },
  53. ])
  54. })
  55. test('has profile icon : mister)', () => {
  56. ability.can = vi.fn(() => false)
  57. accessProfile.avatarId = 100
  58. accessProfile.gender = GENDER.MISTER
  59. const result = menuBuilder.build() as MenuGroup
  60. expect(result.icon).toEqual({
  61. avatarId: 100,
  62. avatarByDefault: '/images/default/men-1.png',
  63. })
  64. })
  65. test('has profile icon : miss)', () => {
  66. ability.can = vi.fn(() => false)
  67. accessProfile.avatarId = 100
  68. accessProfile.gender = GENDER.MISS
  69. const result = menuBuilder.build() as MenuGroup
  70. expect(result.icon).toEqual({
  71. avatarId: 100,
  72. avatarByDefault: '/images/default/women-1.png',
  73. })
  74. })
  75. test('has no items', () => {
  76. ability.can = vi.fn(() => false)
  77. const group = menuBuilder.build()
  78. // AccountMenuBuilder retourne toujours un groupe
  79. // @ts-ignore
  80. expect(group.children).toEqual([])
  81. // Still has the logout action
  82. // @ts-ignore
  83. expect(group.actions).toEqual([
  84. {
  85. label: 'logout',
  86. icon: undefined,
  87. to: 'https://mydomain.com/#/logout',
  88. type: MENU_LINK_TYPE.V1,
  89. active: false,
  90. },
  91. ])
  92. })
  93. test('has only rights for menu my_schedule_page', () => {
  94. ability.can = vi.fn(
  95. (action: string, subject: string) =>
  96. action === 'display' && subject === 'my_schedule_page',
  97. )
  98. // @ts-ignore
  99. expect(menuBuilder.build().children[0]).toEqual({
  100. label: 'my_schedule_page',
  101. icon: undefined,
  102. to: 'https://mydomain.com/#/my_calendar',
  103. type: MENU_LINK_TYPE.V1,
  104. active: false,
  105. })
  106. })
  107. test('has only rights for menu attendance_bookings_menu', () => {
  108. ability.can = vi.fn(
  109. (action: string, subject: string) =>
  110. action === 'display' && subject === 'attendance_bookings_page',
  111. )
  112. // @ts-ignore
  113. expect(menuBuilder.build().children[0]).toEqual({
  114. label: 'attendance_bookings_menu',
  115. icon: undefined,
  116. to: 'https://mydomain.com/#/own_attendance',
  117. type: MENU_LINK_TYPE.V1,
  118. active: false,
  119. })
  120. })
  121. test('has only rights for menu my_attendance', () => {
  122. ability.can = vi.fn(
  123. (action: string, subject: string) =>
  124. action === 'display' && subject === 'my_attendance_page',
  125. )
  126. // @ts-ignore
  127. expect(menuBuilder.build().children[0]).toEqual({
  128. label: 'my_attendance',
  129. icon: undefined,
  130. to: 'https://mydomain.com/#/my_attendances/list/',
  131. type: MENU_LINK_TYPE.V1,
  132. active: false,
  133. })
  134. })
  135. test('has only rights for menu my_invitation', () => {
  136. ability.can = vi.fn(
  137. (action: string, subject: string) =>
  138. action === 'display' && subject === 'my_invitation_page',
  139. )
  140. // @ts-ignore
  141. expect(menuBuilder.build().children[0]).toEqual({
  142. label: 'my_invitation',
  143. icon: undefined,
  144. to: 'https://mydomain.com/#/my_invitations/list/',
  145. type: MENU_LINK_TYPE.V1,
  146. active: false,
  147. })
  148. })
  149. test('has only rights for menu my_students', () => {
  150. ability.can = vi.fn(
  151. (action: string, subject: string) =>
  152. action === 'display' && subject === 'my_students_page',
  153. )
  154. // @ts-ignore
  155. expect(menuBuilder.build().children[0]).toEqual({
  156. label: 'my_students',
  157. icon: undefined,
  158. to: 'https://mydomain.com/#/my_students/list/',
  159. type: MENU_LINK_TYPE.V1,
  160. active: false,
  161. })
  162. })
  163. test('has only rights for menu my_students_education_students', () => {
  164. ability.can = vi.fn(
  165. (action: string, subject: string) =>
  166. action === 'display' &&
  167. subject === 'my_students_education_students_page',
  168. )
  169. // @ts-ignore
  170. expect(menuBuilder.build().children[0]).toEqual({
  171. label: 'my_students_education_students',
  172. icon: undefined,
  173. to: 'https://mydomain.com/#/my_students_education_students/list/',
  174. type: MENU_LINK_TYPE.V1,
  175. active: false,
  176. })
  177. })
  178. test('has only rights for menu my_education_students', () => {
  179. ability.can = vi.fn(
  180. (action: string, subject: string) =>
  181. action === 'display' && subject === 'my_education_students_page',
  182. )
  183. // @ts-ignore
  184. expect(menuBuilder.build().children[0]).toEqual({
  185. label: 'my_education_students',
  186. icon: undefined,
  187. to: 'https://mydomain.com/#/main/my_profile/123/dashboard/my_education_students/list/',
  188. type: MENU_LINK_TYPE.V1,
  189. active: false,
  190. })
  191. })
  192. test('has only rights for menu send_an_email', () => {
  193. ability.can = vi.fn(
  194. (action: string, subject: string) =>
  195. action === 'display' && subject === 'send_an_email_page',
  196. )
  197. // @ts-ignore
  198. expect(menuBuilder.build().children[0]).toEqual({
  199. label: 'send_an_email',
  200. icon: undefined,
  201. to: 'https://mydomain.com/#/list/create/emails',
  202. type: MENU_LINK_TYPE.V1,
  203. active: false,
  204. })
  205. })
  206. test('has only rights for menu my_documents', () => {
  207. ability.can = vi.fn(
  208. (action: string, subject: string) =>
  209. action === 'display' && subject === 'my_documents_page',
  210. )
  211. // @ts-ignore
  212. expect(menuBuilder.build().children[0]).toEqual({
  213. label: 'my_documents',
  214. icon: undefined,
  215. to: 'https://mydomain.com/#/main/my_profile/123/dashboard/show/my_access_file',
  216. type: MENU_LINK_TYPE.V1,
  217. active: false,
  218. })
  219. })
  220. test('has only rights for menu my_profile', () => {
  221. ability.can = vi.fn(
  222. (action: string, subject: string) =>
  223. action === 'display' && subject === 'my_profile_page',
  224. )
  225. // @ts-ignore
  226. expect(menuBuilder.build().children[0]).toEqual({
  227. label: 'my_profile',
  228. icon: undefined,
  229. to: 'https://mydomain.com/#/main/my_profile/123/dashboard',
  230. type: MENU_LINK_TYPE.V1,
  231. active: false,
  232. })
  233. })
  234. test('has only rights for menu adherent_list', () => {
  235. ability.can = vi.fn(
  236. (action: string, subject: string) =>
  237. action === 'display' && subject === 'adherent_list_page',
  238. )
  239. // @ts-ignore
  240. expect(menuBuilder.build().children[0]).toEqual({
  241. label: 'adherent_list',
  242. icon: undefined,
  243. to: 'https://mydomain.com/#/adherent_contacts/list/',
  244. type: MENU_LINK_TYPE.V1,
  245. active: false,
  246. })
  247. })
  248. test('has only rights for menu my_bills', () => {
  249. ability.can = vi.fn(
  250. (action: string, subject: string) =>
  251. action === 'display' && subject === 'my_bills_page',
  252. )
  253. // @ts-ignore
  254. expect(menuBuilder.build().children[0]).toEqual({
  255. label: 'my_bills',
  256. icon: undefined,
  257. to: 'https://mydomain.com/#/main/my_profile/123/dashboard/show/my_bills',
  258. type: MENU_LINK_TYPE.V1,
  259. active: false,
  260. })
  261. })
  262. test('has only rights for menu print_my_licence', () => {
  263. ability.can = vi.fn(
  264. (action: string, subject: string) =>
  265. action === 'display' && subject === 'cmf_licence_person_page',
  266. )
  267. // @ts-ignore
  268. expect(menuBuilder.build().children[0]).toEqual({
  269. label: 'print_my_licence',
  270. icon: undefined,
  271. to: 'https://mydomain.com/#/licence_cmf/user',
  272. type: MENU_LINK_TYPE.V1,
  273. active: false,
  274. })
  275. })
  276. })