-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ADD: unit tests for object helpers * ADD: unit tests for numbers helpers * ADD: unit tests for array helpers
- Loading branch information
1 parent
5887561
commit 6354119
Showing
4 changed files
with
474 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { Arr } from '../../lib/utils/array'; | ||
|
||
describe('Array Helper', () => { | ||
beforeEach(async () => {}); | ||
|
||
// it('should throw exception', () => { | ||
// const arr = {}; | ||
// expect(Arr.toObj(arr as [], [])).toThrow(InvalidValue); | ||
// }); | ||
|
||
it('should return object', () => { | ||
const arr = [ | ||
2, | ||
['bar', 'piyush', 'intent'], | ||
{ | ||
foo: 'bar', | ||
}, | ||
[{ bar: 'foo' }], | ||
]; | ||
const keys = ['foo', 'chhabra', 'best framework', 'obj']; | ||
expect(Arr.toObj(arr, keys)).toStrictEqual([ | ||
{ | ||
foo: 'bar', | ||
chhabra: 'piyush', | ||
'best framework': 'intent', | ||
}, | ||
{ | ||
foo: { bar: 'foo' }, | ||
}, | ||
]); | ||
}); | ||
|
||
// it('should throw exception', () => { | ||
// const arr = {}; | ||
// expect(Arr.isArray(arr as [], true)).toThrow(InvalidValue); | ||
// }); | ||
|
||
it('should return false', () => { | ||
const arr = {}; | ||
expect(Arr.isArray(arr as [])).toBeFalsy(); | ||
}); | ||
|
||
it('should return false', () => { | ||
const arr = []; | ||
expect(Arr.isArray(arr)).toBeTruthy(); | ||
}); | ||
|
||
it('should return array of nested members flattened', () => { | ||
const arr = [1, [2, 3, 4], [[5, 6], [[7], 8], 9]]; | ||
expect(Arr.collapse(arr)).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]); | ||
}); | ||
|
||
it('should return array with random order of input', () => { | ||
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; | ||
const mockMath = Object.create(global.Math); | ||
mockMath.random = () => 0.5; | ||
global.Math = mockMath; | ||
expect(Arr.random(arr)).toStrictEqual([1, 6, 2, 8, 3, 7, 4, 9, 5]); | ||
}); | ||
|
||
it('should return array with sorted order of input', () => { | ||
const arr = [6, 1, 2, 8, 3, 7, 4, 9, 5]; | ||
expect(Arr.sort(arr)).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]); | ||
}); | ||
|
||
it('should return array with descending order of input', () => { | ||
const arr = [6, 1, 2, 8, 3, 7, 4, 9, 5]; | ||
expect(Arr.sortDesc(arr)).toStrictEqual([9, 8, 7, 6, 5, 4, 3, 2, 1]); | ||
}); | ||
|
||
it('should return array with common elements from both input arrays', () => { | ||
const arr1 = [1, 2, 3, 4, 5]; | ||
const arr2 = [4, 5, 6, 7, 8]; | ||
expect(Arr.intersect(arr1, arr2)).toStrictEqual([4, 5]); | ||
}); | ||
|
||
it('should return array with elements keys mentioned to pick', () => { | ||
const arr = [ | ||
{ developer: { id: 1, name: 'Taylor' } }, | ||
{ developer: { id: 2, name: 'Abigail' } }, | ||
]; | ||
const pick = ['*.developer.name']; | ||
expect(Arr.pick(arr, pick)).toStrictEqual([ | ||
{ developer: { name: 'Taylor' } }, | ||
{ developer: { name: 'Abigail' } }, | ||
]); | ||
}); | ||
|
||
it('should return array with all elements except the keys mentioned', () => { | ||
const arr = [ | ||
{ developer: { id: 1, name: 'Taylor' } }, | ||
{ developer: { id: 2, name: 'Abigail' } }, | ||
]; | ||
const pick = ['*.developer.name']; | ||
expect(Arr.except(arr, pick)).toStrictEqual([ | ||
{ developer: { id: 1 } }, | ||
{ developer: { id: 2 } }, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { Num } from '../../lib/utils/number'; | ||
|
||
describe('Numbers Helper', () => { | ||
beforeEach(async () => {}); | ||
|
||
it('should abbrevate with en locale to 1 decimal point precision', () => { | ||
const number = 12345; | ||
const options = { locale: 'en' }; | ||
expect(Num.abbreviate(number, options)).toBe('12.3K'); | ||
}); | ||
|
||
it('should abbrevate with en locale to 3 decimal precision', () => { | ||
const number = 12345; | ||
const options = { precision: 3, locale: 'en' }; | ||
expect(Num.abbreviate(number, options)).toBe('12.345K'); | ||
}); | ||
|
||
it('should abbrevate with en-IN locale to 3 decimal precision', () => { | ||
const number = 12345; | ||
const options = { precision: 3, locale: 'en-IN' }; | ||
expect(Num.abbreviate(number, options)).toBe('12.345T'); | ||
}); | ||
|
||
it('should return number itself', () => { | ||
const number = 12345; | ||
const min = 12300; | ||
const max = 12400; | ||
expect(Num.clamp(number, min, max)).toBe(number); | ||
}); | ||
|
||
it('should return minimum number', () => { | ||
const number = 12345; | ||
const min = 12350; | ||
const max = 12400; | ||
expect(Num.clamp(number, min, max)).toBe(min); | ||
}); | ||
|
||
it('should return maximum number', () => { | ||
const number = 12345; | ||
const min = 12300; | ||
const max = 12340; | ||
expect(Num.clamp(number, min, max)).toBe(max); | ||
}); | ||
|
||
it('should return number in currency style in INR', () => { | ||
const number = 12345; | ||
const options = { currency: 'INR', locale: 'en' }; | ||
expect(Num.currency(number, options)).toBe('₹12,345.00'); | ||
}); | ||
|
||
it('should return number in currency style in USD', () => { | ||
const number = 12345; | ||
const options = { currency: 'USD', locale: 'en' }; | ||
expect(Num.currency(number, options)).toBe('$12,345.00'); | ||
}); | ||
|
||
it('should return number in file size format', () => { | ||
const number = 12345; | ||
expect(Num.fileSize(number)).toBe('12.3KB'); | ||
}); | ||
|
||
it('should return number in file size format with precision 3', () => { | ||
const number = 123456789; | ||
const options = { precision: 3 }; | ||
expect(Num.fileSize(number, options)).toBe('123.457MB'); | ||
}); | ||
|
||
it('should return number in humanize form with precision 1', () => { | ||
const number = 12345; | ||
const options = { precision: 1, locale: 'en' }; | ||
expect(Num.forHumans(number, options)).toBe('12.3 thousand'); | ||
}); | ||
|
||
it('should return number in humanize form with precision 3', () => { | ||
const number = 123456789; | ||
const options = { precision: 3, locale: 'en' }; | ||
expect(Num.forHumans(number, options)).toBe('123.457 million'); | ||
}); | ||
|
||
it('should return number in number system format with precision 1(default)', () => { | ||
const number = 12345.78; | ||
const options = { locale: 'en' }; | ||
expect(Num.format(number, options)).toBe('12,345.8'); | ||
}); | ||
|
||
it('should return number in percents when passed as decimal portion with precision 1(default)', () => { | ||
const number = 17.8; | ||
const options = { locale: 'en' }; | ||
expect(Num.percentage(number, options)).toBe('17.8%'); | ||
}); | ||
|
||
it('should return number in ordinal format', () => { | ||
const number = 231; | ||
expect(Num.ordinal(number)).toBe('231st'); | ||
}); | ||
|
||
it('should return number in ordinal format', () => { | ||
const number = 12345; | ||
expect(Num.ordinal(number)).toBe('12345th'); | ||
}); | ||
|
||
it('should return number in english words', () => { | ||
const number = 12345; | ||
expect(Num.spell(number)).toBe( | ||
'twelve thousand three hundred and forty five only', | ||
); | ||
}); | ||
|
||
it('should return false', () => { | ||
const number = '12345'; | ||
expect(Num.isInteger(number)).toBe(false); | ||
}); | ||
it('should return true', () => { | ||
const number = 12345; | ||
expect(Num.isInteger(number)).toBe(true); | ||
}); | ||
}); |
Oops, something went wrong.