Forráskód Böngészése

complete unit tests

Olivier Massot 1 éve
szülő
commit
1dec95184f

+ 2 - 2
services/layout/menuBuilder/abstractMenuBuilder.ts

@@ -146,14 +146,14 @@ abstract class AbstractMenuBuilder implements MenuBuilder {
       const { pageName, icon } = item
 
       if (this.ability.can('display', pageName)) {
-        const to = this.router.resolve({ name: pageName }).href
+        const to = this.router.resolve({ name: pageName })
         if (!to) {
           throw new Error('unknown page name : ' + pageName)
         }
         children.push({
           icon: icon ? { name: icon } : undefined,
           label: pageName,
-          to,
+          to: to.href,
           type: MENU_LINK_TYPE.INTERNAL,
           active: false,
         })

+ 30 - 18
tests/units/services/layout/menuBuilder/abstractMenuBuilder.test.ts

@@ -6,7 +6,6 @@ import AbstractMenuBuilder from '~/services/layout/menuBuilder/abstractMenuBuild
 import type { IconItem, MenuGroup, MenuItem, MenuItems } from '~/types/layout'
 import { MENU_LINK_TYPE } from '~/types/enum/layout'
 import type { AccessProfile, organizationState } from '~/types/interfaces'
-import {result} from 'lodash-es';
 
 class TestableAbstractMenuBuilder extends AbstractMenuBuilder {
   static readonly menuName = 'TestableMenu'
@@ -37,12 +36,8 @@ class TestableAbstractMenuBuilder extends AbstractMenuBuilder {
     return super.buildSubmenu(menuBuilder)
   }
 
-  public addChildItemIfAllowed(
-    children: MenuItems,
-    pageName: string,
-    icon?: IconItem,
-  ) {
-    return super.addChildItemIfAllowed(children, pageName, icon)
+  public makeChildren(items: Array<{ pageName: string; icon?: string }>) {
+    return super.makeChildren(items)
   }
 }
 
@@ -175,7 +170,7 @@ describe('buildSubmenu', () => {
   })
 })
 
-describe('addChildItemIfAllowed', () => {
+describe('makeChildren', () => {
   test('simple call', () => {
     ability.can = vi.fn(() => true)
     // @ts-ignore
@@ -183,9 +178,30 @@ describe('addChildItemIfAllowed', () => {
       return { href: 'foo' }
     })
 
-    const children: MenuItems = []
+    const children: MenuItems = menuBuilder.makeChildren([
+      { pageName: 'foo_page', icon: 'fas fa-dog' },
+    ])
 
-    menuBuilder.addChildItemIfAllowed(children, 'foo_page')
+    expect(children).toEqual([
+      {
+        label: 'foo_page',
+        icon: { name: 'fas fa-dog' },
+        to: 'foo',
+        type: 0,
+        active: false,
+      },
+    ])
+  })
+  test('no icon', () => {
+    ability.can = vi.fn(() => true)
+    // @ts-ignore
+    router.resolve = vi.fn(() => {
+      return { href: 'foo' }
+    })
+
+    const children: MenuItems = menuBuilder.makeChildren([
+      { pageName: 'foo_page' },
+    ])
 
     expect(children).toEqual([
       { label: 'foo_page', icon: undefined, to: 'foo', type: 0, active: false },
@@ -194,9 +210,9 @@ describe('addChildItemIfAllowed', () => {
   test('not allowed', () => {
     ability.can = vi.fn(() => false)
 
-    const children: MenuItems = []
-
-    menuBuilder.addChildItemIfAllowed(children, 'foo_page')
+    const children: MenuItems = menuBuilder.makeChildren([
+      { pageName: 'foo_page' },
+    ])
 
     expect(children).toEqual([])
   })
@@ -207,12 +223,8 @@ describe('addChildItemIfAllowed', () => {
       return null
     })
 
-    const children: MenuItems = []
-
     expect(() =>
-      menuBuilder.addChildItemIfAllowed(children, 'foo_page'),
+      menuBuilder.makeChildren([{ pageName: 'foo_page' }]),
     ).toThrowError()
-
-    expect(children).toEqual([])
   })
 })

+ 42 - 106
tests/units/services/layout/menuBuilder/parametersMenuBuilder.test.ts

@@ -1,17 +1,32 @@
+import { AssertionError } from 'node:assert'
 import { describe, test, expect, beforeEach, vi } from 'vitest'
 import type { RuntimeConfig } from '@nuxt/schema'
 import type { AnyAbility } from '@casl/ability/dist/types'
 import type { Router } from 'vue-router'
 import type { AccessProfile, organizationState } from '~/types/interfaces'
 import ParametersMenuBuilder from '~/services/layout/menuBuilder/parametersMenuBuilder'
-import type { MenuGroup } from '~/types/layout'
-import { MENU_LINK_TYPE } from '~/types/enum/layout'
+import type { IconItem, MenuGroup, MenuItem, MenuItems } from '~/types/layout'
+
+class TestableParametersMenuBuilder extends ParametersMenuBuilder {
+  public createGroup(
+    label: string,
+    icon?: IconItem,
+    children: MenuItems = [],
+    actions: Array<MenuItem> = [],
+  ): MenuGroup {
+    return super.createGroup(label, icon, children, actions)
+  }
+
+  public makeChildren(items: Array<{ pageName: string; icon?: string }>) {
+    return super.makeChildren(items)
+  }
+}
 
 let runtimeConfig: RuntimeConfig
 let ability: AnyAbility
 let organizationProfile: organizationState
 let accessProfile: AccessProfile
-let menuBuilder: ParametersMenuBuilder
+let menuBuilder: TestableParametersMenuBuilder
 let router: Router
 
 beforeEach(() => {
@@ -24,7 +39,7 @@ beforeEach(() => {
 
   runtimeConfig.baseUrlAdminLegacy = 'https://mydomain.com/'
 
-  menuBuilder = new ParametersMenuBuilder(
+  menuBuilder = new TestableParametersMenuBuilder(
     runtimeConfig,
     ability,
     organizationProfile,
@@ -40,119 +55,40 @@ describe('getMenuName', () => {
 })
 
 describe('build', () => {
-  test('has all items', () => {
-    ability.can = vi.fn(() => true)
+  test('simple call', () => {
     // @ts-ignore
-    organizationProfile.isSchool = vi.fn(() => true)
-    organizationProfile.hasModule = vi.fn((name) => name === 'Sms')
-
-    // Should return a MenuGroup
-    const result = menuBuilder.build() as MenuGroup
-
-    expect(result.label).toEqual('parameters')
-    expect(result.icon).toEqual(undefined)
-    // @ts-ignore
-    expect(result.children.length).toEqual(11)
-  })
-
-  test('has no items', () => {
-    ability.can = vi.fn(() => false)
-    expect(menuBuilder.build()).toEqual(null)
-  })
-
-  test('has only rights for menu general_params', () => {
-    ability.can = vi.fn(
-      (action: string, subject: string) =>
-        action === 'display' && subject === 'parameters_page',
-    )
-
-    // @ts-ignore
-    expect(menuBuilder.build().children[0]).toEqual({
-      label: 'general_params',
-      icon: { name: 'fas fa-cogs' },
-      to: 'https://mydomain.com/#/parameters',
-      type: MENU_LINK_TYPE.V1,
-      active: false,
-    })
-  })
-
-  test('has only rights for menu communication_params', () => {
-    ability.can = vi.fn(
-      (action: string, subject: string) =>
-        action === 'display' && subject === 'parameters_communication_page',
-    )
-
-    // @ts-ignore
-    expect(menuBuilder.build().children[0]).toEqual({
-      label: 'communication_params',
-      icon: { name: 'fas fa-comments' },
-      to: 'https://mydomain.com/#/parameters/communication',
-      type: MENU_LINK_TYPE.V1,
-      active: false,
+    menuBuilder.makeChildren = vi.fn((items) => {
+      if (items.length !== 11) {
+        throw new AssertionError()
+      }
+      return ['foo', 'bar']
     })
-  })
-
-  test('has only rights for menu students_params', () => {
-    ability.can = vi.fn(
-      (action: string, subject: string) =>
-        action === 'display' && subject === 'parameters_student_page',
-    )
 
     // @ts-ignore
-    expect(menuBuilder.build().children[0]).toEqual({
-      label: 'students_params',
-      icon: { name: 'fas fa-users' },
-      to: 'https://mydomain.com/#/parameters/student',
-      type: MENU_LINK_TYPE.V1,
-      active: false,
+    menuBuilder.createGroup = vi.fn(() => {
+      return { children: ['foo', 'bar'] }
     })
-  })
-
-  test('has only rights for menu education_params', () => {
-    ability.can = vi.fn(
-      (action: string, subject: string) =>
-        action === 'display' && subject === 'parameters_education_page',
-    )
+    const result = menuBuilder.build()
 
-    // @ts-ignore
-    expect(menuBuilder.build().children[0]).toEqual({
-      label: 'education_params',
-      icon: { name: 'fas fa-graduation-cap' },
-      to: 'https://mydomain.com/#/parameters/education',
-      type: MENU_LINK_TYPE.V1,
-      active: false,
-    })
+    expect(result).toEqual({ children: ['foo', 'bar'] })
+    expect(menuBuilder.makeChildren).toHaveBeenCalledOnce()
+    expect(menuBuilder.createGroup).toHaveBeenCalledOnce()
   })
 
-  test('has only rights for menu bills_params', () => {
-    ability.can = vi.fn(
-      (action: string, subject: string) =>
-        action === 'display' && subject === 'parameters_bills_page',
-    )
-
+  test('has no items', () => {
     // @ts-ignore
-    expect(menuBuilder.build().children[0]).toEqual({
-      label: 'bills_params',
-      icon: { name: 'fas fa-euro-sign' },
-      to: 'https://mydomain.com/#/parameters/billing',
-      type: MENU_LINK_TYPE.V1,
-      active: false,
+    menuBuilder.makeChildren = vi.fn((items) => {
+      if (items.length !== 11) {
+        throw new AssertionError()
+      }
+      return []
     })
-  })
+    menuBuilder.createGroup = vi.fn()
 
-  test('has only rights for menu secure_params', () => {
-    ability.can = vi.fn(
-      (action: string, subject: string) =>
-        action === 'display' && subject === 'parameters_secure_page',
-    )
+    const result = menuBuilder.build()
 
-    // @ts-ignore
-    expect(menuBuilder.build().children[0]).toEqual({
-      label: 'secure_params',
-      icon: { name: 'fas fa-lock' },
-      to: 'https://mydomain.com/#/parameters/secure',
-      type: MENU_LINK_TYPE.V1,
-      active: false,
-    })
+    expect(result).toEqual(null)
+    expect(menuBuilder.makeChildren).toHaveBeenCalledOnce()
+    expect(menuBuilder.createGroup).toHaveBeenCalledTimes(0)
   })
 })