From f74f86f6198b107131d785efbe94857ccb677814 Mon Sep 17 00:00:00 2001 From: LeaG588 Date: Mon, 8 Jul 2024 17:58:22 +0300 Subject: [PATCH 1/2] finish tests in the project --- modules/ecs6-class/line.js | 9 ++- modules/ecs6-class/point.js | 10 ++- modules/geometry-calculation.js | 17 +++-- package.json | 10 ++- tests/modules/ecs6-class/line.test.js | 82 +++++++++++++++++++++++ tests/modules/ecs6-class/point.test.js | 56 ++++++++++++++++ tests/modules/geometry-calucation.test.js | 74 ++++++++++++++++++++ 7 files changed, 247 insertions(+), 11 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-calucation.test.js diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index b3bf0f7..0eda957 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -1,6 +1,7 @@ const Point = require("./point"); class Line { + constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) { this.point1 = point1; this.point2 = point2; @@ -9,6 +10,8 @@ class Line { } calculateSlope = () => { + if (this.point1.x === this.point2.x) + throw new Error("don't divide by 0") this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) } @@ -31,9 +34,11 @@ class Line { } getPointByY(y) { - let x = (y - this.slope) / this.n; + if (this.slope === 0) + throw new Error("don't divide by 0") + let x = (y - this.n) / this.slope; return new Point({ x, y }) } } -module.exports = Line \ No newline at end of file +module.exports = Line diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index e81b4a4..c02f9c1 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,14 +1,20 @@ class Point { - constructor({x=0, y=0}={}) { + constructor({ x = 0, y = 0 } = {}) { + if (!(typeof (x) === "number") || !(typeof (y) === "number")) + throw new Error('x and y must be of the number type') this.x = x; this.y = y; } moveVertical(value) { + if (!(typeof (value) === "number")) + throw new Error('value must be of the number type') this.y += value; } moveHorizontal(value) { + if (!(typeof (value) === "number")) + throw new Error('value must be of the number type') this.x += value; } } -module.exports = Point \ No newline at end of file +module.exports = Point diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 66856dc..655ee45 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -1,13 +1,19 @@ -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 new Error('Invalid input. Expected objects of type 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 new Error('Must be of the Line type') if (line1.slope === line2.slope) { if (line1.n === line2.n) { return true @@ -24,11 +30,14 @@ const calculateJunctionPoint = (line1, line2) => { } const isPointOnLine = (line, point) => { - const proxyLine = new Line(line.point1, point) + if (!(line instanceof Line) || !(point instanceof Point)) { + throw new Error("The objects should be of the Line and Point types") + } + const proxyLine = new Line({ point1: line.point1, point2: point })//0,y proxyLine.calculateSlope() if (line.slope === proxyLine.slope) { proxyLine.calculateNOfLineFunction() - if (line.n === proxyLine.n2) { + if (line.n === proxyLine.n) { return true } } diff --git a/package.json b/package.json index 56bf17b..0293951 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" @@ -18,6 +19,9 @@ }, "homepage": "https://github.com/gemtechd/build-tests#readme", "author": "gemtechd", - "license": "ISC" - + "license": "ISC", + "directories": { + "test": "tests" + }, + "devDependencies": {} } diff --git a/tests/modules/ecs6-class/line.test.js b/tests/modules/ecs6-class/line.test.js new file mode 100644 index 0000000..e8224f4 --- /dev/null +++ b/tests/modules/ecs6-class/line.test.js @@ -0,0 +1,82 @@ +const Line = require('../../../modules/ecs6-class/line') +const Point = require('../../../modules/ecs6-class/point') + +describe('LINE', () => { + describe('CONSTRUCTOR', () => { + it('should instantiate the class with the correct values', () => { + const l = new Line({ slope: 3, n: 7 }); + expect(l.slope).toBe(3); + expect(l.n).toBe(7); + }); + }), + describe('CALCULATE_N_Of_LINE_FUNCTION', () => { + + it('calculateNOfLineFunction calculates the correct value of n', () => { + const line = new Line({ point1: { x: 0, y: 0 }, point2: { x: 2, y: 2 } }); + line.calculateSlope(); + line.calculateNOfLineFunction(); + expect(line.n).toBe(0); + }); + }), + describe('CALCULATE_SLOPE', () => { + it('should return error if the point1.x - point2.x =0', () => { + const point1 = new Point({ x: 5, y: 9 }) + const point2 = new Point({ x: 5, y: 2 }) + const line = new Line({ point1: point1, point2: point2 }) + expect(() => line.calculateSlope()).toThrowError("don't divide by 0") + }) + it('should return if the function calculate slope between two points', () => { + const point1 = new Point({ x: 1, y: 2 }); + const point2 = new Point({ x: 3, y: 4 }); + const line = new Line({ point1, point2 }); + line.calculateSlope(); + expect(line.slope).toBe(1); + }); + }), + describe('GET_POINT_ON_Y_AXIS', () => { + it('Get Point on Y axis', () => { + const line = new Line({ slope: 1, n: 0 }); + const point = line.getPointOnYAsis(); + expect(point.x).toBe(0); + expect(point.y).toBe(0); + }); + }), + describe('GET_POINT_BY_Y', () => { + it('Get Point by Y', () => { + const line = new Line({ slope: 2, n: 3 }); + const expectedPoint = new Point({ x: 1, y: 5 }); + const point = line.getPointByY(5); + expect(point.x).toBe(expectedPoint.x); + expect(point.y).toBe(expectedPoint.y); + }); + it('should return error.message if the slope = 0', () => { + const line = new Line({ n: 1, slope: 0 }) + expect(() => line.getPointByY(5)).toThrowError("don't divide by 0") + }); + }), + describe('GET_POINT_BY_X', () => { + it('get point by x', () => { + const line = new Line({ slope: 1, n: 1 }) + const point = line.getPointByX(2) + expect(point.x).toBe(2) + expect(point.y).toBe(3) + }); + }), + + describe('GET_POINT_ON_X_AXIS', () => { + it('Get Point on X Axis', () => { + const line = new Line({ slope: 2, n: 3 }); + const expectedPoint = line.getPointByY(0); + const point = line.getPointOnXAsis(); + expect(point.x).toBe(expectedPoint.x); + expect(point.y).toBe(expectedPoint.y); + }); + }) + +}) + + + + + + diff --git a/tests/modules/ecs6-class/point.test.js b/tests/modules/ecs6-class/point.test.js new file mode 100644 index 0000000..70b5872 --- /dev/null +++ b/tests/modules/ecs6-class/point.test.js @@ -0,0 +1,56 @@ +const Point = require('../../../modules/ecs6-class/point') + +describe('POINT', () => { + describe('CONSTRUCTOR', () => { + it('Check if a point is created with default values', () => { + const point = new Point(); + expect(point.x).toBe(0); + expect(point.y).toBe(0); + }); + it('should return x=5 and y=6', () => { + const point = new Point({ x: 5, y: 6 }) + expect(point.x).toBe(5) + expect(point.y).toBe(6) + }) + it('should return error.message if the values is not number type', () => { + const invalidX = 'hello'; + const invalidY = true; + expect(() => new Point({ x: invalidX, y: invalidY })).toThrowError('x and y must be of the number type'); + }) + it('should return error.message if the values is not number type', () => { + const invalidX = 'hello'; + const invalidY = 5; + expect(() => new Point({ x: invalidX, y: invalidY })).toThrowError('x and y must be of the number type'); + }) + it('should return error.message if the values is not number type', () => { + const invalidX = 5; + const invalidY = 'hello'; + expect(() => new Point({ x: invalidX, y: invalidY })).toThrowError('x and y must be of the number type'); + }) + }), + describe('MOVE_VETRCAL', () => { + it('should return error.meaasge if the value is not number type', () => { + const point = new Point({ x: 1, y: 2 }); + const value = "3"; + expect(() => point.moveVertical(value)).toThrowError('value must be of the number type'); + }) + it('Move Point Vertically', () => { + const point = new Point({ x: 1, y: 2 }); + point.moveVertical(3); + expect(point.y).toBe(5); + }); + }), + describe('HORIZONTA', () => { + it('should return error.meaasge if the value is not number', () => { + const point = new Point({ x: 1, y: 2 }); + const value = "3"; + expect(() => point.moveHorizontal(value)).toThrowError('value must be of the number type'); + }) + it('Move Point Horizontally', () => { + const point = new Point({ x: 2, y: 3 }); + point.moveHorizontal(-1); + expect(point.x).toBe(1); + }); + + }) +}) diff --git a/tests/modules/geometry-calucation.test.js b/tests/modules/geometry-calucation.test.js new file mode 100644 index 0000000..2949757 --- /dev/null +++ b/tests/modules/geometry-calucation.test.js @@ -0,0 +1,74 @@ +const Point = require('../../modules/ecs6-class/point') +const Line = require('../../modules/ecs6-class/line') +const { calculateDistance, isPointOnLine, calculateJunctionPoint } = require('../../modules/geometry-calculation') +describe('GEOMETRY_CALCULATION', () => { + describe('CALCULATE_DISTANCE', () => { + it('should return error.message', () => { + const point1 = new Point({ x: 2, y: 3 }); + const point2 = new Line({ x: 5, y: 7 }); + expect(() => calculateDistance(point1, point2)).toThrowError('Invalid input. Expected objects of type point.'); + }); + it('should calculate the correct distance between two points', () => { + const result = calculateDistance(point1 = new Point({ x: 6, y: 8 }), point2 = new Point({ x: 2, y: 5 })); + expect(result).toBe(5) + }); + }), + describe('IS_POINT_ON_LINE', () => { + it('should return error if the objects are not Line or Point types', function () { + const line = new Line({ point1: new Point({ x: 2, y: 3 }), point2: new Point({ x: 4, y: 6 }) }); + const point = new Line({ point1: new Point({ x: 0, y: 0 }) }); + expect(() => isPointOnLine(line, point)).toThrowError("The objects should be of the Line and Point types"); + }); + it('should return true if the point lies on the line', () => { + const line = new Line({ point1: { x: 2, y: 5 }, slope: 2, n: 1 }); + const point = new Point({ x: 3, y: 7 }); + const result = isPointOnLine(line, point); + expect(result).toBe(true); + }); + it('should return false if the point does not lie on the line', () => { + const line = new Line({ slope: 2, n: 1 }); + const point = new Point({ x: 1, y: 3 }); + const result = isPointOnLine(line, point); + expect(result).toBe(false); + }); + it('should return false if the point is parallels', () => { + const line = new Line({ point1: { x: 6, y: 13 }, slope: 2, n: 1 }); + const point = new Point({ x: 1, y: 5 }); + const result = isPointOnLine(line, point); + expect(result).toBe(false); + }) + it('should return false if the point does not lie on the line', () => { + const line = new Line({ slope: 4, n: -1 }); + const point = new Point({ x: 1, y: 4 }); + const result = isPointOnLine(line, point); + expect(result).toBe(false); + }); + }), + describe('CALCULATE_JUNCTION_POINT', () => { + it('should return error if it is not of the Line type', () => { + const line1 = new Line({ slope: 2, n: 4 }); + const line2 = new Point({ x: 3, y: 5 }); + expect(() => calculateJunctionPoint(line1, line2)).toThrowError('Must be of the Line type'); + + }); + it('should return true if slopes and intercepts are equal', () => { + const line1 = new Line({ slope: 2, n: 1 }); + const line2 = new Line({ slope: 2, n: 1 }); + const result = calculateJunctionPoint(line1, line2); + expect(result).toBe(true); + }); + it('should return false if slopes are equal but intercepts are different', () => { + const line1 = new Line({ slope: 2, n: 1 }); + const line2 = new Line({ slope: 2, n: 5 }); + const result = calculateJunctionPoint(line1, line2); + expect(result).toBe(false); + }); + it('should calculate the junction point correctly for two lines with different slopes', () => { + const line1 = new Line({ slope: 2, n: 1, getPointByX: function (x) { return { x: x, y: 2 * x + 1 } } }); + const line2 = new Line({ slope: -1, n: 7, getPointByX: function (x) { return { x: x, y: -x + 7 } } }); + const result = calculateJunctionPoint(line1, line2); + expect(result).toEqual({ x: 2, y: 5 }); + }); + }); +}) + From 20c6716dd99faa805e84d9cea02f9b4b76bb2f8d Mon Sep 17 00:00:00 2001 From: LeaG588 Date: Tue, 9 Jul 2024 13:47:55 +0300 Subject: [PATCH 2/2] i did change in the geometry-calculation.test.js --- tests/modules/geometry-calucation.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/modules/geometry-calucation.test.js b/tests/modules/geometry-calucation.test.js index 2949757..0e9a9f4 100644 --- a/tests/modules/geometry-calucation.test.js +++ b/tests/modules/geometry-calucation.test.js @@ -72,3 +72,4 @@ describe('GEOMETRY_CALCULATION', () => { }); }) +