Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
};
20 changes: 14 additions & 6 deletions modules/ecs6-class/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(); // הוספת קריאה כאן
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove hebrew comments

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
Expand All @@ -48,3 +50,9 @@ export default class Line {


}






1 change: 1 addition & 0 deletions modules/ecs6-class/point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export default class Point {
}
}


70 changes: 70 additions & 0 deletions modules/tests/line.test.ts
Original file line number Diff line number Diff line change
@@ -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();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

execute only one function on each test
this comment is for all the tests

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();
});
});







34 changes: 34 additions & 0 deletions modules/tests/point.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
"test": "jest",
"test:coverage": "jest --coverage"
},
"repository": {
"type": "git",
Expand All @@ -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"
}
Expand Down
6 changes: 6 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"types": ["jest"],
"esModuleInterop": true
}
}