From 4f0b5aed2f55a6888d5b6eed0c8ce8f855be12cb Mon Sep 17 00:00:00 2001 From: S-Granevich Date: Tue, 23 Jul 2024 10:45:57 +0300 Subject: [PATCH 1/2] add tests to all function --- modules/ecs6-class/line.js | 21 +++++ modules/ecs6-class/point.js | 15 ++++ modules/geometry-calculation.js | 37 ++++++++- package.json | 3 +- tests/modules/esc-6-class/line.test.js | 82 +++++++++++++++++++ tests/modules/esc-6-class/point.test.js | 50 ++++++++++++ tests/modules/geometry-calculation.test.js | 91 ++++++++++++++++++++++ 7 files changed, 294 insertions(+), 5 deletions(-) create mode 100644 tests/modules/esc-6-class/line.test.js create mode 100644 tests/modules/esc-6-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..5a3c07e 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -2,6 +2,15 @@ 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 new Error("The entered arguments is not valid!") + } + if(typeof(slope)!=="undefined" && typeof(slope)!=="number"){ + throw new Error("slop should get a number") + } + if(typeof(n)!=="undefined" && typeof(n)!=="number"){ + throw new Error("n should get a number") + } this.point1 = point1; this.point2 = point2; this.slope = slope; @@ -26,11 +35,23 @@ class Line { getPointByX(x) { + if(x===undefined){ + throw new Error("the function should get number") + } + if(typeof(x)!="number"){ + throw new Error("the function should get number") + } let y = this.slope * x + this.n return new Point({ x, y }) } getPointByY(y) { + if(y===undefined){ + throw new Error("the function should get number") + } + if(typeof(y)!="number"){ + throw new Error("the function should get 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..5ee6359 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,12 +1,27 @@ class Point { constructor({x=0, y=0}={}) { + if(typeof(x)!=="number"||typeof(y)!=="number"){ + throw new Error("the function should get number") + } this.x = x; this.y = y; } moveVertical(value) { + if(value===undefined){ + throw new Error("the function should get number") + } + if(typeof(value)!="number"){ + throw new Error("the function should get number") + } this.y += value; } moveHorizontal(value) { + if(value===undefined){ + throw new Error("the function should get number") + } + if(typeof(value)!="number"){ + throw new Error("the function should get number") + } this.x += value; } } diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 6e11643..8610c53 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -1,13 +1,30 @@ const Line = require('./ecs6-class/line') +const Point = require('./ecs6-class/point') const calculateDistance = (point1, point2) => { - let distanceX = (point2.x - point1.x) ** 2; - let distanceY = (point2.y - point2.y) ** 2; - const distance = Math.sqrt(distanceX + distanceY); - return distance; + 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 - 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 +41,18 @@ const calculateJunctionPoint = (line1, line2) => { } const isPointOnLine = (line, point) => { + if(line===undefined||point===undefined){ + throw new Error('the function must get an two arguments') + } + if(!(line instanceof Line)){ + throw new Error("the arguments is not instance of 'line'") + + } + if(!(point instanceof Point)){ + throw new Error("the arguments is not instance of '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/tests/modules/esc-6-class/line.test.js b/tests/modules/esc-6-class/line.test.js new file mode 100644 index 0000000..61b9729 --- /dev/null +++ b/tests/modules/esc-6-class/line.test.js @@ -0,0 +1,82 @@ +const Line = require('../../../modules/ecs6-class/line') +const Point = require('../../../modules/ecs6-class/point') +const point1 = new Point({ x: 1, y: 2 }) +const point2 = new Point({ x: 3, y: 4 }) +const line = new Line({ point1, point2,n:2,slope:4 }) +const line1 = new Line({}) +let lineTest=new Line({}) + +describe ('ERRORS',()=>{ + it('Error checker for constructor',()=>{ + expect(()=>new Line({point1:new Line({})})).toThrow('The entered arguments is not valid!') + expect(()=>new Line({n:"abc"})).toThrow('n should get a number') + expect(()=>new Line({point2:true})).toThrow('The entered arguments is not valid!') + expect(()=>new Line({point2:new Point['hello']})).toThrow('Point.hello is not a constructor') + expect(()=>new Line({slope:"sss"})).toThrow('slop should get a number') + + }) +}) + +describe('CALCULATE_SLOP', () => { + it('should calculate the slop', () => { + line.calculateSlope() + expect(line.slope).toBe(1) + + }) +}) + + +describe('CALCULATE_N_OF_LINE_FUNCTION', () => { + it('should calculate n of line', () => { + line.calculateNOfLineFunction() + expect(line.n).toBe(1) + }) +}) +describe('GET_POINT_ON_Y_ASIS',()=>{ + it('should return getPointOnYAsis as per 0',()=>{ + expect(line.getPointOnYAsis()).toEqual({x:0,y:1}) + }) +}) +describe('GET_POINT_ON_X_ASIS',()=>{ + it('should return getPointOnXAsis as per 0',()=>{ + expect(line.getPointOnXAsis()).toEqual({x:-1,y:0}) + }) +}) +describe('GET_POINT_BY_X',()=>{ + it('should retutn y by x',()=>{ + expect(line.getPointByX(3)).toEqual({x:3,y:4}) + }) +}) + +describe('ERRORS',()=>{ + it('Error checker for function getPointByX',()=>{ + expect(()=>lineTest.getPointByX('a')).toThrow('the function should get number') + expect(()=>lineTest.getPointByX(['acc','add'])).toThrow('the function should get number') + expect(()=>lineTest.getPointByX(true)).toThrow('the function should get number') + expect(()=>lineTest.getPointByX()).toThrow('the function should get number') + expect(()=>lineTest.getPointByX((v)=>v)).toThrow('the function should get number') + + }) + + +}) + +describe ('GET_POINT_BY_Y',()=>{ + it('should return x by y',()=>{ + expect(line.getPointByY(5)).toEqual({x:4,y:5}) + }) +}) + +describe('ERRORS',()=>{ + it('Error checker for function getPointByY',()=>{ + expect(()=>lineTest.getPointByY('a')).toThrow('the function should get number') + expect(()=>lineTest.getPointByY(['acc','add'])).toThrow('the function should get number') + expect(()=>lineTest.getPointByY(true)).toThrow('the function should get number') + expect(()=>lineTest.getPointByY()).toThrow('the function should get number') + expect(()=>lineTest.getPointByY((v)=>v)).toThrow('the function should get number') + + }) + +}) + + diff --git a/tests/modules/esc-6-class/point.test.js b/tests/modules/esc-6-class/point.test.js new file mode 100644 index 0000000..fb91c0a --- /dev/null +++ b/tests/modules/esc-6-class/point.test.js @@ -0,0 +1,50 @@ +const Point=require ('../../../modules/ecs6-class/point') +let point=new Point({}) + +describe('MOVE_VERTICAL',()=>{ + it('should move y',()=>{ + const point=new Point({x:1,y:2}) + point.moveVertical(2) + expect(point).toEqual({x:1,y:4}) + }) +}) +describe('ERRORS',()=>{ + it('Error checker for function moveVertical',()=>{ + expect(()=>point.moveVertical('a')).toThrow('the function should get number') + expect(()=>point.moveVertical(['acc','add'])).toThrow('the function should get number') + expect(()=>point.moveVertical(true)).toThrow('the function should get number') + expect(()=>point.moveVertical()).toThrow('the function should get number') + expect(()=>point.moveVertical((v)=>v)).toThrow('the function should get number') + + }) +}) + +describe('MOVE_HORIZONTAL',()=>{ + it('should move x',()=>{ + const point=new Point({x:1,y:2}) + point.moveHorizontal(2) + expect(point).toEqual({x:3,y:2}) + }) +}) +describe('ERRORS',()=>{ + it('Error checker for function moveHorizontal',()=>{ + expect(()=>point.moveHorizontal('a')).toThrow('the function should get number') + expect(()=>point.moveHorizontal(['acc','add'])).toThrow('the function should get number') + expect(()=>point.moveHorizontal(true)).toThrow('the function should get number') + expect(()=>point.moveHorizontal()).toThrow('the function should get number') + expect(()=>point.moveHorizontal((v)=>v)).toThrow('the function should get number') + + }) +}) +describe('ERRORS',()=>{ + it('Error checker for constructor',()=>{ + expect(()=>new Point({x:'a'})).toThrow('the function should get number') + expect(()=>new Point({x:['acc','add']})).toThrow('the function should get number') + expect(()=>new Point({y:true})).toThrow('the function should get number') + expect(()=>new Point({y:(v)=>v})).toThrow('the function should get number') + expect(()=>new Point({x:()=>false})).toThrow('the function should get number') + +}) +}) + + diff --git a/tests/modules/geometry-calculation.test.js b/tests/modules/geometry-calculation.test.js new file mode 100644 index 0000000..acd57ff --- /dev/null +++ b/tests/modules/geometry-calculation.test.js @@ -0,0 +1,91 @@ +const Line = require('../../modules/ecs6-class/line') +const Point = require('../../modules/ecs6-class/point') +const {calculateDistance,calculateJunctionPoint,isPointOnLine}=require('../../modules/geometry-calculation') + +const point1 = new Point({ x: 4, y: 3 }) +const point2 = new Point({ x: 3, y: 2 }) +const point3 = new Point({ x: 2, y: 2 }) +const point4 = new Point({ x: 2, y: 1 }) +const point5 = new Point({ x: 2, y: 2 }) +const point6 = new Point({ x: 1, y: 1 }) + +const line1 = new Line({ point1, point2, slope: 2, n: 3 }) +const line2 = new Line({ point1: point3, point2: point4, slope: 2, n: 5 }) +const line3 = new Line({ point1: point3, point2: point4, slope: 4, n: 3 }) +const line4 = new Line({ point1: point5, point2: point6, slope: 1, n: 0 }) +const line5 = new Line({ point1: point5, point2: point4, slope: 1, n: 3 }) + + +describe('CALCULATE_DISTANCE', () => { + it('should distance ', () => { + expect(calculateDistance(point1, point2)).toEqual(1.4142135623730951) + }) +}) +describe('ERRORS',()=>{ + it('Error checker for function calculateDistance',()=>{ + expect(()=>calculateDistance('aaa')).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('should return true',()=>{ + expect(calculateJunctionPoint(line1, line1)).toEqual(true) + }) + it('should return false',()=>{ + expect(calculateJunctionPoint(line1, line2)).toEqual(false) + }) + it('should return junctionPoint',()=>{ + expect(calculateJunctionPoint(line2, line3)).toEqual({ x: 1, y: 7 }) + }) +}) +describe('ERRORS',()=>{ + it('Error checker for function calculateJunctionPoint',()=>{ + + 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('aaa')).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('should return true if the slope and n equals',()=>{ + expect(isPointOnLine(line4, point6)).toEqual(true) + }) + it('should return false if the slope equal and n not equal',()=>{ + expect(isPointOnLine(line5, point6)).toEqual(false) + }) + it('should return true if the slope and n not equals',()=>{ + expect(isPointOnLine(line1, point1)).toEqual(false) + + }) + +}) +describe('Errors',()=>{ + expect (()=>(isPointOnLine())).toThrow('the function must get an two arguments') + expect(()=>(isPointOnLine(new Line({}),'aaa'))).toThrow( "the arguments is not instance of 'point'") + expect (()=>isPointOnLine(new Point(),new Point())).toThrow("the arguments is not instance of 'line'") + expect(()=>(isPointOnLine('aaa'))).toThrow('the function must get an two arguments') + expect(()=>(isPointOnLine('true'))).toThrow('the function must get an two arguments') + expect(()=>(isPointOnLine(()=>(false)))).toThrow('the function must get an two arguments') + +}) From 7d3334c2834eaeb5df4ef4e9680d182b8dceca7a Mon Sep 17 00:00:00 2001 From: perkal512 Date: Sun, 28 Jul 2024 18:28:02 +0300 Subject: [PATCH 2/2] finish the tests --- modules/ecs6-class/line.js | 14 +- modules/ecs6-class/point.js | 7 +- modules/geometry-calculation.js | 60 +++++++- package.json | 6 +- tests/modules/esc-6-class/line.test.js | 60 +++++++- tests/modules/esc-6-class/point.test.js | 32 ++-- tests/modules/geometry-calculation.test.js | 166 +++++++++++++++------ 7 files changed, 273 insertions(+), 72 deletions(-) diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index 5a3c07e..d6ac047 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -2,9 +2,14 @@ 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 new Error("The entered arguments is not valid!") + if(!(point1 instanceof Point)){ + throw new Error("The entered argument point1 is not valid!") } + if (!(point2 instanceof Point)){ + throw new Error("The entered argument point2 is not valid!") + } + + if(typeof(slope)!=="undefined" && typeof(slope)!=="number"){ throw new Error("slop should get a number") } @@ -18,7 +23,11 @@ class Line { } calculateSlope = () => { + // if(this.point1.x - this.point2.x==0){ + // throw new Error("it is impossible to divide by 0") + // } this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) + } calculateNOfLineFunction = () => { @@ -27,6 +36,7 @@ class Line { getPointOnXAsis() { return this.getPointByY(0) + } getPointOnYAsis() { diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index 5ee6359..73d8aa6 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("the function should get number") + if(typeof(x)!=="number"){ + throw new Error("x should be of type number") + } + if(typeof(y)!=="number"){ + throw new Error("y should be of type number") } this.x = x; this.y = y; diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 8610c53..d396889 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -18,16 +18,48 @@ 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 line1 and line2') + + } + if((!(line1 instanceof Line)) && (!(line2 instanceof Line))){ + throw new Error("line1 and line2 should be of type 'Line'") + } + if((!(line1 instanceof Line))){ + throw new Error("line1 should be of type 'Line'") + } + if((!(line2 instanceof Line))){ + throw new Error("line2 should be of type 'Line'") + } + + + + + if(line1.slope===undefined){ + line1.calculateSlope() + } + if(line2.slope===undefined){ + line2.calculateSlope() + } + if(line1.n===undefined){ + line1.calculateNOfLineFunction() } - if((!(line1 instanceof Line)) || (!(line2 instanceof Line))){ - throw new Error("the arguments is not instance of 'Line'") + if(line2.n===undefined){ + line2.calculateNOfLineFunction() } + + + // if(line1.n===undefined){ + + // } + // if(line2.n===undefined){ + + // } + if (line1.slope === line2.slope) { if (line1.n === line2.n) { - return true + return true } else { return false @@ -39,11 +71,21 @@ const calculateJunctionPoint = (line1, line2) => { return junctionPoint } } - const isPointOnLine = (line, point) => { - if(line===undefined||point===undefined){ + + if(line===undefined && point===undefined){ throw new Error('the function must get an two arguments') } + + if(line===undefined){ + throw new Error('the function must accept a line') + } + + if(point===undefined){ + throw new Error('the function must accept a point') + + } + if(!(line instanceof Line)){ throw new Error("the arguments is not instance of 'line'") @@ -53,8 +95,14 @@ const isPointOnLine = (line, point) => { } + if(line.slope === undefined){ + line.calculateSlope() + } + + const proxyLine = new Line({ point1: line.point1, point2: point }) proxyLine.calculateSlope() + if (line.slope === proxyLine.slope) { proxyLine.calculateNOfLineFunction() if (line.n === proxyLine.n) { diff --git a/package.json b/package.json index a0cfee7..d2adae6 100644 --- a/package.json +++ b/package.json @@ -4,11 +4,11 @@ "description": "practice unit tests in javascript", "main": "index.js", "scripts": { - "test": "jest", - "coverage":"npm run test -- --coverage" + "test": "jest" }, "dependencies": { - "jest": "^29.7.0" + "jest": "^29.7.0", + "coverage":"npm run test -- --coverage" }, "repository": { "type": "git", diff --git a/tests/modules/esc-6-class/line.test.js b/tests/modules/esc-6-class/line.test.js index 61b9729..ceb7d4c 100644 --- a/tests/modules/esc-6-class/line.test.js +++ b/tests/modules/esc-6-class/line.test.js @@ -6,11 +6,28 @@ const line = new Line({ point1, point2,n:2,slope:4 }) const line1 = new Line({}) let lineTest=new Line({}) +describe('CONSTRUCTOR',()=>{ + it('should return n after is built the constructor',()=>{ + expect(line.n).toBe(2) + }) + it('should return slope after is built the constructor',()=>{ + expect(line.slope).toBe(4) + }) + it('should return point1 after is built the constructor',()=>{ + expect(line.point1).toEqual({x:1,y:2}) + }) + it('should return point2 after is built the constructor',()=>{ + expect(line.point2).toEqual({x:3,y:4}) + }) + + +}) + describe ('ERRORS',()=>{ it('Error checker for constructor',()=>{ - expect(()=>new Line({point1:new Line({})})).toThrow('The entered arguments is not valid!') + expect(()=>new Line({point1:new Line({})})).toThrow('The entered argument point1 is not valid!') expect(()=>new Line({n:"abc"})).toThrow('n should get a number') - expect(()=>new Line({point2:true})).toThrow('The entered arguments is not valid!') + expect(()=>new Line({point2:true})).toThrow('The entered argument point2 is not valid!') expect(()=>new Line({point2:new Point['hello']})).toThrow('Point.hello is not a constructor') expect(()=>new Line({slope:"sss"})).toThrow('slop should get a number') @@ -18,30 +35,63 @@ describe ('ERRORS',()=>{ }) describe('CALCULATE_SLOP', () => { - it('should calculate the slop', () => { + it('should calculate the slope', () => { line.calculateSlope() expect(line.slope).toBe(1) }) }) +describe('Error',()=>{ + it("Error checker for function calculateSlope",()=>{ + const line={point1:new Point,point2:new Point,slope:undefined,n:5} + console.log(line); + expect(()=>line.calculateSlope()).toThrow('line.calculateSlope is not a function') + }) +}) + describe('CALCULATE_N_OF_LINE_FUNCTION', () => { it('should calculate n of line', () => { line.calculateNOfLineFunction() - expect(line.n).toBe(1) + expect(line.slope).toBe(1) + }) + }) describe('GET_POINT_ON_Y_ASIS',()=>{ it('should return getPointOnYAsis as per 0',()=>{ expect(line.getPointOnYAsis()).toEqual({x:0,y:1}) }) + + it('mocking getPointByX function in getPointOnYAxis method', () => { + const line = new Line({}); + line.getPointByX = jest.fn().mockReturnValue({ x: 0, y: 5 }); + + const result = line.getPointOnYAsis(); + + expect(result).toEqual({ x: 0, y: 5 }); + }) + }) describe('GET_POINT_ON_X_ASIS',()=>{ it('should return getPointOnXAsis as per 0',()=>{ expect(line.getPointOnXAsis()).toEqual({x:-1,y:0}) }) + + + it('mocking getPointByY function in getPointOnXAxis method', () => { + const line = new Line({}); + getPointByY = jest.fn().mockReturnValue({ x: 5, y: 0 }); + + const result = line.getPointOnXAsis(); + + expect(result).toEqual({ x: NaN, y:0}); + }) + }) + + describe('GET_POINT_BY_X',()=>{ it('should retutn y by x',()=>{ expect(line.getPointByX(3)).toEqual({x:3,y:4}) @@ -65,6 +115,8 @@ describe ('GET_POINT_BY_Y',()=>{ it('should return x by y',()=>{ expect(line.getPointByY(5)).toEqual({x:4,y:5}) }) + + }) describe('ERRORS',()=>{ diff --git a/tests/modules/esc-6-class/point.test.js b/tests/modules/esc-6-class/point.test.js index fb91c0a..31b9e32 100644 --- a/tests/modules/esc-6-class/point.test.js +++ b/tests/modules/esc-6-class/point.test.js @@ -1,6 +1,29 @@ const Point=require ('../../../modules/ecs6-class/point') let point=new Point({}) +describe('CONSTRUCTOR',()=>{ + it('should return x after is built the constructor',()=>{ + expect(point.x).toBe(0) + }) + it('should return y after is built the constructor',()=>{ + expect(point.y).toBe(0) + }) + + + +}) + +describe('ERRORS',()=>{ + it('Error checker for constructor',()=>{ + expect(()=>new Point({x:'a'})).toThrow('x should be of type number') + expect(()=>new Point({x:['acc','add']})).toThrow('x should be of type number') + expect(()=>new Point({y:true})).toThrow('y should be of type number') + expect(()=>new Point({y:(v)=>v})).toThrow('y should be of type number') + expect(()=>new Point({x:()=>false})).toThrow('x should be of type number') + +}) +}) + describe('MOVE_VERTICAL',()=>{ it('should move y',()=>{ const point=new Point({x:1,y:2}) @@ -36,15 +59,6 @@ describe('ERRORS',()=>{ }) }) -describe('ERRORS',()=>{ - it('Error checker for constructor',()=>{ - expect(()=>new Point({x:'a'})).toThrow('the function should get number') - expect(()=>new Point({x:['acc','add']})).toThrow('the function should get number') - expect(()=>new Point({y:true})).toThrow('the function should get number') - expect(()=>new Point({y:(v)=>v})).toThrow('the function should get number') - expect(()=>new Point({x:()=>false})).toThrow('the function should get number') -}) -}) diff --git a/tests/modules/geometry-calculation.test.js b/tests/modules/geometry-calculation.test.js index acd57ff..1199eeb 100644 --- a/tests/modules/geometry-calculation.test.js +++ b/tests/modules/geometry-calculation.test.js @@ -1,8 +1,8 @@ const Line = require('../../modules/ecs6-class/line') -const Point = require('../../modules/ecs6-class/point') -const {calculateDistance,calculateJunctionPoint,isPointOnLine}=require('../../modules/geometry-calculation') +const Point = require('../../modules/ecs6-class/point') +const { calculateDistance, calculateJunctionPoint, isPointOnLine } = require('../../modules/geometry-calculation') -const point1 = new Point({ x: 4, y: 3 }) +const point1 = new Point({ x: 4, y: 3 }) const point2 = new Point({ x: 3, y: 2 }) const point3 = new Point({ x: 2, y: 2 }) const point4 = new Point({ x: 2, y: 1 }) @@ -21,71 +21,145 @@ describe('CALCULATE_DISTANCE', () => { expect(calculateDistance(point1, point2)).toEqual(1.4142135623730951) }) }) -describe('ERRORS',()=>{ - it('Error checker for function calculateDistance',()=>{ - expect(()=>calculateDistance('aaa')).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('ERRORS', () => { + it('Error checker for function calculateDistance', () => { + expect(() => calculateDistance('aaa')).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('should return true',()=>{ +describe('CALCULATE_JUNCTION_POINT', () => { + it('should return true', () => { expect(calculateJunctionPoint(line1, line1)).toEqual(true) }) - it('should return false',()=>{ + it('should return false', () => { expect(calculateJunctionPoint(line1, line2)).toEqual(false) }) - it('should return junctionPoint',()=>{ + it('should return junctionPoint', () => { expect(calculateJunctionPoint(line2, line3)).toEqual({ x: 1, y: 7 }) }) + + it('should slope line1',()=>{ + const line=new Line({point1,point2,slope:undefined,n:5}) + expect(calculateJunctionPoint(line, line1)).toEqual({ x: 2, y: 7 }) + + }) + it('should slope line2',()=>{ + const line=new Line({point1,point2,slope:undefined,n:5}) + expect(calculateJunctionPoint(line1, line)).toEqual({ x: 2, y: 7 }) + + }) + + it('should n line1',()=>{ + const line=new Line({point1,point2,slope:3,n:undefined}) + expect(calculateJunctionPoint(line, line1)).toEqual({ x: 12, y: 27 }) + + }) + it('should n line2',()=>{ + const line=new Line({point1,point2,slope:4,n:undefined}) + expect(calculateJunctionPoint(line1, line)).toEqual({ x: 8, y: 19 }) + + }) + + + it('mocking calculateJunctionPoint function', () => { + const line1 = new Line({}); + const line2 = new Line({}); + line1.slope = 2; + line2.slope = 3; + line1.n = 1; + line2.n = 2; + + line1.getPointByX = jest.fn().mockReturnValue({ x: 3, y: 5 }); + + const result = calculateJunctionPoint(line1, line2); + + expect(result).toEqual({ x: 3, y: 5 }); + }) + + }) -describe('ERRORS',()=>{ - it('Error checker for function calculateJunctionPoint',()=>{ - - 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('aaa')).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('ERRORS', () => { + it('Error checker for function calculateJunctionPoint', () => { + + expect(() => calculateJunctionPoint()).toThrow('the function must get line1 and line2') + expect(() => calculateJunctionPoint(new Point({}), new Line({}))).toThrow("line1 should be of type 'Line'") + expect(() => calculateJunctionPoint(new Line({}), new Point({}))).toThrow("line2 should be of type 'Line'") + expect(() => calculateJunctionPoint(['a', 'b'])).toThrow('the function must get line1 and line2') + expect(() => calculateJunctionPoint(new Line({}))).toThrow('the function must get line1 and line2') + expect(() => calculateJunctionPoint(true)).toThrow("the function must get line1 and line2") + expect(() => calculateJunctionPoint({})).toThrow('the function must get line1 and line2') + expect(() => calculateJunctionPoint(point1, point2)).toThrow("line1 and line2 should be of type 'Line'") + expect(() => calculateJunctionPoint(new Point({}), new Point({}))).toThrow("line1 and line2 should be of type 'Line'") + expect(() => calculateJunctionPoint([], [])).toThrow("line1 and line2 should be of type 'Line'") + expect(() => calculateJunctionPoint('a', 'b')).toThrow("line1 and line2 should be of type 'Line'") + expect(() => calculateJunctionPoint(1, 2)).toThrow("line1 and line2 should be of type 'Line'") + expect(() => calculateJunctionPoint(new Line({ point1: {}, point2: {}, n: 5 }), new Line({}))).toThrow("The entered argument point1 is not valid!") + expect(() => calculateJunctionPoint(new Line({}), new Line({ point1: {}, point2: {}, slope: 5 }))).toThrow("The entered argument point1 is not valid!") + }) }) -describe('IS_POINT_ON_LINE',()=>{ - it('should return true if the slope and n equals',()=>{ +describe('IS_POINT_ON_LINE', () => { + it('should return true if the slope and n equals', () => { + expect(isPointOnLine(line4, point6)).toEqual(true) }) - it('should return false if the slope equal and n not equal',()=>{ + it('should return false if the slope equal and n not equal', () => { expect(isPointOnLine(line5, point6)).toEqual(false) }) - it('should return true if the slope and n not equals',()=>{ + it('should return true if the slope and n not equals', () => { expect(isPointOnLine(line1, point1)).toEqual(false) }) + + it('should return slope if was missing',()=>{ + const line=new Line({point1,point2,slope:undefined,n:5}) + + expect(isPointOnLine(line,point1)).toBe(false) + console.log(line.slope); + }) + + + + it('mocking isPointOnLine function', () => { + const line = new Line({}); + const point = new Point({}); + line.slope = 1; + line.n = 2; + + const proxyLine = new Line({}); + proxyLine.slope = 1; + proxyLine.n = 2; + proxyLine.calculateNOfLineFunction = jest.fn(); + + const result = isPointOnLine(line, point); + + expect(result).toBe(false); + }) + }) -describe('Errors',()=>{ - expect (()=>(isPointOnLine())).toThrow('the function must get an two arguments') - expect(()=>(isPointOnLine(new Line({}),'aaa'))).toThrow( "the arguments is not instance of 'point'") - expect (()=>isPointOnLine(new Point(),new Point())).toThrow("the arguments is not instance of 'line'") - expect(()=>(isPointOnLine('aaa'))).toThrow('the function must get an two arguments') - expect(()=>(isPointOnLine('true'))).toThrow('the function must get an two arguments') - expect(()=>(isPointOnLine(()=>(false)))).toThrow('the function must get an two arguments') +describe('Errors', () => { + it('Error checker for function isPointOnLine', () => { + expect(() => (isPointOnLine())).toThrow("the function must get an two arguments") + expect(() => (isPointOnLine(new Line({}), 'aaa'))).toThrow("the arguments is not instance of 'point'") + expect(() => (isPointOnLine(new Point(), new Point()))).toThrow("the arguments is not instance of 'line'") + expect(() => (isPointOnLine({ line: new Line({}) }))).toThrow( "the function must accept a point") + expect(() => (isPointOnLine(undefined,new Point({}) ))).toThrow('the function must accept a line') + expect(() => (isPointOnLine( () => (false) ))).toThrow('the function must accept a point') + + +}) })