|
1 | 1 | import Rectangle from '../Rectangle'
|
2 | 2 |
|
3 |
| -const rectangle = new Rectangle(3, 4) |
| 3 | +describe('Rectangle', () => { |
| 4 | + describe('Constructor', () => { |
| 5 | + test('creates a rectangle with valid dimensions', () => { |
| 6 | + const rectangle = new Rectangle(5, 3) |
| 7 | + expect(rectangle).toBeInstanceOf(Rectangle) |
| 8 | + expect(rectangle.length).toBe(5) |
| 9 | + expect(rectangle.breadth).toBe(3) |
| 10 | + }) |
4 | 11 |
|
5 |
| -test('The perimeter of rectangle with length equal to 3 and breadth equal to 4', () => { |
6 |
| - expect(parseFloat(rectangle.perimeter().toFixed(2))).toEqual(14.0) |
7 |
| -}) |
| 12 | + test('throws an error if length is not a positive number', () => { |
| 13 | + expect(() => new Rectangle(-1, 3)).toThrow( |
| 14 | + 'length must be a positive number.' |
| 15 | + ) |
| 16 | + expect(() => new Rectangle(0, 3)).toThrow( |
| 17 | + 'length must be a positive number.' |
| 18 | + ) |
| 19 | + expect(() => new Rectangle('5', 3)).toThrow( |
| 20 | + 'length must be a positive number.' |
| 21 | + ) |
| 22 | + }) |
| 23 | + |
| 24 | + test('throws an error if breadth is not a positive number', () => { |
| 25 | + expect(() => new Rectangle(5, -1)).toThrow( |
| 26 | + 'breadth must be a positive number.' |
| 27 | + ) |
| 28 | + expect(() => new Rectangle(5, 0)).toThrow( |
| 29 | + 'breadth must be a positive number.' |
| 30 | + ) |
| 31 | + expect(() => new Rectangle(5, '3')).toThrow( |
| 32 | + 'breadth must be a positive number.' |
| 33 | + ) |
| 34 | + }) |
| 35 | + }) |
| 36 | + |
| 37 | + describe('Perimeter Calculation', () => { |
| 38 | + test('calculates perimeter correctly', () => { |
| 39 | + const rectangle = new Rectangle(5, 3) |
| 40 | + expect(rectangle.perimeter()).toBe(16) |
| 41 | + }) |
| 42 | + |
| 43 | + test('calculates perimeter correctly for a square', () => { |
| 44 | + const square = new Rectangle(4, 4) |
| 45 | + expect(square.perimeter()).toBe(16) |
| 46 | + }) |
| 47 | + }) |
| 48 | + |
| 49 | + describe('Area Calculation', () => { |
| 50 | + test('calculates area correctly', () => { |
| 51 | + const rectangle = new Rectangle(5, 3) |
| 52 | + expect(rectangle.area()).toBe(15) |
| 53 | + }) |
| 54 | + |
| 55 | + test('calculates area correctly for a square', () => { |
| 56 | + const square = new Rectangle(4, 4) |
| 57 | + expect(square.area()).toBe(16) |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + describe('Getters', () => { |
| 62 | + test('length getter returns correct value', () => { |
| 63 | + const rectangle = new Rectangle(5, 3) |
| 64 | + expect(rectangle.length).toBe(5) |
| 65 | + }) |
| 66 | + |
| 67 | + test('breadth getter returns correct value', () => { |
| 68 | + const rectangle = new Rectangle(5, 3) |
| 69 | + expect(rectangle.breadth).toBe(3) |
| 70 | + }) |
| 71 | + }) |
8 | 72 |
|
9 |
| -test('The area of rectangle with length equal to 3 and breadth equal to 4', () => { |
10 |
| - expect(parseFloat(rectangle.area().toFixed(2))).toEqual(12.0) |
| 73 | + describe('toString Method', () => { |
| 74 | + test('returns correct string representation', () => { |
| 75 | + const rectangle = new Rectangle(5, 3) |
| 76 | + expect(rectangle.toString()).toBe( |
| 77 | + 'Rectangle: length = 5, breadth = 3, area = 15, perimeter = 16' |
| 78 | + ) |
| 79 | + }) |
| 80 | + }) |
11 | 81 | })
|
0 commit comments