Преглед изворни кода

add myAccessesMenuBuilder.test.ts and myFamilyMenuBuilder.test.ts

Olivier Massot пре 2 година
родитељ
комит
38c10995eb

+ 59 - 0
tests/units/services/menuBuilder/myAccessesMenuBuilder.test.ts

@@ -0,0 +1,59 @@
+
+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 MyAccessesMenuBuilder from "~/services/menuBuilder/myAccessesMenuBuilder";
+import {MenuGroup, MenuItem} 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: MyAccessesMenuBuilder
+
+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 MyAccessesMenuBuilder(runtimeConfig, ability, organizationProfile, accessProfile)
+})
+
+
+describe('getMenuName', () => {
+    test('validate name', () => {
+        expect(menuBuilder.getMenuName()).toEqual("MyAccesses")
+    })
+})
+
+describe('build', () => {
+    test('default : has no items, even with rights', () => {
+        ability.can = vi.fn(() => true)
+        expect(menuBuilder.build()).toEqual(null)
+    })
+
+    test('with enabled multi accesses', () => {
+        accessProfile.multiAccesses = [
+            { id: 1, name: 'Bob' },
+            { id: 2, name: 'Séraphin' },
+            { id: 3, name: 'Lilou' },
+        ]
+
+        const result = menuBuilder.build() as MenuGroup
+
+        expect(result.label).toEqual('multiAccesses')
+        expect(result.icon).toEqual({name: 'fas fa-building'})
+
+        expect(result.children).toEqual([
+            { label: 'Bob', icon: undefined, to: 'https://mydomain.com/switch/1', type: MENU_LINK_TYPE.V1, active: false },
+            { label: 'Séraphin', icon: undefined, to: 'https://mydomain.com/switch/2', type: MENU_LINK_TYPE.V1, active: false },
+            { label: 'Lilou', icon: undefined, to: 'https://mydomain.com/switch/3', type: MENU_LINK_TYPE.V1, active: false }
+        ])
+    })
+})
+

+ 72 - 0
tests/units/services/menuBuilder/myFamilyMenuBuilder.test.ts

@@ -0,0 +1,72 @@
+import {describe, expect, test} from 'vitest'
+import {RuntimeConfig} from "@nuxt/schema";
+import {AnyAbility} from "@casl/ability/dist/types";
+import {AccessProfile, organizationState} from "~/types/interfaces";
+import MyFamilyMenuBuilder from "~/services/menuBuilder/myFamilyMenuBuilder";
+import {GENDER} from "~/types/enum/enums";
+import {MENU_LINK_TYPE} from "~/types/enum/layout";
+import {MenuGroup} from "~/types/layout";
+
+let runtimeConfig: RuntimeConfig
+let ability: AnyAbility
+let organizationProfile: organizationState
+let accessProfile: AccessProfile
+let menuBuilder: MyFamilyMenuBuilder
+
+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 MyFamilyMenuBuilder(runtimeConfig, ability, organizationProfile, accessProfile)
+})
+
+
+describe('getMenuName', () => {
+    test('validate name', () => {
+        expect(menuBuilder.getMenuName()).toEqual("MyFamily")
+    })
+})
+
+describe('build', () => {
+    test('no item by default', () => {
+        ability.can = vi.fn(() => true)
+        expect(menuBuilder.build()).toEqual(null)
+    })
+
+    test('with family accesses', () => {
+        ability.can = vi.fn(() => true)
+
+        organizationProfile.id = 100
+        accessProfile.id = 1
+
+        accessProfile.familyAccesses = [
+            { id: 1, name: 'Bob', givenName: 'Dupont', gender: GENDER.MISTER, avatarId: 1 },
+            { id: 2, name: 'Séraphin', givenName: 'Dupuis', gender: GENDER.MISTER, avatarId: 2 },
+            { id: 3, name: 'Lilou', givenName: 'Dubois', gender: GENDER.MISS, avatarId: 3 },
+        ]
+
+        accessProfile.originalAccess = {
+            id: 4,
+            name: 'Tony',
+            givenName: 'Soprano',
+            gender: GENDER.MISTER,
+            avatarId: 4,
+            isSuperAdminAccess: false,
+            organization: { id: 100, name: 'my_organization' }
+        }
+
+        const result = menuBuilder.build() as MenuGroup
+
+        expect(result.children).toEqual([
+            { label: 'Dupont Bob', icon: { avatarId: 1, avatarByDefault: '/images/default/men-1.png'}, to: 'https://mydomain.com/switch_user/100/1/1', type: MENU_LINK_TYPE.V1, active: false },
+            { label: 'Dupuis Séraphin', icon: { avatarId: 2, avatarByDefault: '/images/default/men-1.png'}, to: 'https://mydomain.com/switch_user/100/1/2', type: MENU_LINK_TYPE.V1, active: false },
+            { label: 'Dubois Lilou', icon: { avatarId: 3, avatarByDefault: '/images/default/women-1.png'}, to: 'https://mydomain.com/switch_user/100/1/3', type: MENU_LINK_TYPE.V1, active: false },
+            { label: 'Soprano Tony', icon: undefined, to: 'https://mydomain.com/switch_user/100/4/exit', type: MENU_LINK_TYPE.V1, active: false }
+        ])
+    })
+})
+