diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index b3bf0f7..2d1e80b 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -2,6 +2,23 @@ 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 should be an instance of Point'); + } + + if (!(point2 instanceof Point)) { + throw new Error('point2 should be an instance of Point'); + } + + if (n !== undefined && typeof n !== 'number') { + throw new Error('n should be a number or undefined'); + } + + if (slope !== undefined && typeof slope !== 'number') { + throw new Error('slope should be a number or undefined'); + } + this.point1 = point1; this.point2 = point2; this.slope = slope; @@ -9,13 +26,26 @@ class Line { } calculateSlope = () => { - this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) + + if (this.point1.x === this.point2.x && this.point1.y === this.point2.y) { + throw Error('Both points have the same values ​​so it is not a line'); + } + + if (this.point1.x === this.point2.x) { + throw Error('x of Point1 is the same as x of point2. It is not a line'); + + } + + this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x); } + + calculateNOfLineFunction = () => { this.n = this.point1.y - this.slope * this.point1.x } + getPointOnXAsis() { return this.getPointByY(0) } @@ -24,16 +54,30 @@ class Line { return this.getPointByX(0) } - getPointByX(x) { + if (x === undefined) + throw new Error('x is undefined') + + if (typeof x !== 'number') + throw new Error('x is not a number') + let y = this.slope * x + this.n return new Point({ x, y }) } getPointByY(y) { - let x = (y - this.slope) / this.n; + if (y === undefined) + throw Error('y is undefined') + + if (typeof y !== 'number') + throw Error('y is not a 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 + + + diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index e81b4a4..6058b13 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,14 +1,37 @@ + + 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 are not number') + if (typeof (x) !== 'number') + throw new Error('x is not number') + if (typeof (y) !== 'number') + throw new Error('y is not number') this.x = x; this.y = y; } + moveVertical(value) { + + if (!value) + throw new Error('value is undefined') + if (typeof value !== 'number') + throw new Error('value is not number') this.y += value; } + + moveHorizontal(value) { + if (!value) + throw Error('value is undefined') + if (typeof value !== 'number') + throw Error('value is not number') + 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 deleted file mode 100644 index 66856dc..0000000 --- a/modules/geometry-calculation.js +++ /dev/null @@ -1,42 +0,0 @@ -const Line = require('./ecs6-class/line') - -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; -} - -const calculateJunctionPoint = (line1, line2) => { - if (line1.slope === line2.slope) { - if (line1.n === line2.n) { - return true - } - else { - return false - } - } - else { - const x = (line1.n - line2.n) / (line2.slope - line1.slope) - const junctionPoint = line1.getPointByX(x); - return junctionPoint - } -} - -const isPointOnLine = (line, point) => { - const proxyLine = new Line(line.point1, point) - proxyLine.calculateSlope() - if (line.slope === proxyLine.slope) { - proxyLine.calculateNOfLineFunction() - if (line.n === proxyLine.n2) { - return true - } - } - return false -} - -module.exports = { - calculateDistance, - calculateJunctionPoint, - isPointOnLine -} diff --git a/modules/geometry-calculator.js b/modules/geometry-calculator.js new file mode 100644 index 0000000..160786d --- /dev/null +++ b/modules/geometry-calculator.js @@ -0,0 +1,96 @@ +const Line = require('./ecs6-class/line') +const Point = require('./ecs6-class/point') + +const calculateDistance = (point1, point2) => { + + if (!(point1 instanceof Point)) { + throw new Error('point1 is not a Point'); + } + if (!(point2 instanceof Point)) { + throw new Error('point2 is not a 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)) { + throw new Error('line1 is not a Line') + } + + if (!(line2 instanceof Line)) { + throw new 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 + } + else { + return false + } + } + else { + const x = (line1.n - line2.n) / (line2.slope - line1.slope) + const junctionPoint = line1.getPointByX(x); + return junctionPoint + } +} + +const isPointOnLine = (line, point) => { + + if (!(line instanceof Line)) { + throw new Error('line is not a Line') + } + if (!(point instanceof Point)) { + throw new Error('point is not a Point') + } + if(!line.slope){ + line.calculateSlope() + } + + if(!line.n){ + line.calculateNOfLineFunction() + } + + + const proxyLine = new Line({ point1: line.point1, point2: point }) + proxyLine.calculateSlope() + if (line.slope === proxyLine.slope) { + proxyLine.calculateNOfLineFunction() + if (line.n === proxyLine.n) { + return true + } + else { + return false; + } + } + return false +} + +module.exports = { + calculateDistance, + calculateJunctionPoint, + isPointOnLine +} diff --git a/package-lock.json b/package-lock.json index 61f4a09..e6f6f63 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1053,10 +1053,11 @@ } }, "node_modules/braces": { - "version": "3.0.2", - "license": "MIT", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", + "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", "dependencies": { - "fill-range": "^7.0.1" + "fill-range": "^7.1.1" }, "engines": { "node": ">=8" @@ -1401,8 +1402,9 @@ } }, "node_modules/fill-range": { - "version": "7.0.1", - "license": "MIT", + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", + "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", "dependencies": { "to-regex-range": "^5.0.1" }, @@ -1586,7 +1588,8 @@ }, "node_modules/is-number": { "version": "7.0.0", - "license": "MIT", + "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", + "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", "engines": { "node": ">=0.12.0" } @@ -2803,7 +2806,8 @@ }, "node_modules/to-regex-range": { "version": "5.0.1", - "license": "MIT", + "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", + "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", "dependencies": { "is-number": "^7.0.0" }, diff --git a/package.json b/package.json index 56bf17b..6f76dc2 100644 --- a/package.json +++ b/package.json @@ -4,11 +4,10 @@ "description": "practice unit tests in javascript", "main": "index.js", "scripts": { - "test": "jest" - }, - "dependencies": { - "jest": "^29.7.0" + "test": "jest", + "test:coverage": "npm run test -- --coverage" }, + "repository": { "type": "git", "url": "git+https://github.com/gemtechd/build-tests.git" @@ -19,5 +18,4 @@ "homepage": "https://github.com/gemtechd/build-tests#readme", "author": "gemtechd", "license": "ISC" - } diff --git a/tests/geometry-calculator.test.js b/tests/geometry-calculator.test.js new file mode 100644 index 0000000..0202652 --- /dev/null +++ b/tests/geometry-calculator.test.js @@ -0,0 +1,139 @@ + +const { calculateDistance, calculateJunctionPoint, isPointOnLine } = require('../modules/geometry-calculator') +const Line = require('../modules/ecs6-class/line'); +const Point = require('../modules/ecs6-class/point'); +const mockConstructor = 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; +}); + + +let line1, line2, line +let point1, point2 +describe('CALACULATE_DISTANCE', () => { + + it(' should checks if the parameters are of the correct type', () => { + expect(() => calculateDistance('ab', mockConstructor(new Point()))).toThrow('point1 is not a Point'); + expect(() => calculateDistance(mockConstructor(new Point(), 'av'))).toThrow('point2 is not a Point'); + }) + + it('calculates the distance correctly between two points', () => { + point1 = mockConstructor(new Point({ x: 1, y: 2 })); + point2 = mockConstructor(new Point({ x: 4, y: 6 })); + expect(calculateDistance(point1, point2)).toBeCloseTo(5, 5); + }); +}); + + + +describe('CALCULATE_JUNCTION_POINT', () => { + + it(' should checks if the parameters are of the correct type', () => { + expect(() => calculateJunctionPoint('ab', mockConstructor(new Line({ n: 5 })))).toThrow('line1 is not a Line'); + expect(() => calculateJunctionPoint(mockConstructor(new Line({ n: 5 }), 'av'))).toThrow('line2 is not a Line'); + }) + + it('should calculate the n and slope values for line1 and then proceed with the calculation', () => { + line1 = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 2, y: 5 })), point2: mockConstructor(new Point({ x: 3, y: 6 })) })); + line2 = mockConstructor(new Line({ slope: 1, n: 1 })); + expect(calculateJunctionPoint(line1, line2)).toBe(false) + }) + + it('should calculate the n and slope values for line2 and then proceed with the calculation', () => { + line1 = mockConstructor(new Line({ slope: 1, n: 1 })); + line2 = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 2, y: 5 })), point2: mockConstructor(new Point({ x: 3, y: 6 })) })); + expect(calculateJunctionPoint(line1, line2)).toBe(false) + }) + + it('should calculate the n value for line1 and then proceed with the calculation', () => { + line1 = mockConstructor(new Line({ slope: 1, point1: mockConstructor(new Point({ x: 2, y: 5 })) })); + line2 = mockConstructor(new Line({ slope: 1, n: 1 })); + expect(calculateJunctionPoint(line1, line2)).toBe(false) + }) + + it('should calculate the n value for line2 and then proceed with the calculation', () => { + line1 = mockConstructor(new Line({ slope: 1, n: 1 })); + line2 = mockConstructor(new Line({ slope: 1, point1: mockConstructor(new Point({ x: 2, y: 5 })) })); + expect(calculateJunctionPoint(line1, line2)).toBe(false) + }) + + it('should calculate the slope value for line1 and then proceed with the calculation', () => { + line1 = mockConstructor(new Line({ n: 1, point1: mockConstructor(new Point({ x: 2, y: 5 })) })); + line2 = mockConstructor(new Line({ slope: 1, n: 1 })); + expect(calculateJunctionPoint(line1, line2)).toBeTruthy() + }) + + it('should calculate the slope value for line2 and then proceed with the calculation', () => { + line1 = mockConstructor(new Line({ slope: 1, n: 1 })); + line2 = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 2, y: 5 })), n: 1 })); + expect(calculateJunctionPoint(line1, line2)).toStrictEqual(mockConstructor(new Point({ x: 2, y: 12 }))) + }) + + it('should return true if the lines are parallel and identical', () => { + line1 = mockConstructor(new Line({ slope: 1, n: 1 })); + line2 = mockConstructor(new Line({ slope: 1, n: 1 })); + expect(calculateJunctionPoint(line1, line2)).toBe(true); + }); + + it('should return false if the lines are parallel but not identical', () => { + line1 = mockConstructor(new Line({ slope: 1, n: 1 })) + line2 = mockConstructor(new Line({ slope: 1, n: 2 })); + expect(calculateJunctionPoint(line1, line2)).toBe(false); + }); + +}) + + +describe('IS_POINTON_LINE', () => { + + it(' should checks if the parameters are of the correct type', () => { + line1 = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 4, y: 6 })), point2: mockConstructor(new Point({ x: 8, y: 9 })), slope: 1, n: 10 })) + point1 = mockConstructor(new Point({ x: 4, y: 6 })) + expect(() => isPointOnLine(line1, "abc")).toThrow('point is not a Point'); + expect(() => isPointOnLine("c", point1)).toThrow('line 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',()=>{ + line1 = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 4, y: 6 })), point2: mockConstructor(new Point({ x: 8, y: 9 })), n: 10 })) + point1 = mockConstructor(new Point({ x: 2, y: 5 })) + expect(isPointOnLine(line1, point1)).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',()=>{ + line1 = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 4, y: 6 })), point2: mockConstructor(new Point({ x: 8, y: 9 })), slope: 1 })) + point1 = mockConstructor(new Point({ x: 3, y: 7 })) + expect(isPointOnLine(line1, point1)).toBe(false); + }) + + + it('should return false if the point does not lie on the given line', () => { + line1 = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 4, y: 6 })), point2: mockConstructor(new Point({ x: 8, y: 9 })), slope: 1, n: 10 })) + point1 = mockConstructor(new Point({ x: 5, y: 4 })); + expect(isPointOnLine(line1, point1)).toBe(false); + }); + + + it('should return false if the point lies on the line with the same slope but not the same value of n', () => { + point1 = mockConstructor(new Point({ x: 5, y: 4 })) + line1 = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 4, y: 6 })), point2: mockConstructor(new Point({ x: 8, y: 9 })), slope: -2, n: 5 })); + const result = isPointOnLine(line1, point1) + expect(result).toBe(false) + }) + + it('should return true if the point lies on the given line', () => { + point1 = mockConstructor(new Point({ x: 5, y: 4 })) + line1 = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 4, y: 6 })), point2: mockConstructor(new Point({ x: 8, y: 9 })), slope: -2, n: 14 })) + expect(isPointOnLine(line1, point1)).toBe(true) + }) + +}) diff --git a/tests/modules/ecs6-class/line.test.js b/tests/modules/ecs6-class/line.test.js new file mode 100644 index 0000000..9c6f824 --- /dev/null +++ b/tests/modules/ecs6-class/line.test.js @@ -0,0 +1,192 @@ +const Line = require('../../../modules/ecs6-class/line'); +const Point = require('../../../modules/ecs6-class/point'); +const mockConstructor = jest.fn(constructor) + + + +describe('CONSTRUCTOR', () => { + + it('should initialize point2, n and slope to default values when no parameters are provided', () => { + + const line1 = new Line({ point1: new Point({ x: 2, y: 5 }) }); + expect(line1.point1.x).toBe(2); + expect(line1.point1.y).toBe(5); + expect(line1.point2.x).toBe(0); + expect(line1.point2.y).toBe(0); + expect(line1.n).toBe(undefined); + expect(line1.slope).toBe(undefined); + }) + + it('should initialize point1, n and slope to default values when no parameters are provided', () => { + + const line2 = new Line({ point2: new Point({ x: 2, y: 5 }) }); + expect(line2.point1.x).toBe(0); + expect(line2.point1.y).toBe(0); + expect(line2.point2.x).toBe(2); + expect(line2.point2.y).toBe(5); + expect(line2.n).toBe(undefined); + expect(line2.slope).toBe(undefined); + }) + + + it('should initialize point1, point2 and n to default values when no parameters are provided', () => { + const line3 = new Line({ slope: 5 }); + expect(line3.point1.x).toBe(0); + expect(line3.point1.y).toBe(0); + expect(line3.point2.x).toBe(0); + expect(line3.point2.y).toBe(0); + expect(line3.n).toBe(undefined); + expect(line3.slope).toBe(5); + }) + + + it('should initialize point1,point2 and slope to default values when no parameters are provided', () => { + const line4 = new Line({ n: 3 }); + expect(line4.point1.x).toBe(0); + expect(line4.point1.y).toBe(0); + expect(line4.point2.x).toBe(0); + expect(line4.point2.y).toBe(0); + expect(line4.n).toBe(3); + expect(line4.slope).toBe(undefined); + }) + +}); + +it('should throw error when point1 is not Point ', () => { + expect(() => new Line({ point1: "bbb", point2: new Point({ x: 2, y: 3 }), slope: 5, n: 3 })).toThrow('point1 should be an instance of Point'); +}) + +it('should throw error when point2 is not Point ', () => { + expect(() => new Line({ point1: new Point({ x: 2, y: 3 }), point2: "aaa", slope: 5, n: 3 })).toThrow('point2 should be an instance of Point'); +}) + +it('should throw error when n is not number or is not get the defualt value:undefined', () => { + expect(() => new Line({ point1: new Point({ x: 2, y: 3 }), point2: new Point({ x: 2, y: 3 }), slope: 5, n: "3" })).toThrow('n should be a number or undefined'); +}) + +it('should throw error when slope is not number or is not get the defualt value:undefined', () => { + expect(() => new Line({ point1: new Point({ x: 2, y: 3 }), point2: new Point({ x: 2, y: 3 }), slope: "5", n: 3 })).toThrow('slope should be a number or undefined'); +}) + + +it('Line constructor initialized correctly', () => { + const line = new Line({ point1: new Point(1, 2), point2: new Point(3, 4), slope: 2, n: 3 }); + expect(line.point1).toEqual(new Point(1, 2)); + expect(line.point2).toEqual(new Point(3, 4)); + expect(line.slope).toBe(2); + expect(line.n).toBe(3); +}) + +describe('CALCULATE_SLOPE', () => { + + + it('should result in NaN for the same points (division by zero)', () => { + const line = (new Line({ point1: mockConstructor(new Point({ x: 8, y: 5 })), point2: mockConstructor(new Point({ x: 8, y: 5 })) })); + expect(() => line.calculateSlope()).toThrow('Both points have the same values ​​so it is not a line') + }); + + it('throw error for points with the same x-coordinate', () => { + const line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 5, y: 2 })), point2: mockConstructor(new Point({ x: 5, y: 7 })) })); + expect(() => line.calculateSlope()).toThrow('x of Point1 is the same as x of point2. It is not a line') + }) + + + it('should calculate slope as 0 for points with the same y-coordinate', () => { + const line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 1, y: 5 })), point2: mockConstructor(new Point({ x: 2, y: 5 })) })); + line.calculateSlope(); + expect(line.slope).toBeCloseTo(0); + }); + + + + it('should calculate the slope correctly for different points with positive slope', () => { + const line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 1, y: 2 })), point2: mockConstructor(new Point({ x: 3, y: 8 })) })); + line.calculateSlope(); + expect(line.slope).toEqual(3); + }); + + it('should calculate the slope correctly for different points with negative slope', () => { + const line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 4, y: 0 })), point2: mockConstructor(new Point({ x: 2, y: 6 })) })); + line.calculateSlope(); + expect(line.slope).toBe(-3); + }); + +}) + +describe('CALCULATENOFLINEFUNCTION', () => { + + it('calculates the correct n value ', () => { + const line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 2, y: 5 })), slope: 3 })); + line.calculateNOfLineFunction(); + expect(line.n).toEqual(-1); + }); + +}) + + + +describe('GET_POINT_ON_X_ASIS', () => { + + + it('returns the correct point on the x-axis for the given slope and n value', () => { + const line = mockConstructor(new Line({ slope: 2, n: -6 })); + expect(line.getPointOnXAsis()).toEqual(mockConstructor(new Point({ x: 3, y: 0 }))); + }) + +}) + +describe('GET_POINT_ON_Y_ASIS', () => { + + + it('returns the correct point on the y-axis for the given slope and n value', () => { + const line = mockConstructor(new Line({ slope: 5, n: 5 })); + expect(line.getPointOnYAsis()).toEqual(mockConstructor(new Point({ x: 0, y: 5 }))); + + }) +}) + +describe('GET_POINT_BY_X', () => { + + + it('should throw error when x is undefined', () => { + const line = mockConstructor(new Line({ slope: 5, n: 7 })); + expect(() => line.getPointByX()).toThrow('x is undefined'); + }) + + it('should throw error when x is not a number', () => { + const line = mockConstructor(new Line({ slope: 5, n: 7 })); + expect(() => line.getPointByX("aaa")).toThrow('x is not a number'); + }) + + it('getPointByX returns the correct Point for the given x value', () => { + const line = mockConstructor(new Line({ slope: 5, n: 7 })); + expect(line.getPointByX(4)).toEqual(mockConstructor(new Point({ x: 4, y: 27 }))); + }) + +}) + + +describe('GET_POINT_BY_Y', () => { + + it('should throw error when y is undefined', () => { + const line = mockConstructor(new Line({ slope: 5, n: 7 })); + expect(() => line.getPointByY()).toThrow('y is undefined'); + }) + + it('should throw error when y is not a number', () => { + const line = mockConstructor(new Line({ slope: 5, n: 7 })); + expect(() => line.getPointByY("aaa")).toThrow('y is not a number'); + }) + + + it('getPointByY returns the correct Point for the given y value', () => { + const line = mockConstructor(new Line({ slope: 5, n: 5 })); + expect(line.getPointByY(20)).toEqual(mockConstructor(new Point({ x: 3, y: 20 }))); + }) + + + +}) + + + diff --git a/tests/modules/ecs6-class/point.test.js b/tests/modules/ecs6-class/point.test.js new file mode 100644 index 0000000..afd282f --- /dev/null +++ b/tests/modules/ecs6-class/point.test.js @@ -0,0 +1,97 @@ +const Point = require('../../../modules/ecs6-class/point'); +const mockConstructor = jest.fn(constructor); + +let point + +describe('CONSTRUCTOR', () => { + + + it('should initialize x and y to default values when no parameters are provided', () => { + point = new Point(); + 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: 4 }) + expect(point.x).toBe(4); + expect(point.y).toBe(0); + }) + + it('should initialize x to default value when no parameters are provided', () => { + point = new Point({ y: 3 }) + expect(point.x).toBe(0); + expect(point.y).toBe(3); + }) + + + + it('should throw error when x or y is not number ', () => { + expect(() => new Point({ x: '4', y: '3' })).toThrow('x and y are not number') + expect(() => new Point({ x: '4', y: 3 })).toThrow('x is not number'); + expect(() => new Point({ x: 4, y: '3' })).toThrow('y is not number'); + + }) + + it('Point constructor initialized correctly', () => { + point = new Point({ x: 3, y: 4 }) + expect(point.x).toBe(3) + expect(point.y).toBe(4) + + }) +}) + +describe('MOVEVERTICAL', () => { + + + + it('should throw error when value is undefined', () => { + point = mockConstructor(new Point()) + expect(() => point.moveVertical()).toThrow('value is undefined'); + }) + + it('should throw error when value is not number ', () => { + point = mockConstructor(new Point()) + expect(() => point.moveVertical("aaa")).toThrow('value is not number'); + }) + + it('should increment the y coordinate by the given value', () => { + point = mockConstructor(new Point()); + const initialValue = point.y; + const incrementValue = 3; + point.moveVertical(incrementValue); + expect(point.y).toBe(initialValue + incrementValue); + }) + + +}) + + + +describe('MOVEHORIZONTAL', () => { + + it('should throw error when value is undefined', () => { + point = mockConstructor(new Point()) + expect(() => point.moveHorizontal()).toThrow('value is undefined'); + }) + + it('should throw error when value is not number ', () => { + point = mockConstructor(new Point()) + expect(() => point.moveHorizontal("aaa")).toThrow('value is not number'); + }) + + it('should increment the x coordinate by the given value', () => { + point = mockConstructor(new Point()); + const initialValue = point.x; + const incrementValue = 3; + point.moveHorizontal(incrementValue); + expect(point.x).toBe(initialValue + incrementValue); + + }) + +}) + + + + +