From 788819de18270c9381757342e136e935db60ddae Mon Sep 17 00:00:00 2001 From: gitty markowitch Date: Tue, 9 Jul 2024 15:38:19 +0300 Subject: [PATCH 1/5] tests for point lune and geometry-calculation --- modules/ecs6-class/line.js | 45 +++++- modules/ecs6-class/point.js | 29 +++- modules/geometry-calculation.js | 31 +++- package.json | 3 +- tests/modules/ecs6-class/line.test.js | 164 +++++++++++++++++++++ tests/modules/ecs6-class/point.test.js | 73 +++++++++ tests/modules/geometry-calculation.test.js | 78 ++++++++++ 7 files changed, 409 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 b3bf0f7..90c0acf 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -1,14 +1,29 @@ -const Point = require("./point"); +const { Point } = require("./point"); class Line { constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) { + if (!(point1 instanceof Point)) + throw new Error('point1 is not point') this.point1 = point1; + if (!(point2 instanceof Point)) + throw new Error('point2 is not point') this.point2 = point2; + if (slope !== undefined && typeof slope !== 'number') + throw new Error('slope is not number') this.slope = slope; + if (n !== undefined && typeof (n) !== 'number') + throw new Error('n is not number') this.n = n; } calculateSlope = () => { + if(this.point1.x === this.point2.x&&this.point1.y === this.point2.y){ + throw new Error('The point1 is equal to the point2 so it is not a line') + } + if(this.point1.x === this.point2.x){ + throw new Error('The x of point1 is equal to the x of point2 so it is not a line') + } + this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) } @@ -26,14 +41,38 @@ class Line { getPointByX(x) { + if(x===undefined){ + throw Error('x is not a defined') + } + if(typeof(x)!=="number"){ + + throw Error('x is not a number') + } let y = this.slope * x + this.n return new Point({ x, y }) } getPointByY(y) { - let x = (y - this.slope) / this.n; + if(y===undefined){ + throw Error('y is not defined') + } + if(typeof(y)!=="number"){ + + throw Error('y is not number') + } + + let x = (y - this.n) / this.slope; return new Point({ x, y }) } } -module.exports = Line \ No newline at end of file +module.exports = { + Line, + calculateSlope: Line.prototype.calculateSlope, + calculateNOfLineFunction: Line.prototype.calculateNOfLineFunction, + getPointOnXAsis: Line.prototype.getPointOnXAsis, + getPointOnYAsis: Line.prototype.getPointOnYAsis, + getPointByX: Line.prototype.getPointByX, + getPointByY: Line.prototype.getPointByY + +}; \ No newline at end of file diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index e81b4a4..f21a7bf 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,14 +1,39 @@ class Point { - constructor({x=0, y=0}={}) { + + constructor({ x = 0, y = 0 } = {}) { + if (typeof (x) !== "number" && typeof (y) != "number") { + throw Error('x and y are not number') + } + if (typeof (x) != "number") { + throw Error('x is not number') + } + if (typeof (y) != "number") { + throw Error('y is not number') + } this.x = x; this.y = y; } + moveVertical(value) { + if (!value) + throw Error("value is not define") + if(typeof(value)!=="number") + throw Error("value is not a number") this.y += value; } moveHorizontal(value) { + if (!value) + throw Error("value is not define") + if(typeof(value)!=="number") + throw Error("value is not a number") this.x += value; } } -module.exports = Point \ No newline at end of file + + +module.exports = { + Point:Point, + moveVertical: Point.prototype.moveVertical, + moveHorizontal: Point.prototype.moveHorizontal + }; \ No newline at end of file diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 66856dc..7307070 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -1,13 +1,21 @@ -const Line = require('./ecs6-class/line') + +const {Line } = require("./ecs6-class/line"); +const {Point } = require("./ecs6-class/point"); const calculateDistance = (point1, point2) => { + if (!(point1 instanceof Point) || !(point2 instanceof Point)) { + throw Error('point1 or point2 is not 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 instanceof Line) || !(line2 instanceof Line)) { + throw Error('line1 or line2 is not a line'); + } if (line1.slope === line2.slope) { if (line1.n === line2.n) { return true @@ -23,18 +31,25 @@ const calculateJunctionPoint = (line1, line2) => { } } + const isPointOnLine = (line, point) => { - const proxyLine = new Line(line.point1, point) - proxyLine.calculateSlope() + if (!(line instanceof Line) || !(point instanceof Point)) { + throw new Error('line is not point or point is not a line'); + } + + const proxyLine = new Line({ point1: line.point1, point2: point }); + proxyLine.calculateSlope(); + if (line.slope === proxyLine.slope) { - proxyLine.calculateNOfLineFunction() + proxyLine.calculateNOfLineFunction(); + if (line.n === proxyLine.n2) { - return true + return true; } } - return false -} + return false; +}; module.exports = { calculateDistance, calculateJunctionPoint, diff --git a/package.json b/package.json index 56bf17b..d43de7c 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "practice unit tests in javascript", "main": "index.js", "scripts": { - "test": "jest" + "test": "jest", + "test:coverage": "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 new file mode 100644 index 0000000..3098a2e --- /dev/null +++ b/tests/modules/ecs6-class/line.test.js @@ -0,0 +1,164 @@ +const line = require('../../../modules/ecs6-class/line'); +const { Line, calculateSlope, calculateNOfLineFunction, getPointOnXAsis, getPointOnYAsis, getPointByX, getPointByY } = require('../../../modules/ecs6-class/line'); +const { Point } = require('../../../modules/ecs6-class/point') +describe('CONSTRUCTORE', () => {}) + + it('should initialize to default values when no parameters are provided', () => { + const line = new Line({ point1: new Point({ x: 1, y: 5 }) }); + expect(line.point1).toStrictEqual(new Point({ x: 1, y: 5 })) + expect(line.point2).toStrictEqual(new Point()) + expect(line.n).toBe(undefined) + expect(line.slope).toBe(undefined) + const line1 = new Line({ point2: new Point({ x: 1, y: 5 }) }); + expect(line1.point1).toStrictEqual(new Point()) + expect(line1.point2).toStrictEqual(new Point({ x: 1, y: 5 })) + expect(line1.n).toBe(undefined) + expect(line1.slope).toBe(undefined) + const line2 = new Line({ n: 5 }); + expect(line2.point1).toStrictEqual(new Point()) + expect(line2.point2).toStrictEqual(new Point()) + expect(line2.n).toBe(5) + expect(line2.slope).toBe(undefined) + const line3 = new Line({ slope: 5 }); + expect(line3.point1).toStrictEqual(new Point()) + expect(line3.point2).toStrictEqual(new Point()) + expect(line3.n).toBe(undefined) + expect(line3.slope).toBe(5) + const line4 = new Line({}) + expect(line4?.point1).toStrictEqual(new Point()) + expect(line4?.point2).toStrictEqual(new Point()) + expect(line4?.n).toBe(undefined) + expect(line4?.slope).toBe(undefined) + + }) + it('Checks if the parameters are of the correct type', () => { + + expect(() => new Line({ point1: "ab", point2: new Point({ x: 5, y: 5 }), slope: 1, n: 5 })).toThrow('point1 is not point'); + expect(() => new Line({ point1: new Point({ x: 5, y: 5 }), point2: "ab", slope: 1, n: 5 })).toThrow('point2 is not point'); + expect(() => new Line({ point1: new Point({ x: 5, y: 5 }), point2: new Point({ x: 5, y: 5 }), slope: "d", n: 5 })).toThrow('slope is not number'); + expect(() => new Line({ point1: new Point({ x: 5, y: 5 }), point2: new Point({ x: 5, y: 5 }), slope: 1, n: "d" })).toThrow('n is not number'); + }) + it('Point constructor initializes point1 and point2 and slope and n correctly', () => { + const line = new Line({ point1: new Point({ x: 5, y: 8 }), point2: new Point({ x: 5, y: 8 }), slope: 5, n: 8 }); + expect(line.point1).toEqual(new Point({ x: 5, y: 8 })); + expect(line.point2).toEqual(new Point({ x: 5, y: 8 })); + expect(line.slope).toBe(5); + expect(line.n).toBe(8); + + }); + + +describe('CALCULATESLOPE', () => { + + + it('should The slope is 0 as the line is vertical (point1= point2) ', () => { + const line = new Line(({ point1: new Point({ x: 5, y: 8 }), point2: new Point({ x: 5, y: 8 }), n: 8 })) + expect(() => line.calculateSlope()).toThrow('The point1 is equal to the point2 so it is not a line'); + + }) + + it('should The slope is undefined as the line is vertical (y1 = y2) ', () => { + const line = new Line(({ point1: new Point({ x: 5, y: 8 }), point2: new Point({ x: 3, y: 8 }), slope: 5, n: 8 })) + line.calculateSlope() + expect(line.slope).toBeCloseTo(0); + + }) + it('should The slope is undefined as the line is vertical (x1=x2)', () => { + const line = new Line({ point1: new Point({ x: 5, y: 9 }), point2: new Point({ x: 5, y: 8 }), n: 8 }); + expect(() => line.calculateSlope()).toThrow('The x of point1 is equal to the x of point2 so it is not a line'); + }); + + + + it('should move vertically correctly', () => { + const line = new Line(({ point1: new Point({ x: -1, y: 8 }), point2: new Point({ x: 2, y: 5 }), slope: 5, n: 8 })) + line.calculateSlope() + expect(line.slope).toBe(-1); + + }) + + +}) +describe('CALCULATENOFLINEFUNCTION', () => { + it(' should move vertically correctly', () => { + const line = new Line(({ point1: new Point({ x: 5, y: 8 }), point2: new Point({ x: 5, y: 5 }), slope: 1, n: 8 })) + line.calculateNOfLineFunction() + expect(line.n).toBe(3); + + }) + +}) + + + +describe('GETPOINTBYX', () => { + it('should x is not define ', () => { + const line = new Line({}) + expect(() => line.getPointByX()).toThrow('x is not a defined'); + }) + + it('should x is not number ', () => { + const line = new Line({}) + expect(() => line.getPointByX("abc")).toThrow('x is not a number'); + }) + + it(' should check that the returned value is of type point', () => { + const line = new Line({}) + const point = line.getPointByX(5) + expect(point).toBeInstanceOf(Point) + + }) + it('should checks that point is returned with correct values', () => { + const line = new Line(({ point1: new Point({ x: 5, y: 8 }), point2: new Point({ x: 5, y: 5 }), slope: 1, n: 8 })) + initialx = line.point1.x + const point = line.getPointByX(5) + expect(point.x).toBe(initialx) + expect(point.y).toBe(13) + }) +}) +describe('GETPOINTBY', () => { + it('should y is not define ', () => { + const line = new Line({}) + expect(() => line.getPointByY()).toThrow('y is not defined'); + })}) + + it('should y is not number ', () => { + const line = new Line({}) + expect(() => line.getPointByY("abc")).toThrow('y is not number'); + }) + + it(' should check that the returned value is of type point', () => { + const line = new Line({}) + const point = line.getPointByY(5) + expect(point).toBeInstanceOf(Point) + + }) + it('should checks that point is returned with correct values', () => { + const line = new Line(({ point1: new Point({ x: 8, y: 6 }), point2: new Point({ x: 5, y: 5 }), slope: 1, n: 3 })) + initialy = line.point1.y + const point = line.getPointByY(6) + expect(point.x).toBe(3) + expect(point.y).toBe(initialy) + }) + + describe('GETPOINTONXASIS', () => { + it('returns the correct point on the y-axis for the given slope and n value', () => { + const line = new Line(({ slope: 2, n: -6 })) + const point = line.getPointOnXAsis() + expect(point.y).toBe(0) + expect(point.x).toBe(3) + + + })}) + describe('GETPOINTONXASIS', () => { + it('returns the correct point on the y-axis for the given slope and n value', () => { + const line = new Line({slope: 5, n: 5 }) + const point = line.getPointOnYAsis() + expect(point.y).toBe(5) + expect(point.x).toBe(0) + + + })}) + + + diff --git a/tests/modules/ecs6-class/point.test.js b/tests/modules/ecs6-class/point.test.js new file mode 100644 index 0000000..d3fdf83 --- /dev/null +++ b/tests/modules/ecs6-class/point.test.js @@ -0,0 +1,73 @@ + + +const {Point,moveHorizontal}=require('../../../modules/ecs6-class/point') +describe('CONSTRUCTORE', () => { + + it('should initialize x and y to default values when no parameters are provided', () => { + + const point= new Point(); + expect(point.x).toBe(0) + expect(point.y).toBe(0) + const point1 = new Point({x:5}); + expect(point1.x).toBe(5) + expect(point1.y).toBe(0) + const point2 = new Point({y:5}); + expect(point2.x).toBe(0) + expect(point2.y).toBe(5) + + }); + it('should throw error when x or y is not a number', () => { + const x = "abc"; + const y = "s"; + + expect(() => new Point({ x: x, y: 5 })).toThrow('x is not number'); + expect(() => new Point({ x: 5, y: y })).toThrow('y is not number'); + expect(() => new Point({ x: x, y: y })).toThrow('x and y are not number'); + }); + + it('Point constructor initializes x and y correctly', () => { + const point = new Point({x:3,y: 4}); + expect(point.x).toBe(3); + expect(point.y).toBe(4); + }); +}) +describe('MOVEVERTICAL', () => { + it('should value is not define ', () => { + const point = new Point() + expect(()=>point.moveVertical()).toThrow('value is not define'); + }) + + it("should value is not a number",() => { + const point = new Point() + expect(()=>point.moveVertical("abc")).toThrow('value is not a number'); + }) + + it('should move vertically correctly', () => { + const point=new Point({x:5,y:2}) + point.moveVertical(5) + expect(point.y).toBe(7) + + }) +}) + + describe('MOVEHORIZONAL', () => { + it('should value is not define ', () => { + const point = new Point() + expect(()=>point.moveHorizontal()).toThrow('value is not define'); + }) + + it("should value is not a number",() => { + const point = new Point() + expect(()=>point.moveHorizontal("abc")).toThrow('value is not a number'); + }) + + it('should move vertically correctly', () => { + const point=new Point({x:5,y:2}) + point.moveHorizontal(5) + expect(point.x).toBe(5+5) + + }) + + +}) + diff --git a/tests/modules/geometry-calculation.test.js b/tests/modules/geometry-calculation.test.js new file mode 100644 index 0000000..e54483f --- /dev/null +++ b/tests/modules/geometry-calculation.test.js @@ -0,0 +1,78 @@ +// "calculates the distance between two points correctly" + +const { Line } = require("../../modules/ecs6-class/line"); +const { Point } = require("../../modules/ecs6-class/point"); +const { isPointOnLine, calculateDistance, calculateJunctionPoint } = require('../../modules/geometry-calculation') + +describe('CALCULATEDISTANCE', () => { + it(' should checks if the parameters are of the correct type', () => { + expect(() => calculateDistance('ab', 'df')).toThrow('point1 or point2 is not point'); + expect(() => calculateDistance('ab', new Point())).toThrow('point1 or point2 is not point'); + expect(() => calculateDistance(new Point(), 'av')).toThrow('point1 or point2 is not point'); + }) + it(' should checks the distance between two lines if it correct', () => { + + const line = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 9 }), slope: 1, n: 5 }) + const distance = calculateDistance(line.point1, line.point2) + expect(distance).toBe(5) + }) +}) + describe('CALCULATEJUNCTIONPOINT', () => { + it(' should checks if the parameters are of the correct type', () => { + expect(() => calculateJunctionPoint('ab', 'df')).toThrow('line1 or line2 is not a line'); + expect(() => calculateJunctionPoint('ab', new Point())).toThrow('line1 or line2 is not a line'); + expect(() => calculateJunctionPoint(new Point(), 'av')).toThrow('line1 or line2 is not a line'); + }) + it('should return true for specific input', () => { + + const line = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 9 }), slope: 1, n: 5 }) + const line1 = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 5 }), slope: 1, n: 5 }) + const result = calculateJunctionPoint(line, line1) + expect(result).toBeTruthy(); + }); + it('should return false for specific input', () => { + + const line = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 9 }), slope: 1, n: 5 }) + const line1 = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 5 }), slope: 1, n: 6 }) + const result = calculateJunctionPoint(line, line1) + + expect(result).toBeFalsy(); + }); + // it(' should check that the returned value is of type point', () => { + // const line = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 9 }), slope: 1, n: 10 }) + // const line1 = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 5 }), slope: 5, n: 2 }) + // const result = calculateJunctionPoint(line, line1) + // expect(result).toBeInstanceOf(Point) + + // }) + it(' should calculateJunctionPoint return the correct junction point for the given lines', () => { + + const line = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 9 }), slope: 1, n: 10 }) + const line1 = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 5 }), slope: 5, n: 2 }) + const result = calculateJunctionPoint(line, line1) + expect(result).toEqual(new Point({ x: 2, y: 12 })); + + }); + + + }) + + it(' should checks if the parameters are of the correct type', () => { + const line = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 9 }), slope: 1, n: 10 }) + const point = new Point({ x: 4, y: 6 }) + expect(() => isPointOnLine("k", "j")).toThrow('line is not point or point is not a line'); + expect(() => isPointOnLine(line, "abc")).toThrow('line is not point or point is not a line'); + expect(() => isPointOnLine("c", point)).toThrow('line is not point or point is not a line'); + }) + it('should Point lies isnt on a line with slope and n values', () => { + const line = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 9 }), slope: 2, n:-6 }) + const point=new Point({ x: 5, y: 4 }) + const result=isPointOnLine(line,point) + expect(result).toBeFalsy(); + }) + // it('should Point lies on a line with slope and n values', () => { + // const line = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 9 }), slope: -2, n:14 }) + // const point=new Point({ x: 5, y: 4 }) + // const result=isPointOnLine(line,point) + // expect(result).toBeTruthy(); + // }) From f790152605fed60d1f069ca75ae1ab131a5699b2 Mon Sep 17 00:00:00 2001 From: gitty markowitch Date: Thu, 18 Jul 2024 18:42:28 +0300 Subject: [PATCH 2/5] add tests to point line and geometry calculation --- modules/ecs6-class/point.js | 1 + modules/geometry-calculation.js | 26 ++- tests/modules/ecs6-class/line.test.js | 192 +++++++++++---------- tests/modules/ecs6-class/point.test.js | 123 ++++++------- tests/modules/geometry-calculation.test.js | 150 +++++++++------- 5 files changed, 266 insertions(+), 226 deletions(-) diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index f21a7bf..fbe5518 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,6 +1,7 @@ class Point { constructor({ x = 0, y = 0 } = {}) { + if (typeof (x) !== "number" && typeof (y) != "number") { throw Error('x and y are not number') } diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 7307070..3342bcf 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -1,21 +1,29 @@ -const {Line } = require("./ecs6-class/line"); -const {Point } = require("./ecs6-class/point"); +const { Line } = require("./ecs6-class/line"); +const { Point } = require("./ecs6-class/point"); const calculateDistance = (point1, point2) => { if (!(point1 instanceof Point) || !(point2 instanceof Point)) { - throw Error('point1 or point2 is not point'); + throw Error('point1 or point2 is not 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 instanceof Line) || !(line2 instanceof Line)) { - throw Error('line1 or line2 is not a line'); + if (!(line1 instanceof Line) && !(line2 instanceof Line)) { + throw Error('line1 and line2 is not a line'); + } + + if (!(line1 instanceof Line)) { + throw Error('line1 is not a line'); + } + + if (!(line2 instanceof Line)) { + throw Error('line2 is not a line'); } + if (line1.slope === line2.slope) { if (line1.n === line2.n) { return true @@ -42,10 +50,12 @@ const isPointOnLine = (line, point) => { if (line.slope === proxyLine.slope) { proxyLine.calculateNOfLineFunction(); - - if (line.n === proxyLine.n2) { + if (line.n === proxyLine.n) { return true; } + else { + return false + } } return false; diff --git a/tests/modules/ecs6-class/line.test.js b/tests/modules/ecs6-class/line.test.js index 3098a2e..ec9d2b8 100644 --- a/tests/modules/ecs6-class/line.test.js +++ b/tests/modules/ecs6-class/line.test.js @@ -1,87 +1,85 @@ -const line = require('../../../modules/ecs6-class/line'); + const { Line, calculateSlope, calculateNOfLineFunction, getPointOnXAsis, getPointOnYAsis, getPointByX, getPointByY } = require('../../../modules/ecs6-class/line'); const { Point } = require('../../../modules/ecs6-class/point') -describe('CONSTRUCTORE', () => {}) - - it('should initialize to default values when no parameters are provided', () => { - const line = new Line({ point1: new Point({ x: 1, y: 5 }) }); - expect(line.point1).toStrictEqual(new Point({ x: 1, y: 5 })) - expect(line.point2).toStrictEqual(new Point()) - expect(line.n).toBe(undefined) - expect(line.slope).toBe(undefined) - const line1 = new Line({ point2: new Point({ x: 1, y: 5 }) }); - expect(line1.point1).toStrictEqual(new Point()) - expect(line1.point2).toStrictEqual(new Point({ x: 1, y: 5 })) - expect(line1.n).toBe(undefined) - expect(line1.slope).toBe(undefined) - const line2 = new Line({ n: 5 }); - expect(line2.point1).toStrictEqual(new Point()) - expect(line2.point2).toStrictEqual(new Point()) - expect(line2.n).toBe(5) - expect(line2.slope).toBe(undefined) - const line3 = new Line({ slope: 5 }); - expect(line3.point1).toStrictEqual(new Point()) - expect(line3.point2).toStrictEqual(new Point()) - expect(line3.n).toBe(undefined) - expect(line3.slope).toBe(5) - const line4 = new Line({}) - expect(line4?.point1).toStrictEqual(new Point()) - expect(line4?.point2).toStrictEqual(new Point()) - expect(line4?.n).toBe(undefined) - expect(line4?.slope).toBe(undefined) - - }) - it('Checks if the parameters are of the correct type', () => { - - expect(() => new Line({ point1: "ab", point2: new Point({ x: 5, y: 5 }), slope: 1, n: 5 })).toThrow('point1 is not point'); - expect(() => new Line({ point1: new Point({ x: 5, y: 5 }), point2: "ab", slope: 1, n: 5 })).toThrow('point2 is not point'); - expect(() => new Line({ point1: new Point({ x: 5, y: 5 }), point2: new Point({ x: 5, y: 5 }), slope: "d", n: 5 })).toThrow('slope is not number'); - expect(() => new Line({ point1: new Point({ x: 5, y: 5 }), point2: new Point({ x: 5, y: 5 }), slope: 1, n: "d" })).toThrow('n is not number'); - }) - it('Point constructor initializes point1 and point2 and slope and n correctly', () => { - const line = new Line({ point1: new Point({ x: 5, y: 8 }), point2: new Point({ x: 5, y: 8 }), slope: 5, n: 8 }); - expect(line.point1).toEqual(new Point({ x: 5, y: 8 })); - expect(line.point2).toEqual(new Point({ x: 5, y: 8 })); - expect(line.slope).toBe(5); - expect(line.n).toBe(8); - - }); +const mockConstructorPoint = jest.fn(constructor); +const mockConstructorLine = jest.fn(constructor); +let line +describe('CONSTRUCTORE', () => { }) + + +it('should initialize to default values when no parameters are provided', () => { + line = new Line({ point1: mockConstructorPoint(new Point({ x: 1, y: 5 })) }); + expect(line.point1).toStrictEqual(mockConstructorPoint(new Point({ x: 1, y: 5 }))) + expect(line.point2).toStrictEqual(mockConstructorPoint(new Point())) + expect(line.n).toBe(undefined) + expect(line.slope).toBe(undefined) + line = new Line({ point2: mockConstructorPoint(new Point({ x: 1, y: 5 })) }); + expect(line.point1).toStrictEqual(mockConstructorPoint(new Point())) + expect(line.point2).toStrictEqual(mockConstructorPoint(new Point({ x: 1, y: 5 }))) + expect(line.n).toBe(undefined) + expect(line.slope).toBe(undefined) + line = new Line({ n: 5 }); + expect(line.point1).toStrictEqual(mockConstructorPoint(new Point())) + expect(line.point2).toStrictEqual(mockConstructorPoint(new Point())) + expect(line.n).toBe(5) + expect(line.slope).toBe(undefined) + line = new Line({ slope: 5 }); + expect(line.point1).toStrictEqual(mockConstructorPoint(new Point())) + expect(line.point2).toStrictEqual(mockConstructorPoint(new Point())) + expect(line.n).toBe(undefined) + expect(line.slope).toBe(5) + line = new Line({}) + expect(line.point1).toStrictEqual(mockConstructorLine(new Point())) + expect(line.point2).toStrictEqual(mockConstructorPoint(new Point())) + expect(line.n).toBe(undefined) + expect(line.slope).toBe(undefined) +}) +it('Checks if the parameters are of the correct type', () => { -describe('CALCULATESLOPE', () => { + expect(() => new Line({ point1: "ab", point2: new Point({ x: 5, y: 5 }), slope: 1, n: 5 })).toThrow('point1 is not point'); + expect(() => new Line({ point1:mockConstructorPoint( new Point({ x: 5, y: 5 })), point2: "ab", slope: 1, n: 5 })).toThrow('point2 is not point'); + expect(() => new Line({ point1: new Point({ x: 5, y: 5 }), point2:mockConstructorPoint( new Point({ x: 5, y: 5 })), slope: "d", n: 5 })).toThrow('slope is not number'); + expect(() => new Line({ point1: new Point({ x: 5, y: 5 }), point2: mockConstructorPoint(new Point({ x: 5, y: 5 })), slope: 1, n: "d" })).toThrow('n is not number'); +}) +it('Point constructor initializes point1 and point2 and slope and n correctly', () => { + line = new Line({ point1:mockConstructorPoint( new Point({ x: 5, y: 8 })), point2:mockConstructorPoint( new Point({ x: 5, y: 8 })), slope: 5, n: 8 }); + expect(line.point1).toEqual(mockConstructorPoint(new Point({ x: 5, y: 8 }))); + expect(line.point2).toEqual(mockConstructorPoint(new Point({ x: 5, y: 8 }))); + expect(line.slope).toBe(5); + expect(line.n).toBe(8); +}); +describe('CALCULATE_SLOPE', () => { it('should The slope is 0 as the line is vertical (point1= point2) ', () => { - const line = new Line(({ point1: new Point({ x: 5, y: 8 }), point2: new Point({ x: 5, y: 8 }), n: 8 })) + line = mockConstructorLine(new Line(({ point1: mockConstructorPoint(new Point({ x: 5, y: 8 })), point2: mockConstructorPoint(new Point({ x: 5, y: 8 })), n: 8 }))) expect(() => line.calculateSlope()).toThrow('The point1 is equal to the point2 so it is not a line'); }) it('should The slope is undefined as the line is vertical (y1 = y2) ', () => { - const line = new Line(({ point1: new Point({ x: 5, y: 8 }), point2: new Point({ x: 3, y: 8 }), slope: 5, n: 8 })) + line = mockConstructorLine(new Line(({ point1: mockConstructorPoint(new Point({ x: 5, y: 8 })), point2: mockConstructorPoint(new Point({ x: 3, y: 8 })), slope: 5, n: 8 }))) line.calculateSlope() expect(line.slope).toBeCloseTo(0); }) it('should The slope is undefined as the line is vertical (x1=x2)', () => { - const line = new Line({ point1: new Point({ x: 5, y: 9 }), point2: new Point({ x: 5, y: 8 }), n: 8 }); + line = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 5, y: 9 })), point2: mockConstructorPoint(new Point({ x: 5, y: 8 })), n: 8 })); expect(() => line.calculateSlope()).toThrow('The x of point1 is equal to the x of point2 so it is not a line'); }); - - it('should move vertically correctly', () => { - const line = new Line(({ point1: new Point({ x: -1, y: 8 }), point2: new Point({ x: 2, y: 5 }), slope: 5, n: 8 })) + line = mockConstructorLine(new Line(({ point1: mockConstructorPoint(new Point({ x: -1, y: 8 })), point2: mockConstructorPoint(new Point({ x: 2, y: 5 })), slope: 5, n: 8 }))); line.calculateSlope() expect(line.slope).toBe(-1); }) - }) -describe('CALCULATENOFLINEFUNCTION', () => { +describe('CALCULATE_N_OF_LINE_FUNCTION', () => { it(' should move vertically correctly', () => { - const line = new Line(({ point1: new Point({ x: 5, y: 8 }), point2: new Point({ x: 5, y: 5 }), slope: 1, n: 8 })) + line = mockConstructorLine(new Line(({ point1: mockConstructorPoint(new Point({ x: 5, y: 8 })), point2: mockConstructorPoint(new Point({ x: 5, y: 5 })), slope: 1, n: 8 }))); line.calculateNOfLineFunction() expect(line.n).toBe(3); @@ -89,9 +87,7 @@ describe('CALCULATENOFLINEFUNCTION', () => { }) - - -describe('GETPOINTBYX', () => { +describe('GET_POINT_BY_X', () => { it('should x is not define ', () => { const line = new Line({}) expect(() => line.getPointByX()).toThrow('x is not a defined'); @@ -116,49 +112,55 @@ describe('GETPOINTBYX', () => { expect(point.y).toBe(13) }) }) -describe('GETPOINTBY', () => { +describe('GET_POINT_B_Y', () => { it('should y is not define ', () => { const line = new Line({}) expect(() => line.getPointByY()).toThrow('y is not defined'); - })}) - - it('should y is not number ', () => { - const line = new Line({}) - expect(() => line.getPointByY("abc")).toThrow('y is not number'); }) +}) - it(' should check that the returned value is of type point', () => { - const line = new Line({}) - const point = line.getPointByY(5) - expect(point).toBeInstanceOf(Point) +it('should y is not number ', () => { + const line = new Line({}) + expect(() => line.getPointByY("abc")).toThrow('y is not number'); +}) - }) - it('should checks that point is returned with correct values', () => { - const line = new Line(({ point1: new Point({ x: 8, y: 6 }), point2: new Point({ x: 5, y: 5 }), slope: 1, n: 3 })) - initialy = line.point1.y - const point = line.getPointByY(6) +it(' should check that the returned value is of type point', () => { + const line = new Line({}) + const point = line.getPointByY(5) + expect(point).toBeInstanceOf(Point) + +}) +it('should checks that point is returned with correct values', () => { + const line = new Line(({ point1: new Point({ x: 8, y: 6 }), point2: new Point({ x: 5, y: 5 }), slope: 1, n: 3 })) + initialy = line.point1.y + const point = line.getPointByY(6) + expect(point.x).toBe(3) + expect(point.y).toBe(initialy) +}) + +describe('GET_POINT_ON_X_ASIS', () => { + + it('returns the correct point on the y-axis for the given slope and n value', () => { + + const line = new Line(({ slope: 2, n: -6 })) + const point = line.getPointOnXAsis() + expect(point.y).toBe(0) expect(point.x).toBe(3) - expect(point.y).toBe(initialy) + + + }) +}) +describe('GET_POINT_ON_Y_ASIS', () => { + it('returns the correct point on the y-axis for the given slope and n value', () => { + const line = new Line({ slope: 5, n: 5 }) + const point = line.getPointOnYAsis() + expect(point.y).toBe(5) + expect(point.x).toBe(0) + + + }) +}) + - describe('GETPOINTONXASIS', () => { - it('returns the correct point on the y-axis for the given slope and n value', () => { - const line = new Line(({ slope: 2, n: -6 })) - const point = line.getPointOnXAsis() - expect(point.y).toBe(0) - expect(point.x).toBe(3) - - - })}) - describe('GETPOINTONXASIS', () => { - it('returns the correct point on the y-axis for the given slope and n value', () => { - const line = new Line({slope: 5, n: 5 }) - const point = line.getPointOnYAsis() - expect(point.y).toBe(5) - expect(point.x).toBe(0) - - - })}) - - diff --git a/tests/modules/ecs6-class/point.test.js b/tests/modules/ecs6-class/point.test.js index d3fdf83..2d366bb 100644 --- a/tests/modules/ecs6-class/point.test.js +++ b/tests/modules/ecs6-class/point.test.js @@ -1,73 +1,74 @@ - -const {Point,moveHorizontal}=require('../../../modules/ecs6-class/point') +const {Point,moveHorizontal, moveVertical}=require('../../../modules/ecs6-class/point') +const mockConstructor = jest.fn(constructor); +let point describe('CONSTRUCTORE', () => { - - it('should initialize x and y to default values when no parameters are provided', () => { - - const point= new Point(); - expect(point.x).toBe(0) - expect(point.y).toBe(0) - const point1 = new Point({x:5}); - expect(point1.x).toBe(5) - expect(point1.y).toBe(0) - const point2 = new Point({y:5}); - expect(point2.x).toBe(0) - expect(point2.y).toBe(5) - - }); - it('should throw error when x or y is not a number', () => { - const x = "abc"; - const y = "s"; - - expect(() => new Point({ x: x, y: 5 })).toThrow('x is not number'); - expect(() => new Point({ x: 5, y: y })).toThrow('y is not number'); - expect(() => new Point({ x: x, y: y })).toThrow('x and y are not number'); - }); - + + it('should throw error when x or y is not a number', () => { + const x = "abc"; + const y = "s"; + expect(() => new Point({ x: x, y: 5 })).toThrow('x is not number'); + expect(() => new Point({ x: 5, y: y })).toThrow('y is not number'); + expect(() => new Point({ x: x, y: y })).toThrow('x and y are not number'); + }); + it('should initialize x and y to default values when no parameters are provided', () => { + point = new Point({x:0,y:0}); + expect(point.x).toBe(0) + expect(point.y).toBe(0) + point = new Point({x:5}); + expect(point.x).toBe(5) + expect(point.y).toBe(0) + point = new Point({y:5}); + expect(point.x).toBe(0) + expect(point.y).toBe(5) + + }); + it('Point constructor initializes x and y correctly', () => { - const point = new Point({x:3,y: 4}); + point = new Point({x:3,y: 4}); expect(point.x).toBe(3); expect(point.y).toBe(4); }); + }) -describe('MOVEVERTICAL', () => { - it('should value is not define ', () => { - const point = new Point() - expect(()=>point.moveVertical()).toThrow('value is not define'); - }) - - it("should value is not a number",() => { - const point = new Point() - expect(()=>point.moveVertical("abc")).toThrow('value is not a number'); - }) - it('should move vertically correctly', () => { - const point=new Point({x:5,y:2}) - point.moveVertical(5) - expect(point.y).toBe(7) - - }) + +describe('MOVE_VERTICAL', () => { + + it('should value is not define ', () => { + point=mockConstructor(new Point()) + expect(()=>point.moveVertical()).toThrow('value is not define'); + }) + it("should value is not a number",() => { + + expect(()=>point.moveVertical("abc")).toThrow('value is not a number'); + }) + it('should update y correctly when moved vertically', () => { + point=mockConstructor(new Point({x:5,y:2})) + point.moveVertical(5) + expect(point.y).toBe(7) + }) + }) +describe('MOVE_HORIZONAL', () => { - describe('MOVEHORIZONAL', () => { - it('should value is not define ', () => { - const point = new Point() - expect(()=>point.moveHorizontal()).toThrow('value is not define'); - }) - - it("should value is not a number",() => { - const point = new Point() - expect(()=>point.moveHorizontal("abc")).toThrow('value is not a number'); - }) - - it('should move vertically correctly', () => { - const point=new Point({x:5,y:2}) - point.moveHorizontal(5) - expect(point.x).toBe(5+5) + + it('should value is not define ', () => { + point = mockConstructor(new Point()) + expect(()=>point.moveHorizontal()).toThrow('value is not define'); + }) + + it("should value is not a number",() => { + + expect(()=>point.moveHorizontal("abc")).toThrow('value is not a number'); + }) - }) - - -}) + it('should move vertically correctly', () => { + point=mockConstructor(new Point({x:5,y:2})) + point.moveHorizontal(5) + expect(point.x).toBe(5+5) + + }) + }) + diff --git a/tests/modules/geometry-calculation.test.js b/tests/modules/geometry-calculation.test.js index e54483f..8cc85ec 100644 --- a/tests/modules/geometry-calculation.test.js +++ b/tests/modules/geometry-calculation.test.js @@ -1,78 +1,104 @@ -// "calculates the distance between two points correctly" -const { Line } = require("../../modules/ecs6-class/line"); + +const { Line, getPointByX, getPointByY, } = require("../../modules/ecs6-class/line"); + const { Point } = require("../../modules/ecs6-class/point"); const { isPointOnLine, calculateDistance, calculateJunctionPoint } = require('../../modules/geometry-calculation') +const mockConstructorPoint = jest.fn(constructor); +const mockConstructorLine = jest.fn(constructor); +Line.prototype.getPointByX = jest.fn().mockImplementation((x) => { + return new Point({ x: 2, y: 12 }); +}); +Line.prototype.calculateSlope = jest.fn().mockImplementation(() => { + line.slope = 1; +}); + +Line.prototype.calculateNOfLineFunction = jest.fn().mockImplementation(() => { + line.n = 5; +}); -describe('CALCULATEDISTANCE', () => { + +let line +describe('CALCULATE_DISTANCE', () => { it(' should checks if the parameters are of the correct type', () => { expect(() => calculateDistance('ab', 'df')).toThrow('point1 or point2 is not point'); - expect(() => calculateDistance('ab', new Point())).toThrow('point1 or point2 is not point'); - expect(() => calculateDistance(new Point(), 'av')).toThrow('point1 or point2 is not point'); + expect(() => calculateDistance('ab', mockConstructorPoint(new Point()))).toThrow('point1 or point2 is not point'); + expect(() => calculateDistance(mockConstructorPoint(new Point(), 'av'))).toThrow('point1 or point2 is not point'); }) it(' should checks the distance between two lines if it correct', () => { - const line = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 9 }), slope: 1, n: 5 }) + line = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: 1, n: 5 })) const distance = calculateDistance(line.point1, line.point2) expect(distance).toBe(5) }) }) - describe('CALCULATEJUNCTIONPOINT', () => { - it(' should checks if the parameters are of the correct type', () => { - expect(() => calculateJunctionPoint('ab', 'df')).toThrow('line1 or line2 is not a line'); - expect(() => calculateJunctionPoint('ab', new Point())).toThrow('line1 or line2 is not a line'); - expect(() => calculateJunctionPoint(new Point(), 'av')).toThrow('line1 or line2 is not a line'); - }) - it('should return true for specific input', () => { - - const line = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 9 }), slope: 1, n: 5 }) - const line1 = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 5 }), slope: 1, n: 5 }) - const result = calculateJunctionPoint(line, line1) - expect(result).toBeTruthy(); - }); - it('should return false for specific input', () => { - - const line = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 9 }), slope: 1, n: 5 }) - const line1 = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 5 }), slope: 1, n: 6 }) - const result = calculateJunctionPoint(line, line1) - - expect(result).toBeFalsy(); - }); - // it(' should check that the returned value is of type point', () => { - // const line = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 9 }), slope: 1, n: 10 }) - // const line1 = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 5 }), slope: 5, n: 2 }) - // const result = calculateJunctionPoint(line, line1) - // expect(result).toBeInstanceOf(Point) - - // }) - it(' should calculateJunctionPoint return the correct junction point for the given lines', () => { - - const line = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 9 }), slope: 1, n: 10 }) - const line1 = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 5 }), slope: 5, n: 2 }) - const result = calculateJunctionPoint(line, line1) - expect(result).toEqual(new Point({ x: 2, y: 12 })); - - }); +describe('CALCULATE_JUNCTION_POINT', () => { + + + it(' should checks if the line1 and line2 are of the correct type', () => { + expect(() => calculateJunctionPoint('ab', 'df')).toThrow('line1 and line2 is not a line'); + }) + + it(' should checks if the line1 is of the correct type', () => { + expect(() => calculateJunctionPoint('ab', mockConstructorLine(new Line({})))).toThrow('line1 is not a line'); + }) + + it(' should checks if the line2 is of the correct type', () => { + expect(() => calculateJunctionPoint(mockConstructorLine(new Line({})), 'av')).toThrow('line2 is not a line'); + }) + it('should return true for specific input', () => { + + line = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: 1, n: 5 })) + const line1 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 5 })), slope: 1, n: 5 })) + const result = calculateJunctionPoint(line, line1) + expect(result).toBeTruthy(); + }); + it('should return false for specific input', () => { + + line = mockConstructorLine(new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 9 }), slope: 1, n: 5 })) + const line1 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 5 })), slope: 1, n: 6 })) + const result = calculateJunctionPoint(line, line1) + expect(result).toBeFalsy(); + }); + it(' should calculateJunctionPoint return the correct junction point for the given lines', () => { + line = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: 1, n: 10 })) + const line1 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 5 })), slope: 5, n: 2 })) + const result = calculateJunctionPoint(line, line1) + expect(result).toEqual(mockConstructorPoint(new Point({ x: 2, y: 12 }))); + + }); + + +}) +describe('IS_POINT_ON_LINE', () => { + let point + it(' should checks if the parameters are of the correct type', () => { + line = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: 1, n: 10 })) + point = mockConstructorPoint(new Point({ x: 4, y: 6 })) + expect(() => isPointOnLine("k", "j")).toThrow('line is not point or point is not a line'); + expect(() => isPointOnLine(line, "abc")).toThrow('line is not point or point is not a line'); + expect(() => isPointOnLine("c", point)).toThrow('line is not point or point is not a line'); + }) + it('should Point lies isnt on a line with slope and n values', () => { + line = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: 2, n: -6 })) + point = mockConstructorPoint(new Point({ x: 5, y: 4 })) + const result = isPointOnLine(line, point) + expect(result).toBeFalsy(); }) - - it(' should checks if the parameters are of the correct type', () => { - const line = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 9 }), slope: 1, n: 10 }) - const point = new Point({ x: 4, y: 6 }) - expect(() => isPointOnLine("k", "j")).toThrow('line is not point or point is not a line'); - expect(() => isPointOnLine(line, "abc")).toThrow('line is not point or point is not a line'); - expect(() => isPointOnLine("c", point)).toThrow('line is not point or point is not a line'); - }) - it('should Point lies isnt on a line with slope and n values', () => { - const line = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 9 }), slope: 2, n:-6 }) - const point=new Point({ x: 5, y: 4 }) - const result=isPointOnLine(line,point) - expect(result).toBeFalsy(); - }) - // it('should Point lies on a line with slope and n values', () => { - // const line = new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 9 }), slope: -2, n:14 }) - // const point=new Point({ x: 5, y: 4 }) - // const result=isPointOnLine(line,point) - // expect(result).toBeTruthy(); - // }) + it('should Point lies isnt on a line with slope and n values', () => { + line = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: -2, n: -6 })) + point = mockConstructorPoint(new Point({ x: 5, y: 4 })) + const result = isPointOnLine(line, point) + expect(result).toBeFalsy(); + }) + it('should Point lies on a line with slope and n values', () => { + line =mockConstructorLine( new Line({ point1:mockConstructorPoint( new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: -2, n: 14 })) + point = mockConstructorPoint(new Point({ x: 5, y: 4 })) + const result = isPointOnLine(line, point) + expect(result).toBeTruthy(); + }) +}) + + From abca3419b65d86f54fec0efba60a213c07f56efa Mon Sep 17 00:00:00 2001 From: gitty markowitch Date: Sun, 21 Jul 2024 15:04:09 +0300 Subject: [PATCH 3/5] add mocks --- tests/modules/ecs6-class/line.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/modules/ecs6-class/line.test.js b/tests/modules/ecs6-class/line.test.js index ec9d2b8..c59cb8f 100644 --- a/tests/modules/ecs6-class/line.test.js +++ b/tests/modules/ecs6-class/line.test.js @@ -84,7 +84,7 @@ describe('CALCULATE_N_OF_LINE_FUNCTION', () => { expect(line.n).toBe(3); }) - + }) describe('GET_POINT_BY_X', () => { From 076f6ac8db9205438719f4d850fba892d8bf0f65 Mon Sep 17 00:00:00 2001 From: gitty markowitch Date: Mon, 22 Jul 2024 19:55:03 +0300 Subject: [PATCH 4/5] fix the tests --- modules/ecs6-class/line.js | 20 ++-- modules/ecs6-class/point.js | 9 +- modules/geometry-calculation.js | 26 +++- package.json | 3 - tests/modules/ecs6-class/line.test.js | 89 +++++++------- tests/modules/ecs6-class/point.test.js | 102 ++++++++-------- tests/modules/geometry-calculation.test.js | 133 ++++++++++++++++----- 7 files changed, 233 insertions(+), 149 deletions(-) diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index 90c0acf..b14da39 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -1,4 +1,4 @@ -const { Point } = require("./point"); +const Point = require("./point"); class Line { constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) { @@ -18,10 +18,11 @@ class Line { calculateSlope = () => { if(this.point1.x === this.point2.x&&this.point1.y === this.point2.y){ - throw new Error('The point1 is equal to the point2 so it is not a line') + throw new Error('Both points have the same values so it is not a line' + ) } if(this.point1.x === this.point2.x){ - throw new Error('The x of point1 is equal to the x of point2 so it is not a line') + throw new Error('The x of point1 is equal to the x of point2 so this line is not function') } this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) @@ -66,13 +67,6 @@ class Line { } } -module.exports = { - Line, - calculateSlope: Line.prototype.calculateSlope, - calculateNOfLineFunction: Line.prototype.calculateNOfLineFunction, - getPointOnXAsis: Line.prototype.getPointOnXAsis, - getPointOnYAsis: Line.prototype.getPointOnYAsis, - getPointByX: Line.prototype.getPointByX, - getPointByY: Line.prototype.getPointByY - -}; \ No newline at end of file +module.exports = Line + + diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index fbe5518..b175ea6 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -33,8 +33,7 @@ class Point { -module.exports = { - Point:Point, - moveVertical: Point.prototype.moveVertical, - moveHorizontal: Point.prototype.moveHorizontal - }; \ No newline at end of file +module.exports = Point + + + \ No newline at end of file diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 3342bcf..c35e51a 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -1,6 +1,6 @@ -const { Line } = require("./ecs6-class/line"); -const { Point } = require("./ecs6-class/point"); +const Line = require("./ecs6-class/line"); +const Point = require("./ecs6-class/point"); const calculateDistance = (point1, point2) => { if (!(point1 instanceof Point) || !(point2 instanceof Point)) { @@ -23,8 +23,20 @@ const calculateJunctionPoint = (line1, line2) => { if (!(line2 instanceof Line)) { throw Error('line2 is not a line'); } - + 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 } @@ -47,9 +59,15 @@ const isPointOnLine = (line, point) => { const proxyLine = new Line({ point1: line.point1, point2: point }); proxyLine.calculateSlope(); - + if (!line.slope) { + line.calculateSlope() + } + if(!line.n){ + line.calculateNOfLineFunction() + } if (line.slope === proxyLine.slope) { proxyLine.calculateNOfLineFunction(); + if (line.n === proxyLine.n) { return true; } diff --git a/package.json b/package.json index d43de7c..6935a4b 100644 --- a/package.json +++ b/package.json @@ -7,9 +7,6 @@ "test": "jest", "test:coverage": "npm run test -- --coverage" }, - "dependencies": { - "jest": "^29.7.0" - }, "repository": { "type": "git", "url": "git+https://github.com/gemtechd/build-tests.git" diff --git a/tests/modules/ecs6-class/line.test.js b/tests/modules/ecs6-class/line.test.js index c59cb8f..f3e0b2b 100644 --- a/tests/modules/ecs6-class/line.test.js +++ b/tests/modules/ecs6-class/line.test.js @@ -1,36 +1,43 @@ -const { Line, calculateSlope, calculateNOfLineFunction, getPointOnXAsis, getPointOnYAsis, getPointByX, getPointByY } = require('../../../modules/ecs6-class/line'); -const { Point } = require('../../../modules/ecs6-class/point') -const mockConstructorPoint = jest.fn(constructor); -const mockConstructorLine = jest.fn(constructor); +const Line = require('../../../modules/ecs6-class/line'); +const Point = require('../../../modules/ecs6-class/point') +const mockConstructor = jest.fn(constructor); let line describe('CONSTRUCTORE', () => { }) -it('should initialize to default values when no parameters are provided', () => { - line = new Line({ point1: mockConstructorPoint(new Point({ x: 1, y: 5 })) }); - expect(line.point1).toStrictEqual(mockConstructorPoint(new Point({ x: 1, y: 5 }))) - expect(line.point2).toStrictEqual(mockConstructorPoint(new Point())) +it('should initialize point2, n and slope to default values when no parameters are provided', () => { + line = new Line({ point1: mockConstructor(new Point({ x: 1, y: 5 })) }); + expect(line.point1).toStrictEqual(mockConstructor(new Point({ x: 1, y: 5 }))) + expect(line.point2).toStrictEqual(mockConstructor(new Point())) expect(line.n).toBe(undefined) expect(line.slope).toBe(undefined) - line = new Line({ point2: mockConstructorPoint(new Point({ x: 1, y: 5 })) }); - expect(line.point1).toStrictEqual(mockConstructorPoint(new Point())) - expect(line.point2).toStrictEqual(mockConstructorPoint(new Point({ x: 1, y: 5 }))) +}) +it('should initialize point1, n and value to default values when no parameters are provided', () => { + line = new Line({ point2: mockConstructor(new Point({ x: 1, y: 5 })) }); + expect(line.point1).toStrictEqual(mockConstructor(new Point())) + expect(line.point2).toStrictEqual(mockConstructor(new Point({ x: 1, y: 5 }))) expect(line.n).toBe(undefined) expect(line.slope).toBe(undefined) +}) +it('should initialize point1, point2 and slope to default values when no parameters are provided', () => { line = new Line({ n: 5 }); - expect(line.point1).toStrictEqual(mockConstructorPoint(new Point())) - expect(line.point2).toStrictEqual(mockConstructorPoint(new Point())) + expect(line.point1).toStrictEqual(mockConstructor(new Point())) + expect(line.point2).toStrictEqual(mockConstructor(new Point())) expect(line.n).toBe(5) expect(line.slope).toBe(undefined) +}) +it('should initialize point1, point2 and n to default values when no parameters are provided', () => { line = new Line({ slope: 5 }); - expect(line.point1).toStrictEqual(mockConstructorPoint(new Point())) - expect(line.point2).toStrictEqual(mockConstructorPoint(new Point())) + expect(line.point1).toStrictEqual(mockConstructor(new Point())) + expect(line.point2).toStrictEqual(mockConstructor(new Point())) expect(line.n).toBe(undefined) expect(line.slope).toBe(5) +}) +it('should initialize to default values when no parameters are provided', () => { line = new Line({}) - expect(line.point1).toStrictEqual(mockConstructorLine(new Point())) - expect(line.point2).toStrictEqual(mockConstructorPoint(new Point())) + expect(line.point1).toStrictEqual(mockConstructor(new Point())) + expect(line.point2).toStrictEqual(mockConstructor(new Point())) expect(line.n).toBe(undefined) expect(line.slope).toBe(undefined) @@ -38,14 +45,14 @@ it('should initialize to default values when no parameters are provided', () => it('Checks if the parameters are of the correct type', () => { expect(() => new Line({ point1: "ab", point2: new Point({ x: 5, y: 5 }), slope: 1, n: 5 })).toThrow('point1 is not point'); - expect(() => new Line({ point1:mockConstructorPoint( new Point({ x: 5, y: 5 })), point2: "ab", slope: 1, n: 5 })).toThrow('point2 is not point'); - expect(() => new Line({ point1: new Point({ x: 5, y: 5 }), point2:mockConstructorPoint( new Point({ x: 5, y: 5 })), slope: "d", n: 5 })).toThrow('slope is not number'); - expect(() => new Line({ point1: new Point({ x: 5, y: 5 }), point2: mockConstructorPoint(new Point({ x: 5, y: 5 })), slope: 1, n: "d" })).toThrow('n is not number'); + expect(() => new Line({ point1: mockConstructor(new Point({ x: 5, y: 5 })), point2: "ab", slope: 1, n: 5 })).toThrow('point2 is not point'); + expect(() => new Line({ point1: new Point({ x: 5, y: 5 }), point2: mockConstructor(new Point({ x: 5, y: 5 })), slope: "d", n: 5 })).toThrow('slope is not number'); + expect(() => new Line({ point1: new Point({ x: 5, y: 5 }), point2: mockConstructor(new Point({ x: 5, y: 5 })), slope: 1, n: "d" })).toThrow('n is not number'); }) it('Point constructor initializes point1 and point2 and slope and n correctly', () => { - line = new Line({ point1:mockConstructorPoint( new Point({ x: 5, y: 8 })), point2:mockConstructorPoint( new Point({ x: 5, y: 8 })), slope: 5, n: 8 }); - expect(line.point1).toEqual(mockConstructorPoint(new Point({ x: 5, y: 8 }))); - expect(line.point2).toEqual(mockConstructorPoint(new Point({ x: 5, y: 8 }))); + line = new Line({ point1: mockConstructor(new Point({ x: 5, y: 8 })), point2: mockConstructor(new Point({ x: 5, y: 8 })), slope: 5, n: 8 }); + expect(line.point1).toEqual(mockConstructor(new Point({ x: 5, y: 8 }))); + expect(line.point2).toEqual(mockConstructor(new Point({ x: 5, y: 8 }))); expect(line.slope).toBe(5); expect(line.n).toBe(8); @@ -53,24 +60,24 @@ it('Point constructor initializes point1 and point2 and slope and n correctly', describe('CALCULATE_SLOPE', () => { it('should The slope is 0 as the line is vertical (point1= point2) ', () => { - line = mockConstructorLine(new Line(({ point1: mockConstructorPoint(new Point({ x: 5, y: 8 })), point2: mockConstructorPoint(new Point({ x: 5, y: 8 })), n: 8 }))) - expect(() => line.calculateSlope()).toThrow('The point1 is equal to the point2 so it is not a line'); + line = mockConstructor(new Line(({ point1: mockConstructor(new Point({ x: 5, y: 8 })), point2: mockConstructor(new Point({ x: 5, y: 8 })), n: 8 }))) + expect(() => line.calculateSlope()).toThrow('Both points have the same values so it is not a line'); }) it('should The slope is undefined as the line is vertical (y1 = y2) ', () => { - line = mockConstructorLine(new Line(({ point1: mockConstructorPoint(new Point({ x: 5, y: 8 })), point2: mockConstructorPoint(new Point({ x: 3, y: 8 })), slope: 5, n: 8 }))) + line = mockConstructor(new Line(({ point1: mockConstructor(new Point({ x: 5, y: 8 })), point2: mockConstructor(new Point({ x: 3, y: 8 })), slope: 5, n: 8 }))) line.calculateSlope() expect(line.slope).toBeCloseTo(0); }) it('should The slope is undefined as the line is vertical (x1=x2)', () => { - line = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 5, y: 9 })), point2: mockConstructorPoint(new Point({ x: 5, y: 8 })), n: 8 })); - expect(() => line.calculateSlope()).toThrow('The x of point1 is equal to the x of point2 so it is not a line'); + line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 5, y: 9 })), point2: mockConstructor(new Point({ x: 5, y: 8 })), n: 8 })); + expect(() => line.calculateSlope()).toThrow('The x of point1 is equal to the x of point2 so this line is not function'); }); it('should move vertically correctly', () => { - line = mockConstructorLine(new Line(({ point1: mockConstructorPoint(new Point({ x: -1, y: 8 })), point2: mockConstructorPoint(new Point({ x: 2, y: 5 })), slope: 5, n: 8 }))); + line = mockConstructor(new Line(({ point1: mockConstructor(new Point({ x: -1, y: 8 })), point2: mockConstructor(new Point({ x: 2, y: 5 })), slope: 5, n: 8 }))); line.calculateSlope() expect(line.slope).toBe(-1); @@ -79,33 +86,33 @@ describe('CALCULATE_SLOPE', () => { }) describe('CALCULATE_N_OF_LINE_FUNCTION', () => { it(' should move vertically correctly', () => { - line = mockConstructorLine(new Line(({ point1: mockConstructorPoint(new Point({ x: 5, y: 8 })), point2: mockConstructorPoint(new Point({ x: 5, y: 5 })), slope: 1, n: 8 }))); + line = mockConstructor(new Line(({ point1: mockConstructor(new Point({ x: 5, y: 8 })), point2: mockConstructor(new Point({ x: 5, y: 5 })), slope: 1, n: 8 }))); line.calculateNOfLineFunction() expect(line.n).toBe(3); }) - + }) describe('GET_POINT_BY_X', () => { it('should x is not define ', () => { - const line = new Line({}) + const line = mockConstructor(new Line({})) expect(() => line.getPointByX()).toThrow('x is not a defined'); }) it('should x is not number ', () => { - const line = new Line({}) + const line = mockConstructor(new Line({})) expect(() => line.getPointByX("abc")).toThrow('x is not a number'); }) it(' should check that the returned value is of type point', () => { - const line = new Line({}) + const line = mockConstructor(new Line({})) const point = line.getPointByX(5) expect(point).toBeInstanceOf(Point) }) it('should checks that point is returned with correct values', () => { - const line = new Line(({ point1: new Point({ x: 5, y: 8 }), point2: new Point({ x: 5, y: 5 }), slope: 1, n: 8 })) + const line = mockConstructor(new Line(({ point1: new Point({ x: 5, y: 8 }), point2: new Point({ x: 5, y: 5 }), slope: 1, n: 8 }))) initialx = line.point1.x const point = line.getPointByX(5) expect(point.x).toBe(initialx) @@ -114,24 +121,24 @@ describe('GET_POINT_BY_X', () => { }) describe('GET_POINT_B_Y', () => { it('should y is not define ', () => { - const line = new Line({}) + const line = mockConstructor(new Line({})) expect(() => line.getPointByY()).toThrow('y is not defined'); }) }) it('should y is not number ', () => { - const line = new Line({}) + const line = mockConstructor(new Line({})) expect(() => line.getPointByY("abc")).toThrow('y is not number'); }) it(' should check that the returned value is of type point', () => { - const line = new Line({}) + const line = mockConstructor(new Line({})) const point = line.getPointByY(5) expect(point).toBeInstanceOf(Point) }) it('should checks that point is returned with correct values', () => { - const line = new Line(({ point1: new Point({ x: 8, y: 6 }), point2: new Point({ x: 5, y: 5 }), slope: 1, n: 3 })) + const line = mockConstructor(new Line(({ point1: mockConstructor(new Point({ x: 8, y: 6 })), point2: mockConstructor(new Point({ x: 5, y: 5 })), slope: 1, n: 3 }))) initialy = line.point1.y const point = line.getPointByY(6) expect(point.x).toBe(3) @@ -142,7 +149,7 @@ describe('GET_POINT_ON_X_ASIS', () => { it('returns the correct point on the y-axis for the given slope and n value', () => { - const line = new Line(({ slope: 2, n: -6 })) + const line = mockConstructor(new Line(({ slope: 2, n: -6 }))) const point = line.getPointOnXAsis() expect(point.y).toBe(0) expect(point.x).toBe(3) @@ -153,7 +160,7 @@ describe('GET_POINT_ON_X_ASIS', () => { }) describe('GET_POINT_ON_Y_ASIS', () => { it('returns the correct point on the y-axis for the given slope and n value', () => { - const line = new Line({ slope: 5, n: 5 }) + const line = mockConstructor(new Line({ slope: 5, n: 5 })) const point = line.getPointOnYAsis() expect(point.y).toBe(5) expect(point.x).toBe(0) diff --git a/tests/modules/ecs6-class/point.test.js b/tests/modules/ecs6-class/point.test.js index 2d366bb..43b538a 100644 --- a/tests/modules/ecs6-class/point.test.js +++ b/tests/modules/ecs6-class/point.test.js @@ -1,74 +1,76 @@ -const {Point,moveHorizontal, moveVertical}=require('../../../modules/ecs6-class/point') +const Point = require('../../../modules/ecs6-class/point') const mockConstructor = jest.fn(constructor); let point describe('CONSTRUCTORE', () => { - + it('should throw error when x or y is not a number', () => { - const x = "abc"; - const y = "s"; - expect(() => new Point({ x: x, y: 5 })).toThrow('x is not number'); - expect(() => new Point({ x: 5, y: y })).toThrow('y is not number'); - expect(() => new Point({ x: x, y: y })).toThrow('x and y are not number'); - }); - it('should initialize x and y to default values when no parameters are provided', () => { - point = new Point({x:0,y:0}); - expect(point.x).toBe(0) - expect(point.y).toBe(0) - point = new Point({x:5}); - expect(point.x).toBe(5) - expect(point.y).toBe(0) - point = new Point({y:5}); - expect(point.x).toBe(0) - expect(point.y).toBe(5) - - }); - - it('Point constructor initializes x and y correctly', () => { - point = new Point({x:3,y: 4}); - expect(point.x).toBe(3); - expect(point.y).toBe(4); - }); - + const x = "abc"; + const y = "s"; + expect(() => new Point({ x: x, y: 5 })).toThrow('x is not number'); + expect(() => new Point({ x: 5, y: y })).toThrow('y is not number'); + expect(() => new Point({ x: x, y: y })).toThrow('x and y are not number'); + }); + it('should initialize x and y to default values when no parameters are provided', () => { + point = new Point({ x: 0, y: 0 }); + expect(point.x).toBe(0) + expect(point.y).toBe(0)}) + it('should initialize y to default value when no parameters are provided', () => { + point = new Point({ x: 5 }); + expect(point.x).toBe(5) + expect(point.y).toBe(0)}) + it('should initialize x to default value when no parameters are provided', () => { + point = new Point({ y: 5 }); + expect(point.x).toBe(0) + expect(point.y).toBe(5) + + }); + + it('Point constructor initializes x and y correctly', () => { + point = new Point({ x: 3, y: 4 }); + expect(point.x).toBe(3); + expect(point.y).toBe(4); + }); + }) describe('MOVE_VERTICAL', () => { it('should value is not define ', () => { - point=mockConstructor(new Point()) - expect(()=>point.moveVertical()).toThrow('value is not define'); + point = mockConstructor(new Point()) + expect(() => point.moveVertical()).toThrow('value is not define'); }) - it("should value is not a number",() => { - - expect(()=>point.moveVertical("abc")).toThrow('value is not a number'); + it("should value is not a number", () => { + + expect(() => point.moveVertical("abc")).toThrow('value is not a number'); }) it('should update y correctly when moved vertically', () => { - point=mockConstructor(new Point({x:5,y:2})) + point = mockConstructor(new Point({ x: 5, y: 2 })) point.moveVertical(5) expect(point.y).toBe(7) }) - + }) describe('MOVE_HORIZONAL', () => { it('should value is not define ', () => { - point = mockConstructor(new Point()) - expect(()=>point.moveHorizontal()).toThrow('value is not define'); - }) - - it("should value is not a number",() => { - - expect(()=>point.moveHorizontal("abc")).toThrow('value is not a number'); - }) - - it('should move vertically correctly', () => { - point=mockConstructor(new Point({x:5,y:2})) - point.moveHorizontal(5) - expect(point.x).toBe(5+5) - - }) - }) + point = mockConstructor(new Point()) + expect(() => point.moveHorizontal()).toThrow('value is not define'); + }) + + it("should value is not a number", () => { + + expect(() => point.moveHorizontal("abc")).toThrow('value is not a number'); + }) + + it('should move vertically correctly', () => { + point = mockConstructor(new Point({ x: 5, y: 2 })) + point.moveHorizontal(5) + expect(point.x).toBe(5 + 5) + + }) +}) diff --git a/tests/modules/geometry-calculation.test.js b/tests/modules/geometry-calculation.test.js index 8cc85ec..8cc5f23 100644 --- a/tests/modules/geometry-calculation.test.js +++ b/tests/modules/geometry-calculation.test.js @@ -1,11 +1,9 @@ -const { Line, getPointByX, getPointByY, } = require("../../modules/ecs6-class/line"); - -const { Point } = require("../../modules/ecs6-class/point"); +const Line = require("../../modules/ecs6-class/line"); +const Point = require("../../modules/ecs6-class/point"); const { isPointOnLine, calculateDistance, calculateJunctionPoint } = require('../../modules/geometry-calculation') -const mockConstructorPoint = jest.fn(constructor); -const mockConstructorLine = jest.fn(constructor); +const mockConstructor = jest.fn(constructor); Line.prototype.getPointByX = jest.fn().mockImplementation((x) => { return new Point({ x: 2, y: 12 }); }); @@ -14,7 +12,7 @@ Line.prototype.calculateSlope = jest.fn().mockImplementation(() => { }); Line.prototype.calculateNOfLineFunction = jest.fn().mockImplementation(() => { - line.n = 5; + line.n = 5; }); @@ -22,12 +20,12 @@ let line describe('CALCULATE_DISTANCE', () => { it(' should checks if the parameters are of the correct type', () => { expect(() => calculateDistance('ab', 'df')).toThrow('point1 or point2 is not point'); - expect(() => calculateDistance('ab', mockConstructorPoint(new Point()))).toThrow('point1 or point2 is not point'); - expect(() => calculateDistance(mockConstructorPoint(new Point(), 'av'))).toThrow('point1 or point2 is not point'); + expect(() => calculateDistance('ab', mockConstructor(new Point()))).toThrow('point1 or point2 is not point'); + expect(() => calculateDistance(mockConstructor(new Point(), 'av'))).toThrow('point1 or point2 is not point'); }) it(' should checks the distance between two lines if it correct', () => { - line = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: 1, n: 5 })) + line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 4, y: 6 })), point2: mockConstructor(new Point({ x: 8, y: 9 })), slope: 1, n: 5 })) const distance = calculateDistance(line.point1, line.point2) expect(distance).toBe(5) }) @@ -38,35 +36,85 @@ describe('CALCULATE_JUNCTION_POINT', () => { it(' should checks if the line1 and line2 are of the correct type', () => { expect(() => calculateJunctionPoint('ab', 'df')).toThrow('line1 and line2 is not a line'); }) - + it(' should checks if the line1 is of the correct type', () => { - expect(() => calculateJunctionPoint('ab', mockConstructorLine(new Line({})))).toThrow('line1 is not a line'); + expect(() => calculateJunctionPoint('ab', mockConstructor(new Line({})))).toThrow('line1 is not a line'); }) - + it(' should checks if the line2 is of the correct type', () => { - expect(() => calculateJunctionPoint(mockConstructorLine(new Line({})), 'av')).toThrow('line2 is not a line'); + expect(() => calculateJunctionPoint(mockConstructor(new Line({})), 'av')).toThrow('line2 is not a line'); }) + it('should calculate the slope value for line1 and then proceed with the calculation', () => { + + line = mockConstructor(new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 7, y: 9 }), n: 5 })) + const line1 = mockConstructor(new Line({ slope: 1, n: 4 })) + const result = calculateJunctionPoint(line, line1) + expect(result).toBe(false); + }); + it('should calculate the slope value for line2 and then proceed with the calculation', () => { + + line = mockConstructor(new Line({ slope: 1, n: 4 })) + const line1 = mockConstructor(new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 7, y: 9 }), n: 5 })) + const result = calculateJunctionPoint(line, line1) + expect(result).toBe(false); + }); + it('should calculate the slope value for line and line2 and then proceed with the calculation', () => { + + line = mockConstructor(new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 7, y: 9 }), n: 5 })) + const line1 = mockConstructor(new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 7, y: 9 }), n: 4 })) + const result = calculateJunctionPoint(line, line1) + expect(result).toBe(false); + }); + // ----------------- + it('should calculate the n value for line1 and then proceed with the calculation', () => { + + line = mockConstructor(new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 7, y: 9 }), slope: 1 })) + const line1 = mockConstructor(new Line({ slope: 1, n: 4 })) + const result = calculateJunctionPoint(line, line1) + expect(result).toBe(false); + }); + it('should calculate the n value for line2 and then proceed with the calculation', () => { + + line = mockConstructor(new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 7, y: 9 }), slope: 1, n: 5 })) + const line1 = mockConstructor(new Line({ slope: 1 })) + const result = calculateJunctionPoint(line, line1) + expect(result).toBe(false); + }); + it('should calculate the n value for line1 and line2 and then proceed with the calculation', () => { + + line = mockConstructor(new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 7, y: 9 }), slope: 1 })) + const line1 = mockConstructor(new Line({ slope: 1 })) + const result = calculateJunctionPoint(line, line1) + expect(result).toBe(false); + }); + + + + + + it('should return true for specific input', () => { - line = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: 1, n: 5 })) - const line1 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 5 })), slope: 1, n: 5 })) + line = mockConstructor(new Line({ slope: 1, n: 5 })) + const line1 = mockConstructor(new Line({ slope: 1, n: 5 })) const result = calculateJunctionPoint(line, line1) - expect(result).toBeTruthy(); + expect(result).toBe(true); }); + it('should return false for specific input', () => { - line = mockConstructorLine(new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 9 }), slope: 1, n: 5 })) - const line1 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 5 })), slope: 1, n: 6 })) + line = mockConstructor(new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 8, y: 9 }), slope: 1, n: 5 })) + const line1 = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 4, y: 6 })), point2: mockConstructor(new Point({ x: 8, y: 5 })), slope: 1, n: 6 })) const result = calculateJunctionPoint(line, line1) - expect(result).toBeFalsy(); + expect(result).toBe(false); }); it(' should calculateJunctionPoint return the correct junction point for the given lines', () => { - line = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: 1, n: 10 })) - const line1 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 5 })), slope: 5, n: 2 })) + line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 4, y: 6 })), point2: mockConstructor(new Point({ x: 8, y: 9 })), slope: 1, n: 10 })) + const line1 = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 4, y: 6 })), point2: mockConstructor(new Point({ x: 8, y: 5 })), slope: 5, n: 2 })) const result = calculateJunctionPoint(line, line1) - expect(result).toEqual(mockConstructorPoint(new Point({ x: 2, y: 12 }))); + expect(result).toEqual(mockConstructor(new Point({ x: 2, y: 12 }))); }); @@ -75,29 +123,48 @@ describe('CALCULATE_JUNCTION_POINT', () => { describe('IS_POINT_ON_LINE', () => { let point it(' should checks if the parameters are of the correct type', () => { - line = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: 1, n: 10 })) - point = mockConstructorPoint(new Point({ x: 4, y: 6 })) + line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 4, y: 6 })), point2: mockConstructor(new Point({ x: 8, y: 9 })), slope: 1, n: 10 })) + point = mockConstructor(new Point({ x: 4, y: 6 })) expect(() => isPointOnLine("k", "j")).toThrow('line is not point or point is not a line'); expect(() => isPointOnLine(line, "abc")).toThrow('line is not point or point is not a line'); expect(() => isPointOnLine("c", point)).toThrow('line is not point or point is not a line'); }) + it('should calculate the slope of the line when it is missing and then proceed to check if the point lies on the line', () => { + line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 4, y: 6 })), point2: mockConstructor(new Point({ x: 8, y: 9 })), n: -6 })) + point = mockConstructor(new Point({ x: 5, y: 4 })) + const result = isPointOnLine(line, point) + expect(result).toBe(false); + }) + it('should calculate the n of the line when it is missing and then proceed to check if the point lies on the line', () => { + line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 4, y: 6 })), point2: mockConstructor(new Point({ x: 8, y: 9 })), slope: 1 })) + point = mockConstructor(new Point({ x: 5, y: 4 })) + const result = isPointOnLine(line, point) + expect(result).toBe(false); + }) + + it('should Point lies isnt on a line with slope and n values', () => { + line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 4, y: 6 })), point2: mockConstructor(new Point({ x: 8, y: 9 })), slope: 2, })) + point = mockConstructor(new Point({ x: 5, y: 4 })) + const result = isPointOnLine(line, point) + expect(result).toBe(false); + }) it('should Point lies isnt on a line with slope and n values', () => { - line = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: 2, n: -6 })) - point = mockConstructorPoint(new Point({ x: 5, y: 4 })) + line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 4, y: 6 })), point2: mockConstructor(new Point({ x: 8, y: 9 })), slope: 2, n: -6 })) + point = mockConstructor(new Point({ x: 5, y: 4 })) const result = isPointOnLine(line, point) - expect(result).toBeFalsy(); + expect(result).toBe(false); }) it('should Point lies isnt on a line with slope and n values', () => { - line = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: -2, n: -6 })) - point = mockConstructorPoint(new Point({ x: 5, y: 4 })) + line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 4, y: 6 })), point2: mockConstructor(new Point({ x: 8, y: 9 })), slope: -2, n: -6 })) + point = mockConstructor(new Point({ x: 5, y: 4 })) const result = isPointOnLine(line, point) - expect(result).toBeFalsy(); + expect(result).toBe(false); }) it('should Point lies on a line with slope and n values', () => { - line =mockConstructorLine( new Line({ point1:mockConstructorPoint( new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: -2, n: 14 })) - point = mockConstructorPoint(new Point({ x: 5, y: 4 })) + line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 4, y: 6 })), point2: mockConstructor(new Point({ x: 8, y: 9 })), slope: -2, n: 14 })) + point = mockConstructor(new Point({ x: 5, y: 4 })) const result = isPointOnLine(line, point) - expect(result).toBeTruthy(); + expect(result).toBe(true); }) }) From 3cf7cd06b389cbd6096f23b8f79477e4fcc03014 Mon Sep 17 00:00:00 2001 From: gitty markowitch Date: Mon, 22 Jul 2024 19:57:40 +0300 Subject: [PATCH 5/5] remove comment --- tests/modules/ecs6-class/point.test.js | 1 - tests/modules/geometry-calculation.test.js | 6 ------ 2 files changed, 7 deletions(-) diff --git a/tests/modules/ecs6-class/point.test.js b/tests/modules/ecs6-class/point.test.js index 43b538a..2f1ee9a 100644 --- a/tests/modules/ecs6-class/point.test.js +++ b/tests/modules/ecs6-class/point.test.js @@ -1,4 +1,3 @@ - const Point = require('../../../modules/ecs6-class/point') const mockConstructor = jest.fn(constructor); let point diff --git a/tests/modules/geometry-calculation.test.js b/tests/modules/geometry-calculation.test.js index 8cc5f23..f93c52f 100644 --- a/tests/modules/geometry-calculation.test.js +++ b/tests/modules/geometry-calculation.test.js @@ -65,7 +65,6 @@ describe('CALCULATE_JUNCTION_POINT', () => { const result = calculateJunctionPoint(line, line1) expect(result).toBe(false); }); - // ----------------- it('should calculate the n value for line1 and then proceed with the calculation', () => { line = mockConstructor(new Line({ point1: new Point({ x: 4, y: 6 }), point2: new Point({ x: 7, y: 9 }), slope: 1 })) @@ -88,11 +87,6 @@ describe('CALCULATE_JUNCTION_POINT', () => { expect(result).toBe(false); }); - - - - - it('should return true for specific input', () => { line = mockConstructor(new Line({ slope: 1, n: 5 }))