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
16 changes: 12 additions & 4 deletions modules/ecs6-class/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ class Line {
}

calculateSlope = () => {
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
}
if ((this.point1.x - this.point2.x)===0)
throw new Error('error division by 0');

this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x);
}


calculateNOfLineFunction = () => {
this.n = this.point1.y - this.slope * this.point1.x
Expand All @@ -30,10 +34,14 @@ class Line {
return new Point({ x, y })
}


getPointByY(y) {
let x = (y - this.slope) / this.n;
if (this.slope === 0)
throw new Error("don't divide by 0")
let x = (y - this.n) / this.slope;
return new Point({ x, y })
}
}


}
module.exports = Line
11 changes: 11 additions & 0 deletions modules/ecs6-class/point.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
class Point {
constructor({x=0, y=0}={}) {
if (!(typeof (x) === "number") || !(typeof (y) === "number"))
throw new Error('x and y must be of the number type')
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 one is not a number?
I expect to get => if x is not a number : new Error(the parameter x must be a number) and if y is not a number: new Error(parameter y is not a number) if both are not numbers: new Error(both parameter must be type of number)

this.x = x;
this.y = y;
}
moveVertical(value) {
if (typeof value !== 'number') {
throw new Error('Invalid input. value should be a number.');
}
this.y += value;
}
moveHorizontal(value) {
if (typeof value !== 'number') {
throw new Error('Invalid input. value should be a number.');
}
this.x += value;
}
}




module.exports = Point
31 changes: 27 additions & 4 deletions modules/geometry-calculation.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
const Line = require('./ecs6-class/line')
const Point=require('./ecs6-class/point')

const calculateDistance = (point1, point2) => {
if(!(point1 instanceof Point)&&!(point2 instanceof Point))
throw new Error('point1 and point2 must be of the Point type')
if (!(point1 instanceof Point))
throw new Error('point1 must be of the Point type')
if (!(point2 instanceof Point))
throw new Error('point2 must be of the Point type')
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) => {
if(!(line1 instanceof Line)&&!(line2 instanceof Line))
throw new Error('line1 and line2 must be of the Line type')
if (!(line1 instanceof Line))
throw new Error('line1 must be of the Line type')
if (!(line2 instanceof Line))
throw new Error('line2 must be of the Line type')
if (line1.slope === line2.slope) {
if (line1.n === line2.n) {
return true
Expand All @@ -23,18 +37,27 @@ const calculateJunctionPoint = (line1, line2) => {
}
}


const isPointOnLine = (line, point) => {
const proxyLine = new Line(line.point1, point)
if (!(line instanceof Line)&&!(point instanceof Point)) {
throw new Error("The object line should be of the Line type and object point must be of the Point type")
}
if (!(line instanceof Line)) {
throw new Error("The object line should be of the Line type")
}
if (!(point instanceof Point)) {
throw new Error("The object point should be of the Point type")
}
const proxyLine = new Line({ point1: line.point1, point2: point })
proxyLine.calculateSlope()
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 of line calculated?
is the n of line calculated?

proxyLine.calculateNOfLineFunction()
if (line.n === proxyLine.n2) {
if (line.n === proxyLine.n) {
return true
}
}
return false
}

module.exports = {
calculateDistance,
calculateJunctionPoint,
Expand Down
18 changes: 11 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "practice unit tests in javascript",
"main": "index.js",
"scripts": {
"test": "jest"
"test": "jest",
"test: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.

jest is not in the dependecy section

Expand Down
79 changes: 79 additions & 0 deletions test/modules/ecs6-class/line.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const Line =require('../../../modules/ecs6-class/line')
const Point = require('../../../modules/ecs6-class/point')
const{calculateSlope}=require('../../../modules/ecs6-class/line')

const mockConstructor1=jest.fn(constructor)
const mockConstructor=jest.fn(constructor)
const mockCalculateSlope=jest.fn(calculateSlope)
Copy link
Owner

Choose a reason for hiding this comment

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

this way of mocks is total wrong
mock classes the right way and restore the mocks at the end of the tests


describe('constructor tests',()=>{

it('should create a Line instance with correct slope, and n values',()=>{
const l=new Line({n:4,slope:7})
expect(l.slope).toBe(7)
expect(l.n).toBe(4)

})
it('should throw an error for invalid input types',()=>{
const l=new Line({n:true,slope:7})
expect(()=>l.toThrow('Invalid input should be numbers'))
})
})
describe('calculacalculateSlope function test',()=>{
it('calculacalculateSlope should throw an error when dividing by zeroteSlope',()=>{
const p1=mockConstructor(new Point({x:5,y:6}))
const p2=mockConstructor(new Point({x:5,y:3}))
const line=mockConstructor1(new Line({point1:p1,point2:p2}))
expect(()=>line.calculateSlope()).toThrowError('error division by 0')

})
it('should return 2',()=>{
const line=mockConstructor1(new Line({point1:new Point({x:4,y:8}),point2:new Point({x:5,y:10})}))
line.calculateSlope()
expect(line.slope).toBe(2)

})
})
describe(' getPointByY',()=>{
it('should return new point acoording the y value',()=>{
const line=mockConstructor1(new Line({point1:new Point({x:0,y:4}),n:6,slope:2}));
const l=line.getPointByY(line.point1.y)
expect(l).toEqual(new Point({x:-1,y:4}))
})
it('shuold throw error when divison by zero',()=>{
const line=mockConstructor1(new Line({point1:new Point({x:0,y:4}),point2:new Point({x:0,y:6}),slope:0}));
expect(()=>line.getPointByY(line.point1.y)).toThrow("don't divide by 0")
})
})
describe('getPointByX tests',()=>{
it('check if the function return the currect value of y',()=>{
const line=mockConstructor1(new Line({point1:new Point({x:8,y:0}),n:7,slope:5}))
const l=line.getPointByX(line.point1.x)
expect(l).toEqual(new Point({x:8,y:47}))
})
})

describe('getPointOnXAsis tests', () => {
it('should return the point on the x-axis with y-coordinate 0', () => {
const line =mockConstructor1(new Line({ point1: new Point({ x: 8, y: 0 }), n: 7, slope: 5 }))
const pointOnXAxis = line.getPointOnXAsis();
expect(pointOnXAxis).toEqual(new Point({ x: -1.4, y: 0 }));
});
});
describe('getPointOnYAsis tests', () => {
it('should return the point on the y-axis with x-coordinate 0', () => {
const line =mockConstructor1(new Line({ point1: new Point({ x: 0, y: 6 }), n: 7, slope: 5 })) ;
const pointOnYAxis = line.getPointOnYAsis();
expect(pointOnYAxis).toEqual(new Point({ x: 0, y: 7 }));
});
});

describe('calculateNOfLineFunction tests',()=>{
it('should update the n with the corrent value',()=>{
const l=mockConstructor1(new Line({point1:new Point({x:0,y:0}),point2:new Point({x:2,y:2}),slope:5}))
mockCalculateSlope(l.calculateSlope())
l.calculateNOfLineFunction()
expect(l.n).toEqual(0)
})
})

54 changes: 54 additions & 0 deletions test/modules/ecs6-class/point.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

const Point = require('../../../modules/ecs6-class/point');


const mockConstructor = jest.fn(constructor);

describe('Constructor tests', () => {
it ('check if the constructor get variable' ,()=>{
const point=new Point({x:1,y:2})
expect(point.x).toBe(1)
expect(point.y).toBe(2)
})

it('should throw an error for invalid input types', () => {
expect(() => new Point({ x:"rudi",y:'5'})).toThrowError('x and y must be of the number type');
})
it('should throw error when the values is not number type', () => {
expect(() => new Point({ x: "#", y: 4 })).toThrowError('x and y must be of the number type');
})
it('should throw error when the values is not number type', () => {
expect(() => new Point({ x: 2, y: false })).toThrowError('x and y must be of the number type');
})
it('Point constructor should set default values if no parameters are provided', () => {
const point=new Point()
expect(point.x).toBe(0)
expect(point.y).toBe(0)

})

});
describe('check the function moveVertical',()=>{
it('moveVertical should update the y coordinate correctly',()=>{
const point = mockConstructor(new Point({ x: 1, y: 2 }))
point.moveVertical(3);
expect(point.y).toBe(5);
})
it('moveVertical should only accept a number as input',()=>{
const point = mockConstructor(new Point({ x: 0, y: 8 }))
expect(() => point.moveVertical('5')).toThrow('Invalid input. value should be a number.');
})
})
describe('check the function moveHorizontal',()=>{
it('moveHorizontal should update the y coordinate correctly',()=>{
const point=mockConstructor(new Point(0,0));
point.moveHorizontal(6)
expect(point.x).toBe(6)

})
it('moveHorizontal should only accept a number as input',()=>{
const point=mockConstructor(new Point({x:4,y:5}))
expect(() => point.moveHorizontal('invalid')).toThrow('Invalid input. value should be a number.');
})

})
Loading