|
| 1 | +import Trapezoid from '../Trapezoid' |
| 2 | + |
| 3 | +describe('Trapezoid', () => { |
| 4 | + describe('Constructor', () => { |
| 5 | + test('creates a trapezoid with valid dimensions', () => { |
| 6 | + const trapezoid = new Trapezoid(5, 10, 7) |
| 7 | + expect(trapezoid).toBeInstanceOf(Trapezoid) |
| 8 | + expect(trapezoid.base1).toBe(5) |
| 9 | + expect(trapezoid.base2).toBe(10) |
| 10 | + expect(trapezoid.height).toBe(7) |
| 11 | + expect(trapezoid.sides).toEqual([null, null]) |
| 12 | + }) |
| 13 | + |
| 14 | + test('creates a trapezoid with all sides known', () => { |
| 15 | + const trapezoid = new Trapezoid(5, 10, 7, 3, 4) |
| 16 | + expect(trapezoid.sides).toEqual([3, 4]) |
| 17 | + }) |
| 18 | + |
| 19 | + test('throws an error if any dimension is invalid', () => { |
| 20 | + expect(() => new Trapezoid(-5, 10, 7)).toThrow( |
| 21 | + 'base1 must be a positive number.' |
| 22 | + ) |
| 23 | + expect(() => new Trapezoid(5, -10, -7)).toThrow( |
| 24 | + 'base2 must be a positive number.' |
| 25 | + ) |
| 26 | + expect(() => new Trapezoid(5, NaN, 7)).toThrow( |
| 27 | + 'base2 must be a positive number.' |
| 28 | + ) |
| 29 | + expect(() => new Trapezoid(5, 10, -7)).toThrow( |
| 30 | + 'height must be a positive number.' |
| 31 | + ) |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + describe('Area Calculation', () => { |
| 36 | + test('calculates area correctly', () => { |
| 37 | + const trapezoid = new Trapezoid(5, 10, 7) |
| 38 | + expect(trapezoid.area()).toBe(52.5) // Area = ((5 + 10) / 2) * 7 |
| 39 | + }) |
| 40 | + }) |
| 41 | + |
| 42 | + describe('Perimeter Calculation', () => { |
| 43 | + test('calculates perimeter correctly when all sides are known', () => { |
| 44 | + const trapezoid = new Trapezoid(5, 10, 7, 3, 4) |
| 45 | + expect(trapezoid.perimeter()).toBe(22) // Perimeter = base1 + base2 + sideA + sideB |
| 46 | + }) |
| 47 | + |
| 48 | + test('throws an error if not all sides are known', () => { |
| 49 | + const trapezoid = new Trapezoid(5, 10, 7) |
| 50 | + expect(() => trapezoid.perimeter()).toThrow( |
| 51 | + 'Cannot calculate perimeter: not all sides are known.' |
| 52 | + ) |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + describe('Getters', () => { |
| 57 | + test('base1 getter returns correct value', () => { |
| 58 | + const trapezoid = new Trapezoid(5, 10, 7) |
| 59 | + expect(trapezoid.base1).toBe(5) |
| 60 | + }) |
| 61 | + |
| 62 | + test('base2 getter returns correct value', () => { |
| 63 | + const trapezoid = new Trapezoid(5, 10, 7) |
| 64 | + expect(trapezoid.base2).toBe(10) |
| 65 | + }) |
| 66 | + |
| 67 | + test('height getter returns correct value', () => { |
| 68 | + const trapezoid = new Trapezoid(5, 10, 7) |
| 69 | + expect(trapezoid.height).toBe(7) |
| 70 | + }) |
| 71 | + |
| 72 | + test('sides getter returns correct values', () => { |
| 73 | + const trapezoid = new Trapezoid(5, 10, 7, 3, 4) |
| 74 | + expect(trapezoid.sides).toEqual([3, 4]) |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + describe('String Representation', () => { |
| 79 | + test('returns correct string representation when sides are unknown', () => { |
| 80 | + const trapezoid = new Trapezoid(5, 10, 7) |
| 81 | + expect(trapezoid.toString()).toBe( |
| 82 | + 'Trapezoid: base1 = 5, base2 = 10, height = 7, sideA = unknown, sideB = unknown, area = 52.5, perimeter = unknown' |
| 83 | + ) |
| 84 | + }) |
| 85 | + |
| 86 | + test('returns correct string representation when all sides are known', () => { |
| 87 | + const trapezoid = new Trapezoid(5, 10, 7, 3, 4) |
| 88 | + expect(trapezoid.toString()).toBe( |
| 89 | + 'Trapezoid: base1 = 5, base2 = 10, height = 7, sideA = 3, sideB = 4, area = 52.5, perimeter = 22' |
| 90 | + ) |
| 91 | + }) |
| 92 | + }) |
| 93 | +}) |
0 commit comments