mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00

* feat: used js object intead of switch * pref: optimzed the algo with regex & replace method * feat: add hex validation with test case * feat: add type validation * chore: fix grammar mistake * docs: add binLookup comments
32 lines
1.0 KiB
JavaScript
32 lines
1.0 KiB
JavaScript
import hexToBinary from '../HexToBinary'
|
|
|
|
describe('Testing hexToBinary', () => {
|
|
it('expects throw error in invalid types', () => {
|
|
expect(() => hexToBinary(false)).toThrowError()
|
|
expect(() => hexToBinary(null)).toThrowError()
|
|
expect(() => hexToBinary(23464)).toThrowError()
|
|
})
|
|
|
|
it('expects throw error in invalid hex', () => {
|
|
expect(() => hexToBinary('Hello i am not a valid Hex')).toThrowError()
|
|
expect(() => hexToBinary('Gf46f')).toThrowError()
|
|
expect(() => hexToBinary('M')).toThrowError()
|
|
})
|
|
|
|
it('expects to return correct hexadecimal value', () => {
|
|
expect(hexToBinary('8')).toBe('1000')
|
|
})
|
|
|
|
it('expects to return correct binary value for more than one hex digit', () => {
|
|
expect(hexToBinary('EA')).toBe('11101010')
|
|
})
|
|
|
|
it('expects to test its robustness as it should be case-insensitive', () => {
|
|
expect(hexToBinary('4d')).toBe('01001101')
|
|
})
|
|
|
|
it('expects to return correct hexadecimal value, matching (num).toString(2)', () => {
|
|
expect(hexToBinary('F')).toBe(parseInt('F', 16).toString(2))
|
|
})
|
|
})
|