From 7b82283ec7390be7e1683101a67b72f95fa0760a Mon Sep 17 00:00:00 2001 From: GiliLev Date: Sun, 21 Jul 2024 11:22:44 +0300 Subject: [PATCH 1/4] add tests --- modules/ecs6-class/line.js | 27 ++++-- modules/ecs6-class/point.js | 9 ++ modules/geometry-calculation.js | 54 +++++++++-- package.json | 2 +- tests/modules/ecs6-class/line.test.js | 84 ++++++++++++++++ tests/modules/ecs6-class/point.test.js | 50 ++++++++++ tests/modules/geometry-calculation.test.js | 108 +++++++++++++++++++++ 7 files changed, 320 insertions(+), 14 deletions(-) create mode 100644 tests/modules/ecs6-class/line.test.js create mode 100644 tests/modules/ecs6-class/point.test.js create mode 100644 tests/modules/geometry-calculation.test.js diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index 826c675..fab35c6 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -1,7 +1,16 @@ 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(typeof(point1.x)!='number'||typeof(point2.x)!='number'){ + throw Error('The points should have 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; @@ -9,19 +18,25 @@ class Line { } calculateSlope = () => { - this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) + if(this.point1.x === this.point2.x){ + this.slope = 0; + } + else{ + this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) + } + } calculateNOfLineFunction = () => { this.n = this.point1.y - this.slope * this.point1.x } - getPointOnXAsis() { - return this.getPointByY(0) + getPointOnXAsis(func=this.getPointByY(0)) { + return func() } - getPointOnYAsis() { - return this.getPointByX(0) + getPointOnYAsis(func=this.getPointByX(0)) { + return func() } diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index e81b4a4..bf5eff5 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,12 +1,21 @@ class Point { constructor({x=0, y=0}={}) { + if(typeof(x)!='number'||typeof(y)!='number'){ + throw Error('The values should have 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; } } diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 6e11643..66471e6 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -1,13 +1,30 @@ -const Line = require('./ecs6-class/line') +const Line = require('./ecs6-class/line'); +const Point = require('./ecs6-class/point'); + + + const calculateDistance = (point1, point2) => { + if(typeof(point1)!='object'||typeof(point1)!='object'){ + throw Error('The value is of an invalid type') + } + if(!point1.x||!point2.x){ + throw Error('The value is of an invalid type') + } + 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) => { +const calculateJunctionPoint = (line1, line2,func) => { + if(typeof(line1)!='object'||typeof(line2)!='object'){ + throw Error('The value is of an invalid type') + } + if(!line1.slope||!line2.slope){ + throw Error('The value is of an invalid type') + } if (line1.slope === line2.slope) { if (line1.n === line2.n) { return true @@ -18,16 +35,39 @@ const calculateJunctionPoint = (line1, line2) => { } else { const x = (line1.n - line2.n) / (line2.slope - line1.slope) - const junctionPoint = line1.getPointByX(x); + if(!func){ + func=line1.getPointByX(x) + } + const junctionPoint = func; return junctionPoint } } -const isPointOnLine = (line, point) => { +const isPointOnLine = (line, point,func1,func2) => { + if(typeof(line)!='object'||typeof(point)!='object'){ + throw Error('The value is of an invalid type') + } + if(!line.slope||!point.x){ + throw Error('The value is of an invalid type') + } + const proxyLine = new Line({ point1: line.point1, point2: point }) - proxyLine.calculateSlope() + if(!func1){ + func1= proxyLine.calculateSlope() + + } + else{ + proxyLine.slope = func1; + } + if (line.slope === proxyLine.slope) { - proxyLine.calculateNOfLineFunction() + if(!func2){ + func2=proxyLine.calculateNOfLineFunction() + + } + else{ + proxyLine.n= func2; + } if (line.n === proxyLine.n) { return true } diff --git a/package.json b/package.json index 56bf17b..deb3314 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "practice unit tests in javascript", "main": "index.js", "scripts": { - "test": "jest" + "test": "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..c78dc2b --- /dev/null +++ b/tests/modules/ecs6-class/line.test.js @@ -0,0 +1,84 @@ +const Line = require('../../../modules/ecs6-class/line'); +const Point = require('../../../modules/ecs6-class/point'); +const mock = jest.fn().mockReturnValueOnce({x: 19,y: 0}).mockReturnValueOnce({x: 0,y: 9.5}) + + +describe('Check calculateSlope function', () => { + test(' 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) + }) + test(' 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: 5, y: 8 }); + let line1 = new Line({ point1, point2 }); + line1.calculateSlope(); + expect(line1.slope).toBe(0) + }) + test(' 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('Check calculateNOfLineFunction function', () => { + test('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) + }) +}) + +describe('Check getPointOnXAsis function', () => { + test(' Should return point on X asis', () => { + 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, slope: -0.5 }); + expect(line1.getPointOnXAsis(mock)).toEqual({ x: 19, y: 0 }) + }) +}) + +describe('Check getPointOnYAsis function', () => { + test(' Should return point on Y asis', () => { + 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, slope: -0.5 }); + expect(line1.getPointOnYAsis(mock)).toEqual({ x: 0, y: 9.5 }) + }) +}) + +describe('Check getPointByX function', () => { + test(' Should return point', () => { + let line1 = new Line({ slope: 2, n: 7 }); + expect(line1.getPointByX(6)).toEqual({ x: 6, y: 19 }) + }) +}) + +describe('Check getPointByY function', () => { + test('Should return point ', () => { + let line1 = new Line({ slope: 2, n: 7 }); + expect(line1.getPointByY(3)).toEqual({ x: -2, y: 3 }) + }) +}) + +describe('ERRORS', () => { + test('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(' should have Point type') + expect(() => line1= new Line({point1,point2:[1,2],n:4,slope:6})).toThrow(' should have 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') + + })}) + diff --git a/tests/modules/ecs6-class/point.test.js b/tests/modules/ecs6-class/point.test.js new file mode 100644 index 0000000..1b2ddb5 --- /dev/null +++ b/tests/modules/ecs6-class/point.test.js @@ -0,0 +1,50 @@ +const Point = require('../../../modules/ecs6-class/point'); + +describe('Check calculateSlope function', () => { + test('Should return the point after move vertical with the number he received', () => { + let point1 = new Point({ x: 5, y: 7 }); + point1.moveVertical(2) + expect(point1).toEqual({ x: 5, y: 9 }) + }) + + describe('ERRORS', () => { + test('should throw error when the type is not number', () => { + let point1 = new Point({ x: 5, y: 7 }); + expect(() => point1.moveVertical('lll')).toThrow('The value is of an invalid type') + expect(() => point1.moveVertical(point1)).toThrow('The value is of an invalid type') + expect(() => point1.moveVertical(false)).toThrow('The value is of an invalid type') + expect(() => point1.moveVertical([1, 2])).toThrow('The value is of an invalid type') + expect(() => point1.moveVertical()).toThrow('The value is of an invalid type') + }) + }) +}) + +describe('Check moveHorizontal function', () => { + test('Should return the point after move horizontal with the number he received', () => { + let point1 = new Point({ x: 5, y: 7 }); + point1.moveHorizontal(2) + expect(point1).toEqual({ x: 7, y: 7 }) + }) + + + describe('ERRORS', () => { + test('should throw error when the type is not number', () => { + let point1 = new Point({ x: 5, y: 7 }); + expect(() => point1.moveHorizontal('lll')).toThrow('The value is of an invalid type') + expect(() => point1.moveHorizontal(point1)).toThrow('The value is of an invalid type') + expect(() => point1.moveHorizontal(false)).toThrow('The value is of an invalid type') + expect(() => point1.moveHorizontal([1, 2])).toThrow('The value is of an invalid type') + expect(() => point1.moveHorizontal()).toThrow('The value is of an invalid type') + }) + }) +}) + + +describe('ERRORS', () => { + test('should throw error when the values of the point is not valid', () => { + let point1; + expect(() => point1= new Point({x:'l',y: 4})).toThrow('The values should have a number') + expect(() => point1= new Point({x:6,y:[8]})).toThrow('The values should have a number') + expect(() => point1= new Point({x:'u',y:'p'})).toThrow('The values should have a number') + expect(() => point1= new Point({x:false,y:true})).toThrow('The values should have a number') +})}) diff --git a/tests/modules/geometry-calculation.test.js b/tests/modules/geometry-calculation.test.js new file mode 100644 index 0000000..9c4931e --- /dev/null +++ b/tests/modules/geometry-calculation.test.js @@ -0,0 +1,108 @@ +const Line = require('../../modules/ecs6-class/line'); +const Point = require('../../modules/ecs6-class/point'); +const geometry = require('../../modules/geometry-calculation') +const mock = jest.fn().mockReturnValue({x: -7,y: -40}) +const mock2 = jest.fn().mockReturnValueOnce(3.5).mockReturnValueOnce(4).mockReturnValue(7) +const mock3 = jest.fn().mockReturnValueOnce(-10.5).mockReturnValueOnce(-4).mockReturnValue(9) + + +describe('Check calculateDistance function', () => { + test(' Should return the square root of the sum of the distances between x and y of the points', () => { + let point1 = new Point({ x: 5, y: 7 }); + let point2 = new Point({ x: 3, y: 8 }); + expect(geometry.calculateDistance(point1, point2)).toBe(2.23606797749979); + }) + describe('ERRORS', () => { + test('should throw error when the type is not Point', () => { + expect(() => geometry.calculateDistance(1, 2)).toThrow('The value is of an invalid type') + expect(() => geometry.calculateDistance('o', 'p')).toThrow('The value is of an invalid type') + expect(() => geometry.calculateDistance(false, true)).toThrow('The value is of an invalid type') + expect(() => geometry.calculateDistance([1, 2], [2, 3])).toThrow('The value is of an invalid type') + expect(() => geometry.calculateDistance()).toThrow('The value is of an invalid type') + + }) + }) +}) + + +describe('Check calculateJunctionPoint function', () => { + test('Should return calculate Junction Point', () => { + let point1 = new Point({ x: 5, y: 7 }); + let point2 = new Point({ x: 3, y: 8 }); + let point3 = new Point({ x: 7, y: 0 }); + let point4 = new Point({ x: 11, y: 8 }); + let line1 = new Line({ point1, point2, n: 9, slope: 7 }); + let line2 = new Line({ point3, point4, n: 2, slope: 6 }); + expect(geometry.calculateJunctionPoint(line1, line2,mock())).toEqual({ x: -7, y: -40 }); + }) + + test('Should return true if the lines are on the same axis', () => { + let point1 = new Point({ x: 5, y: 7 }); + let point2 = new Point({ x: 3, y: 8 }); + let point3 = new Point({ x: 7, y: 0 }); + let point4 = new Point({ x: 11, y: 8 }); + let line1 = new Line({ point1, point2, n: 9, slope: 7 }); + let line2 = new Line({ point3, point4, n: 9, slope: 7 }); + expect(geometry.calculateJunctionPoint(line1, line2,mock())).toBe(true); + }) + + test('Should return false if the lines are parallel', () => { + let point1 = new Point({ x: 5, y: 7 }); + let point2 = new Point({ x: 3, y: 8 }); + let point3 = new Point({ x: 7, y: 0 }); + let point4 = new Point({ x: 11, y: 8 }); + let line1 = new Line({ point1, point2, n: 9, slope: 7 }); + let line2 = new Line({ point3, point4, n: 2, slope: 7 }); + expect(geometry.calculateJunctionPoint(line1, line2,mock())).toBe(false); + }) + + describe('ERRORS', () => { + test('should throw error when the type is not line', () => { + expect(() => geometry.calculateJunctionPoint(1, 2,mock())).toThrow('The value is of an invalid type') + expect(() => geometry.calculateJunctionPoint('o', 'p',mock())).toThrow('The value is of an invalid type') + expect(() => geometry.calculateJunctionPoint(false, true,mock())).toThrow('The value is of an invalid type') + expect(() => geometry.calculateJunctionPoint([1, 2], [2, 3],mock())).toThrow('The value is of an invalid type') + expect(() => geometry.calculateJunctionPoint()).toThrow('The value is of an invalid type') + + }) +}) +}) + +describe('Check isPointOnLine function', () => { + test(' Should return false if the point is not on the line nor on the same slope', () => { + let point1 = new Point({ x: 5, y: 7 }); + let point2 = new Point({ x: 3, y: 8 }); + let point3 = new Point({ x: 7, y: 0 }); + let line1 = new Line({ point1, point2, n: 9, slope: 7 }); + expect(geometry.isPointOnLine(line1, point3,mock2(),mock3())).toBe(false); + }) + test(' Should return false if the point is on the same slope and not on the line', () => { + let point1 = new Point({ x: 5, y: 16 }); + let point2 = new Point({ x: 3, y: 8 }); + let point3 = new Point({ x: 3, y: 2 }); + let line1 = new Line({ point1, point2, n: 9, slope: 7 }); + expect(geometry.isPointOnLine(line1, point3,mock2(),mock3())).toBe(false); + }) + + test(' Should return true if the point on the same slope', () => { + let point1 = new Point({ x: 5, y: 44 }); + let point2 = new Point({ x: 3, y: 8 }); + let point3 = new Point({ x: 3, y: 30 }); + let line1 = new Line({ point1, point2, n: 9, slope: 7 }); + expect(geometry.isPointOnLine(line1, point3,mock2(),mock3())).toBe(true); + }) + + + describe('ERRORS', () => { + test('should throw error when the type is not valid', () => { + expect(() => geometry.isPointOnLine(1, 2,mock2(),mock3())).toThrow('The value is of an invalid type') + expect(() => geometry.isPointOnLine('o', 'p',mock2(),mock3())).toThrow('The value is of an invalid type') + expect(() => geometry.isPointOnLine(false, true,mock2(),mock3())).toThrow('The value is of an invalid type') + expect(() => geometry.isPointOnLine([1, 2], [2, 3],mock2(),mock3())).toThrow('The value is of an invalid type') + expect(() => geometry.isPointOnLine()).toThrow('The value is of an invalid type') + + }) +}) +}) + + From f84bdb903cf1eba8fc346b890e5041e44352900e Mon Sep 17 00:00:00 2001 From: GiliLev Date: Tue, 23 Jul 2024 13:17:44 +0300 Subject: [PATCH 2/4] Fix comments and add mock --- modules/ecs6-class/line.js | 42 +++++++----- modules/ecs6-class/point.js | 6 +- modules/geometry-calculation.js | 42 +++--------- package.json | 3 +- tests/modules/ecs6-class/line.test.js | 74 +++++++++++++++------- tests/modules/geometry-calculation.test.js | 61 +++++++++++------- 6 files changed, 129 insertions(+), 99 deletions(-) diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index fab35c6..e0eafda 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -1,14 +1,15 @@ const Point = require("./point"); + class Line { - constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }={}) { - if(typeof(point1.x)!='number'||typeof(point2.x)!='number'){ + constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined } = {}) { + if (!(point1 instanceof Point) || !(point2 instanceof Point)) { throw Error('The points should have Point type') } - if(typeof(n)!='number'&& typeof(n)!='undefined'){ + if (typeof (n) !== 'number' && typeof (n) !== 'undefined') { throw Error('The n should have a number') } - if(typeof(slope)!='number'&& typeof(slope)!='undefined'){ + if (typeof (slope) !== 'number' && typeof (slope) !== 'undefined') { throw Error('The slope should have a number') } this.point1 = point1; @@ -17,35 +18,46 @@ class Line { this.n = n; } - calculateSlope = () => { - if(this.point1.x === this.point2.x){ + calculateSlope() { + if (this.point1.x === this.point2.x) { this.slope = 0; } - else{ - this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) + else { + + return this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) + } - + } - calculateNOfLineFunction = () => { - this.n = this.point1.y - this.slope * this.point1.x + calculateNOfLineFunction() { + + return this.n = this.point1.y - this.slope * this.point1.x + + } - getPointOnXAsis(func=this.getPointByY(0)) { - return func() + getPointOnXAsis() { + return this.getPointByY(0) } - getPointOnYAsis(func=this.getPointByX(0)) { - return func() + getPointOnYAsis() { + return this.getPointByX(0) } getPointByX(x) { + if (typeof (x) !== 'number') { + throw Error('The value is of an invalid type') + } let y = this.slope * x + this.n return new Point({ x, y }) } getPointByY(y) { + if (typeof (y) !== 'number') { + throw Error('The value is of an invalid type') + } 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 bf5eff5..e24e591 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,19 +1,19 @@ class Point { constructor({x=0, y=0}={}) { - if(typeof(x)!='number'||typeof(y)!='number'){ + if(typeof(x)!=='number'||typeof(y)!=='number'){ throw Error('The values should have a number') } this.x = x; this.y = y; } moveVertical(value) { - if(typeof(value)!= 'number'){ + if(typeof(value)!== 'number'){ throw Error('The value is of an invalid type') } this.y += value; } moveHorizontal(value) { - if(typeof(value)!= 'number'){ + if(typeof(value)!== 'number'){ throw Error('The value is of an invalid type') } this.x += value; diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 66471e6..0c0c856 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -5,24 +5,17 @@ const Point = require('./ecs6-class/point'); const calculateDistance = (point1, point2) => { - if(typeof(point1)!='object'||typeof(point1)!='object'){ + if (!(point1 instanceof Point) || !(point1 instanceof Point)) { throw Error('The value is of an invalid type') } - if(!point1.x||!point2.x){ - throw Error('The value is of an invalid type') - } - let distanceX = (point2.x - point1.x) ** 2; let distanceY = (point2.y - point1.y) ** 2; const distance = Math.sqrt(distanceX + distanceY); return distance; } -const calculateJunctionPoint = (line1, line2,func) => { - if(typeof(line1)!='object'||typeof(line2)!='object'){ - throw Error('The value is of an invalid type') - } - if(!line1.slope||!line2.slope){ +const calculateJunctionPoint = (line1, line2) => { + if (!(line1 instanceof Line) || !(line2 instanceof Line)) { throw Error('The value is of an invalid type') } if (line1.slope === line2.slope) { @@ -35,39 +28,20 @@ const calculateJunctionPoint = (line1, line2,func) => { } else { const x = (line1.n - line2.n) / (line2.slope - line1.slope) - if(!func){ - func=line1.getPointByX(x) - } - const junctionPoint = func; + const junctionPoint=line1.getPointByX(x); return junctionPoint } } -const isPointOnLine = (line, point,func1,func2) => { - if(typeof(line)!='object'||typeof(point)!='object'){ - throw Error('The value is of an invalid type') - } - if(!line.slope||!point.x){ +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 }) - if(!func1){ - func1= proxyLine.calculateSlope() - - } - else{ - proxyLine.slope = func1; - } - + proxyLine.slope=proxyLine.calculateSlope() if (line.slope === proxyLine.slope) { - if(!func2){ - func2=proxyLine.calculateNOfLineFunction() - - } - else{ - proxyLine.n= func2; - } + proxyLine.n=proxyLine.calculateNOfLineFunction() if (line.n === proxyLine.n) { return true } diff --git a/package.json b/package.json index deb3314..05a4561 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "practice unit tests in javascript", "main": "index.js", "scripts": { - "test": "jest --coverage" + "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 index c78dc2b..f31e80d 100644 --- a/tests/modules/ecs6-class/line.test.js +++ b/tests/modules/ecs6-class/line.test.js @@ -1,6 +1,12 @@ const Line = require('../../../modules/ecs6-class/line'); const Point = require('../../../modules/ecs6-class/point'); -const mock = jest.fn().mockReturnValueOnce({x: 19,y: 0}).mockReturnValueOnce({x: 0,y: 9.5}) + +const getPointByYMock = jest + .spyOn(Line.prototype, 'getPointByY') + +const getPointByXMock = jest + .spyOn(Line.prototype, 'getPointByX') + describe('Check calculateSlope function', () => { @@ -39,28 +45,39 @@ describe('Check calculateNOfLineFunction function', () => { }) describe('Check getPointOnXAsis function', () => { - test(' Should return point on X asis', () => { - 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, slope: -0.5 }); - expect(line1.getPointOnXAsis(mock)).toEqual({ x: 19, y: 0 }) + test('It should be returned if in the call to the function the function getPointByY was called', () => { + const line1 = new Line(); + line1.getPointOnXAsis(); + expect(getPointByYMock).toHaveBeenCalled(); + }); }) -}) -describe('Check getPointOnYAsis function', () => { - test(' Should return point on Y asis', () => { - 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, slope: -0.5 }); - expect(line1.getPointOnYAsis(mock)).toEqual({ x: 0, y: 9.5 }) + describe('Check getPointOnXYAsis function', () => { + test('It should be returned if in the call to the function the function getPointByX was called', () => { + const line1 = new Line(); + line1.getPointOnYAsis(); + expect(getPointByXMock).toHaveBeenCalled(); + }); }) -}) + describe('Check getPointByX function', () => { test(' Should return point', () => { let line1 = new Line({ slope: 2, n: 7 }); expect(line1.getPointByX(6)).toEqual({ x: 6, y: 19 }) }) + describe('ERRORS', () => { + test('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', () => { @@ -68,17 +85,30 @@ describe('Check getPointByY function', () => { let line1 = new Line({ slope: 2, n: 7 }); expect(line1.getPointByY(3)).toEqual({ x: -2, y: 3 }) }) + describe('ERRORS', () => { + test('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") + }) + }) }) describe('ERRORS', () => { test('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 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(' should have Point type') - expect(() => line1= new Line({point1,point2:[1,2],n:4,slope:6})).toThrow(' should have 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') - - })}) + expect(() => line1 = new Line({ point1: 9, point2, n: 4, slope: 6 })).toThrow(' should have Point type') + expect(() => line1 = new Line({ point1, point2: [1, 2], n: 4, slope: 6 })).toThrow(' should have 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') + + }) +}) diff --git a/tests/modules/geometry-calculation.test.js b/tests/modules/geometry-calculation.test.js index 9c4931e..1d3290d 100644 --- a/tests/modules/geometry-calculation.test.js +++ b/tests/modules/geometry-calculation.test.js @@ -1,9 +1,23 @@ const Line = require('../../modules/ecs6-class/line'); const Point = require('../../modules/ecs6-class/point'); const geometry = require('../../modules/geometry-calculation') -const mock = jest.fn().mockReturnValue({x: -7,y: -40}) -const mock2 = jest.fn().mockReturnValueOnce(3.5).mockReturnValueOnce(4).mockReturnValue(7) -const mock3 = jest.fn().mockReturnValueOnce(-10.5).mockReturnValueOnce(-4).mockReturnValue(9) + + +const getPointByXMock = jest + .spyOn(Line.prototype, 'getPointByX').mockImplementation() + .mockReturnValue({ x: -7, y: -40 }) + +const calculateSlopeMock = jest + .spyOn(Line.prototype, 'calculateSlope').mockImplementation() + .mockReturnValueOnce(0).mockReturnValueOnce(7).mockReturnValue(7) + + +const calculateNOfLineFunctionMock = jest + .spyOn(Line.prototype, 'calculateNOfLineFunction').mockImplementation() + .mockReturnValue(9) + + + describe('Check calculateDistance function', () => { @@ -33,7 +47,8 @@ describe('Check calculateJunctionPoint function', () => { let point4 = new Point({ x: 11, y: 8 }); let line1 = new Line({ point1, point2, n: 9, slope: 7 }); let line2 = new Line({ point3, point4, n: 2, slope: 6 }); - expect(geometry.calculateJunctionPoint(line1, line2,mock())).toEqual({ x: -7, y: -40 }); + expect(geometry.calculateJunctionPoint(line1, line2)).toEqual({ x: -7, y: -40 }); + }) test('Should return true if the lines are on the same axis', () => { @@ -43,7 +58,7 @@ describe('Check calculateJunctionPoint function', () => { let point4 = new Point({ x: 11, y: 8 }); let line1 = new Line({ point1, point2, n: 9, slope: 7 }); let line2 = new Line({ point3, point4, n: 9, slope: 7 }); - expect(geometry.calculateJunctionPoint(line1, line2,mock())).toBe(true); + expect(geometry.calculateJunctionPoint(line1, line2)).toBe(true); }) test('Should return false if the lines are parallel', () => { @@ -53,35 +68,33 @@ describe('Check calculateJunctionPoint function', () => { let point4 = new Point({ x: 11, y: 8 }); let line1 = new Line({ point1, point2, n: 9, slope: 7 }); let line2 = new Line({ point3, point4, n: 2, slope: 7 }); - expect(geometry.calculateJunctionPoint(line1, line2,mock())).toBe(false); + expect(geometry.calculateJunctionPoint(line1, line2)).toBe(false); }) describe('ERRORS', () => { test('should throw error when the type is not line', () => { - expect(() => geometry.calculateJunctionPoint(1, 2,mock())).toThrow('The value is of an invalid type') - expect(() => geometry.calculateJunctionPoint('o', 'p',mock())).toThrow('The value is of an invalid type') - expect(() => geometry.calculateJunctionPoint(false, true,mock())).toThrow('The value is of an invalid type') - expect(() => geometry.calculateJunctionPoint([1, 2], [2, 3],mock())).toThrow('The value is of an invalid type') + expect(() => geometry.calculateJunctionPoint(1, 2)).toThrow('The value is of an invalid type') + expect(() => geometry.calculateJunctionPoint('o', 'p')).toThrow('The value is of an invalid type') + expect(() => geometry.calculateJunctionPoint(false, true)).toThrow('The value is of an invalid type') + expect(() => geometry.calculateJunctionPoint([1, 2], [2, 3],)).toThrow('The value is of an invalid type') expect(() => geometry.calculateJunctionPoint()).toThrow('The value is of an invalid type') }) -}) + }) }) describe('Check isPointOnLine function', () => { test(' Should return false if the point is not on the line nor on the same slope', () => { - let point1 = new Point({ x: 5, y: 7 }); - let point2 = new Point({ x: 3, y: 8 }); let point3 = new Point({ x: 7, y: 0 }); - let line1 = new Line({ point1, point2, n: 9, slope: 7 }); - expect(geometry.isPointOnLine(line1, point3,mock2(),mock3())).toBe(false); + let line1 = new Line(); + expect(geometry.isPointOnLine(line1, point3)).toBe(false); }) test(' Should return false if the point is on the same slope and not on the line', () => { let point1 = new Point({ x: 5, y: 16 }); let point2 = new Point({ x: 3, y: 8 }); let point3 = new Point({ x: 3, y: 2 }); - let line1 = new Line({ point1, point2, n: 9, slope: 7 }); - expect(geometry.isPointOnLine(line1, point3,mock2(),mock3())).toBe(false); + let line1 = new Line({ point1, point2, n: 11, slope: 7 }); + expect(geometry.isPointOnLine(line1, point3)).toBe(false); }) test(' Should return true if the point on the same slope', () => { @@ -89,20 +102,20 @@ describe('Check isPointOnLine function', () => { let point2 = new Point({ x: 3, y: 8 }); let point3 = new Point({ x: 3, y: 30 }); let line1 = new Line({ point1, point2, n: 9, slope: 7 }); - expect(geometry.isPointOnLine(line1, point3,mock2(),mock3())).toBe(true); + expect(geometry.isPointOnLine(line1, point3)).toBe(true); }) - + describe('ERRORS', () => { test('should throw error when the type is not valid', () => { - expect(() => geometry.isPointOnLine(1, 2,mock2(),mock3())).toThrow('The value is of an invalid type') - expect(() => geometry.isPointOnLine('o', 'p',mock2(),mock3())).toThrow('The value is of an invalid type') - expect(() => geometry.isPointOnLine(false, true,mock2(),mock3())).toThrow('The value is of an invalid type') - expect(() => geometry.isPointOnLine([1, 2], [2, 3],mock2(),mock3())).toThrow('The value is of an invalid type') + expect(() => geometry.isPointOnLine(1, 2)).toThrow('The value is of an invalid type') + expect(() => geometry.isPointOnLine('o', 'p')).toThrow('The value is of an invalid type') + expect(() => geometry.isPointOnLine(false, true,)).toThrow('The value is of an invalid type') + expect(() => geometry.isPointOnLine([1, 2], [2, 3])).toThrow('The value is of an invalid type') expect(() => geometry.isPointOnLine()).toThrow('The value is of an invalid type') }) -}) + }) }) From df6d004ad53c8619d22b8aeecba023590716e4ff Mon Sep 17 00:00:00 2001 From: GiliLev Date: Wed, 24 Jul 2024 15:29:51 +0300 Subject: [PATCH 3/4] Fix comments other than mock --- modules/ecs6-class/line.js | 22 +++- modules/ecs6-class/point.js | 7 +- modules/geometry-calculation.js | 14 ++- package.json | 2 +- tests/modules/ecs6-class/line.test.js | 138 ++++++++++++++++----- tests/modules/ecs6-class/point.test.js | 18 +-- tests/modules/geometry-calculation.test.js | 41 ++++-- 7 files changed, 178 insertions(+), 64 deletions(-) diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index e0eafda..63044f0 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -3,8 +3,11 @@ const Point = require("./point"); class Line { constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined } = {}) { - if (!(point1 instanceof Point) || !(point2 instanceof Point)) { - throw Error('The points should have Point type') + 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') @@ -20,18 +23,18 @@ class Line { calculateSlope() { if (this.point1.x === this.point2.x) { - this.slope = 0; + throw Error("it isn't a real geometry line") } else { - + return this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) - + } } calculateNOfLineFunction() { - + this.calculateSlope() return this.n = this.point1.y - this.slope * this.point1.x @@ -50,6 +53,8 @@ class Line { if (typeof (x) !== 'number') { throw Error('The value is of an invalid type') } + this.calculateSlope() + this.calculateNOfLineFunction() let y = this.slope * x + this.n return new Point({ x, y }) } @@ -58,6 +63,11 @@ class Line { if (typeof (y) !== 'number') { throw Error('The value is of an invalid type') } + this.calculateSlope() + this.calculateNOfLineFunction() + if(this.slope===0){ + throw Error('The slope cannot be 0') + } 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 e24e591..c7e4940 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,7 +1,10 @@ class Point { constructor({x=0, y=0}={}) { - if(typeof(x)!=='number'||typeof(y)!=='number'){ - throw Error('The values should have a number') + 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; diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 0c0c856..9f944fa 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -15,9 +15,19 @@ const calculateDistance = (point1, point2) => { } const calculateJunctionPoint = (line1, line2) => { - if (!(line1 instanceof Line) || !(line2 instanceof Line)) { - throw Error('The value is of an invalid type') + 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') } + line1.calculateSlope() + line2.calculateSlope() + line1.calculateNOfLineFunction() + line2.calculateNOfLineFunction() if (line1.slope === line2.slope) { if (line1.n === line2.n) { return true diff --git a/package.json b/package.json index 05a4561..d43de7c 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "test": "jest", - "test:coverage": "jest --coverage" + "test:coverage": "npm run test -- --coverage" }, "dependencies": { "jest": "^29.7.0" diff --git a/tests/modules/ecs6-class/line.test.js b/tests/modules/ecs6-class/line.test.js index f31e80d..a32d80e 100644 --- a/tests/modules/ecs6-class/line.test.js +++ b/tests/modules/ecs6-class/line.test.js @@ -10,64 +10,104 @@ const getPointByXMock = jest describe('Check calculateSlope function', () => { - test(' Should return the slope of the line', () => { + 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) }) - test(' 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: 5, y: 8 }); - let line1 = new Line({ point1, point2 }); - line1.calculateSlope(); - expect(line1.slope).toBe(0) - }) - test(' Should return 0 if the points are in the same position on the x-axis', () => { + 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', () => { - test('Should return n of the line', () => { + 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', () => { - test('It should be returned if in the call to the function the function getPointByY was called', () => { - const line1 = new Line(); - line1.getPointOnXAsis(); - expect(getPointByYMock).toHaveBeenCalled(); - }); - }) + 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 getPointOnXYAsis function', () => { - test('It should be returned if in the call to the function the function getPointByX was called', () => { - const line1 = new Line(); - line1.getPointOnYAsis(); - expect(getPointByXMock).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', () => { - test(' Should return point', () => { - let line1 = new Line({ slope: 2, n: 7 }); - expect(line1.getPointByX(6)).toEqual({ x: 6, y: 19 }) + 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: 6.5 }) + + }) + 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: 6.5 }) + 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', () => { - test('should throw error when the type is not valid', () => { + 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 }); @@ -81,12 +121,36 @@ describe('Check getPointByX function', () => { }) describe('Check getPointByY function', () => { - test('Should return point ', () => { - let line1 = new Line({ slope: 2, n: 7 }); - expect(line1.getPointByY(3)).toEqual({ x: -2, y: 3 }) + 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: 7, 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', () => { - test('should throw error when the type is not valid', () => { + 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 }); @@ -96,16 +160,22 @@ describe('Check getPointByY function', () => { 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', () => { - test('should throw error when the values of the line is not valid', () => { + 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(' should have Point type') - expect(() => line1 = new Line({ point1, point2: [1, 2], n: 4, slope: 6 })).toThrow(' should have Point type') + 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') diff --git a/tests/modules/ecs6-class/point.test.js b/tests/modules/ecs6-class/point.test.js index 1b2ddb5..2bb990e 100644 --- a/tests/modules/ecs6-class/point.test.js +++ b/tests/modules/ecs6-class/point.test.js @@ -1,14 +1,14 @@ const Point = require('../../../modules/ecs6-class/point'); describe('Check calculateSlope function', () => { - test('Should return the point after move vertical with the number he received', () => { + it('Should return the point after move vertical with the number he received', () => { let point1 = new Point({ x: 5, y: 7 }); point1.moveVertical(2) expect(point1).toEqual({ x: 5, y: 9 }) }) describe('ERRORS', () => { - test('should throw error when the type is not number', () => { + it('should throw error when the type is not number', () => { let point1 = new Point({ x: 5, y: 7 }); expect(() => point1.moveVertical('lll')).toThrow('The value is of an invalid type') expect(() => point1.moveVertical(point1)).toThrow('The value is of an invalid type') @@ -20,7 +20,7 @@ describe('Check calculateSlope function', () => { }) describe('Check moveHorizontal function', () => { - test('Should return the point after move horizontal with the number he received', () => { + it('Should return the point after move horizontal with the number he received', () => { let point1 = new Point({ x: 5, y: 7 }); point1.moveHorizontal(2) expect(point1).toEqual({ x: 7, y: 7 }) @@ -28,7 +28,7 @@ describe('Check moveHorizontal function', () => { describe('ERRORS', () => { - test('should throw error when the type is not number', () => { + it('should throw error when the type is not number', () => { let point1 = new Point({ x: 5, y: 7 }); expect(() => point1.moveHorizontal('lll')).toThrow('The value is of an invalid type') expect(() => point1.moveHorizontal(point1)).toThrow('The value is of an invalid type') @@ -41,10 +41,10 @@ describe('Check moveHorizontal function', () => { describe('ERRORS', () => { - test('should throw error when the values of the point is not valid', () => { + it('should throw error when the values of the point is not valid', () => { let point1; - expect(() => point1= new Point({x:'l',y: 4})).toThrow('The values should have a number') - expect(() => point1= new Point({x:6,y:[8]})).toThrow('The values should have a number') - expect(() => point1= new Point({x:'u',y:'p'})).toThrow('The values should have a number') - expect(() => point1= new Point({x:false,y:true})).toThrow('The values should have a number') + expect(() => point1= new Point({x:'l',y: 4})).toThrow('The first value should be a number') + expect(() => point1= new Point({x:6,y:[8]})).toThrow('The second value should be a number') + expect(() => point1= new Point({x:'u',y:'p'})).toThrow('The first value should be a number') + expect(() => point1= new Point({x:false,y:true})).toThrow('The first value should be a number') })}) diff --git a/tests/modules/geometry-calculation.test.js b/tests/modules/geometry-calculation.test.js index 1d3290d..962a8bf 100644 --- a/tests/modules/geometry-calculation.test.js +++ b/tests/modules/geometry-calculation.test.js @@ -18,16 +18,14 @@ const calculateNOfLineFunctionMock = jest - - describe('Check calculateDistance function', () => { - test(' Should return the square root of the sum of the distances between x and y of the points', () => { + it(' Should return the square root of the sum of the distances between x and y of the points', () => { let point1 = new Point({ x: 5, y: 7 }); let point2 = new Point({ x: 3, y: 8 }); expect(geometry.calculateDistance(point1, point2)).toBe(2.23606797749979); }) describe('ERRORS', () => { - test('should throw error when the type is not Point', () => { + it('should throw error when the type is not Point', () => { expect(() => geometry.calculateDistance(1, 2)).toThrow('The value is of an invalid type') expect(() => geometry.calculateDistance('o', 'p')).toThrow('The value is of an invalid type') expect(() => geometry.calculateDistance(false, true)).toThrow('The value is of an invalid type') @@ -72,12 +70,35 @@ describe('Check calculateJunctionPoint function', () => { }) describe('ERRORS', () => { - test('should throw error when the type is not line', () => { - expect(() => geometry.calculateJunctionPoint(1, 2)).toThrow('The value is of an invalid type') - expect(() => geometry.calculateJunctionPoint('o', 'p')).toThrow('The value is of an invalid type') - expect(() => geometry.calculateJunctionPoint(false, true)).toThrow('The value is of an invalid type') - expect(() => geometry.calculateJunctionPoint([1, 2], [2, 3],)).toThrow('The value is of an invalid type') - expect(() => geometry.calculateJunctionPoint()).toThrow('The value is of an invalid type') + test('should throw error when the values are not line', () => { + let point1 = new Point({ x: 5, y: 7 }); + let point2 = new Point({ x: 3, y: 8 }); + let line1 = new Line({ point1, point2, n: 9, slope: 7 }); + expect(() => geometry.calculateJunctionPoint(1, 2)).toThrow('The values are of an invalid type') + expect(() => geometry.calculateJunctionPoint('o', 'p')).toThrow('The values are of an invalid type') + expect(() => geometry.calculateJunctionPoint(false, true)).toThrow('The values are of an invalid type') + expect(() => geometry.calculateJunctionPoint([1, 2], [5, 8])).toThrow('The values are of an invalid type') + expect(() => geometry.calculateJunctionPoint()).toThrow('The values are of an invalid type') + }) + + test('should throw error when the first value is not line', () => { + let point1 = new Point({ x: 5, y: 7 }); + let point2 = new Point({ x: 3, y: 8 }); + let line1 = new Line({ point1, point2, n: 9, slope: 7 }); + expect(() => geometry.calculateJunctionPoint(1, line1)).toThrow('The first value is of an invalid type') + expect(() => geometry.calculateJunctionPoint('o', line1)).toThrow('The first value is of an invalid type') + expect(() => geometry.calculateJunctionPoint(false, line1)).toThrow('The first value is of an invalid type') + expect(() => geometry.calculateJunctionPoint([1, 2], line1)).toThrow('The first value is of an invalid type') + }) + + test('should throw error when the second value is not line', () => { + let point1 = new Point({ x: 5, y: 7 }); + let point2 = new Point({ x: 3, y: 8 }); + let line1 = new Line({ point1, point2, n: 9, slope: 7 }); + expect(() => geometry.calculateJunctionPoint(line1, 1)).toThrow('The second value is of an invalid type') + expect(() => geometry.calculateJunctionPoint(line1, 'o')).toThrow('The second value is of an invalid type') + expect(() => geometry.calculateJunctionPoint(line1, false)).toThrow('The second value is of an invalid type') + expect(() => geometry.calculateJunctionPoint(line1, [1, 2])).toThrow('The second value is of an invalid type') }) }) From 4b3294ff41d4bbd9492ff02adca056c6e4e2cd2e Mon Sep 17 00:00:00 2001 From: GLev Date: Mon, 29 Jul 2024 09:27:07 +0300 Subject: [PATCH 4/4] fix --- modules/ecs6-class/line.js | 28 +++++++--- modules/geometry-calculation.js | 24 +++++--- tests/modules/ecs6-class/line.test.js | 6 +- tests/modules/geometry-calculation.test.js | 64 ++++++++++++++++++---- 4 files changed, 92 insertions(+), 30 deletions(-) diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index 63044f0..64025bd 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -26,16 +26,17 @@ class Line { throw Error("it isn't a real geometry line") } else { - - return this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) - + this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) } } calculateNOfLineFunction() { - this.calculateSlope() - return this.n = this.point1.y - this.slope * this.point1.x + if (!this.slope) { + this.calculateSlope() + + } + this.n = this.point1.y - this.slope * this.point1.x } @@ -53,8 +54,14 @@ class Line { 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 }) } @@ -63,9 +70,14 @@ class Line { if (typeof (y) !== 'number') { throw Error('The value is of an invalid type') } - this.calculateSlope() - this.calculateNOfLineFunction() - if(this.slope===0){ + 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; diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 9f944fa..0a82f72 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -24,10 +24,20 @@ const calculateJunctionPoint = (line1, line2) => { if (!(line2 instanceof Line)) { throw Error('The second value is of an invalid type') } - line1.calculateSlope() - line2.calculateSlope() - line1.calculateNOfLineFunction() - line2.calculateNOfLineFunction() + 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 @@ -38,7 +48,7 @@ const calculateJunctionPoint = (line1, line2) => { } else { const x = (line1.n - line2.n) / (line2.slope - line1.slope) - const junctionPoint=line1.getPointByX(x); + const junctionPoint = line1.getPointByX(x); return junctionPoint } } @@ -49,9 +59,9 @@ const isPointOnLine = (line, point) => { } const proxyLine = new Line({ point1: line.point1, point2: point }) - proxyLine.slope=proxyLine.calculateSlope() + proxyLine.calculateSlope() if (line.slope === proxyLine.slope) { - proxyLine.n=proxyLine.calculateNOfLineFunction() + proxyLine.calculateNOfLineFunction() if (line.n === proxyLine.n) { return true } diff --git a/tests/modules/ecs6-class/line.test.js b/tests/modules/ecs6-class/line.test.js index a32d80e..0739c59 100644 --- a/tests/modules/ecs6-class/line.test.js +++ b/tests/modules/ecs6-class/line.test.js @@ -81,14 +81,14 @@ describe('Check getPointByX function', () => { 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: 6.5 }) + 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: 6.5 }) + 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', () => { @@ -125,7 +125,7 @@ describe('Check getPointByY function', () => { 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: 7, y: 6 }) + 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 }); diff --git a/tests/modules/geometry-calculation.test.js b/tests/modules/geometry-calculation.test.js index 962a8bf..02ab4ac 100644 --- a/tests/modules/geometry-calculation.test.js +++ b/tests/modules/geometry-calculation.test.js @@ -3,18 +3,18 @@ const Point = require('../../modules/ecs6-class/point'); const geometry = require('../../modules/geometry-calculation') -const getPointByXMock = jest - .spyOn(Line.prototype, 'getPointByX').mockImplementation() - .mockReturnValue({ x: -7, y: -40 }) +// const getPointByXMock = jest +// .spyOn(Line.prototype, 'getPointByX').mockImplementation() +// .mockReturnValue({ x: -7, y: -40 }) -const calculateSlopeMock = jest - .spyOn(Line.prototype, 'calculateSlope').mockImplementation() - .mockReturnValueOnce(0).mockReturnValueOnce(7).mockReturnValue(7) +// const calculateSlopeMock = jest +// .spyOn(Line.prototype, 'calculateSlope').mockImplementation() +// .mockReturnValueOnce(0).mockReturnValueOnce(7).mockReturnValue(7) -const calculateNOfLineFunctionMock = jest - .spyOn(Line.prototype, 'calculateNOfLineFunction').mockImplementation() - .mockReturnValue(9) +// const calculateNOfLineFunctionMock = jest +// .spyOn(Line.prototype, 'calculateNOfLineFunction').mockImplementation() +// .mockReturnValue(9) @@ -44,7 +44,7 @@ describe('Check calculateJunctionPoint function', () => { let point3 = new Point({ x: 7, y: 0 }); let point4 = new Point({ x: 11, y: 8 }); let line1 = new Line({ point1, point2, n: 9, slope: 7 }); - let line2 = new Line({ point3, point4, n: 2, slope: 6 }); + let line2 = new Line({ point1:point3, point2:point4, n: 2, slope: 6 }); expect(geometry.calculateJunctionPoint(line1, line2)).toEqual({ x: -7, y: -40 }); }) @@ -55,7 +55,7 @@ describe('Check calculateJunctionPoint function', () => { let point3 = new Point({ x: 7, y: 0 }); let point4 = new Point({ x: 11, y: 8 }); let line1 = new Line({ point1, point2, n: 9, slope: 7 }); - let line2 = new Line({ point3, point4, n: 9, slope: 7 }); + let line2 = new Line({point1: point3, point2:point4, n: 9, slope: 7 }); expect(geometry.calculateJunctionPoint(line1, line2)).toBe(true); }) @@ -65,9 +65,49 @@ describe('Check calculateJunctionPoint function', () => { let point3 = new Point({ x: 7, y: 0 }); let point4 = new Point({ x: 11, y: 8 }); let line1 = new Line({ point1, point2, n: 9, slope: 7 }); - let line2 = new Line({ point3, point4, n: 2, slope: 7 }); + let line2 = new Line({ point1: point3, point2: point4, n: 2, slope: 7 }); expect(geometry.calculateJunctionPoint(line1, line2)).toBe(false); }) + test('Should return answer and computer the slpoe of line1 if it is not sent', () => { + let point1 = new Point({ x: 5, y: 7 }); + let point2 = new Point({ x: 3, y: 8 }); + let point3 = new Point({ x: 7, y: 0 }); + let point4 = new Point({ x: 11, y: 8 }); + let line1 = new Line({ point1, point2, n: 9 }); + let line2 = new Line({ point1: point3, point2: point4, n: 2, slope: -0.5 }); + expect(geometry.calculateJunctionPoint(line1, line2)).toBe(false); + }) + + test('Should return answer and computer the slpoe of line2 if it is not sent', () => { + let point1 = new Point({ x: 5, y: 7 }); + let point2 = new Point({ x: 3, y: 8 }); + let point3 = new Point({ x: 7, y: 0 }); + let point4 = new Point({ x: 11, y: 8 }); + let line1 = new Line({ point1, point2, n: 9, slope: 2 }); + let line2 = new Line({ point1: point3, point2: point4, n: 2 }); + expect(geometry.calculateJunctionPoint(line1, line2)).toBe(false); + }) + + test('Should return answer and computer the n of line1 if it is not sent', () => { + let point1 = new Point({ x: 5, y: 7 }); + let point2 = new Point({ x: 3, y: 8 }); + let point3 = new Point({ x: 7, y: 0 }); + let point4 = new Point({ x: 11, y: 8 }); + let line1 = new Line({ point1, point2, slope: 7 }); + let line2 = new Line({ point1: point3, point2: point4, n: 2, slope: 7 }); + expect(geometry.calculateJunctionPoint(line1, line2)).toBe(false); + }) + + test('Should return answer and computer the n of line2 if it is not sent', () => { + let point1 = new Point({ x: 5, y: 7 }); + let point2 = new Point({ x: 3, y: 8 }); + let point3 = new Point({ x: 7, y: 0 }); + let point4 = new Point({ x: 11, y: 8 }); + let line1 = new Line({ point1, point2, n: 9, slope: 7 }); + let line2 = new Line({ point1: point3, point2: point4, slope: 7 }); + expect(geometry.calculateJunctionPoint(line1, line2)).toBe(false); + }) + describe('ERRORS', () => { test('should throw error when the values are not line', () => {