mirror of
https://github.com/dzeiocom/libs.git
synced 2025-04-22 10:52:11 +00:00
fix: Add tests to have a better array support
Signed-off-by: Avior <f.bouillon@aptatio.com>
This commit is contained in:
parent
9779e453f0
commit
c664af98ba
@ -4,7 +4,6 @@ import { isObject, objectClean, objectClone, objectEqual, objectKeys, objectLoop
|
|||||||
|
|
||||||
describe('Throw if parameter is not an object', () => {
|
describe('Throw if parameter is not an object', () => {
|
||||||
it('should works', () => {
|
it('should works', () => {
|
||||||
// @ts-ignore
|
|
||||||
expect(objectKeys).toThrow()
|
expect(objectKeys).toThrow()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -19,6 +18,16 @@ describe('Object Map tests', () => {
|
|||||||
return [index, value]
|
return [index, value]
|
||||||
})).toEqual([['pouet', 'first'],['toto','second']])
|
})).toEqual([['pouet', 'first'],['toto','second']])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should works on arrays', () => {
|
||||||
|
const obj = [
|
||||||
|
'first',
|
||||||
|
'second'
|
||||||
|
]
|
||||||
|
expect(objectMap(obj, (value, index) => {
|
||||||
|
return [index, value]
|
||||||
|
})).toEqual([[0, 'first'],[1, 'second']])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Object Loop Tests', () => {
|
describe('Object Loop Tests', () => {
|
||||||
@ -39,6 +48,23 @@ describe('Object Loop Tests', () => {
|
|||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should work on arrays', () => {
|
||||||
|
const obj = [
|
||||||
|
true,
|
||||||
|
'object-util'
|
||||||
|
]
|
||||||
|
objectLoop(obj, (value, key) => {
|
||||||
|
if (key === 0) {
|
||||||
|
expect(value).toBe(true)
|
||||||
|
} else if (key === 1) {
|
||||||
|
expect(value).toBe('object-util')
|
||||||
|
} else {
|
||||||
|
throw "it should not come here"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
it('Should return false', () => {
|
it('Should return false', () => {
|
||||||
const obj = {
|
const obj = {
|
||||||
pouet: true
|
pouet: true
|
||||||
@ -76,20 +102,28 @@ describe('Object To Array Tests', () => {
|
|||||||
}
|
}
|
||||||
expect(objectValues(obj)).toEqual(['first', 'second'])
|
expect(objectValues(obj)).toEqual(['first', 'second'])
|
||||||
})
|
})
|
||||||
|
// it('shoud work on arrays', () => {
|
||||||
|
// const obj = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||||
|
// expect(objectValues(obj)).toEqual(obj)
|
||||||
|
// })
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Object Keys Tests', () => {
|
describe('Object Keys Tests', () => {
|
||||||
it('Should Works', () => {
|
it('Should work on objects', () => {
|
||||||
const obj = {
|
const obj = {
|
||||||
pouet: 'first',
|
pouet: 'first',
|
||||||
toto: 'second'
|
toto: 'second'
|
||||||
}
|
}
|
||||||
expect(objectKeys(obj)).toEqual(['pouet', 'toto'])
|
expect(objectKeys(obj)).toEqual(['pouet', 'toto'])
|
||||||
})
|
})
|
||||||
|
it('shoud work on arrays', () => {
|
||||||
|
const obj = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||||
|
expect(objectKeys(obj)).toEqual(obj)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Object Size Tests', () => {
|
describe('Object Size Tests', () => {
|
||||||
it('shoud return length of the object', () => {
|
it('shoud return length of an object', () => {
|
||||||
const obj = {
|
const obj = {
|
||||||
index0: true,
|
index0: true,
|
||||||
index1: false,
|
index1: false,
|
||||||
@ -105,6 +139,10 @@ describe('Object Size Tests', () => {
|
|||||||
}
|
}
|
||||||
expect(objectSize(obj)).toBe(11)
|
expect(objectSize(obj)).toBe(11)
|
||||||
})
|
})
|
||||||
|
it('shoud return length of an array', () => {
|
||||||
|
const obj = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||||
|
expect(objectSize(obj)).toBe(10)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Object sort Tests', () => {
|
describe('Object sort Tests', () => {
|
||||||
@ -118,6 +156,10 @@ describe('Object sort Tests', () => {
|
|||||||
b: 'first'
|
b: 'first'
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
// it('should sort an array (yes stupid)', () => {
|
||||||
|
// const arr = [2, 1, 0]
|
||||||
|
// expect(objectSort(arr, (a, b) => a - b)).toEqual([0, 1, 2])
|
||||||
|
// })
|
||||||
it('should sort by the specified key', () => {
|
it('should sort by the specified key', () => {
|
||||||
const obj = {
|
const obj = {
|
||||||
b: 'first',
|
b: 'first',
|
||||||
@ -317,6 +359,10 @@ describe('Object Omit Tests', () => {
|
|||||||
const obj = {a: 'a', b: 'c', c: 'b'}
|
const obj = {a: 'a', b: 'c', c: 'b'}
|
||||||
expect(objectOmit(Object.freeze(obj), 'b', 'd')).toEqual({a: 'a', c: 'b'})
|
expect(objectOmit(Object.freeze(obj), 'b', 'd')).toEqual({a: 'a', c: 'b'})
|
||||||
})
|
})
|
||||||
|
it('should work with an array', () => {
|
||||||
|
const obj = [1, 2, 3, 4]
|
||||||
|
expect(objectOmit(obj, 1, 3)).toEqual([1,undefined,3,undefined])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Is Object Tests', () => {
|
describe('Is Object Tests', () => {
|
||||||
@ -338,4 +384,5 @@ describe('Is Object Tests', () => {
|
|||||||
it('object is an "object"', () => {
|
it('object is an "object"', () => {
|
||||||
expect(isObject({})).toBe(true)
|
expect(isObject({})).toBe(true)
|
||||||
})
|
})
|
||||||
|
it('array is an object', () => expect(isObject([])).toBe(true))
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user