| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- import { describe, test, it, expect } from 'vitest'
- import ObjectUtils from '~/services/utils/objectUtils'
- import StringUtils from '~/services/utils/stringUtils'
- 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 }
- const 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')
- // })
- })
- describe('hash', () => {
- test('empty object', async () => {
- expect(await ObjectUtils.hash({})).toBe(
- 'bf21a9e8fbc5a3846fb05b4fa0859e0917b2202f',
- )
- })
- test('simple object', async () => {
- expect(await ObjectUtils.hash({ a: 1, b: 2, c: 3 })).toBe(
- 'e7ec4a8f2309bdd4c4c57cb2adfb79c91a293597',
- )
- })
- test('simple unsorted object', async () => {
- expect(await ObjectUtils.hash({ b: 2, a: 1, c: 3 })).toBe(
- 'e7ec4a8f2309bdd4c4c57cb2adfb79c91a293597',
- )
- })
- })
|