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
57 changes: 53 additions & 4 deletions modules/ecs6-class/line.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,44 @@
const Point = require("./point");


class Line {
constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) {
constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined } = {}) {
if (!(point1 instanceof Point)) {
throw Error('The point1 should be a Point type')
}
if (!(point2 instanceof Point)) {
throw Error('The point2 should be a Point type')
}
if (typeof (n) !== 'number' && typeof (n) !== 'undefined') {
throw Error('The n should have a number')
}
if (typeof (slope) !== 'number' && typeof (slope) !== 'undefined') {
throw Error('The slope should have a number')
}
this.point1 = point1;
this.point2 = point2;
this.slope = slope;
this.n = n;
}

calculateSlope = () => {
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
calculateSlope() {
if (this.point1.x === this.point2.x) {
throw Error("it isn't a real geometry line")
}
else {
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
}

}

calculateNOfLineFunction = () => {
calculateNOfLineFunction() {
if (!this.slope) {
this.calculateSlope()

}
this.n = this.point1.y - this.slope * this.point1.x


}

getPointOnXAsis() {
Expand All @@ -26,11 +51,35 @@ class Line {


getPointByX(x) {
if (typeof (x) !== 'number') {
throw Error('The value is of an invalid type')
}
if (!this.slope) {
this.calculateSlope()

}
if (!this.n) {
this.calculateNOfLineFunction()
}

let y = this.slope * x + this.n
return new Point({ x, y })
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 the slope or the line are not calculated yet?

Copy link
Owner

Choose a reason for hiding this comment

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

first check what is the n value
you can call only the n, it will calculate the slope if the slope is undefined

}

getPointByY(y) {
if (typeof (y) !== 'number') {
throw Error('The value is of an invalid type')
}
if (!this.slope) {
this.calculateSlope()
}
if (!this.n) {
this.calculateNOfLineFunction()
}

if (this.slope === 0) {
throw Error('The slope cannot be 0')
}
let x = (y - this.n) / this.slope;
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 when the slope is not calculated or is zero?

Copy link
Owner

Choose a reason for hiding this comment

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

The same as the previous remark

return new Point({ x, y })
}
Expand Down
12 changes: 12 additions & 0 deletions modules/ecs6-class/point.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
class Point {
constructor({x=0, y=0}={}) {
if(typeof(x)!=='number'){
throw Error('The first value should be a number')
}
if(typeof(y)!=='number'){
throw Error('The second value should be a number')
}
this.x = x;
this.y = y;
}
moveVertical(value) {
if(typeof(value)!== 'number'){
throw Error('The value is of an invalid type')
}
this.y += value;
}
moveHorizontal(value) {
if(typeof(value)!== 'number'){
throw Error('The value is of an invalid type')
}
this.x += value;
}
}
Expand Down
38 changes: 36 additions & 2 deletions modules/geometry-calculation.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
const Line = require('./ecs6-class/line')
const Line = require('./ecs6-class/line');
const Point = require('./ecs6-class/point');




const calculateDistance = (point1, point2) => {
if (!(point1 instanceof Point) || !(point1 instanceof Point)) {
throw Error('The value is of an invalid type')
Copy link
Owner

Choose a reason for hiding this comment

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

Which of the point is invalid?
You can do a better check

}
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 Error('The values are of an invalid type')
}
if (!(line1 instanceof Line)) {
throw Error('The first value is of an invalid type')
}
if (!(line2 instanceof Line)) {
throw Error('The second value is of an invalid type')
}
if (!line1.slope) {
line1.calculateSlope()

}
if (!line2.slope) {
line2.calculateSlope()
}
if (!line1.n) {
line1.calculateNOfLineFunction()
}
if (!line2.n) {
line2.calculateNOfLineFunction()
}

if (line1.slope === line2.slope) {
if (line1.n === line2.n) {
return true
Expand All @@ -24,6 +54,10 @@ const calculateJunctionPoint = (line1, line2) => {
}

const isPointOnLine = (line, point) => {
if (!(line instanceof Line) || !(point instanceof Point)) {
throw Error('The value is of an invalid 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.

line has a calculated slope already?

Copy link
Owner

Choose a reason for hiding this comment

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

the calculateSlope returns a value???
you are updating the slope into undefined

Expand Down
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 correct section

Copy link
Owner

Choose a reason for hiding this comment

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

????

Expand Down
184 changes: 184 additions & 0 deletions tests/modules/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.

Call the test functions with the it expression

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

const getPointByYMock = jest
.spyOn(Line.prototype, 'getPointByY')

const getPointByXMock = jest
.spyOn(Line.prototype, 'getPointByX')

Copy link
Owner

Choose a reason for hiding this comment

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

where do you restore the mocks?
I prefer using the jest.mock and not the spyOn
but if you use it, restore it

Copy link
Owner

Choose a reason for hiding this comment

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

use this mocks inside the test where they are needed



describe('Check calculateSlope function', () => {
it(' Should return the slope of the line', () => {
let point1 = new Point({ x: 5, y: 7 });
let point2 = new Point({ x: 3, y: 8 });
let line1 = new Line({ point1, point2 });
line1.calculateSlope();
expect(line1.slope).toBe(-0.5)
})
it(' Should return 0 if the points are in the same position on the x-axis', () => {
let point1 = new Point({ x: 5, y: 7 });
let point2 = new Point({ x: 3, y: 7 });
let line1 = new Line({ point1, point2 });
line1.calculateSlope();
expect(line1.slope).toBe(0)
})
describe('ERRORS', () => {
it('should throw error when the type is not valid', () => {
let point1 = new Point({ x: 3, y: 7 });
let point2 = new Point({ x: 3, y: 8 });
let line1 = new Line({ point1, point2 });
expect(() => line1.calculateSlope()).toThrow("it isn't a real geometry line")


})
})

})

describe('Check calculateNOfLineFunction function', () => {
it('Should return n of the line', () => {
let point1 = new Point({ x: 5, y: 7 });
let point2 = new Point({ x: 3, y: 8 });
let line1 = new Line({ point1, point2, slope: -0.5 });
line1.calculateNOfLineFunction();
expect(line1.n).toBe(9.5)
})
it('Should return n of the line and computer the slpoe if it is not sent', () => {
let point1 = new Point({ x: 5, y: 7 });
let point2 = new Point({ x: 3, y: 8 });
let line1 = new Line({ point1, point2 });
line1.calculateNOfLineFunction();
expect(line1.slope).toBe(-0.5)
expect(line1.n).toBe(9.5)
})
})

describe('Check getPointOnXAsis function', () => {
Copy link
Owner

Choose a reason for hiding this comment

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

You are testing only if the mock was called, but where are the tests about the values of the function??

it('It should be returned if in the call to the function the function getPointByY was called', () => {
let point1 = new Point({ x: 5, y: 7 });
let point2 = new Point({ x: 3, y: 8 });
const line1 = new Line({point1,point2,n:7,slope:6});
line1.getPointOnXAsis();
expect(getPointByYMock).toHaveBeenCalled();
});
})

describe('Check getPointOnYAsis function', () => {
it('It should be returned if in the call to the function the function getPointByX was called', () => {
let point1 = new Point({ x: 5, y: 7 });
let point2 = new Point({ x: 3, y: 8 });
const line1 = new Line({point1,point2,n:7,slope:6});
line1.getPointOnYAsis();
expect(getPointByXMock).toHaveBeenCalled();
});
})


describe('Check getPointByX function', () => {
it(' Should return point', () => {
let point1 = new Point({ x: 5, y: 7 });
let point2 = new Point({ x: 3, y: 8 });
let line1 = new Line({ point1, point2, slope: 2, n: 7 });
expect(line1.getPointByX(6)).toEqual({ x: 6, y: 19 })

})
it('Should return point and computer the slpoe if it is not sent', () => {
let point1 = new Point({ x: 5, y: 7 });
let point2 = new Point({ x: 3, y: 8 });
let line1 = new Line({ point1, point2, n: 7 });
expect(line1.getPointByX(6)).toEqual({ x: 6, y: 4 })
expect(line1.slope).toBe(-0.5)
})
it('Should return point and computer the n if it is not sent', () => {
let point1 = new Point({ x: 5, y: 7 });
let point2 = new Point({ x: 3, y: 8 });
let line1 = new Line({ point1, point2, slope:-0.5 });
expect(line1.getPointByX(6)).toEqual({ x: 6, y: 6.5 })
expect(line1.n).toBe(9.5)
})
it('Should return a point and calculate n and the slope if they are not sent', () => {
let point1 = new Point({ x: 5, y: 7 });
let point2 = new Point({ x: 3, y: 8 });
let line1 = new Line({ point1, point2});
expect(line1.getPointByX(6)).toEqual({ x: 6, y: 6.5 })
expect(line1.slope).toBe(-0.5)
expect(line1.n).toBe(9.5)
})
describe('ERRORS', () => {
it('should throw error when the type is not valid', () => {
let point1 = new Point({ x: 5, y: 7 });
let point2 = new Point({ x: 3, y: 8 });
let line1 = new Line({ point1, point2 });
expect(() => line1.getPointByX({ x: 1 })).toThrow('The value is of an invalid type')
expect(() => line1.getPointByX('o')).toThrow('The value is of an invalid type')
expect(() => line1.getPointByX(false)).toThrow('The value is of an invalid type')
expect(() => line1.getPointByX([1, 2])).toThrow('The value is of an invalid type')
expect(() => line1.getPointByX()).toThrow("The value is of an invalid type")
})
})
})

describe('Check getPointByY function', () => {
it('Should return point ', () => {
let point1 = new Point({ x: 5, y: 7 });
let point2 = new Point({ x: 3, y: 8 });
let line1 = new Line({ point1,point2,slope: 2, n: 7 });
expect(line1.getPointByY(6)).toEqual({ x: -0.5, y: 6 })
})
it('Should return point and computer the slpoe if it is not sent', () => {
let point1 = new Point({ x: 5, y: 7 });
let point2 = new Point({ x: 3, y: 8 });
let line1 = new Line({ point1, point2, n: 9.5 });
expect(line1.getPointByY(6)).toEqual({ x: 7, y: 6 })
expect(line1.slope).toBe(-0.5)
})
it('Should return point and computer the n if it is not sent', () => {
let point1 = new Point({ x: 5, y: 7 });
let point2 = new Point({ x: 3, y: 8 });
let line1 = new Line({ point1, point2, slope:-0.5 });
expect(line1.getPointByY(6)).toEqual({ x: 7, y: 6 })
expect(line1.n).toBe(9.5)
})
it('Should return a point and calculate n and the slope if they are not sent', () => {
let point1 = new Point({ x: 5, y: 7 });
let point2 = new Point({ x: 3, y: 8 });
let line1 = new Line({ point1, point2});
expect(line1.getPointByY(6)).toEqual({ x: 7, y: 6 })
expect(line1.slope).toBe(-0.5)
expect(line1.n).toBe(9.5)
})
describe('ERRORS', () => {
it('should throw error when the type is not valid', () => {
let point1 = new Point({ x: 5, y: 7 });
let point2 = new Point({ x: 3, y: 8 });
let line1 = new Line({ point1, point2 });
expect(() => line1.getPointByY({ x: 2 })).toThrow('The value is of an invalid type')
expect(() => line1.getPointByY('o')).toThrow('The value is of an invalid type')
expect(() => line1.getPointByY(false)).toThrow('The value is of an invalid type')
expect(() => line1.getPointByY([1, 2])).toThrow('The value is of an invalid type')
expect(() => line1.getPointByY()).toThrow("The value is of an invalid type")
})
it('should throw error when The slope is equal to 0',()=>{
let point1 = new Point({ x: 5, y: 8 });
let point2 = new Point({ x: 3, y: 8 });
let line1= new Line({ point1, point2 ,slope:0});
expect(() => line1.getPointByY(21)).toThrow("The slope cannot be 0")
})
})
})

describe('ERRORS', () => {
it('should throw error when the values of the line is not valid', () => {
let point1 = new Point({ x: 2, y: 3 })
let point2 = new Point({ x: 4, y: 5 })
let line1;
expect(() => line1 = new Line({ point1: 9, point2, n: 4, slope: 6 })).toThrow('The point1 should be a Point type')
expect(() => line1 = new Line({ point1, point2: [1, 2], n: 4, slope: 6 })).toThrow('The point2 should be a Point type')
expect(() => line1 = new Line({ point1, point2, n: 'c', slope: 6 })).toThrow('The n should have a number')
expect(() => line1 = new Line({ point1, point2, n: 4, slope: false })).toThrow('The slope should have a number')

})
})

Loading