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')
    // })
})

