diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..31364f0 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,7 @@ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'node', + transform: { + '^.+\\.tsx?$': 'ts-jest', + }, +}; \ No newline at end of file diff --git a/modules/ecs6-class/line.ts b/modules/ecs6-class/line.ts index e2d6086..2719731 100644 --- a/modules/ecs6-class/line.ts +++ b/modules/ecs6-class/line.ts @@ -12,26 +12,28 @@ export default class Line { this.n = n; } - - calculateSlope() { this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) } + calculateNOfLineFunction() { + this.calculateSlope(); // הוספת קריאה כאן if (this.slope) - this.n = this.point1.y - this.slope * this.point1.x + this.n = this.point1.y - this.slope * this.point1.x; } getPointOnXAsis() { - return this.getPointByY(0) + this.calculateSlope(); // הוספת קריאה כאן + return this.getPointByY(0); } - getPointOnYAsis() { - return this.getPointByX(0) + this.calculateSlope(); // הוספת קריאה כאן + return this.getPointByX(0); } + getPointByX(x: number) { if (this.slope && this.n) { let y = this.slope * x + this.n @@ -48,3 +50,9 @@ export default class Line { } + + + + + + diff --git a/modules/ecs6-class/point.ts b/modules/ecs6-class/point.ts index 0b6c320..bea12fe 100644 --- a/modules/ecs6-class/point.ts +++ b/modules/ecs6-class/point.ts @@ -13,3 +13,4 @@ export default class Point { } } + diff --git a/modules/tests/line.test.ts b/modules/tests/line.test.ts new file mode 100644 index 0000000..337593e --- /dev/null +++ b/modules/tests/line.test.ts @@ -0,0 +1,70 @@ +import Line from '../ecs6-class/line'; +import Point from '../ecs6-class/point'; + +describe('Line class', () => { + let line: Line; + let point1: Point; + let point2: Point; + + beforeEach(() => { + point1 = new Point({ x: 1, y: 2 }); + point2 = new Point({ x: 3, y: 4 }); + line = new Line({ point1, point2 }); + }); + + it('should calculate slope correctly', () => { + line.calculateSlope(); + expect(line.slope).toBe(1); // (2 - 4) / (1 - 3) = 1 + }); + + it('should calculate n of line function correctly', () => { + line.calculateNOfLineFunction(); + expect(line.n).toBe(1); // 2 - 1 * 1 = 1 + }); + + it('should get point on X axis correctly', () => { + line.calculateNOfLineFunction(); + const pointOnX = line.getPointOnXAsis(); + expect(pointOnX).toEqual(new Point({ x: -1, y: 0 })); // x = (0 - 1) / 1 + }); + + it('should get point on Y axis correctly', () => { + line.calculateNOfLineFunction(); + const pointOnY = line.getPointOnYAsis(); + expect(pointOnY).toEqual(new Point({ x: 0, y: 1 })); // y = 1 * 0 + 1 + }); + + it('should get point by X correctly', () => { + line.calculateNOfLineFunction(); + const pointByX = line.getPointByX(2); + expect(pointByX).toEqual(new Point({ x: 2, y: 3 })); // y = 1 * 2 + 1 + }); + + it('should get point by Y correctly', () => { + line.calculateNOfLineFunction(); + const pointByY = line.getPointByY(3); + expect(pointByY).toEqual(new Point({ x: 2, y: 3 })); // x = (3 - 1) / 1 + }); + it('calculateNOfLineFunction should not set n if slope is undefined', () => { + const line = new Line({ point1: new Point({ x: 1, y: 2 }), point2: new Point({ x: 3, y: 4 }) }); + line.calculateNOfLineFunction(); + expect(line.n).toBeUndefined(); + }); + + it('getPointByX should return undefined if slope or n is undefined', () => { + const line = new Line(); + expect(line.getPointByX(5)).toBeUndefined(); + }); + + it('getPointByY should return undefined if slope or n is undefined', () => { + const line = new Line(); + expect(line.getPointByY(5)).toBeUndefined(); + }); +}); + + + + + + + diff --git a/modules/tests/point.test.ts b/modules/tests/point.test.ts new file mode 100644 index 0000000..6da9264 --- /dev/null +++ b/modules/tests/point.test.ts @@ -0,0 +1,34 @@ +import Point from '../ecs6-class/point'; + +describe('Point class', () => { + let point: Point; + + beforeEach(() => { + point = new Point({ x: 5, y: 10 }); + }); + + test('should initialize with default values', () => { + const defaultPoint = new Point(); + expect(defaultPoint.x).toBe(0); + expect(defaultPoint.y).toBe(0); + }); + + test('should initialize with provided values', () => { + expect(point.x).toBe(5); + expect(point.y).toBe(10); + }); + + test('should move vertically', () => { + point.moveVertical(5); + expect(point.y).toBe(15); + point.moveVertical(-3); + expect(point.y).toBe(12); + }); + + test('should move horizontally', () => { + point.moveHorizontal(3); + expect(point.x).toBe(8); + point.moveHorizontal(-2); + expect(point.x).toBe(6); + }); +}); \ No newline at end of file diff --git a/package.json b/package.json index f9371ee..d4691d2 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "", "main": "index.js", "scripts": { - "test": "jest" + "test": "jest", + "test:coverage": "jest --coverage" }, "repository": { "type": "git", @@ -22,6 +23,7 @@ "devDependencies": { "@types/jest": "^30.0.0", "jest": "^30.0.4", + "ts-jest": "^29.4.0", "ts-node": "^10.9.2", "typescript": "^5.8.3" } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..9282fab --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "types": ["jest"], + "esModuleInterop": true + } +} \ No newline at end of file