From df095f0ed50d59f648e461c4bb9119744740f184 Mon Sep 17 00:00:00 2001 From: B-Rosenbaum Date: Tue, 23 Jul 2024 11:11:12 +0300 Subject: [PATCH 1/5] build tests --- modules/ecs6-class/line.js | 24 +++++ modules/ecs6-class/point.js | 18 +++- modules/geometry-calculation.js | 23 ++++- package.json | 3 +- test/modules/ecs6-class/line.test.js | 97 ++++++++++++++++++ test/modules/ecs6-class/point.test.js | 73 ++++++++++++++ test/modules/geometry-calculation.test.js | 116 ++++++++++++++++++++++ 7 files changed, 350 insertions(+), 4 deletions(-) create mode 100644 test/modules/ecs6-class/line.test.js create mode 100644 test/modules/ecs6-class/point.test.js create mode 100644 test/modules/geometry-calculation.test.js diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index 826c675..8204f7a 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -2,6 +2,16 @@ const Point = require("./point"); class Line { constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) { + if (typeof(slope) !== "undefined" && typeof(slope) !== "number") { + throw new Error("slope is not valid!") + } + + if (typeof(n) !== "undefined" && typeof(n) !== "number") { + throw new Error("n is not valid!") + } + if ((!(point1 instanceof Point)) || (!(point2 instanceof Point))) { + throw new Error("the object not instance of 'Point'!") + } this.point1 = point1; this.point2 = point2; this.slope = slope; @@ -26,11 +36,25 @@ class Line { getPointByX(x) { + if (x === undefined) { + throw new Error('x is undefined!') + } + if (typeof (x) !== "number") { + throw new Error("x is not a number!") + } + let y = this.slope * x + this.n return new Point({ x, y }) } getPointByY(y) { + if (y === undefined) { + throw new Error('y is undefined!') + } + if (typeof (y) !== "number") { + throw new Error("y is not a number!") + } + 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..597ad68 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,12 +1,28 @@ class Point { - constructor({x=0, y=0}={}) { + constructor({ x = 0, y = 0 } = {}) { + if(typeof(x)!=="number" || typeof(y)!=="number" ){ + throw new Error('argument is not a number!') + } this.x = x; this.y = y; + } moveVertical(value) { + if (value === undefined) { + throw new Error('the value is undefined!') + } + if (typeof (value) != "number") { + throw new Error("the value is not a number!") + } this.y += value; } moveHorizontal(value) { + if (value === undefined) { + throw new Error('the value is undefined!') + } + if (typeof (value) != "number") { + throw new Error("the value is not a number!") + } this.x += value; } } diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 6e11643..3390b92 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -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('the function must get an arguments!') + } + if((!(point1 instanceof Point))||(!(point2 instanceof Point))){ + throw new Error("the arguments is not instance of 'Point'!") + } let distanceX = (point2.x - point1.x) ** 2; - let distanceY = (point2.y - point2.y) ** 2; + let distanceY = (point2.y - point1.y) ** 2; const distance = Math.sqrt(distanceX + distanceY); return distance; } const calculateJunctionPoint = (line1, line2) => { + if(line1===undefined || line2===undefined ){ + throw new Error('the function must get an arguments!') + } + if((!(line1 instanceof Line))||(!(line2 instanceof Line))){ + throw new Error("the arguments is not instance of 'Line'!") + } if (line1.slope === line2.slope) { if (line1.n === line2.n) { return true @@ -24,6 +37,12 @@ const calculateJunctionPoint = (line1, line2) => { } const isPointOnLine = (line, point) => { + if(line===undefined || point===undefined ){ + throw new Error('the function must get an arguments!') + } + if((!(line instanceof Line))||(!(point instanceof Point))){ + throw new Error("the arguments is not instance of '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..a0cfee7 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", + "coverage":"npm run test -- --coverage" }, "dependencies": { "jest": "^29.7.0" diff --git a/test/modules/ecs6-class/line.test.js b/test/modules/ecs6-class/line.test.js new file mode 100644 index 0000000..17afb21 --- /dev/null +++ b/test/modules/ecs6-class/line.test.js @@ -0,0 +1,97 @@ +const Line = require("../../../modules/ecs6-class/line") +const Point = require("../../../modules/ecs6-class/point") +let point1 = new Point({ x: 3, y: 4 }) +let point2 = new Point({ x: 1, y: 2 }) +let line = new Line({ point1, point2 }) +let line1 = new Line({ point1, point2 }) +const myline = new Line({}) + +describe('ERROR_CONSTRUCTOR', () => { + it('', () => { + expect(() => new Line({ point1: new Line({}) })).toThrow("the object not instance of 'Point'!") + expect(() => new Line({ point1: false })).toThrow("the object not instance of 'Point'!") + expect(() => new Line({ n: 'hello' })).toThrow("n is not valid!") + expect(() => new Line({ slope: 'iii' })).toThrow("slope is not valid!") + expect(() => new Line({ point1: new Point['hello'] })).toThrow("Point.hello is not a constructor") + + }) +}) +describe('CALCULATE_SLOPE', () => { + it('should calculate the slope', () => { + line.calculateSlope() + expect(line.slope).toBe(1) + }) +}) + +describe('CALCULATE_N_LINE_FUNCTION', () => { + it('should calculate the n', () => { + line.calculateNOfLineFunction() + expect(line.n).toBe(1) + }) +}) + +describe('RETURN_THE_POINT_ON_X_ASIS', () => { + it('should return the slope', () => { + line.getPointOnXAsis() + expect(line.getPointByY(2)).toEqual({ x: 1, y: 2 }) + }) + it('mock function on getPointOnXAsis function', () => { + jest.spyOn(line1, 'getPointByY').mockImplementation((y) => { + return { x: 0, y }; + }); + + const points = line.getPointOnYAsis(); + + expect(points).toEqual({ x: 0, y: 1 }); + }) +}) + +describe('RETURN_THE_POINT_ON_Y_ASIS', () => { + it('', () => { + line.getPointOnYAsis() + expect(line.getPointByX(2)).toEqual({ x: 2, y: 3 }) + }) + it('mock function on getPointOnYAsis function', () => { + jest.spyOn(line1, 'getPointByX').mockImplementation((y) => { + return { x: 0, y }; + }); + + const points = line.getPointOnXAsis(); + + expect(points).toEqual({ x: -1, y: 0 }); + }) +}) + +describe('GET_POINT_BY_X', () => { + it('', () => { + expect(line.getPointByX(2)).toEqual({ x: 2, y: 3 }) + }) + describe('ERROR', () => { + it('', () => { + expect(() => line.getPointByX()).toThrow('x is undefined!') + expect(() => line.getPointByX(true)).toThrow('x is not a number!') + expect(() => line.getPointByX(() => false)).toThrow('x is not a number!') + expect(() => line.getPointByX([1, 2, 3, 4])).toThrow('x is not a number!') + expect(() => line.getPointByX({ x: 1, y: 2 })).toThrow('x is not a number!') + }) + }) +}) + +describe('GET_POINT_BY_Y', () => { + it('', () => { + expect(line.getPointByY(9)).toEqual({ x: 8, y: 9 }) + }) + describe('ERROR', () => { + it('', () => { + expect(() => line.getPointByY()).toThrow('y is undefined!') + expect(() => line.getPointByY(true)).toThrow('y is not a number!') + expect(() => line.getPointByY(() => false)).toThrow('y is not a number!') + expect(() => line.getPointByY([1, 2, 3, 4])).toThrow('y is not a number!') + expect(() => line.getPointByY({ x: 1, y: 2 })).toThrow('y is not a number!') + }) + }) +}) + + + + diff --git a/test/modules/ecs6-class/point.test.js b/test/modules/ecs6-class/point.test.js new file mode 100644 index 0000000..b962d6b --- /dev/null +++ b/test/modules/ecs6-class/point.test.js @@ -0,0 +1,73 @@ +const Point = require('../../../modules/ecs6-class/point') +let mypoint = new Point() + +describe('MOVE_VERTICAL', () => { + + it('should add to this.y the value', () => { + let point = new Point({}) + point.moveVertical(6) + expect(point).toEqual({ x: 0, y: 6 }); + }) + + it('should add to this.y the value', () => { + let point = new Point({ x: 2, y: 3 }) + point.moveVertical(6) + expect(point).toEqual({ x: 2, y: 9 }); + }) + + describe('ERROR', () => { + it('', () => { + expect(() => mypoint.moveVertical(a => (a))).toThrow('the value is not a number!') + expect(() => mypoint.moveVertical(true)).toThrow('the value is not a number!') + expect(() => mypoint.moveVertical('aaa')).toThrow('the value is not a number!') + expect(() => mypoint.moveVertical(['aaa', 'bbb'])).toThrow('the value is not a number!') + expect(() => mypoint.moveVertical()).toThrow('the value is undefined!') + }) + }) +}) + +describe('MOVE_HORIZONTAL', () => { + + it('should add to this.x the value', () => { + let point = new Point({}) + point.moveHorizontal(6) + expect(point).toEqual({ x: 6, y: 0 }); + }) + + it('should add to this.x the value', () => { + let point = new Point({ x: 5, y: 2 }) + point.moveHorizontal(6) + expect(point).toEqual({ x: 11, y: 2 }); + }) + + describe('ERROR', () => { + it('', () => { + expect(() => mypoint.moveHorizontal(v => (v))).toThrow('the value is not a number!') + expect(() => mypoint.moveHorizontal(true)).toThrow('the value is not a number!') + expect(() => mypoint.moveHorizontal('aaa')).toThrow('the value is not a number!') + expect(() => mypoint.moveHorizontal(['aaa', 'bbb'])).toThrow('the value is not a number!') + expect(() => mypoint.moveHorizontal()).toThrow('the value is undefined!') + }) + }) +}) + +describe('CONSTRUCTOR_ERROR', () => { + it('', () => { + expect(() => new Point({ x: 'x', y: 'y' })).toThrow('argument is not a number!') + expect(() => new Point({ x: 'x' })).toThrow('argument is not a number!') + expect(() => new Point({ y: 'y' })).toThrow('argument is not a number!') + expect(() => new Point({ x: true })).toThrow('argument is not a number!') + expect(() => new Point({ y: false })).toThrow('argument is not a number!') + expect(() => new Point({ x: ['a', 'b'] })).toThrow('argument is not a number!') + expect(() => new Point({ y: ['c', 'd'] })).toThrow('argument is not a number!') + expect(() => new Point({ x: ['a', 'b'], y: ['c', 'd'] })).toThrow('argument is not a number!') + expect(() => new Point({ x: [true, true, false] })).toThrow('argument is not a number!') + expect(() => new Point({ y: [false, true] })).toThrow('argument is not a number!') + expect(() => new Point({ x: [true, false], y: [false, true] })).toThrow('argument is not a number!') + expect(() => new Point({ x: () => true })).toThrow('argument is not a number!') + expect(() => new Point({ y: () => false })).toThrow('argument is not a number!') + expect(() => new Point({ x: () => { }, y: () => true })).toThrow('argument is not a number!') + }) +}) + + diff --git a/test/modules/geometry-calculation.test.js b/test/modules/geometry-calculation.test.js new file mode 100644 index 0000000..eab2142 --- /dev/null +++ b/test/modules/geometry-calculation.test.js @@ -0,0 +1,116 @@ +const Line = require("../../modules/ecs6-class/line") +const Point = require("../../modules/ecs6-class/point") +const { calculateDistance, calculateJunctionPoint, isPointOnLine } = require("../../modules/geometry-calculation") + +let point1 = new Point({ x: 4, y: 3 }) +let point2 = new Point({ x: 3, y: 2 }) +let point3 = new Point({ x: 2, y: 2 }) +let point4 = new Point({ x: 2, y: 1 }) +let point5 = new Point({ x: 2, y: 2 }) +let point6 = new Point({ x: 1, y: 1 }) + + +let line1 = new Line({point1, point2,slope:2,n:3}) +let line2 = new Line({point3, point4,slope:2,n:5}) +let line3 = new Line({point3, point4,slope:4,n:3}) +let line4 = new Line({point5, point6,slope:1,n:0}) +let line5 = new Line({point5, point4,slope:1,n:3}) +let line6 = new Line({point5, point4,slope:1,n:3}) + + + + +describe('CALCULATE_DISTANCE', () => { + it('return the sqrt for distance to point1 with point2', () => { + expect(calculateDistance(point1,point2)).toBe(1.4142135623730951) + }) + describe('ERROR',()=>{ + it('',()=>{ + expect(()=>calculateDistance()).toThrow('the function must get an arguments!') + expect(()=>calculateDistance(new Point({}),new Line({}))).toThrow("the arguments is not instance of 'Point'!") + expect(()=>calculateDistance('wow')).toThrow('the function must get an arguments!') + expect(()=>calculateDistance(['a','b'])).toThrow('the function must get an arguments!') + expect(()=>calculateDistance(new Line({}))).toThrow('the function must get an arguments!') + expect(()=>calculateDistance(true)).toThrow('the function must get an arguments!') + expect(()=>calculateDistance({})).toThrow('the function must get an arguments!') + expect(()=>calculateDistance(line1,line2)).toThrow("the arguments is not instance of 'Point'!") + expect(()=>calculateDistance(new Line({}),new Line({}))).toThrow("the arguments is not instance of 'Point'!") + expect(()=>calculateDistance([],[])).toThrow("the arguments is not instance of 'Point'!") + expect(()=>calculateDistance('a','b')).toThrow("the arguments is not instance of 'Point'!") + expect(()=>calculateDistance(1,2)).toThrow("the arguments is not instance of 'Point'!") + }) + }) +}) + +describe('CALCULATE_JUNCTION_POINT', () => { + it('', () => { + expect(calculateJunctionPoint(line1,line1)).toBe(true) + }) + + it('', () => { + expect(calculateJunctionPoint(line1,line2)).toBe(false) + }) + + it('', () => { + expect(calculateJunctionPoint(line2,line3)).toEqual({x: 1, y: 7}) + }) + it('mock function on calculateJunctionPoint function', () => { + jest.spyOn(line1, 'getPointByX').mockImplementation((y) => { + return { x: 0, y }; + }); + + const lines = calculateJunctionPoint(line6,line6); + + expect(lines).toEqual(true); + }) + describe('ERROR',()=>{ + it('',()=>{ + expect(()=>calculateJunctionPoint()).toThrow('the function must get an arguments!') + expect(()=>calculateJunctionPoint(new Point({}),new Line({}))).toThrow("the arguments is not instance of 'Line'!") + expect(()=>calculateJunctionPoint('wow')).toThrow('the function must get an arguments!') + expect(()=>calculateJunctionPoint(['a','b'])).toThrow('the function must get an arguments!') + expect(()=>calculateJunctionPoint(new Line({}))).toThrow('the function must get an arguments!') + expect(()=>calculateJunctionPoint(true)).toThrow('the function must get an arguments!') + expect(()=>calculateJunctionPoint({})).toThrow('the function must get an arguments!') + expect(()=>calculateJunctionPoint(point1,point2)).toThrow("the arguments is not instance of 'Line'!") + expect(()=>calculateJunctionPoint(new Point({}),new Point({}))).toThrow("the arguments is not instance of 'Line'!") + expect(()=>calculateJunctionPoint([],[])).toThrow("the arguments is not instance of 'Line'!") + expect(()=>calculateJunctionPoint('a','b')).toThrow("the arguments is not instance of 'Line'!") + expect(()=>calculateJunctionPoint(1,2)).toThrow("the arguments is not instance of 'Line'!") + }) + }) + +}) + +describe('IS_POINT_ON_LINE', () => { + it('', () => { + expect(isPointOnLine(line4,point6)).toBe(true) + }) + + it('', () => { + expect(isPointOnLine(line5,point6)).toBe(false) + }) + it('', () => { + expect(isPointOnLine(line1,point1)).toBe(false) + }) + + describe('ERROR',()=>{ + it('',()=>{ + expect(()=>isPointOnLine()).toThrow('the function must get an arguments!') + expect(()=>isPointOnLine(new Point({}),new Line({}))).toThrow("the arguments is not instance of 'Line' and 'Point!") + expect(()=>isPointOnLine('wow')).toThrow('the function must get an arguments!') + expect(()=>isPointOnLine(['a','b'])).toThrow('the function must get an arguments!') + expect(()=>isPointOnLine(new Line({}))).toThrow('the function must get an arguments!') + expect(()=>isPointOnLine(true)).toThrow('the function must get an arguments!') + expect(()=>isPointOnLine({})).toThrow('the function must get an arguments!') + expect(()=>isPointOnLine(point1,point2)).toThrow("the arguments is not instance of 'Line' and 'Point!") + expect(()=>isPointOnLine(line1,line2)).toThrow("the arguments is not instance of 'Line' and 'Point!") + expect(()=>isPointOnLine(new Line({point1:new Line({})}),new Line({}))).toThrow("the object not instance of 'Point'!") + expect(()=>isPointOnLine(new Point({}),new Point({}))).toThrow("the arguments is not instance of 'Line' and 'Point!") + expect(()=>isPointOnLine(point1,line1)).toThrow("the arguments is not instance of 'Line' and 'Point!") + expect(()=>isPointOnLine([],[])).toThrow("the arguments is not instance of 'Line' and 'Point!") + expect(()=>isPointOnLine('a','b')).toThrow("the arguments is not instance of 'Line' and 'Point!") + expect(()=>isPointOnLine(1,2)).toThrow("the arguments is not instance of 'Line' and 'Point!") + }) + }) +}) \ No newline at end of file From 2120381916e54323f0e7ab1e5084c0ebc15a2f37 Mon Sep 17 00:00:00 2001 From: cGrin Date: Wed, 24 Jul 2024 20:37:48 +0300 Subject: [PATCH 2/5] fix the tests --- modules/ecs6-class/line.js | 14 +- modules/ecs6-class/point.js | 7 +- modules/geometry-calculation.js | 49 +++++-- test/modules/ecs6-class/line.test.js | 55 +++++--- test/modules/ecs6-class/point.test.js | 48 ++++--- test/modules/geometry-calculation.test.js | 162 ++++++++++++---------- 6 files changed, 203 insertions(+), 132 deletions(-) diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index 8204f7a..0b6777b 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -9,9 +9,19 @@ class Line { if (typeof(n) !== "undefined" && typeof(n) !== "number") { throw new Error("n is not valid!") } - if ((!(point1 instanceof Point)) || (!(point2 instanceof Point))) { - throw new Error("the object not instance of 'Point'!") + + if ((!(point1 instanceof Point))&& (!(point2 instanceof Point))) { + throw new Error("point1 and point2 not instance of 'Point'!") + } + + if (!(point1 instanceof Point)) { + throw new Error("point1 not instance of 'Point'!") } + + if ( !(point2 instanceof Point)) { + throw new Error("point2 not instance of 'Point'!") + } + this.point1 = point1; this.point2 = point2; this.slope = slope; diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index 597ad68..1f0514b 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 new Error('argument is not a number!') + if(typeof(x)!=="number"){ + throw new Error('x is not a number!') + } + if(typeof(y)!=="number"){ + throw new Error('y is not a number!') } this.x = x; this.y = y; diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 3390b92..c311e04 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -2,11 +2,18 @@ const Line = require('./ecs6-class/line'); const Point = require('./ecs6-class/point'); const calculateDistance = (point1, point2) => { - if(point1===undefined || point2===undefined ){ - throw new Error('the function must get an arguments!') + if(point1===undefined && point2===undefined ){ + throw new Error('the function must get an arguments: point1 and point2!') } - if((!(point1 instanceof Point))||(!(point2 instanceof Point))){ - throw new Error("the arguments is not instance of 'Point'!") + + if((!(point1 instanceof Point))&&(!(point2 instanceof Point))){ + throw new Error("the arguments: point1 and point2 are not instance of 'Point'!") + } + if(!(point1 instanceof Point)){ + throw new Error("point1 is not instance of 'Point'!") + } + if(!(point2 instanceof Point)){ + throw new Error("point2 is not instance of 'Point'!") } let distanceX = (point2.x - point1.x) ** 2; let distanceY = (point2.y - point1.y) ** 2; @@ -16,10 +23,22 @@ const calculateDistance = (point1, point2) => { const calculateJunctionPoint = (line1, line2) => { if(line1===undefined || line2===undefined ){ - throw new Error('the function must get an arguments!') + throw new Error('the function must get an arguments:line1 and line2!') + } + if((!(line1 instanceof Line))&&(!(line2 instanceof Line))){ + throw new Error("line1 and line2 are not instance of 'Line'!") + } + if(!(line1 instanceof Line)){ + throw new Error("line1 is not instance of 'Line'!") + } + if(!(line2 instanceof Line)){ + throw new Error("line2 is not instance of 'Line'!") } - if((!(line1 instanceof Line))||(!(line2 instanceof Line))){ - throw new Error("the arguments is not instance of 'Line'!") + if(line1.slope===undefined || line2.slope===undefined){ + throw new Error('slope is undefined!') + } + if(line1.n===undefined || line2.n===undefined){ + throw new Error('n is undefined!') } if (line1.slope === line2.slope) { if (line1.n === line2.n) { @@ -37,11 +56,17 @@ const calculateJunctionPoint = (line1, line2) => { } const isPointOnLine = (line, point) => { - if(line===undefined || point===undefined ){ - throw new Error('the function must get an arguments!') + if(line===undefined || point===undefined){ + throw new Error('the function must get an arguments: line and point!') + } + if((!(line instanceof Line))&&(!(point instanceof Point))){ + throw new Error("line and point are not instance of 'Line' and 'Point!") } - if((!(line instanceof Line))||(!(point instanceof Point))){ - throw new Error("the arguments is not instance of 'Line' and 'Point!") + if(!(line instanceof Line)){ + throw new Error("line is not instance of 'Line'!") + } + if(!(point instanceof Point)){ + throw new Error("point is not instance of 'Point!") } const proxyLine = new Line({ point1: line.point1, point2: point }) proxyLine.calculateSlope() @@ -58,4 +83,4 @@ module.exports = { calculateDistance, calculateJunctionPoint, isPointOnLine -} +} \ No newline at end of file diff --git a/test/modules/ecs6-class/line.test.js b/test/modules/ecs6-class/line.test.js index 17afb21..0b4fe5b 100644 --- a/test/modules/ecs6-class/line.test.js +++ b/test/modules/ecs6-class/line.test.js @@ -6,16 +6,31 @@ let line = new Line({ point1, point2 }) let line1 = new Line({ point1, point2 }) const myline = new Line({}) -describe('ERROR_CONSTRUCTOR', () => { - it('', () => { - expect(() => new Line({ point1: new Line({}) })).toThrow("the object not instance of 'Point'!") - expect(() => new Line({ point1: false })).toThrow("the object not instance of 'Point'!") - expect(() => new Line({ n: 'hello' })).toThrow("n is not valid!") - expect(() => new Line({ slope: 'iii' })).toThrow("slope is not valid!") - expect(() => new Line({ point1: new Point['hello'] })).toThrow("Point.hello is not a constructor") +describe('LINE_CONSTRUCTOR', () => { + it('should check the line object', () => { + expect(line1.point1.x).toBe(3) + expect(line1.point1.y).toBe(4) + expect(line1.point2.x).toBe(1) + expect(line1.point2.y).toBe(2) + expect(line1.n).toBe(undefined) + expect(line1.slope).toBe(undefined) + }) + describe('ERROR_LINE_CONSTRUCTOR', () => { + it('should throw error if the line not valid', () => { + expect(() => new Line({ point1: new Line({}) })).toThrow("point1 not instance of 'Point'!") + expect(() => new Line({ point1: [1, 2, 3] })).toThrow("point1 not instance of 'Point'!") + expect(() => new Line({ point1: 'aaa' })).toThrow("point1 not instance of 'Point'!") + expect(() => new Line({ point2: new Line({}) })).toThrow("point2 not instance of 'Point'!") + expect(() => new Line({ point1: false })).toThrow("point1 not instance of 'Point'!") + expect(() => new Line({ n: 'hello' })).toThrow("n is not valid!") + expect(() => new Line({ slope: 'iii' })).toThrow("slope is not valid!") + expect(() => new Line({ point1: 'hello' , point2: 'hello' })).toThrow("point1 and point2 not instance of 'Point'!") + expect(() => new Line( {point1: 111 , point2: 222 })).toThrow("point1 and point2 not instance of 'Point'!") + }) }) }) + describe('CALCULATE_SLOPE', () => { it('should calculate the slope', () => { line.calculateSlope() @@ -31,43 +46,37 @@ describe('CALCULATE_N_LINE_FUNCTION', () => { }) describe('RETURN_THE_POINT_ON_X_ASIS', () => { - it('should return the slope', () => { - line.getPointOnXAsis() - expect(line.getPointByY(2)).toEqual({ x: 1, y: 2 }) + it('should return a point on x axis', () => { + expect(line.getPointOnXAsis()).toEqual({ x: -1, y: 0 }) }) it('mock function on getPointOnXAsis function', () => { jest.spyOn(line1, 'getPointByY').mockImplementation((y) => { return { x: 0, y }; }); - const points = line.getPointOnYAsis(); - expect(points).toEqual({ x: 0, y: 1 }); }) }) describe('RETURN_THE_POINT_ON_Y_ASIS', () => { - it('', () => { - line.getPointOnYAsis() - expect(line.getPointByX(2)).toEqual({ x: 2, y: 3 }) + it('should return a point on y axis', () => { + expect(line.getPointOnYAsis()).toEqual({ x: 0, y: 1 }) }) it('mock function on getPointOnYAsis function', () => { - jest.spyOn(line1, 'getPointByX').mockImplementation((y) => { - return { x: 0, y }; + jest.spyOn(line1, 'getPointByX').mockImplementation((x) => { + return { x, y: 0 }; }); - const points = line.getPointOnXAsis(); - expect(points).toEqual({ x: -1, y: 0 }); }) }) describe('GET_POINT_BY_X', () => { - it('', () => { + it('should return y by x', () => { expect(line.getPointByX(2)).toEqual({ x: 2, y: 3 }) }) describe('ERROR', () => { - it('', () => { + it('should throw error if the function:"getPointByX" not get/get not valid arguments', () => { expect(() => line.getPointByX()).toThrow('x is undefined!') expect(() => line.getPointByX(true)).toThrow('x is not a number!') expect(() => line.getPointByX(() => false)).toThrow('x is not a number!') @@ -78,11 +87,11 @@ describe('GET_POINT_BY_X', () => { }) describe('GET_POINT_BY_Y', () => { - it('', () => { + it('should return x by y', () => { expect(line.getPointByY(9)).toEqual({ x: 8, y: 9 }) }) describe('ERROR', () => { - it('', () => { + it('should throw error if the function:"getPointByY" not get/get not valid arguments', () => { expect(() => line.getPointByY()).toThrow('y is undefined!') expect(() => line.getPointByY(true)).toThrow('y is not a number!') expect(() => line.getPointByY(() => false)).toThrow('y is not a number!') diff --git a/test/modules/ecs6-class/point.test.js b/test/modules/ecs6-class/point.test.js index b962d6b..bcc5495 100644 --- a/test/modules/ecs6-class/point.test.js +++ b/test/modules/ecs6-class/point.test.js @@ -1,8 +1,32 @@ const Point = require('../../../modules/ecs6-class/point') let mypoint = new Point() -describe('MOVE_VERTICAL', () => { +describe('POINT_CONSTRUCTOR', () => { + it('should check the point object', () => { + expect(mypoint.x).toBe(0) + expect(mypoint.y).toBe(0) + }) + describe('CONSTRUCTOR_POINT_ERROR', () => { + it('should throw error if the point not valid', () => { + expect(() => new Point({ x: 'x', y: 'y' })).toThrow('x is not a number!') + expect(() => new Point({ x: 'x' })).toThrow('x is not a number!') + expect(() => new Point({ y: 'y' })).toThrow('y is not a number!') + expect(() => new Point({ x: true })).toThrow('x is not a number!') + expect(() => new Point({ y: false })).toThrow('y is not a number!') + expect(() => new Point({ x: ['a', 'b'] })).toThrow('x is not a number!') + expect(() => new Point({ y: ['c', 'd'] })).toThrow('y is not a number!') + expect(() => new Point({ x: ['a', 'b'], y: ['c', 'd'] })).toThrow('x is not a number!') + expect(() => new Point({ x: [true, true, false] })).toThrow('x is not a number!') + expect(() => new Point({ y: [false, true] })).toThrow('y is not a number!') + expect(() => new Point({ x: [true, false], y: [false, true] })).toThrow('x is not a number!') + expect(() => new Point({ x: () => true })).toThrow('x is not a number!') + expect(() => new Point({ y: () => false })).toThrow('y is not a number!') + expect(() => new Point({ x: () => { }, y: () => true })).toThrow('x is not a number!') + }) + }) +}) +describe('MOVE_VERTICAL', () => { it('should add to this.y the value', () => { let point = new Point({}) point.moveVertical(6) @@ -16,7 +40,7 @@ describe('MOVE_VERTICAL', () => { }) describe('ERROR', () => { - it('', () => { + it('should throw error if the function not get/get a valid argument', () => { expect(() => mypoint.moveVertical(a => (a))).toThrow('the value is not a number!') expect(() => mypoint.moveVertical(true)).toThrow('the value is not a number!') expect(() => mypoint.moveVertical('aaa')).toThrow('the value is not a number!') @@ -41,7 +65,7 @@ describe('MOVE_HORIZONTAL', () => { }) describe('ERROR', () => { - it('', () => { + it('should throw error if the function not get/get a valid argument', () => { expect(() => mypoint.moveHorizontal(v => (v))).toThrow('the value is not a number!') expect(() => mypoint.moveHorizontal(true)).toThrow('the value is not a number!') expect(() => mypoint.moveHorizontal('aaa')).toThrow('the value is not a number!') @@ -51,23 +75,5 @@ describe('MOVE_HORIZONTAL', () => { }) }) -describe('CONSTRUCTOR_ERROR', () => { - it('', () => { - expect(() => new Point({ x: 'x', y: 'y' })).toThrow('argument is not a number!') - expect(() => new Point({ x: 'x' })).toThrow('argument is not a number!') - expect(() => new Point({ y: 'y' })).toThrow('argument is not a number!') - expect(() => new Point({ x: true })).toThrow('argument is not a number!') - expect(() => new Point({ y: false })).toThrow('argument is not a number!') - expect(() => new Point({ x: ['a', 'b'] })).toThrow('argument is not a number!') - expect(() => new Point({ y: ['c', 'd'] })).toThrow('argument is not a number!') - expect(() => new Point({ x: ['a', 'b'], y: ['c', 'd'] })).toThrow('argument is not a number!') - expect(() => new Point({ x: [true, true, false] })).toThrow('argument is not a number!') - expect(() => new Point({ y: [false, true] })).toThrow('argument is not a number!') - expect(() => new Point({ x: [true, false], y: [false, true] })).toThrow('argument is not a number!') - expect(() => new Point({ x: () => true })).toThrow('argument is not a number!') - expect(() => new Point({ y: () => false })).toThrow('argument is not a number!') - expect(() => new Point({ x: () => { }, y: () => true })).toThrow('argument is not a number!') - }) -}) diff --git a/test/modules/geometry-calculation.test.js b/test/modules/geometry-calculation.test.js index eab2142..6d888eb 100644 --- a/test/modules/geometry-calculation.test.js +++ b/test/modules/geometry-calculation.test.js @@ -9,108 +9,126 @@ let point4 = new Point({ x: 2, y: 1 }) let point5 = new Point({ x: 2, y: 2 }) let point6 = new Point({ x: 1, y: 1 }) - -let line1 = new Line({point1, point2,slope:2,n:3}) -let line2 = new Line({point3, point4,slope:2,n:5}) -let line3 = new Line({point3, point4,slope:4,n:3}) -let line4 = new Line({point5, point6,slope:1,n:0}) -let line5 = new Line({point5, point4,slope:1,n:3}) -let line6 = new Line({point5, point4,slope:1,n:3}) - - - +let line1 = new Line({ point1, point2, slope: 2, n: 3 }) +let line2 = new Line({ point3, point4, slope: 2, n: 5 }) +let line3 = new Line({ point3, point4, slope: 4, n: 3 }) +let line4 = new Line({ point5, point6, slope: 1, n: 0 }) +let line5 = new Line({ point5, point4, slope: 1, n: 3 }) +let line6 = new Line({ point5, point4, slope: 1, n: 3 }) describe('CALCULATE_DISTANCE', () => { it('return the sqrt for distance to point1 with point2', () => { - expect(calculateDistance(point1,point2)).toBe(1.4142135623730951) + expect(calculateDistance(point1, point2)).toBe(1.4142135623730951) }) - describe('ERROR',()=>{ - it('',()=>{ - expect(()=>calculateDistance()).toThrow('the function must get an arguments!') - expect(()=>calculateDistance(new Point({}),new Line({}))).toThrow("the arguments is not instance of 'Point'!") - expect(()=>calculateDistance('wow')).toThrow('the function must get an arguments!') - expect(()=>calculateDistance(['a','b'])).toThrow('the function must get an arguments!') - expect(()=>calculateDistance(new Line({}))).toThrow('the function must get an arguments!') - expect(()=>calculateDistance(true)).toThrow('the function must get an arguments!') - expect(()=>calculateDistance({})).toThrow('the function must get an arguments!') - expect(()=>calculateDistance(line1,line2)).toThrow("the arguments is not instance of 'Point'!") - expect(()=>calculateDistance(new Line({}),new Line({}))).toThrow("the arguments is not instance of 'Point'!") - expect(()=>calculateDistance([],[])).toThrow("the arguments is not instance of 'Point'!") - expect(()=>calculateDistance('a','b')).toThrow("the arguments is not instance of 'Point'!") - expect(()=>calculateDistance(1,2)).toThrow("the arguments is not instance of 'Point'!") + describe('ERROR', () => { + it('shold throw error if the function "calculateDistance" not get/get not valid arguments', () => { + expect(() => calculateDistance()).toThrow('the function must get an arguments: point1 and point2!') + expect(() => calculateDistance(new Line({}), new Point({}))).toThrow("point1 is not instance of 'Point'!") + expect(() => calculateDistance(new Point({}), new Line({}))).toThrow("point2 is not instance of 'Point'!") + expect(() => calculateDistance('wow')).toThrow("the arguments: point1 and point2 are not instance of 'Point'!") + expect(() => calculateDistance(['a', 'b'])).toThrow("the arguments: point1 and point2 are not instance of 'Point'!") + expect(() => calculateDistance(new Line({}))).toThrow("the arguments: point1 and point2 are not instance of 'Point'!") + expect(() => calculateDistance(true)).toThrow("the arguments: point1 and point2 are not instance of 'Point'!") + expect(() => calculateDistance({})).toThrow("the arguments: point1 and point2 are not instance of 'Point'!") + expect(() => calculateDistance(line1, line2)).toThrow("the arguments: point1 and point2 are not instance of 'Point'!") + expect(() => calculateDistance(new Line({}), new Line({}))).toThrow("the arguments: point1 and point2 are not instance of 'Point'!") + expect(() => calculateDistance([], [])).toThrow("the arguments: point1 and point2 are not instance of 'Point'!") + expect(() => calculateDistance('a', 'b')).toThrow("the arguments: point1 and point2 are not instance of 'Point'!") + expect(() => calculateDistance(1, 2)).toThrow("the arguments: point1 and point2 are not instance of 'Point'!") }) }) }) describe('CALCULATE_JUNCTION_POINT', () => { - it('', () => { - expect(calculateJunctionPoint(line1,line1)).toBe(true) + it('should return true if the slope and the n equal in line1 and line2', () => { + expect(calculateJunctionPoint(line1, line1)).toBe(true) }) - - it('', () => { - expect(calculateJunctionPoint(line1,line2)).toBe(false) + + it('should return false if the slope equal and the n not in line1 and line2', () => { + expect(calculateJunctionPoint(line1, line2)).toBe(false) }) - it('', () => { - expect(calculateJunctionPoint(line2,line3)).toEqual({x: 1, y: 7}) + it('should return the junction point', () => { + expect(calculateJunctionPoint(line2, line3)).toEqual({ x: 1, y: 7 }) }) it('mock function on calculateJunctionPoint function', () => { jest.spyOn(line1, 'getPointByX').mockImplementation((y) => { return { x: 0, y }; }); - const lines = calculateJunctionPoint(line6,line6); + const lines = calculateJunctionPoint(line6, line6); expect(lines).toEqual(true); }) - describe('ERROR',()=>{ - it('',()=>{ - expect(()=>calculateJunctionPoint()).toThrow('the function must get an arguments!') - expect(()=>calculateJunctionPoint(new Point({}),new Line({}))).toThrow("the arguments is not instance of 'Line'!") - expect(()=>calculateJunctionPoint('wow')).toThrow('the function must get an arguments!') - expect(()=>calculateJunctionPoint(['a','b'])).toThrow('the function must get an arguments!') - expect(()=>calculateJunctionPoint(new Line({}))).toThrow('the function must get an arguments!') - expect(()=>calculateJunctionPoint(true)).toThrow('the function must get an arguments!') - expect(()=>calculateJunctionPoint({})).toThrow('the function must get an arguments!') - expect(()=>calculateJunctionPoint(point1,point2)).toThrow("the arguments is not instance of 'Line'!") - expect(()=>calculateJunctionPoint(new Point({}),new Point({}))).toThrow("the arguments is not instance of 'Line'!") - expect(()=>calculateJunctionPoint([],[])).toThrow("the arguments is not instance of 'Line'!") - expect(()=>calculateJunctionPoint('a','b')).toThrow("the arguments is not instance of 'Line'!") - expect(()=>calculateJunctionPoint(1,2)).toThrow("the arguments is not instance of 'Line'!") + describe('ERROR', () => { + it('shold throw error if the function "calculateJunctionPoint" not get/get not valid arguments', () => { + expect(() => calculateJunctionPoint()).toThrow('the function must get an arguments:line1 and line2!') + expect(() => calculateJunctionPoint(new Point({}), new Line({}))).toThrow("line1 is not instance of 'Line'!") + expect(() => calculateJunctionPoint(new Line({}), new Point({}))).toThrow("line2 is not instance of 'Line'!") + expect(() => calculateJunctionPoint(new Line({ point3, point3, n: 3, slope: undefined }), new Line({ point3, point3, slope: undefined }))).toThrow("slope is undefined!") + expect(() => calculateJunctionPoint(new Line({ point3, point3, n: undefined, slope: 2 }), new Line({ point3, point3, n: undefined, slope: 2 }))).toThrow("n is undefined!") + expect(() => calculateJunctionPoint('wow')).toThrow('the function must get an arguments:line1 and line2!') + expect(() => calculateJunctionPoint(['a', 'b'])).toThrow('the function must get an arguments:line1 and line2!') + expect(() => calculateJunctionPoint(new Line({}))).toThrow('the function must get an arguments:line1 and line2!') + expect(() => calculateJunctionPoint(true)).toThrow('the function must get an arguments:line1 and line2!') + expect(() => calculateJunctionPoint({})).toThrow('the function must get an arguments:line1 and line2!') + expect(() => calculateJunctionPoint(point1, point2)).toThrow("line1 and line2 are not instance of 'Line'!") + expect(() => calculateJunctionPoint(new Point({}), new Point({}))).toThrow("line1 and line2 are not instance of 'Line'!") + expect(() => calculateJunctionPoint([], [])).toThrow("line1 and line2 are not instance of 'Line'!") + expect(() => calculateJunctionPoint('a', 'b')).toThrow("line1 and line2 are not instance of 'Line'!") + expect(() => calculateJunctionPoint(1, 2)).toThrow("line1 and line2 are not instance of 'Line'!") }) }) - }) describe('IS_POINT_ON_LINE', () => { - it('', () => { - expect(isPointOnLine(line4,point6)).toBe(true) + it('should return true if the point on line', () => { + expect(isPointOnLine(line4, point6)).toBe(true) }) - it('', () => { - expect(isPointOnLine(line5,point6)).toBe(false) + it('should return false if the point on line', () => { + expect(isPointOnLine(line5, point6)).toBe(false) }) - it('', () => { - expect(isPointOnLine(line1,point1)).toBe(false) + it('should return false if the point on line', () => { + expect(isPointOnLine(line1, point1)).toBe(false) }) - describe('ERROR',()=>{ - it('',()=>{ - expect(()=>isPointOnLine()).toThrow('the function must get an arguments!') - expect(()=>isPointOnLine(new Point({}),new Line({}))).toThrow("the arguments is not instance of 'Line' and 'Point!") - expect(()=>isPointOnLine('wow')).toThrow('the function must get an arguments!') - expect(()=>isPointOnLine(['a','b'])).toThrow('the function must get an arguments!') - expect(()=>isPointOnLine(new Line({}))).toThrow('the function must get an arguments!') - expect(()=>isPointOnLine(true)).toThrow('the function must get an arguments!') - expect(()=>isPointOnLine({})).toThrow('the function must get an arguments!') - expect(()=>isPointOnLine(point1,point2)).toThrow("the arguments is not instance of 'Line' and 'Point!") - expect(()=>isPointOnLine(line1,line2)).toThrow("the arguments is not instance of 'Line' and 'Point!") - expect(()=>isPointOnLine(new Line({point1:new Line({})}),new Line({}))).toThrow("the object not instance of 'Point'!") - expect(()=>isPointOnLine(new Point({}),new Point({}))).toThrow("the arguments is not instance of 'Line' and 'Point!") - expect(()=>isPointOnLine(point1,line1)).toThrow("the arguments is not instance of 'Line' and 'Point!") - expect(()=>isPointOnLine([],[])).toThrow("the arguments is not instance of 'Line' and 'Point!") - expect(()=>isPointOnLine('a','b')).toThrow("the arguments is not instance of 'Line' and 'Point!") - expect(()=>isPointOnLine(1,2)).toThrow("the arguments is not instance of 'Line' and 'Point!") + it('mock function on "isPointOnLine" function',()=>{ + jest.mock('../../modules/ecs6-class/line', () => { + return jest.fn().mockImplementation(({ point1, point2, slope, n }) => ({ + point1, + point2, + slope, + n, + calculateSlope: jest.fn().mockImplementation(function () { + this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x); + }), + calculateNOfLineFunction: jest.fn().mockImplementation(function () { + this.n = this.point1.y - this.slope * this.point1.x; + }), + + })); + + }); + }) + + describe('ERROR', () => { + it('shold throw error if the function "isPointOnLine" not get/get not valid arguments', () => { + expect(() => isPointOnLine()).toThrow('the function must get an arguments: line and point!') + expect(() => isPointOnLine(new Point({}), new Line({}))).toThrow("line and point are not instance of 'Line' and 'Point!") + expect(() => isPointOnLine('wow')).toThrow('the function must get an arguments: line and point!') + expect(() => isPointOnLine(['a', 'b'])).toThrow('the function must get an arguments: line and point!') + expect(() => isPointOnLine(new Line({}))).toThrow('the function must get an arguments: line and point!') + expect(() => isPointOnLine(true)).toThrow('the function must get an arguments: line and point!') + expect(() => isPointOnLine({})).toThrow('the function must get an arguments: line and point!') + expect(() => isPointOnLine(point1, point2)).toThrow("line is not instance of 'Line'") + expect(() => isPointOnLine(line1, line2)).toThrow("point is not instance of 'Point!") + expect(() => isPointOnLine(new Line({ point1: new Line({}) }), new Line({}))).toThrow("point1 not instance of 'Point'!") + expect(() => isPointOnLine(new Point({}), new Point({}))).toThrow("line is not instance of 'Line'!") + expect(() => isPointOnLine(point1, line1)).toThrow("line and point are not instance of 'Line' and 'Point!") + expect(() => isPointOnLine([], [])).toThrow("line and point are not instance of 'Line' and 'Point!") + expect(() => isPointOnLine('a', 'b')).toThrow("line and point are not instance of 'Line' and 'Point!") + expect(() => isPointOnLine(1, 2)).toThrow("line and point are not instance of 'Line' and 'Point!") }) }) -}) \ No newline at end of file +}) From 5e5922e3bef9de6336cde2294ee78fc0a71aa586 Mon Sep 17 00:00:00 2001 From: cGrin Date: Thu, 25 Jul 2024 15:42:57 +0300 Subject: [PATCH 3/5] fix tests --- test/modules/ecs6-class/line.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/modules/ecs6-class/line.test.js b/test/modules/ecs6-class/line.test.js index 0b4fe5b..33a8508 100644 --- a/test/modules/ecs6-class/line.test.js +++ b/test/modules/ecs6-class/line.test.js @@ -7,6 +7,7 @@ let line1 = new Line({ point1, point2 }) const myline = new Line({}) describe('LINE_CONSTRUCTOR', () => { + it('should check the line object', () => { expect(line1.point1.x).toBe(3) expect(line1.point1.y).toBe(4) From d828d0b3de27d97815370428f2e6f2e4905acc33 Mon Sep 17 00:00:00 2001 From: cGrin Date: Thu, 25 Jul 2024 16:35:22 +0300 Subject: [PATCH 4/5] fixxxxx --- modules/ecs6-class/point.js | 1 - package.json | 1 - test/modules/geometry-calculation.test.js | 22 +++++++--------------- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index 1f0514b..308ffb0 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -8,7 +8,6 @@ class Point { } this.x = x; this.y = y; - } moveVertical(value) { if (value === undefined) { diff --git a/package.json b/package.json index a0cfee7..24bf4cd 100644 --- a/package.json +++ b/package.json @@ -20,5 +20,4 @@ "homepage": "https://github.com/gemtechd/build-tests#readme", "author": "gemtechd", "license": "ISC" - } diff --git a/test/modules/geometry-calculation.test.js b/test/modules/geometry-calculation.test.js index 6d888eb..7b865ed 100644 --- a/test/modules/geometry-calculation.test.js +++ b/test/modules/geometry-calculation.test.js @@ -94,22 +94,14 @@ describe('IS_POINT_ON_LINE', () => { }) it('mock function on "isPointOnLine" function',()=>{ - jest.mock('../../modules/ecs6-class/line', () => { - return jest.fn().mockImplementation(({ point1, point2, slope, n }) => ({ - point1, - point2, - slope, - n, - calculateSlope: jest.fn().mockImplementation(function () { - this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x); - }), - calculateNOfLineFunction: jest.fn().mockImplementation(function () { - this.n = this.point1.y - this.slope * this.point1.x; - }), - - })); - + jest.spyOn(line6, 'calculateSlope').mockImplementation((x) => { + return { x, y: 0 }; }); + jest.spyOn(line6, 'calculateNOfLineFunction').mockImplementation((x) => { + return { x, y: 0 }; + }); + const points = isPointOnLine(line6,point6); + expect(points).toEqual(false); }) describe('ERROR', () => { From 4594b4762d8e87e6c4174031024b680c552cb67c Mon Sep 17 00:00:00 2001 From: cGrin Date: Sun, 28 Jul 2024 16:51:26 +0300 Subject: [PATCH 5/5] add changes --- modules/geometry-calculation.js | 1 - test/modules/ecs6-class/point.test.js | 4 +- test/modules/geometry-calculation.test.js | 50 +++++++++++++++-------- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index c311e04..bea8c49 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -54,7 +54,6 @@ const calculateJunctionPoint = (line1, line2) => { return junctionPoint } } - const isPointOnLine = (line, point) => { if(line===undefined || point===undefined){ throw new Error('the function must get an arguments: line and point!') diff --git a/test/modules/ecs6-class/point.test.js b/test/modules/ecs6-class/point.test.js index bcc5495..ad552de 100644 --- a/test/modules/ecs6-class/point.test.js +++ b/test/modules/ecs6-class/point.test.js @@ -1,7 +1,7 @@ const Point = require('../../../modules/ecs6-class/point') -let mypoint = new Point() describe('POINT_CONSTRUCTOR', () => { + let mypoint = new Point() it('should check the point object', () => { expect(mypoint.x).toBe(0) expect(mypoint.y).toBe(0) @@ -40,6 +40,7 @@ describe('MOVE_VERTICAL', () => { }) describe('ERROR', () => { + let mypoint = new Point() it('should throw error if the function not get/get a valid argument', () => { expect(() => mypoint.moveVertical(a => (a))).toThrow('the value is not a number!') expect(() => mypoint.moveVertical(true)).toThrow('the value is not a number!') @@ -65,6 +66,7 @@ describe('MOVE_HORIZONTAL', () => { }) describe('ERROR', () => { + let mypoint = new Point() it('should throw error if the function not get/get a valid argument', () => { expect(() => mypoint.moveHorizontal(v => (v))).toThrow('the value is not a number!') expect(() => mypoint.moveHorizontal(true)).toThrow('the value is not a number!') diff --git a/test/modules/geometry-calculation.test.js b/test/modules/geometry-calculation.test.js index 7b865ed..7e34436 100644 --- a/test/modules/geometry-calculation.test.js +++ b/test/modules/geometry-calculation.test.js @@ -2,22 +2,16 @@ const Line = require("../../modules/ecs6-class/line") const Point = require("../../modules/ecs6-class/point") const { calculateDistance, calculateJunctionPoint, isPointOnLine } = require("../../modules/geometry-calculation") -let point1 = new Point({ x: 4, y: 3 }) -let point2 = new Point({ x: 3, y: 2 }) -let point3 = new Point({ x: 2, y: 2 }) -let point4 = new Point({ x: 2, y: 1 }) -let point5 = new Point({ x: 2, y: 2 }) -let point6 = new Point({ x: 1, y: 1 }) -let line1 = new Line({ point1, point2, slope: 2, n: 3 }) -let line2 = new Line({ point3, point4, slope: 2, n: 5 }) -let line3 = new Line({ point3, point4, slope: 4, n: 3 }) -let line4 = new Line({ point5, point6, slope: 1, n: 0 }) -let line5 = new Line({ point5, point4, slope: 1, n: 3 }) -let line6 = new Line({ point5, point4, slope: 1, n: 3 }) describe('CALCULATE_DISTANCE', () => { - it('return the sqrt for distance to point1 with point2', () => { + let point1 = new Point({ x: 4, y: 3 }) + let point2 = new Point({ x: 3, y: 2 }) + let point3 = new Point({ x: 2, y: 2 }) + let point4 = new Point({ x: 2, y: 1 }) + let line1 = new Line({ point1, point2, slope: 2, n: 3 }) + let line2 = new Line({ point3, point4, slope: 2, n: 5 }) + it(' should return the distance between two points', () => { expect(calculateDistance(point1, point2)).toBe(1.4142135623730951) }) describe('ERROR', () => { @@ -40,11 +34,20 @@ describe('CALCULATE_DISTANCE', () => { }) describe('CALCULATE_JUNCTION_POINT', () => { - it('should return true if the slope and the n equal in line1 and line2', () => { + let point1 = new Point({ x: 4, y: 3 }) + let point2 = new Point({ x: 3, y: 2 }) + let point3 = new Point({ x: 2, y: 2 }) + let point4 = new Point({ x: 2, y: 1 }) + let point5 = new Point({ x: 2, y: 2 }) + let line1 = new Line({ point1, point2, slope: 2, n: 3 }) + let line2 = new Line({ point3, point4, slope: 2, n: 5 }) + let line3 = new Line({ point3, point4, slope: 4, n: 3 }) + let line6 = new Line({ point5, point4, slope: 1, n: 3 }) + + it('should return true when both lines are the same', () => { expect(calculateJunctionPoint(line1, line1)).toBe(true) }) - - it('should return false if the slope equal and the n not in line1 and line2', () => { + it('should return false if both lines are parallel', () => { expect(calculateJunctionPoint(line1, line2)).toBe(false) }) @@ -82,6 +85,17 @@ describe('CALCULATE_JUNCTION_POINT', () => { }) describe('IS_POINT_ON_LINE', () => { + let point1 = new Point({ x: 4, y: 3 }) + let point2 = new Point({ x: 3, y: 2 }) + let point3 = new Point({ x: 2, y: 2 }) + let point4 = new Point({ x: 2, y: 1 }) + let point5 = new Point({ x: 2, y: 2 }) + let point6 = new Point({ x: 1, y: 1 }) + let line1 = new Line({ point1, point2, slope: 2, n: 3 }) + let line2 = new Line({ point3, point4, slope: 2, n: 5 }) + let line4 = new Line({ point5, point6, slope: 1, n: 0 }) + let line5 = new Line({ point5, point4, slope: 1, n: 3 }) + let line6 = new Line({ point5, point4, slope: 1, n: 3 }) it('should return true if the point on line', () => { expect(isPointOnLine(line4, point6)).toBe(true) }) @@ -93,14 +107,14 @@ describe('IS_POINT_ON_LINE', () => { expect(isPointOnLine(line1, point1)).toBe(false) }) - it('mock function on "isPointOnLine" function',()=>{ + it('mock function on "isPointOnLine" function', () => { jest.spyOn(line6, 'calculateSlope').mockImplementation((x) => { return { x, y: 0 }; }); jest.spyOn(line6, 'calculateNOfLineFunction').mockImplementation((x) => { return { x, y: 0 }; }); - const points = isPointOnLine(line6,point6); + const points = isPointOnLine(line6, point6); expect(points).toEqual(false); })