-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: tests added for decimalIsolate and improved comments for clarity
- Loading branch information
Hridyanshu7
committed
Oct 23, 2024
1 parent
5426682
commit cb9cd55
Showing
2 changed files
with
53 additions
and
3 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,45 @@ | ||
import { describe } from 'node:test' | ||
import { decimalIsolate } from '../decimalIsolate' | ||
|
||
const invalidInputs = [ | ||
{ input: NaN, description: 'NaN' }, | ||
{ input: null, description: 'null' }, | ||
{ input: undefined, description: 'undefined' }, | ||
{ input: 'a string', description: 'a string' }, | ||
{ input: { a: 54.34 }, description: 'an object' }, | ||
{ | ||
input: ['OneDotTwoThree', 4.56, 7.89], | ||
description: 'an array with invalid first element' | ||
} | ||
] | ||
|
||
describe('DecimalIsolate', () => { | ||
it('should isolate the decimal part of a positive number', () => { | ||
expect(decimalIsolate(12.34)).toBe(0.34) | ||
}) | ||
|
||
it('should isolate the decimal part of a negative number', () => { | ||
expect(decimalIsolate(-456.789)).toBe(0.789) | ||
}) | ||
|
||
it('should return 0 when the number is a whole number', () => { | ||
expect(decimalIsolate(100)).toBe(0) | ||
}) | ||
|
||
it('should isolate the decimal part of a number string', () => { | ||
expect(decimalIsolate('12.34')).toBe(0.34) | ||
}) | ||
|
||
it('should isolate the decimal part of the first element of an array if it is convertible to a number', () => { | ||
expect(decimalIsolate([98.76, { a: 76.45 }])).toBe(0.76) | ||
}) | ||
|
||
describe('Invalid Inputs', () => { | ||
it.each(invalidInputs)( | ||
'should return 0 for invalid input when input is $description', | ||
({ input }) => { | ||
expect(decimalIsolate(input)).toBe(0) | ||
} | ||
) | ||
}) | ||
}) |