diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index 826c675..153cfd9 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -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 } @@ -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 }) } diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index e81b4a4..c92898c 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -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") + + }; } } diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 6e11643..4ef4416 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -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 @@ -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) { diff --git a/package.json b/package.json index 56bf17b..05a4561 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/tests/modules/ecs6-class/line.test.js b/tests/modules/ecs6-class/line.test.js new file mode 100644 index 0000000..cfe9ffa --- /dev/null +++ b/tests/modules/ecs6-class/line.test.js @@ -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") + }) +}) + diff --git a/tests/modules/ecs6-class/point.test.js b/tests/modules/ecs6-class/point.test.js new file mode 100644 index 0000000..5e15855 --- /dev/null +++ b/tests/modules/ecs6-class/point.test.js @@ -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") + }) + }) + +}) + + + + + + + + + + + diff --git a/tests/modules/geometry-calculation.test.js b/tests/modules/geometry-calculation.test.js new file mode 100644 index 0000000..cf1642c --- /dev/null +++ b/tests/modules/geometry-calculation.test.js @@ -0,0 +1,108 @@ +// const geometryCalculation = require ('../../modules/geometry-calculation'); + +const { calculateDistance, calculateJunctionPoint, isPointOnLine } = require('../../modules/geometry-calculation') +const Line = require('../../modules/ecs6-class/line'); +const Point = require('../../modules/ecs6-class/point'); +const pointTest1 = new Point({ x: 4, y: 7 }); +const pointTest2 = new Point({ x: 8, y: 10 }); +const pointTest3 = new Point({ x: 2, y: 9 }); +const pointTest4 = new Point({ x: -1, y: 0 }); +const pointTest5 = new Point({ x: 1, y: -17 }); +const lineTest1 = new Line({point1: pointTest1, point2: pointTest2, n: 9, slope: 8}); +const lineTest2 = new Line({point1: pointTest2, point2: pointTest1, n: 9, slope: 8}); +const lineTest3 = new Line({point1: pointTest2, point2: pointTest1, n: 2, slope: 8}); +const lineTest4 = new Line({point1: pointTest2, point2: pointTest1, n: 2, slope: 3}); +const lineTest5 = new Line({point1: pointTest3, point2: pointTest1, n: 3, slope: 3}); +const lineTest6 = new Line({point1: pointTest1, point2: pointTest2, n: 9}); +describe('CALCULATE_DISTANCE', () => { + it('calculate the distance', () => { + expect(calculateDistance(pointTest1, pointTest2)).toBe(5); + }); +}) + +describe('CALCULATE_JUNCTION_POINT', () => { + it('n and slope are the same in both lines', () => { + expect(calculateJunctionPoint(lineTest1, lineTest2)).toBe(true); + }); + + it('slope is the same in both lines and n is not the same', () => { + expect(calculateJunctionPoint(lineTest2, lineTest3)).toBe(false); + }); + + it('slope is not the same', () => { + expect(calculateJunctionPoint(lineTest4, lineTest3)).toEqual({"x": 0, "y": 2}); + }); +}) + +describe('IS_POINT_ON_LINE', () => { + + it('Slope and n are not the same on both lines', () => { + expect(isPointOnLine(lineTest4, pointTest4)).toBe(false); + }); + + it('Slope is the same and n is not the same on both lines', () => { + expect(isPointOnLine(lineTest1, pointTest5)).toBe(false); + }); + + it('Slope and n are not the same on both lines', () => { + expect(isPointOnLine(lineTest5, pointTest4)).toBe(true); + }); + +}) + +describe('ERRORS', () => { + + describe('CALCULATE_DISTANCE_ERRORS', () => { + + it('Checks if the function received points', () => { + + expect(() => calculateDistance()).toThrow("Expect to get two points") + }) + it('Checks if the function is missing a point', () => { + + expect(() => calculateDistance(pointTest1)).toThrow("Expect to get two points") + }) + + }) + + describe('CALCULATE_JUNCTION_POINT_ERRORS', () => { + + it('Checks if the function received lines', () => { + + expect(() => calculateJunctionPoint()).toThrow("Expect to get two lines") + }) + it('Checks if the function is missing a line', () => { + + expect(() => calculateJunctionPoint(lineTest1)).toThrow("Expect to get two lines") + }) + it('Checks if the line has no slope', () => { + + expect(() => calculateJunctionPoint(lineTest1, lineTest6)).toThrow("Expect to get line with slope") + }) + it('Checks if the line has no slope', () => { + + expect(() => calculateJunctionPoint(lineTest6, lineTest1)).toThrow("Expect to get line with slope") + }) + + + }) + + describe('IS_POINT_ON_LINE_ERRORS', () => { + + it('Checks if the function received line and point', () => { + + expect(() => isPointOnLine()).toThrow("Expect to get line and point") + }) + it('Checks if the function is missing a line', () => { + + expect(() => isPointOnLine(lineTest1)).toThrow("Expect to get line and point") + }) + it('Checks if the function is missing a point', () => { + + expect(() => isPointOnLine({line:undefined,point:pointTest1})).toThrow("Expect to get line and point") + }) + + }) + +}) +