|  | 
|  | 1 | +import { lengthConversion } from '../LengthConversion.js' | 
|  | 2 | + | 
|  | 3 | +describe('LengthConversion', () => { | 
|  | 4 | +  it.each` | 
|  | 5 | +    length | fromUnit | toUnit   | expected | 
|  | 6 | +    ${10}  | ${'km'}  | ${'m'}   | ${10000} | 
|  | 7 | +    ${100} | ${'m'}   | ${'km'}  | ${0.1} | 
|  | 8 | +    ${5}   | ${'cm'}  | ${'mm'}  | ${50} | 
|  | 9 | +    ${12}  | ${'ft'}  | ${'inch'}| ${144.00000000000003} | 
|  | 10 | +  `( | 
|  | 11 | +    'converts $length $fromUnit to $toUnit', | 
|  | 12 | +    ({ length, fromUnit, toUnit, expected }) => { | 
|  | 13 | +      try { | 
|  | 14 | +        const result = lengthConversion(length, fromUnit, toUnit) | 
|  | 15 | +        expect(result).toBe(expected) | 
|  | 16 | +      } catch (error) { | 
|  | 17 | +        expect(error).toBeUndefined() | 
|  | 18 | +      } | 
|  | 19 | +    } | 
|  | 20 | +  ) | 
|  | 21 | + | 
|  | 22 | +  it.each` | 
|  | 23 | +    length | fromUnit | toUnit   | expected | 
|  | 24 | +    ${10}  | ${'m'}   | ${'km'}  | ${0.01} | 
|  | 25 | +    ${1000}| ${'mm'}  | ${'cm'}  | ${100} | 
|  | 26 | +    ${1}   | ${'inch'}| ${'ft'}  | ${0.08333333333} | 
|  | 27 | +  `( | 
|  | 28 | +    'converts $length $fromUnit to $toUnit (vice versa)', | 
|  | 29 | +    ({ length, fromUnit, toUnit, expected }) => { | 
|  | 30 | +      try { | 
|  | 31 | +        const result = lengthConversion(length, fromUnit, toUnit) | 
|  | 32 | +        expect(result).toBeCloseTo(expected, 10) // Close comparison due to floating-point precision | 
|  | 33 | +      } catch (error) { | 
|  | 34 | +        expect(error).toBeUndefined() | 
|  | 35 | +      } | 
|  | 36 | +    } | 
|  | 37 | +  ) | 
|  | 38 | + | 
|  | 39 | +  it.each` | 
|  | 40 | +    length | fromUnit | toUnit     | expectedError | 
|  | 41 | +    ${10}  | ${'km'}  | ${'invalid'} | ${'Invalid units'} | 
|  | 42 | +    ${5}   | ${'invalid'} | ${'m'}    | ${'Invalid units'} | 
|  | 43 | +  `( | 
|  | 44 | +    'returns error message for invalid units: $fromUnit to $toUnit', | 
|  | 45 | +    ({ length, fromUnit, toUnit, expectedError }) => { | 
|  | 46 | +      try { | 
|  | 47 | +        lengthConversion(length, fromUnit, toUnit) | 
|  | 48 | +      } catch (error) { | 
|  | 49 | +        expect(error.message).toBe(expectedError) | 
|  | 50 | +      } | 
|  | 51 | +    } | 
|  | 52 | +  ) | 
|  | 53 | +}) | 
0 commit comments