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

class Line {
constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) {
if (!(point1 instanceof Point))
throw new Error('point1 is not point')
this.point1 = point1;
if (!(point2 instanceof Point))
throw new Error('point2 is not point')
this.point2 = point2;
if (slope !== undefined && typeof slope !== 'number')
throw new Error('slope is not number')
this.slope = slope;
if (n !== undefined && typeof (n) !== 'number')
throw new Error('n is not number')
this.n = n;
}

calculateSlope = () => {
if(this.point1.x === this.point2.x&&this.point1.y === this.point2.y){
throw new Error('Both points have the same values so it is not a line'
)
}
if(this.point1.x === this.point2.x){
throw new Error('The x of point1 is equal to the x of point2 so this line is not function')
}

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

Expand All @@ -26,14 +42,31 @@ class Line {


getPointByX(x) {
if(x===undefined){
throw Error('x is not a defined')
}
if(typeof(x)!=="number"){

throw Error('x is not a number')
}
let y = this.slope * x + this.n
return new Point({ x, y })
}

getPointByY(y) {
let x = (y - this.slope) / this.n;
if(y===undefined){
throw Error('y is not defined')
}
if(typeof(y)!=="number"){

throw Error('y is not number')
}

let x = (y - this.n) / this.slope;
return new Point({ x, y })
}
}

module.exports = Line
module.exports = Line


29 changes: 27 additions & 2 deletions modules/ecs6-class/point.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
class Point {
constructor({x=0, y=0}={}) {

constructor({ x = 0, y = 0 } = {}) {

if (typeof (x) !== "number" && typeof (y) != "number") {
throw Error('x and y are not number')
}
if (typeof (x) != "number") {
throw Error('x is not number')
}
if (typeof (y) != "number") {
throw Error('y is not number')
}
this.x = x;
this.y = y;
}

moveVertical(value) {
if (!value)
throw Error("value is not define")
if(typeof(value)!=="number")
throw Error("value is not a number")
this.y += value;
}
moveHorizontal(value) {
if (!value)
throw Error("value is not define")
if(typeof(value)!=="number")
throw Error("value is not a number")
this.x += value;
}
}

module.exports = Point


module.exports = Point



63 changes: 53 additions & 10 deletions modules/geometry-calculation.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
const Line = require('./ecs6-class/line')

const Line = require("./ecs6-class/line");
const Point = require("./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.

Get back to the previous import state

const calculateDistance = (point1, point2) => {
if (!(point1 instanceof Point) || !(point2 instanceof Point)) {
throw Error('point1 or point2 is not point');
}
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('line1 and line2 is not a line');
}

if (!(line1 instanceof Line)) {
throw Error('line1 is not a line');
}

if (!(line2 instanceof Line)) {
throw Error('line2 is not a line');
}
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 @@ -23,18 +51,33 @@ const calculateJunctionPoint = (line1, line2) => {
}
}


const isPointOnLine = (line, point) => {
const proxyLine = new Line(line.point1, point)
proxyLine.calculateSlope()
if (!(line instanceof Line) || !(point instanceof Point)) {
throw new Error('line is not point or point is not a line');
}

const proxyLine = new Line({ point1: line.point1, point2: point });
proxyLine.calculateSlope();
if (!line.slope) {
line.calculateSlope()
}
if(!line.n){
line.calculateNOfLineFunction()
}
if (line.slope === proxyLine.slope) {
proxyLine.calculateNOfLineFunction()
if (line.n === proxyLine.n2) {
return true
proxyLine.calculateNOfLineFunction();

if (line.n === proxyLine.n) {
return true;
}
else {
return false
}
}
return false
}

return false;
};
module.exports = {
calculateDistance,
calculateJunctionPoint,
Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
"description": "practice unit tests in javascript",
"main": "index.js",
"scripts": {
"test": "jest"
},
"dependencies": {
"jest": "^29.7.0"
"test": "jest",
"test:coverage": "npm run test -- --coverage"
},
"repository": {
"type": "git",
Expand Down
173 changes: 173 additions & 0 deletions tests/modules/ecs6-class/line.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@

const Line = require('../../../modules/ecs6-class/line');
const Point = require('../../../modules/ecs6-class/point')
const mockConstructor = jest.fn(constructor);
let line
describe('CONSTRUCTORE', () => { })


it('should initialize point2, n and slope to default values when no parameters are provided', () => {
line = new Line({ point1: mockConstructor(new Point({ x: 1, y: 5 })) });
expect(line.point1).toStrictEqual(mockConstructor(new Point({ x: 1, y: 5 })))
expect(line.point2).toStrictEqual(mockConstructor(new Point()))
expect(line.n).toBe(undefined)
expect(line.slope).toBe(undefined)
})
it('should initialize point1, n and value to default values when no parameters are provided', () => {
line = new Line({ point2: mockConstructor(new Point({ x: 1, y: 5 })) });
expect(line.point1).toStrictEqual(mockConstructor(new Point()))
expect(line.point2).toStrictEqual(mockConstructor(new Point({ x: 1, y: 5 })))
expect(line.n).toBe(undefined)
expect(line.slope).toBe(undefined)
})
it('should initialize point1, point2 and slope to default values when no parameters are provided', () => {
line = new Line({ n: 5 });
expect(line.point1).toStrictEqual(mockConstructor(new Point()))
expect(line.point2).toStrictEqual(mockConstructor(new Point()))
expect(line.n).toBe(5)
expect(line.slope).toBe(undefined)
})
it('should initialize point1, point2 and n to default values when no parameters are provided', () => {
line = new Line({ slope: 5 });
expect(line.point1).toStrictEqual(mockConstructor(new Point()))
expect(line.point2).toStrictEqual(mockConstructor(new Point()))
expect(line.n).toBe(undefined)
expect(line.slope).toBe(5)
})
it('should initialize to default values when no parameters are provided', () => {
line = new Line({})
expect(line.point1).toStrictEqual(mockConstructor(new Point()))
expect(line.point2).toStrictEqual(mockConstructor(new Point()))
expect(line.n).toBe(undefined)
expect(line.slope).toBe(undefined)

})
it('Checks if the parameters are of the correct type', () => {

expect(() => new Line({ point1: "ab", point2: new Point({ x: 5, y: 5 }), slope: 1, n: 5 })).toThrow('point1 is not point');
expect(() => new Line({ point1: mockConstructor(new Point({ x: 5, y: 5 })), point2: "ab", slope: 1, n: 5 })).toThrow('point2 is not point');
expect(() => new Line({ point1: new Point({ x: 5, y: 5 }), point2: mockConstructor(new Point({ x: 5, y: 5 })), slope: "d", n: 5 })).toThrow('slope is not number');
expect(() => new Line({ point1: new Point({ x: 5, y: 5 }), point2: mockConstructor(new Point({ x: 5, y: 5 })), slope: 1, n: "d" })).toThrow('n is not number');
})
it('Point constructor initializes point1 and point2 and slope and n correctly', () => {
line = new Line({ point1: mockConstructor(new Point({ x: 5, y: 8 })), point2: mockConstructor(new Point({ x: 5, y: 8 })), slope: 5, n: 8 });
expect(line.point1).toEqual(mockConstructor(new Point({ x: 5, y: 8 })));
expect(line.point2).toEqual(mockConstructor(new Point({ x: 5, y: 8 })));
expect(line.slope).toBe(5);
expect(line.n).toBe(8);

});
describe('CALCULATE_SLOPE', () => {

it('should The slope is 0 as the line is vertical (point1= point2) ', () => {
line = mockConstructor(new Line(({ point1: mockConstructor(new Point({ x: 5, y: 8 })), point2: mockConstructor(new Point({ x: 5, y: 8 })), n: 8 })))
expect(() => line.calculateSlope()).toThrow('Both points have the same values so it is not a line');

})

it('should The slope is undefined as the line is vertical (y1 = y2) ', () => {
line = mockConstructor(new Line(({ point1: mockConstructor(new Point({ x: 5, y: 8 })), point2: mockConstructor(new Point({ x: 3, y: 8 })), slope: 5, n: 8 })))
line.calculateSlope()
expect(line.slope).toBeCloseTo(0);

})
it('should The slope is undefined as the line is vertical (x1=x2)', () => {
line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 5, y: 9 })), point2: mockConstructor(new Point({ x: 5, y: 8 })), n: 8 }));
expect(() => line.calculateSlope()).toThrow('The x of point1 is equal to the x of point2 so this line is not function');
});

it('should move vertically correctly', () => {
line = mockConstructor(new Line(({ point1: mockConstructor(new Point({ x: -1, y: 8 })), point2: mockConstructor(new Point({ x: 2, y: 5 })), slope: 5, n: 8 })));
line.calculateSlope()
expect(line.slope).toBe(-1);

})

})
describe('CALCULATE_N_OF_LINE_FUNCTION', () => {
it(' should move vertically correctly', () => {
line = mockConstructor(new Line(({ point1: mockConstructor(new Point({ x: 5, y: 8 })), point2: mockConstructor(new Point({ x: 5, y: 5 })), slope: 1, n: 8 })));
line.calculateNOfLineFunction()
expect(line.n).toBe(3);

})

})

describe('GET_POINT_BY_X', () => {
it('should x is not define ', () => {
const line = mockConstructor(new Line({}))
expect(() => line.getPointByX()).toThrow('x is not a defined');
})

it('should x is not number ', () => {
const line = mockConstructor(new Line({}))
expect(() => line.getPointByX("abc")).toThrow('x is not a number');
})

it(' should check that the returned value is of type point', () => {
const line = mockConstructor(new Line({}))
const point = line.getPointByX(5)
expect(point).toBeInstanceOf(Point)

})
it('should checks that point is returned with correct values', () => {
const line = mockConstructor(new Line(({ point1: new Point({ x: 5, y: 8 }), point2: new Point({ x: 5, y: 5 }), slope: 1, n: 8 })))
initialx = line.point1.x
const point = line.getPointByX(5)
expect(point.x).toBe(initialx)
expect(point.y).toBe(13)
})
})
describe('GET_POINT_B_Y', () => {
it('should y is not define ', () => {
const line = mockConstructor(new Line({}))
expect(() => line.getPointByY()).toThrow('y is not defined');
})
})

it('should y is not number ', () => {
const line = mockConstructor(new Line({}))
expect(() => line.getPointByY("abc")).toThrow('y is not number');
})

it(' should check that the returned value is of type point', () => {
const line = mockConstructor(new Line({}))
const point = line.getPointByY(5)
expect(point).toBeInstanceOf(Point)

})
it('should checks that point is returned with correct values', () => {
const line = mockConstructor(new Line(({ point1: mockConstructor(new Point({ x: 8, y: 6 })), point2: mockConstructor(new Point({ x: 5, y: 5 })), slope: 1, n: 3 })))
initialy = line.point1.y
const point = line.getPointByY(6)
expect(point.x).toBe(3)
expect(point.y).toBe(initialy)
})

describe('GET_POINT_ON_X_ASIS', () => {

it('returns the correct point on the y-axis for the given slope and n value', () => {

const line = mockConstructor(new Line(({ slope: 2, n: -6 })))
const point = line.getPointOnXAsis()
expect(point.y).toBe(0)
expect(point.x).toBe(3)



})
})
describe('GET_POINT_ON_Y_ASIS', () => {
it('returns the correct point on the y-axis for the given slope and n value', () => {
const line = mockConstructor(new Line({ slope: 5, n: 5 }))
const point = line.getPointOnYAsis()
expect(point.y).toBe(5)
expect(point.x).toBe(0)


})
})



Loading