|
| 1 | +import { decimalIsolate } from '../DecimalIsolate' |
| 2 | + |
| 3 | +const invalidInputs = [ |
| 4 | + { input: NaN, description: 'NaN' }, |
| 5 | + { input: null, description: 'null' }, |
| 6 | + { input: undefined, description: 'undefined' }, |
| 7 | + { input: 'a string', description: 'a string' }, |
| 8 | + { input: { a: 54.34 }, description: 'an object' }, |
| 9 | + { |
| 10 | + input: ['OneDotTwoThree', 4.56, 7.89], |
| 11 | + description: 'an array with invalid first element' |
| 12 | + } |
| 13 | +] |
| 14 | + |
| 15 | +describe('DecimalIsolate', () => { |
| 16 | + it('should isolate the decimal part of a positive number', () => { |
| 17 | + expect(decimalIsolate(12.34)).toBe(0.34) |
| 18 | + }) |
| 19 | + |
| 20 | + it('should isolate the decimal part of a negative number', () => { |
| 21 | + expect(decimalIsolate(-456.789)).toBe(0.789) |
| 22 | + }) |
| 23 | + |
| 24 | + it('should return 0 when the number is a whole number', () => { |
| 25 | + expect(decimalIsolate(100)).toBe(0) |
| 26 | + }) |
| 27 | + |
| 28 | + it('should isolate the decimal part of a number string', () => { |
| 29 | + expect(decimalIsolate('12.34')).toBe(0.34) |
| 30 | + }) |
| 31 | + |
| 32 | + it('should isolate the decimal part of the first element of an array if it is convertible to a number', () => { |
| 33 | + expect(decimalIsolate([98.76, { a: 76.45 }])).toBe(0.76) |
| 34 | + }) |
| 35 | + |
| 36 | + describe('Invalid Inputs', () => { |
| 37 | + it.each(invalidInputs)( |
| 38 | + 'should return 0 for invalid input when input is $description', |
| 39 | + ({ input }) => { |
| 40 | + expect(decimalIsolate(input)).toBe(0) |
| 41 | + } |
| 42 | + ) |
| 43 | + }) |
| 44 | +}) |
0 commit comments