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
34 changes: 32 additions & 2 deletions modules/ecs6-class/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,35 @@ const Point = require("./point");

class Line {
constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) {
if ((!(point1 instanceof Point))) {
throw new Error('the constructor should get point1 arguments of "Point"')
}
if ((!(point2 instanceof Point))) {
throw new Error('the constructor should get point2 arguments of "Point"')
}
if ((typeof (slope) !== 'number' && typeof (slope) !== "undefined")) {
throw new Error('the slope in constractor should get undefined or number')
}
if ((typeof (n) !== "undefined" && typeof (n) !== "number")) {
throw new Error('the n in constractor should get undefined or number')
}
this.point1 = point1;
this.point2 = point2;
this.slope = slope;
this.n = n;
}

calculateSlope() {
calculateSlope() {//שיפוע
if ((this.point1.x - this.point2.x) === 0) {
throw new Error('the argument equal to 0');
}
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
Copy link
Owner

Choose a reason for hiding this comment

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

What happens if point1.x - point2.x === 0??

}

calculateNOfLineFunction() {
calculateNOfLineFunction() {//מרחק
if(this.slope===undefined){
throw new Error('The slope has not yet been defined')
}
this.n = this.point1.y - this.slope * this.point1.x
Copy link
Owner

Choose a reason for hiding this comment

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

was the slope already calculated?

}

Expand All @@ -26,11 +44,23 @@ class Line {


getPointByX(x) {
if (x === undefined) {
throw new Error('the function should get a number')
}
if (typeof (x) != 'number') {
throw new Error('the function should get a number')
}
let y = this.slope * x + this.n
return new Point({ x, y })
}

getPointByY(y) {
if (y === undefined) {
throw new Error('the function should get a number')
}
if (typeof (y) != 'number') {
throw new Error('the function should get a number')
}
let x = (y - this.n) / this.slope;
return new Point({ x, y })
}
Expand Down
15 changes: 15 additions & 0 deletions modules/ecs6-class/point.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
class Point {
constructor({x=0, y=0}={}) {
if(typeof(x)!='number'||typeof(y)!='number'){
throw new Error('the constructor should get a number')
}
this.x = x;
this.y = y;
}
moveVertical(value) {
if(value===undefined){
throw new Error('the function should get a number')
}
if(typeof(value)!='number'){
throw new Error('the function should get a number')
}
this.y += value;
}
moveHorizontal(value) {
if(value===undefined){
throw new Error('the function should get a number')
}
if(typeof(value)!='number'){
throw new Error('the function should get a number')
}
this.x += value;
}
}
Expand Down
40 changes: 37 additions & 3 deletions modules/geometry-calculation.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
const Line = require('./ecs6-class/line')
const Point = require('./ecs6-class/point')

const calculateDistance = (point1, point2) => {
const calculateDistance = (point1, point2) => {//חישוב מרחק
if ((!(point1 instanceof Point)) || point1 === undefined) {
throw new Error('point1 must be of the Point class')
}
if (!(point2 instanceof Point) || point2 === undefined) {
throw new Error('point2 must be of the Point class')
}
let distanceX = (point2.x - point1.x) ** 2;
let distanceY = (point2.y - point2.y) ** 2;
let distanceY = (point2.y - point1.y) ** 2;
const distance = Math.sqrt(distanceX + distanceY);
return distance;
}

const calculateJunctionPoint = (line1, line2) => {
const calculateJunctionPoint = (line1, line2) => {// נקודת אמצע
if (line1 === undefined || !(line1 instanceof Line)) {
throw new Error('line1 must be of the Line class')
}
if (line2 === undefined || !(line2 instanceof Line)) {
throw new Error('line2 must be of the Line class')
}
if(line1.slope===undefined){
throw new Error('The slope in line1 has not yet been defined')
}
if(line2.slope===undefined){
throw new Error('The slope in line2 has not yet been defined')
}
if(line1.n===undefined){
throw new Error('The n in line1 has not yet been defined')
}
if(line2.n===undefined){
throw new Error('The n in line2 has not yet been defined')
}
if (line1.slope === line2.slope) {
Copy link
Owner

Choose a reason for hiding this comment

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

are both slopes calculated?

if (line1.n === line2.n) {
return true
Copy link
Owner

Choose a reason for hiding this comment

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

are both n calculated already?

Expand All @@ -24,8 +49,17 @@ const calculateJunctionPoint = (line1, line2) => {
}

const isPointOnLine = (line, point) => {
if (!(line instanceof Line)) {
throw new Error('the function should get arg of "Line"')
}
if (typeof (point.x) !== 'number' || typeof (point.x) !== 'number' || !(point instanceof Point)) {
throw new Error('the function should get arg of "Point"')
}
const proxyLine = new Line({ point1: line.point1, point2: point })
proxyLine.calculateSlope()
if(line.slope===undefined){
throw new Error('The slope in line has not yet been defined')
}
if (line.slope === proxyLine.slope) {
Copy link
Owner

Choose a reason for hiding this comment

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

is the slope calculated?

proxyLine.calculateNOfLineFunction()
if (line.n === proxyLine.n) {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"description": "practice unit tests in javascript",
"main": "index.js",
"scripts": {
"test": "jest"
"test": "jest",
"coverage": "npm run test -- --coverage"

},
"dependencies": {
"jest": "^29.7.0"
Copy link
Owner

Choose a reason for hiding this comment

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

the jest module is not in the correct place

Expand Down
145 changes: 145 additions & 0 deletions tests/ecs6-class/line.test.js
Copy link
Owner

Choose a reason for hiding this comment

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

  1. give a good description for each test
  2. use the it test function instead of test

Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
const Line = require('../../modules/ecs6-class/line');
const point = require('../../modules/ecs6-class/point');

Copy link
Owner

Choose a reason for hiding this comment

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

don't declare on top of the module a list of parameter, each test should have its own. And if you need the same parameters for a group of tests, declare them inside the describe block in the beforeAll(()=> { } ) function

describe('CALCULATE_SIOPE', () => {
it('', () => {
const line1 = new Line({ point1: new point({ x: 1, y: 1 }), point2: new point({ x: 2, y: 2 }), n: 2, slope: 2 })
line1.calculateSlope()
expect(line1.slope).toBe(1)
});
});

describe('ERRORS', () => {
it('errors for calculateSlope function', () => {
const line2 = new Line({})
expect(() => line2.calculateSlope()).toThrow('the argument equal to 0')
});
});

describe('CALCULATE_N_OF_LINE_FUNCTION', () => {
it('', () => {
const line1 = new Line({ point1: new point({ x: 1, y: 1 }), point2: new point({ x: 2, y: 2 }), slope: 2 })
line1.calculateNOfLineFunction()
expect(line1.n).toBe(-1)
});
});
describe('ERRORS', () => {
it('errors for calculateNOfLineFunction function', () => {
const line2 = new Line({})
expect(() => line2.calculateNOfLineFunction()).toThrow('The slope has not yet been defined')
});
});

describe('GET_POINT_ON_X_ASIS', () => {
it('', () => {
const line1 = new Line({ n: 2, slope: 2 })
expect(line1.getPointOnXAsis()).toEqual({ x: -1, y: 0 })
});
it('mock on getPointOnXAsis', () => {
let mPoint = new point({ x: 2, y: 1 });
const line1 = new Line({ point1: new point({ x: 1, y: 1 }), point2: new point({ x: 2, y: 2 }), n: 2, slope: 2 })
jest.spyOn(line1, 'getPointByX').mockImplementation((y) => {
const x = (y - line1.n) / line1.slope;
mPoint = new point({ x, y });
return mPoint;
});

const result2 = line1.getPointOnYAsis();
Copy link
Owner

Choose a reason for hiding this comment

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

this test is a bit weird: you description is not what you really do

expect(result2).toEqual(mPoint);
});
});


describe('GET_POINT_ON_Y_ASIS', () => {
test('', () => {
const line1 = new Line({ n: 2, slope: 2 })
expect(line1.getPointOnYAsis()).toEqual({ x: 0, y: 2 })
});
test('mock on getPointOnYAsis', () => {
let mPoint = new point({ x: 2, y: 1 });
const line1 = new Line({ point1: new point({ x: 1, y: 1 }), point2: new point({ x: 2, y: 2 }), n: 2, slope: 2 })
jest.spyOn(line1, 'getPointByX').mockImplementation((x) => {
const y = line1.slope * x + line1.n;
mPoint = new point({ x, y });
return mPoint;
});

const result2 = line1.getPointOnYAsis();
expect(result2).toEqual(mPoint);
});

});


describe('GET_POINT_BY_Y', () => {
test('', () => {
const lin = new Line({ n: 0, slope: 1 })
expect(lin.getPointByY(6)).toEqual({ x: 6, y: 6 })
});
});

describe('ERRORS', () => {
test('check errors of getPointByY function', () => {
const TLine = new Line({})
expect(() => TLine.getPointByY('c')).toThrow('the function should get a number')
expect(() => TLine.getPointByY([1, 2])).toThrow('the function should get a number')
expect(() => TLine.getPointByY(true)).toThrow('the function should get a number')
expect(() => TLine.getPointByY()).toThrow('the function should get a number')
expect(() => TLine.getPointByY(f => f)).toThrow('the function should get a number')
});
});


describe('GET_POINT_BY_X', () => {
test('', () => {
const lin1 = new Line({ n: 2, slope: 3 })
expect(lin1.getPointByX(5)).toEqual({ x: 5, y: 17 })
});
});

describe('ERRORS', () => {
test('check errors of getPointByX function', () => {
const TLine = new Line({})
expect(() => TLine.getPointByX('c')).toThrow('the function should get a number')
expect(() => TLine.getPointByX([1, 2])).toThrow('the function should get a number')
expect(() => TLine.getPointByX(true)).toThrow('the function should get a number')
expect(() => TLine.getPointByX()).toThrow('the function should get a number')
expect(() => TLine.getPointByX((f) => f)).toThrow('the function should get a number')
});
});


describe('ERRORS', () => {
test('check errors of constactor point1 and point2', () => {
expect(() => new Line({ point1: 'c' })).toThrow('the constructor should get point1 arguments of "Point"')
expect(() => new Line({ point1: [1, 2] })).toThrow('the constructor should get point1 arguments of "Point"')
expect(() => new Line({ point1: true })).toThrow('the constructor should get point1 arguments of "Point"')
expect(() => new Line({ point1: (f) => f })).toThrow('the constructor should get point1 arguments of "Point"')
expect(() => new Line({ point1: () => f })).toThrow('the constructor should get point1 arguments of "Point"')
});
test('check errors of constactor point2', () => {
expect(() => new Line({ point2: 'c' })).toThrow('the constructor should get point2 arguments of "Point"')
expect(() => new Line({ point2: [1, 2] })).toThrow('the constructor should get point2 arguments of "Point"')
expect(() => new Line({ point2: true })).toThrow('the constructor should get point2 arguments of "Point"')
expect(() => new Line({ point2: (f) => f })).toThrow('the constructor should get point2 arguments of "Point"')
expect(() => new Line({ point2: () => f })).toThrow('the constructor should get point2 arguments of "Point"')
})
});

describe('ERRORS', () => {
it('check errors of constactor n and slope', () => {
expect(() => new Line({ slope: 'c' })).toThrow('the slope in constractor should get undefined or number')
expect(() => new Line({ slope: [1, 2] })).toThrow('the slope in constractor should get undefined or number')
expect(() => new Line({ slope: true })).toThrow('the slope in constractor should get undefined or number')
expect(() => new Line({ slope: (f) => f })).toThrow('the slope in constractor should get undefined or number')
expect(() => new Line({ slope: () => f })).toThrow('the slope in constractor should get undefined or number');

});
it('check errors of constactor slope', () => {
expect(() => new Line({ n: 'c' })).toThrow('the n in constractor should get undefined or number')
expect(() => new Line({ n: [1, 2] })).toThrow('the n in constractor should get undefined or number')
expect(() => new Line({ n: true })).toThrow('the n in constractor should get undefined or number')
expect(() => new Line({ n: (f) => f })).toThrow('the n in constractor should get undefined or number')
expect(() => new Line({ n: () => f })).toThrow('the n in constractor should get undefined or number');
});
});
47 changes: 47 additions & 0 deletions tests/ecs6-class/point.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const point = require('../../modules/ecs6-class/point')
let point1 = new point({})

describe('MOVE_VERTICAL', () => {
it('add a number of y', () => {
point1.moveVertical(5);
expect(point1).toEqual({ x: 0, y: 5 });
});
});

describe('MOVE_HORIZONTAL', () => {
it('add a number of x', () => {
point1.moveHorizontal(5);
expect(point1).toEqual({ x: 5, y: 5 });
});
});

describe('ERRORS', () => {
it('check errors of moveVertical function', () => {
expect(() => point1.moveVertical('c')).toThrow('the function should get a number')
expect(() => point1.moveVertical([1, 2])).toThrow('the function should get a number')
expect(() => point1.moveVertical(true)).toThrow('the function should get a number')
expect(() => point1.moveVertical()).toThrow('the function should get a number')
expect(() => point1.moveVertical((f) => f)).toThrow('the function should get a number')
});
});

describe('ERRORS', () => {
it('check errors of moveHorizontal function', () => {
expect(() => point1.moveHorizontal('c')).toThrow('the function should get a number')
expect(() => point1.moveHorizontal([1, 2])).toThrow('the function should get a number')
expect(() => point1.moveHorizontal(true)).toThrow('the function should get a number')
expect(() => point1.moveHorizontal()).toThrow('the function should get a number')
expect(() => point1.moveHorizontal((f) => f)).toThrow('the function should get a number')
});
});

describe('ERRORS', () => {
it('check errors of constactor', () => {
expect(() => new point({ x: 'c' })).toThrow('the constructor should get a number')
expect(() => new point({ y: [1, 2] })).toThrow('the constructor should get a number')
expect(() => new point({ x: true })).toThrow('the constructor should get a number')
expect(() => new point({ y: (f) => f })).toThrow('the constructor should get a number')
expect(() => new point({ x: () => f })).toThrow('the constructor should get a number')

});
});
Loading