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

calculateSlope = () => {
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
if( this.point1.x==0 && this.point1.y==0 || this.point2.x==0 &&  this.point2.y==0)
throw new Error("Expect to get two points");
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x);
}

calculateNOfLineFunction = () => {
if(this.point1.x==0 && this.point1.y==0)
throw new Error("Expect to get point");
if(this.slope==undefined)
throw new Error("Expect to get slope");
this.n = this.point1.y - this.slope * this.point1.x
}

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


getPointByX(x) {
if(x===undefined)
throw new Error("Expect to get x");
if(this.slope===undefined && this.n===undefined)
throw new Error("Expect to get line with slope and n");
if(this.slope===undefined)
throw new Error("Expect to get line with slope");
if(this.n===undefined)
throw new Error("Expect to get line with n");
let y = this.slope * x + this.n
return new Point({ x, y })
}

getPointByY(y) {
if(y===undefined)
throw new Error("Expect to get y");
if(this.slope===undefined && this.n===undefined)
throw new Error("Expect to get line with slope and n");
if(this.slope===undefined)
throw new Error("Expect to get line with slope");
if(this.n===undefined)
throw new Error("Expect to get line with n");
let x = (y - this.n) / this.slope;
return new Point({ x, y })
}
Expand Down
18 changes: 15 additions & 3 deletions modules/ecs6-class/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@ class Point {
this.x = x;
this.y = y;
}
moveVertical(value) {
this.y += value;
moveVertical(value) {
if(typeof(value) == "number"){
this.y += value
}
else{
throw new Error("Expect to get a number")
};
}

moveHorizontal(value) {
this.x += value;
if(typeof(value) == "number"){
this.x += value
}
else{
throw new Error("Expect to get a number")

};
}
}

Expand Down
18 changes: 17 additions & 1 deletion modules/geometry-calculation.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
const Line = require('./ecs6-class/line')

const calculateDistance = (point1, point2) => {
if((point1 == undefined && point2 == undefined) || point1==undefined || point2==undefined)
{
throw new Error("Expect to get two points")
}
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 == undefined && line2 == undefined) || line1==undefined || line2==undefined)
{
throw new Error("Expect to get two lines")
}
if(line2.slope ==undefined || line1.slope==undefined)
{
throw new Error("Expect to get line with slope")
}
if (line1.slope === line2.slope) {
if (line1.n === line2.n) {
return true
Expand All @@ -24,6 +36,10 @@ const calculateJunctionPoint = (line1, line2) => {
}

const isPointOnLine = (line, point) => {
if(point == undefined || line==undefined || (point == undefined && line==undefined))
{
throw new Error("Expect to get line and point")
}
const proxyLine = new Line({ point1: line.point1, point2: point })
proxyLine.calculateSlope()
if (line.slope === proxyLine.slope) {
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
122 changes: 122 additions & 0 deletions tests/modules/ecs6-class/line.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
const Line = require('../../../modules/ecs6-class/line');
const Point = require('../../../modules/ecs6-class/point');
const pointTest1 = new Point({ x: 10, y: 30 });
const pointTest2 = new Point({ x: 3, y: 9 });
let lineTest = new Line({point1: pointTest1, point2: pointTest2})


describe('CALCULATE_SLOPE', () => {

it('Enters a value for slope', () => {
lineTest.calculateSlope()
expect(lineTest.slope).toBe(3)
})
})

describe('CALCULATE_N_OF_LINE_FUNCTION', () => {

it('Enters a value for n', () => {

lineTest.calculateNOfLineFunction()
expect(lineTest.n).toBe(0)

})
})


describe('GET_POINT_BY_X', () => {

it('get point by x', () => {
let np = lineTest.getPointByX(8)
expect(np).toEqual({"x": 8, "y": 24})
})
it('get negative point by x', () => {
let np = lineTest.getPointByX(-8)
expect(np).toEqual({"x": -8, "y": -24})
})
})

describe('GET_POINT_BY_Y', () => {

it('get point by y', () => {
let np = lineTest.getPointByY(9)
expect(np).toEqual({"x": 3, "y": 9})
})

it('get negative point by y', () => {
let np = lineTest.getPointByY(-9)
expect(np).toEqual({"x": -3, "y": -9})
})
})


describe('GET_POINT_ON_X_ASIS', () => {

it('get point', () => {
let np = lineTest.getPointOnXAsis()
expect(np).toEqual({"x": 0, "y": 0})
})
})

describe('GET_POINT_ON_Y_ASIS', () => {

it('get point', () => {
let np = lineTest.getPointOnYAsis()
expect(np).toEqual({"x": 0, "y": 0})
})
})


describe('ERRORS', () => {

let lineTest1 = new Line({point1: pointTest1})
let lineTest2 = new Line({slope: 7})
let lineTest3 = new Line({point1: pointTest1, point2: pointTest2, slope:9})
let lineTest4 = new Line({point1: pointTest1, point2: pointTest2, n:5})
let lineTest5 = new Line({point1: pointTest1, point2: pointTest2})

it('calculateSlope expect get line with 2 points', () => {
expect(() => lineTest1.calculateSlope()).toThrow("Expect to get two points")
})

it('calculateNOfLineFunction expect get line with point', () => {
expect(() => lineTest2.calculateNOfLineFunction()).toThrow("Expect to get point")
})

it('calculateNOfLineFunction expect get line with slope', () => {
expect(() => lineTest1.calculateNOfLineFunction()).toThrow("Expect to get slope")
})

it('getPointByX expect get x', () => {
expect(() => lineTest3.getPointByX()).toThrow("Expect to get x")
})

it('getPointByX expect get line with n', () => {
expect(() => lineTest3.getPointByX(6)).toThrow("Expect to get line with n")
})

it('getPointByX expect get line with slope', () => {
expect(() => lineTest4.getPointByX(6)).toThrow("Expect to get line with slope")
})

it('getPointByX expect get line with slope and n', () => {
expect(() => lineTest5.getPointByX(6)).toThrow("Expect to get line with slope and n")
})

it('getPointByY expect get y', () => {
expect(() => lineTest3.getPointByY()).toThrow("Expect to get y")
})

it('getPointByY expect get line with n', () => {
expect(() => lineTest3.getPointByY(6)).toThrow("Expect to get line with n")
})

it('getPointByY expect get line with slope', () => {
expect(() => lineTest4.getPointByY(6)).toThrow("Expect to get line with slope")
})

it('getPointByY expect get line with slope and n', () => {
expect(() => lineTest5.getPointByY(6)).toThrow("Expect to get line with slope and n")
})
})

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

describe('MOVE_VERTICAL', () => {

it('Adding number to point.y', () => {
let pointTest = new Point({ x: 3, y: 8 });
pointTest.moveVertical(6)
expect(pointTest.y).toBe(14)

})


it('Adding negative number to point.y', () => {
let pointTest = new Point({ x: 3, y: 8 })
pointTest.moveVertical(-6)
expect(pointTest.y).toBe(2)
})

})

describe('MOVE_HORIZONTAL', () => {
it('Adding number to point.x', () => {
let pointTest = new Point({ x: 3, y: 8 });
pointTest.moveHorizontal(6)
expect(pointTest.x).toBe(9)
})


it('Adding negative number to point.x', () => {
let pointTest = new Point({ x: 3, y: 8 })
pointTest.moveHorizontal(-2)
expect(pointTest.x).toBe(1)
})

})

describe('ERRORS', () => {

let pointTest = new Point({ x: 3, y: 8 });

describe('MOVE_VERTICAL_ERRORS', () => {

it('Adding invalid value to point', () => {

expect(() => pointTest.moveVertical("one")).toThrow("Expect to get a number")
})
it('Adding invalid value to point', () => {

expect(() => pointTest.moveVertical([7,8])).toThrow("Expect to get a number")
})
it('Adding invalid value to point', () => {

expect(() => pointTest.moveVertical({"one":1})).toThrow("Expect to get a number")
})
it('Adding invalid value to point', () => {

expect(() => pointTest.moveVertical(false)).toThrow("Expect to get a number")
})
it('Adding invalid value to point', () => {

expect(() => pointTest.moveVertical()).toThrow("Expect to get a number")
})
})

describe('MOVE_HORIZONTAL_ERRORS', () => {

it('Adding invalid value to point', () => {

expect(() => pointTest.moveHorizontal("one")).toThrow("Expect to get a number")
})
it('Adding invalid value to point', () => {

expect(() => pointTest.moveHorizontal([7,8])).toThrow("Expect to get a number")
})
it('Adding invalid value to point', () => {

expect(() => pointTest.moveHorizontal({"one":1})).toThrow("Expect to get a number")
})
it('Adding invalid value to point', () => {

expect(() => pointTest.moveHorizontal(false)).toThrow("Expect to get a number")
})
it('Adding invalid value to point', () => {

expect(() => pointTest.moveHorizontal()).toThrow("Expect to get a number")
})
})

})











Loading