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

add objectUtils.test.ts

Olivier Massot 3 éve
szülő
commit
96201d5106

+ 9 - 4
services/utils/objectUtils.ts

@@ -9,6 +9,7 @@ export default class ObjectUtils {
   /**
    * Flatten un objet nested en un objet avec un seul niveau avec des noms de propriétés transformées comme cela 'foo.bar'
    * L'objet passé en paramètre reste inchangé car il est cloné
+   *
    * @example  cloneAndFlatten({ a: 1, b: { c: 2 }, d: { e: 3, f: { g: 4, h: 5 } }, i: { j: 6 } }, ['i']) => { a: 1, 'b.c': 2, 'd.e': 3, 'd.f.g': 4, 'd.f.h': 5, i: { j: 6 } } }
    * @param {AnyJson} object
    * @param {Array<string>} excludedProperties
@@ -46,6 +47,7 @@ export default class ObjectUtils {
 
   /**
    * Transforme un objet flattened en un objet nested. L'objet passé en paramètre reste inchangé
+   *
    * @example cloneAndNest({ a: 1, 'b.c': 2, 'd.e': 3, 'd.f.g': 4, 'd.f.h': 5 } ) => { a: 1, b: { c: 2 }, d: { e: 3, f: { g: 4, h: 5 } } }
    * @param {AnyJson} object
    * @return {AnyJson}
@@ -75,6 +77,7 @@ export default class ObjectUtils {
 
   /**
    * Teste si le paramètre est un objet
+   *
    * @param {AnyJson} value
    * @return {boolean}
    */
@@ -86,7 +89,8 @@ export default class ObjectUtils {
   }
 
   /**
-   * Clône l'objet et ses propriétés.
+   * Clone l'objet et ses propriétés.
+   *
    * @param {ObjectUtils} object
    * @return {ObjectUtils}
    */
@@ -100,11 +104,12 @@ export default class ObjectUtils {
   }
 
   /**
-   * Tri un objet par rapport à ses clés (par ordre alpha)
-   * @example sortObjectByKey({b:1, d:2, c:3, a:4}) => {a:4, b:1, c:3, d:2}
+   * Trie un objet selon ses clés (par ordre alphanumérique)
+   *
+   * @example sortObjectsByKey({b:1, d:2, c:3, a:4}) => {a:4, b:1, c:3, d:2}
    * @param toSort
    */
-  static sortObjectByKey (toSort: any): any {
+  static sortObjectsByKey (toSort: AnyJson): any {
     if (typeof toSort !== 'object') {
       throw new TypeError('Expecting an object parameter')
     }

+ 74 - 0
tests/units/services/utils/objectUtils.test.ts

@@ -0,0 +1,74 @@
+import { describe, test, it, expect } from 'vitest'
+import ObjectUtils from "~/services/utils/objectUtils";
+
+describe('cloneAndFlatten', () => {
+    test('If the object is already flat, it should return an identical object', () => {
+        expect(ObjectUtils.cloneAndFlatten({'a': 1, 'b': 2})).toEqual({'a': 1, 'b': 2})
+    })
+    test('With a nested object, it should return an flat object', () => {
+        expect(ObjectUtils.cloneAndFlatten({'a': 1, 'b': { 'c': 3 }})).toEqual({'a': 1, 'b.c': 3})
+    })
+    test('With an empty object, it should return an empty object', () => {
+        expect(ObjectUtils.cloneAndFlatten({})).toEqual({})
+    })
+    // test('With an non-object, it should throw an error', () => {
+    //     // TODO: comment ça pourrait ne pas être un objet?
+    //     expect(ObjectUtils.cloneAndFlatten([])).toThrowError('Expecting an object parameter')
+    // })
+})
+
+describe('cloneAndNest', () => {
+    test('If the object is already nested, it should return an identical object', () => {
+        expect(ObjectUtils.cloneAndNest({'a': 1, 'b': { 'c': 3 }})).toEqual({'a': 1, 'b': { 'c': 3 }})
+    })
+    test('With a flatten object, it should return an nested object', () => {
+        expect(ObjectUtils.cloneAndNest({'a': 1, 'b.c': 3})).toEqual({'a': 1, 'b': { 'c': 3 }})
+    })
+    test('With an empty object, it should return an empty object', () => {
+        expect(ObjectUtils.cloneAndNest({})).toEqual({})
+    })
+})
+
+describe('isObject', () => {
+    test('with object', () => {
+        expect(ObjectUtils.isObject({ 'a': 1 })).toBeTruthy()
+    })
+    test('with array', () => {
+        expect(ObjectUtils.isObject([ 1, 2, 3 ])).toBeFalsy()
+    })
+    test('with null', () => {
+        expect(ObjectUtils.isObject(null)).toBeFalsy()
+    })
+    test('with date', () => {
+        expect(ObjectUtils.isObject(new Date())).toBeFalsy()
+    })
+})
+
+describe('clone', () => {
+    test('with simple object', () => {
+        const initial = { 'a': 1 }
+        let result = ObjectUtils.clone(initial)
+        result['b'] = 2
+        expect(initial).toEqual({ 'a': 1 })
+    })
+    test('with empty object', () => {
+        expect(ObjectUtils.clone({})).toEqual({})
+    })
+    test('with nested object', () => {
+        expect(ObjectUtils.clone({'a': 1, 'b': { 'c': 3 }})).toEqual({'a': 1, 'b': { 'c': 3 }})
+    })
+})
+
+describe('sortObjectsByKey', () => {
+    test('with simple object', () => {
+        expect(ObjectUtils.sortObjectsByKey({b:1, d:2, c:3, a:4})).toEqual({a:4, b:1, c:3, d:2})
+    })
+    test('with empty object', () => {
+        expect(ObjectUtils.sortObjectsByKey({})).toEqual({})
+    })
+    // test('with non-object', () => {
+    //     // TODO: comment ça pourrait ne pas être un objet?
+    //     expect(ObjectUtils.sortObjectsByKey({})).toThrowError('Expecting an object parameter')
+    // })
+})
+