Browse Source

add tests for accessMenuBuilder

Olivier Massot 2 years ago
parent
commit
624619de54

+ 2 - 3
services/menuBuilder/accessMenuBuilder.ts

@@ -16,9 +16,8 @@ export default class AccessMenuBuilder extends AbstractMenuBuilder {
     const children: MenuItems = []
     const children: MenuItems = []
 
 
     if (this.ability.can('display', 'accesses_page')) {
     if (this.ability.can('display', 'accesses_page')) {
-      // TODO: voir si possible de passer par une injection de dépendance plutôt que par un use
-      const organizationProfile = useOrganizationProfileStore()
-      const to = organizationProfile.isSchool ? '/students/list/' : '/adherent/list/'
+      // @ts-ignore
+      const to = this.organizationProfile.isSchool ? '/students/list/' : '/adherent/list/'
       children.push(this.createItem('person', {name: 'fas fa-user'}, to, MENU_LINK_TYPE.V1))
       children.push(this.createItem('person', {name: 'fas fa-user'}, to, MENU_LINK_TYPE.V1))
     }
     }
 
 

+ 108 - 0
tests/units/services/menuBuilder/accessMenuBuilder.test.ts

@@ -0,0 +1,108 @@
+import { describe, test, it, expect } from 'vitest'
+import {RuntimeConfig} from "@nuxt/schema";
+import {AnyAbility} from "@casl/ability/dist/types";
+import {AccessProfile, organizationState} from "~/types/interfaces";
+import AccessMenuBuilder from "~/services/menuBuilder/accessMenuBuilder";
+import {MenuGroup} from "~/types/layout";
+import {MENU_LINK_TYPE} from "~/types/enum/layout";
+
+let runtimeConfig: RuntimeConfig
+let ability: AnyAbility
+let organizationProfile: organizationState
+let accessProfile: AccessProfile
+let menuBuilder: AccessMenuBuilder
+
+beforeEach(()=> {
+    runtimeConfig = vi.fn() as any as RuntimeConfig
+    ability = vi.fn() as any as AnyAbility
+    organizationProfile = vi.fn() as any as organizationState
+    accessProfile = vi.fn() as any as AccessProfile
+
+    runtimeConfig.baseUrlAdminLegacy = 'https://mydomain.com/'
+
+    menuBuilder = new AccessMenuBuilder(runtimeConfig, ability, organizationProfile, accessProfile)
+})
+
+describe('getMenuName', () => {
+    test('validate name', () => {
+        expect(menuBuilder.getMenuName()).toEqual("Access")
+    })
+})
+
+describe('build', () => {
+    test('has all items', () => {
+        // @ts-ignore
+        organizationProfile.isSchool = true
+        ability.can = vi.fn(() => true)
+
+        // Should return a MenuGroup
+        const result = menuBuilder.build() as MenuGroup
+
+        expect(result.label).toEqual('address_book')
+        expect(result.icon).toEqual({name: 'fas fa-address-book'})
+        // @ts-ignore
+        expect(result.children.length).toEqual(6)
+    })
+
+    test('has no items', () => {
+        ability.can = vi.fn(() => false)
+        expect(menuBuilder.build()).toEqual(null)
+    })
+
+    test('has only rights for menu person', () => {
+        ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'accesses_page')
+        // @ts-ignore
+        organizationProfile.isSchool = true
+
+        expect(menuBuilder.build()).toEqual(
+            {label: 'person', icon: {name: 'fas fa-user'}, to: 'https://mydomain.com/students/list/', type: MENU_LINK_TYPE.V1, active: false}
+        )
+
+        // @ts-ignore
+        organizationProfile.isSchool = false
+
+        expect(menuBuilder.build()).toEqual(
+            {label: 'person', icon: {name: 'fas fa-user'}, to: 'https://mydomain.com/adherent/list/', type: MENU_LINK_TYPE.V1, active: false}
+        )
+    })
+
+    test('has only rights for menu family_view', () => {
+        ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'student_registration_page')
+
+        expect(menuBuilder.build()).toEqual(
+            {label: 'family_view', icon: {name: 'fas fa-users'}, to: 'https://mydomain.com/student_registration/new', type: MENU_LINK_TYPE.V1, active: false}
+        )
+    })
+
+    test('has only rights for menu education_student_next_year', () => {
+        ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'education_student_next_year_page')
+
+        expect(menuBuilder.build()).toEqual(
+            {label: 'education_student_next_year', icon: {name: 'fas fa-list-alt'}, to: 'https://mydomain.com/education_student_next_year/list/', type: MENU_LINK_TYPE.V1, active: false}
+        )
+    })
+
+    test('has only rights for menu commissions_page', () => {
+        ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'commissions_page')
+
+        expect(menuBuilder.build()).toEqual(
+            {label: 'commissions', icon: {name: 'fas fa-street-view'}, to: 'https://mydomain.com/commissions/list/', type: MENU_LINK_TYPE.V1, active: false}
+        )
+    })
+
+    test('has only rights for menu network_children_page', () => {
+        ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'network_children_page')
+
+        expect(menuBuilder.build()).toEqual(
+            {label: 'network', icon: {name: 'fas fa-sitemap'}, to: 'https://mydomain.com/networks/list/', type: MENU_LINK_TYPE.V1, active: false}
+        )
+    })
+
+    test('has only rights for menu network_parents_page', () => {
+        ability.can = vi.fn((action: string, subject: string) => action === 'display' && subject === 'network_parents_page')
+
+        expect(menuBuilder.build()).toEqual(
+            {label: 'my_network', icon: {name: 'fas fa-sitemap'}, to: 'https://mydomain.com/network_artist_schools/list/', type: MENU_LINK_TYPE.V1, active: false}
+        )
+    })
+})