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

class Line {
constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) {
if (n != undefined && typeof (n) != 'number' || slope != undefined && typeof (slope) != 'number')
throw new Error('type of n and slope need be number')
if (!(point1 instanceof Point) || !(point2 instanceof Point))
throw new Error('points need be object of Point class')
this.point1 = point1;
this.point2 = point2;
this.slope = slope;
this.n = n;
}

calculateSlope = () => {

if ((this.point1.x - this.point2.x) === 0) {
throw ("It is impossible to divide by 0")
}

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

}

calculateNOfLineFunction = () => {
if (!this.slope)
this.calculateSlope()
this.n = this.point1.y - this.slope * this.point1.x
}

Expand All @@ -24,13 +36,20 @@ class Line {
return this.getPointByX(0)
}


getPointByX(x) {
if (this.slope == undefined)
this.calculateSlope();
if(this.n == undefined)
this.calculateNOfLineFunction();
let y = this.slope * x + this.n
return new Point({ x, y })
}

getPointByY(y) {
if (this.slope == undefined)
this.calculateSlope();
if(this.n == undefined)
this.calculateNOfLineFunction();
let x = (y - this.n) / this.slope;
return new Point({ x, y })
}
Expand Down
19 changes: 18 additions & 1 deletion modules/ecs6-class/point.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
class Point {
constructor({x=0, y=0}={}) {
constructor({ x = 0, y = 0 } = {}) {
this.x = x;
this.y = y;
}

moveVertical(value) {
if (!value) {
throw ("must give isn\t argument")
}
if (typeof (value) != "number") {
throw ("type value isn\t number")
}

this.y += value;

}

moveHorizontal(value) {
if (!value) {
throw ("must give isn\t argument")
}
if (typeof (value) != "number") {
throw ("type value isn\t number")
}
this.x += value;

}
}

Expand Down
27 changes: 25 additions & 2 deletions modules/geometry-calculation.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
const Line = require('./ecs6-class/line')
const Line = require('./ecs6-class/line');
const Point = require('./ecs6-class/point');

const calculateDistance = (point1, point2) => {
if (point1 == undefined || point2 == undefined)
throw new Error('two points were not received')
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 || !line2)
throw ("It should take two arguments")
if (!line1.slope)
line1.calculateSlope()
if (!line1.n)
line1.calculateNOfLineFunction()
if (!line2.slope)
line2.calculateSlope()
if (!line2.n)
line2.calculateNOfLineFunction()
if (line1.slope === line2.slope) {
if (line1.n === line2.n) {
return true
Expand All @@ -24,6 +37,15 @@ const calculateJunctionPoint = (line1, line2) => {
}

const isPointOnLine = (line, point) => {

if (!line || !(line instanceof Line)) {
throw ("line were not received")
}
if (!point || !point instanceof Point) {
throw ("point were not received")
}
if (line.slope === undefined)
line.calculateSlope()
const proxyLine = new Line({ point1: line.point1, point2: point })
proxyLine.calculateSlope()
if (line.slope === proxyLine.slope) {
Expand All @@ -35,6 +57,7 @@ const isPointOnLine = (line, point) => {
return false
}


module.exports = {
calculateDistance,
calculateJunctionPoint,
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":"jest --coverage"
},
"dependencies": {
"jest": "^29.7.0"
Expand Down
85 changes: 85 additions & 0 deletions test/modules/ecs6-class/line.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const Line = require("../../../modules/ecs6-class/line")
const Point = require("../../../modules/ecs6-class/point")

const point1 = new Point({ x: 9, y: 6 })
const point2 = new Point({ x: 7, y: 2 })
const line = new Line({ point1, point2 })

describe("CALCULATE_SLOPE", function () {

test("should return calculateSlope for two points", () => {

const point1 = new Point({ x: 9, y: 6 })
const point2 = new Point({ x: 7, y: 2 })
const line = new Line({ point1, point2 })

line.calculateSlope()
expect(line.slope).toBe(2)
})


})

describe("CALCULATE_N_OF_LINE_FUNCTION", () => {
test("", () => {
line.calculateNOfLineFunction()
expect(line.n).toBe(-12)

})
})
describe("GET_POINT_ON_X_ASIS", () => {
test("", () => {
line.getPointOnXAsis()
expect(line.point1.x).toBe(9)
})
})
describe("GET_POINT_ON_Y_ASIS", () => {
test("", () => {
line.getPointOnYAsis()
expect(line.point1.y).toBe(6)
})
})
describe("GET_POINT_BY_X", () => {
test("", () => {
const point1 = new Point({ x: 9, y: 6 })
const point2 = new Point({ x: 7, y: 2 })
const line = new Line({ point1, point2 })


line.getPointByX()
expect(line.point1.x).toBe(9)
})
})

describe("GET_POINT_BY_Y", () => {
test("", () => {
const point1 = new Point({ x: 9, y: 6 })
const point2 = new Point({ x: 7, y: 2 })
const line = new Line({ point1, point2 })

line.getPointByY()
expect(line.point1.y).toBe(6)
})
})
describe("ERRORS", function () {
test("", () => {

expect(() => new Line({ point1: new Point({ x: 4, y: 3 }), point2: new Point({ x: 7, y: 5 }), n: '@', slope: 'chayale' })).toThrow("type of n and slope need be number")
})
it('', () => {
expect(() => new Line({ point1: 'abc', point2: { x: 5, y: 9 } })).toThrow("points need be object of Point class")

})
it('should throw error when no points have been sent to the calculateSlope function', function () {
ll1 = new Line({})
expect(() => ll1.calculateSlope()).toThrow('It is impossible to divide by 0')
})




})




53 changes: 53 additions & 0 deletions test/modules/ecs6-class/point.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const Point = require("../../../modules/ecs6-class/point")

describe("MOVE_VERTICAL", function () {
it("should return point y", () => {
const point = new Point({ x: 5, y: 7 })
point.moveVertical(2)
expect(point.y).toBe(9)
})
it("should return point y", () => {
const point = new Point({ x: 5, y: 7 })
point.moveVertical(-3)
expect(point.y).toBe(4)
})



})
describe("MOVE_HORZIZONTAL", function () {
it("should return point x", () => {
const point = new Point({ x: 4, y: 6 })
point.moveHorizontal(7)
expect(point.x).toBe(11)
})
it("should return point x", () => {
const point = new Point({ x: 4, y: 6 })
point.moveHorizontal(-3)
expect(point.x).toBe(1)
})
})
describe("ERRORS", function () {
const point = new Point({ x: 9, y: 4 })
it('!value', () => {
expect(() => point.moveVertical()).toThrow('must give isn\t argument')

})
it('typeof(value)!="number"', () => {
expect(() => point.moveVertical("abc")).toThrow('type value isn\t number')
expect(() => point.moveVertical([9, 5])).toThrow('type value isn\t number')
expect(() => point.moveVertical('true')).toThrow('type value isn\t number')
})
it('', () => {
expect(() => point.moveHorizontal()).toThrow('must give isn\t argument')
})

it('', () => {

expect(() => point.moveHorizontal("abc")).toThrow('type value isn\t number')
expect(() => point.moveHorizontal([4, 6])).toThrow('type value isn\t number')
expect(() => point.moveHorizontal('false')).toThrow('type value isn\t number')

})

})
89 changes: 89 additions & 0 deletions test/modules/geometry-calculation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const Point = require("../../modules/ecs6-class/point")
const Line = require("../../modules/ecs6-class/line")
const { calculateDistance, calculateJunctionPoint, isPointOnLine } = require("../../modules/geometry-calculation")
let n = 7
let n1 = 3
let slope = -6
let slope1 = 2

const point1 = new Point({ x: 5, y: 8 })
const point2 = new Point({ x: 1, y: 9 })
const point3 = new Point({ x: 6, y: 2 })
const line1 = new Line({ point1, point2, slope, n })
const line2 = new Line({ point1, point3, slope, n })
const line3 = new Line({ point2, point3, slope1, n })
const line4 = new Line({ point2, point3, slope, n1 })


describe("CALCULATE_DISTANCE", () => {
test("", () => {
expect(calculateDistance(point1, point2)).toEqual(4.123105625617661)
})

})


describe("CALCULATE_JUNCTION_POINT", () => {
test("should return ", () => {
expect(calculateJunctionPoint(line1, line2)).toBe(true)

})
test("should ", () => {

expect(calculateJunctionPoint(line1, line4)).toBe(false)
})
test("", () => {
let l1 = new Line({ point1: new Point({ x: 1, y: 4 }), point2: new Point({ x: 2, y: 7 }) });
let l2 = new Line({ point1: new Point({ x: 6, y: 12 }), point2: new Point({ x: 3, y: 8 }) });
expect(calculateJunctionPoint(l1, l2)).toEqual({ x: 1.7999999999999998, y: 6.3999999999999995 })
})
})


describe("IS_POINT_ON_LINE", () => {

const point5 = new Point({ x: 2, y: 2 })
const point6 = new Point({ x: 1, y: 1 })

const line4 = new Line({ x: point5, y: point6, slope: 1, n: 0 })

test("",()=>{
expect(isPointOnLine(line4, point5)).toBe(true)
})
it("", () => {
let l = new Line({ point1: new Point({ x: 1, y: 4 }), point2: new Point({ x: 2, y: 2 }) });
let p = new Point({ x: 3, y: 7 });
expect(isPointOnLine(l, p)).toBe(false)
})
it("", () => {

expect(isPointOnLine(line4, point6)).toBe(true)
})
it("", () => {

const point5 = new Point({ x: 2, y: 2 })
const point6 = new Point({ x: 1, y: 1 })

const line4 = new Line({ x: point5, y: point6, slope: 1, n: 7 })
expect(isPointOnLine(line4, point6)).toBe(false)
})

})

describe("ERROR", () => {
it('', () => {
expect(() => calculateDistance(point1)).toThrow("two points were not received")
})
it('', () => {
expect(() => calculateJunctionPoint(line2)).toThrow("It should take two arguments")
})
it('', () => {
let l = new Point()
expect(() => isPointOnLine(l)).toThrow("line were not received")
})
it('', () => {
let p = new Line({})
expect(() => isPointOnLine(p)).toThrow("point were not received")
})

})