|
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 | | - ) |
| 1 | +import { metresToFeet, feetToMetres } from '../LengthConversion.js' |
| 2 | + |
| 3 | +describe('metresToFeet', () => { |
| 4 | + test('metresToFeet(0) returns 0', () => { |
| 5 | + expect(metresToFeet(0)).toBe(0) |
| 6 | + }) |
| 7 | + |
| 8 | + test('metresToFeet(1) returns 3.28084', () => { |
| 9 | + expect(metresToFeet(1)).toBeCloseTo(3.28084) |
| 10 | + }) |
| 11 | + |
| 12 | + test('metresToFeet(10) returns 32.8084', () => { |
| 13 | + expect(metresToFeet(10)).toBeCloseTo(32.8084) |
| 14 | + }) |
| 15 | + |
| 16 | + test('metresToFeet(100) returns 328.084', () => { |
| 17 | + expect(metresToFeet(100)).toBeCloseTo(328.084) |
| 18 | + }) |
| 19 | + |
| 20 | + test('throws TypeError for non-number input', () => { |
| 21 | + expect(() => metresToFeet('10')).toThrow(TypeError) |
| 22 | + }) |
| 23 | + |
| 24 | + test('throws TypeError for NaN', () => { |
| 25 | + expect(() => metresToFeet(NaN)).toThrow(TypeError) |
| 26 | + }) |
| 27 | +}) |
| 28 | + |
| 29 | +describe('feetToMetres', () => { |
| 30 | + test('feetToMetres(0) returns 0', () => { |
| 31 | + expect(feetToMetres(0)).toBe(0) |
| 32 | + }) |
| 33 | + |
| 34 | + test('feetToMetres(1) returns 0.3048', () => { |
| 35 | + expect(feetToMetres(1)).toBeCloseTo(0.3048) |
| 36 | + }) |
| 37 | + |
| 38 | + test('feetToMetres(10) returns 3.048', () => { |
| 39 | + expect(feetToMetres(10)).toBeCloseTo(3.048) |
| 40 | + }) |
| 41 | + |
| 42 | + test('feetToMetres(100) returns 30.48', () => { |
| 43 | + expect(feetToMetres(100)).toBeCloseTo(30.48) |
| 44 | + }) |
| 45 | + |
| 46 | + test('throws TypeError for non-number input', () => { |
| 47 | + expect(() => feetToMetres('10')).toThrow(TypeError) |
| 48 | + }) |
| 49 | + |
| 50 | + test('throws TypeError for NaN', () => { |
| 51 | + expect(() => feetToMetres(NaN)).toThrow(TypeError) |
| 52 | + }) |
53 | 53 | }) |
0 commit comments