-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathEllipse.test.js
68 lines (61 loc) · 2.06 KB
/
Ellipse.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import Ellipse from '../Ellipse'
describe('Ellipse', () => {
describe('Constructor', () => {
test('creates an ellipse with valid dimensions', () => {
const ellipse = new Ellipse(5, 10)
expect(ellipse).toBeInstanceOf(Ellipse)
expect(ellipse.radiusX).toBe(5)
expect(ellipse.radiusY).toBe(10)
})
test('throws an error if any dimension is invalid', () => {
expect(() => new Ellipse(-5, 10)).toThrow(
'radiusX must be a positive number.'
)
expect(() => new Ellipse(5, -10)).toThrow(
'radiusY must be a positive number.'
)
expect(() => new Ellipse(NaN, 10)).toThrow(
'radiusX must be a positive number.'
)
expect(() => new Ellipse(5, undefined)).toThrow(
'radiusY must be a positive number.'
)
})
})
describe('Area Calculation', () => {
test('calculates area correctly', () => {
const ellipse = new Ellipse(5, 10)
expect(ellipse.area()).toBeCloseTo(Math.PI * 5 * 10) // Area = π * rX * rY
})
})
describe('Circumference Calculation', () => {
test('calculates circumference correctly', () => {
const ellipse = new Ellipse(5, 10)
expect(ellipse.circumference()).toBeCloseTo(
Math.PI * (3 * (5 + 10) - Math.sqrt((3 * 5 + 10) * (5 + 3 * 10)))
) // Circumference using Ramanujan's approximation
})
})
describe('Getters', () => {
test('radiusX getter returns correct value', () => {
const ellipse = new Ellipse(5, 10)
expect(ellipse.radiusX).toBe(5)
})
test('radiusY getter returns correct value', () => {
const ellipse = new Ellipse(5, 10)
expect(ellipse.radiusY).toBe(10)
})
})
describe('String Representation', () => {
test('returns correct string representation', () => {
const ellipse = new Ellipse(5, 10)
expect(ellipse.toString()).toBe(
`Ellipse: radiusX = 5, radiusY = 10, area = ${
Math.PI * 5 * 10
}, circumference = ${
Math.PI * (3 * (5 + 10) - Math.sqrt((3 * 5 + 10) * (5 + 3 * 10)))
}`
)
})
})
})