Преглед на файлове

add urlUtils.test.ts and validationUtils.test.ts

Olivier Massot преди 2 години
родител
ревизия
379aebad4a

+ 0 - 0
services/rights/abilityUtils.ts


+ 0 - 0
services/utils/url.ts


+ 7 - 7
services/utils/urlUtils.ts

@@ -17,7 +17,7 @@ class UrlUtils {
   }
 
   /**
-   * Prepend the 'https://' part if neither 'http://' of 'https://' is present, else: does nothing
+   * Prepend the 'https://' part if neither 'http://' nor 'https://' prefixes are present, else does nothing
    *
    * @param url
    */
@@ -44,7 +44,7 @@ class UrlUtils {
     uri = UrlUtils.prependHttps(uri)
 
     const urlParams = new URL(uri).searchParams;
-    let value: string | number | null = urlParams.get('page');
+    let value: string | number | null = urlParams.get(parameter);
 
     if (value && (default_ === null || Number.isInteger(default_)) && /^\d+$/.test(value)) {
       // On convertit automatiquement si et seulement la valeur par défaut est elle-même un entier ou n'est pas définie
@@ -56,17 +56,17 @@ class UrlUtils {
 
   /**
    * Extrait l'ID de l'URI passée en paramètre
-   * L'uri est supposée être de la forme `.../foo/bar/{id}`, où l'id est un identifiant numérique
+   * L'URI est supposée être de la forme `.../foo/bar/{id}`, où l'id est un identifiant numérique
    *
    * @param uri
    */
   public static extractIdFromUri (uri: string): number|null {
     const partUri: Array<string> = uri.split('/')
-    const id:any = partUri.pop()
-
-    if(isNaN(id))
-      throw new Error('id is not a number')
+    const id: any = partUri.pop()
 
+    if (!id || isNaN(id)) {
+      throw new Error('no id found')
+    }
     return parseInt(id)
   }
 

+ 90 - 0
tests/units/services/utils/urlUtils.test.ts

@@ -0,0 +1,90 @@
+import { describe, test, it, expect } from 'vitest'
+import UrlUtils from "~/services/utils/urlUtils";
+
+describe('join', () => {
+    test('simple cases', () => {
+        expect(UrlUtils.join('https://mydomain.com', 'foo')).toEqual('https://mydomain.com/foo')
+        expect(UrlUtils.join('https://mydomain.com/', '/foo')).toEqual('https://mydomain.com/foo')
+        expect(UrlUtils.join('https://mydomain.com/', 'foo/')).toEqual('https://mydomain.com/foo/')
+    })
+    test('more parts and various types', () => {
+        expect(UrlUtils.join('https://hoy.com', 'foo', 'bar', 10)).toEqual('https://hoy.com/foo/bar/10')
+    })
+    test('tail is empty', () => {
+        expect(UrlUtils.join('https://hoy.com')).toEqual('https://hoy.com')
+    })
+    test('relative uri', () => {
+        expect(UrlUtils.join('foo', 'bar')).toEqual('foo/bar')
+        expect(UrlUtils.join('/foo/', '/bar/')).toEqual('/foo/bar/')
+    })
+    test('empty base', () => {
+        expect(UrlUtils.join('', 'foo')).toEqual('/foo')
+        expect(UrlUtils.join('')).toEqual('')
+    })
+});
+
+describe('prependHttps', () => {
+    test('uri with no prefix', () => {
+        expect(UrlUtils.prependHttps('opentalent.fr')).toEqual('https://opentalent.fr')
+    })
+    test('uri with http prefix', () => {
+        expect(UrlUtils.prependHttps('http://opentalent.fr')).toEqual('http://opentalent.fr')
+    })
+    test('uri with https prefix', () => {
+        expect(UrlUtils.prependHttps('https://opentalent.fr')).toEqual('https://opentalent.fr')
+    })
+    test('uri with empty string', () => {
+        expect(UrlUtils.prependHttps('')).toEqual('https://')
+    })
+})
+
+describe('getParameter', () => {
+    test('get param from a simple url', () => {
+        expect(UrlUtils.getParameter('https://domain.fr/hello?foo=abc', 'foo')).toEqual('abc')
+    })
+    test('get a numeric param (no default value)', () => {
+        expect(UrlUtils.getParameter('https://domain.fr/hello?foo=123', 'foo')).toEqual(123)
+    })
+    test('get a numeric param (default value is also numeric)', () => {
+        expect(UrlUtils.getParameter('https://domain.fr/hello?foo=123', 'foo', 1)).toEqual(123)
+    })
+    test('get a numeric param (default value is not numeric)', () => {
+        expect(UrlUtils.getParameter('https://domain.fr/hello?foo=123', 'foo', 'hello')).toEqual('123')
+    })
+    test('try to get non-existing param (no default value)', () => {
+        expect(UrlUtils.getParameter('https://domain.fr/hello?foo=abc', 'bar')).toEqual(null)
+    })
+    test('try to get non-existing param, providing a default value', () => {
+        expect(UrlUtils.getParameter('https://domain.fr/hello?foo=abc', 'bar', 'def')).toEqual('def')
+    })
+    test('get param from a relative url', () => {
+        expect(UrlUtils.getParameter('/hello?foo=123', 'foo')).toEqual(123)
+    })
+})
+
+describe('extractIdFromUri', () => {
+    test('extract id from uri', () => {
+        expect(UrlUtils.extractIdFromUri('/api/organizations/1000')).toEqual(1000)
+    })
+    test('try to extract missing id', () => {
+        expect(() => UrlUtils.extractIdFromUri('/api/organizations/first')).toThrowError('no id found')
+    })
+    test('try to extract id from empty string', () => {
+        expect(() => UrlUtils.extractIdFromUri('')).toThrowError('no id found')
+    })
+})
+
+describe('split', () => {
+    test('split relative url', () => {
+        expect(UrlUtils.split('foo/bar/1')).toEqual(['foo', 'bar', '1'])
+    })
+    test('split relative url with prepended slash', () => {
+        expect(UrlUtils.split('/foo/bar/1')).toEqual(['foo', 'bar', '1'])
+    })
+    test('split complete url', () => {
+        expect(UrlUtils.split('https://test.com/api/access/123')).toEqual(['https:', 'test.com', 'api', 'access', '123'])
+    })
+    test('split empty string', () => {
+        expect(UrlUtils.split('')).toEqual([])
+    })
+})

+ 17 - 0
tests/units/services/utils/validationUtils.test.ts

@@ -0,0 +1,17 @@
+import { describe, test, it, expect } from 'vitest'
+import ValidationUtils from "~/services/utils/validationUtils";
+
+describe('validEmail', () => {
+    test('with valid email', () => {
+        const validationUtils = new ValidationUtils()
+        expect(validationUtils.validEmail('foo@gmail.com')).toBeTruthy()
+    })
+    test('with empty string', () => {
+        const validationUtils = new ValidationUtils()
+        expect(validationUtils.validEmail('')).toBeFalsy()
+    })
+    test('with invalid string', () => {
+        const validationUtils = new ValidationUtils()
+        expect(validationUtils.validEmail('azerty')).toBeFalsy()
+    })
+})