From 20b284c2ad178d35c0d9039e9b45ce30f7928959 Mon Sep 17 00:00:00 2001 From: sara raichbard Date: Sun, 21 Jul 2024 15:01:13 +0300 Subject: [PATCH 1/2] add tests --- modules/ecs6-class/line.js | 59 +++++- modules/ecs6-class/point.js | 26 ++- ...-calculation.js => geometry-calculator.js} | 39 +++- package.json | 6 +- tests/geometry-calculator.test.js | 99 ++++++++++ tests/modules/ecs6-class/line.test.js | 177 ++++++++++++++++++ tests/modules/ecs6-class/point.test.js | 91 +++++++++ 7 files changed, 483 insertions(+), 14 deletions(-) rename modules/{geometry-calculation.js => geometry-calculator.js} (50%) create mode 100644 tests/geometry-calculator.test.js create mode 100644 tests/modules/ecs6-class/line.test.js create mode 100644 tests/modules/ecs6-class/point.test.js diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index b3bf0f7..56a142e 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -1,7 +1,24 @@ -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 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,35 @@ 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, calculateSlope: Line.prototype.calculateSlope, + calculateNOfLineFunction: Line.prototype.calculateNOfLineFunction, + getPointOnXAsis: Line.prototype.getPointOnXAsis, + getPointOnYAsis: Line.prototype.getPointOnYAsis, + getPointByX: Line.prototype.getPointByX, + getPointByY: Line.prototype.getPointByY +}; + diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index e81b4a4..4f03d95 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,14 +1,36 @@ 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 is 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, moveHorizontal: Point.prototype.moveHorizontal, moveVertical: Point.prototype.moveVertical }; diff --git a/modules/geometry-calculation.js b/modules/geometry-calculator.js similarity index 50% rename from modules/geometry-calculation.js rename to modules/geometry-calculator.js index 66856dc..8e4e628 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculator.js @@ -1,16 +1,38 @@ -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)) { + 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 - 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)) { + throw new Error('line1 is not a Line'); + } + + if (!(line2 instanceof Line)) { + throw new Error('line2 is not a Line'); + } + if (line1.slope === line2.slope) { if (line1.n === line2.n) { return true + } else { return false @@ -24,13 +46,22 @@ const calculateJunctionPoint = (line1, line2) => { } const isPointOnLine = (line, point) => { - const proxyLine = new Line(line.point1, 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') + } + const proxyLine = new Line({ point1: line.point1, point2: point }) proxyLine.calculateSlope() 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/package.json b/package.json index 56bf17b..cf19111 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" @@ -19,5 +20,4 @@ "homepage": "https://github.com/gemtechd/build-tests#readme", "author": "gemtechd", "license": "ISC" - -} +} \ No newline at end of file diff --git a/tests/geometry-calculator.test.js b/tests/geometry-calculator.test.js new file mode 100644 index 0000000..f93d609 --- /dev/null +++ b/tests/geometry-calculator.test.js @@ -0,0 +1,99 @@ + +const { calculateDistance, calculateJunctionPoint, isPointOnLine } = require('../modules/geometry-calculator') +const { Line, getPointByY, getPointByX } = require('../modules/ecs6-class/line'); +const { Point } = require('../modules/ecs6-class/point'); +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; +}); + + +let line1, line2 +let point1, point2 +describe('CALACULATE_DISTANCE', () => { + + it(' should checks if the parameters are of the correct type', () => { + expect(() => calculateDistance('ab', mockConstructorPoint(new Point()))).toThrow('point1 is not a Point'); + expect(() => calculateDistance(mockConstructorPoint(new Point(), 'av'))).toThrow('point2 is not a Point'); + }) + + it('calculates the distance correctly between two points', () => { + point1 = mockConstructorPoint(new Point({ x: 1, y: 2 })); + point2 = mockConstructorPoint(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', mockConstructorLine(new Line({ n: 5 })))).toThrow('line1 is not a Line'); + expect(() => calculateJunctionPoint(mockConstructorLine(new Line({ n: 5 }), 'av'))).toThrow('line2 is not a Line'); + }) + + it('should return true if the lines are parallel and identical', () => { + line1 = mockConstructorLine(new Line({ slope: 1, n: 1 })); + line2 = mockConstructorLine(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 = mockConstructorLine(new Line({ slope: 1, n: 1 })) + line2 = mockConstructorLine(new Line({ slope: 1, n: 2 })); + expect(calculateJunctionPoint(line1, line2)).toBe(false); + }); + + + it(' should calculateJunctionPoint return the correct junction point for the given lines', () => { + + line1 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: 1, n: 10 })) + line2 = 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(line1, line2) + expect(result).toEqual(mockConstructorPoint(new Point({ x: 2, y: 12 }))); + }); + + +}) + + +describe('IS_POINTON_LINE', () => { + + it(' should checks if the parameters are of the correct type', () => { + line1 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: 1, n: 10 })) + point1 = mockConstructorPoint(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 return false if the point does not lie on the given line', () => { + line1 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: 1, n: 10 })) + point1 = mockConstructorPoint(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 = mockConstructorPoint(new Point({ x: 5, y: 4 })) + line1 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(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 = mockConstructorPoint(new Point({ x: 5, y: 4 })) + line1 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(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..1372a14 --- /dev/null +++ b/tests/modules/ecs6-class/line.test.js @@ -0,0 +1,177 @@ +const { Line, getPointByX, getPointByY } = require('../../../modules/ecs6-class/line'); +const { Point } = require('../../../modules/ecs6-class/point'); +const mockConstructorLine = jest.fn(constructor) +const mockConstructorPoint = jest.fn(constructor) + + +describe('CONSTRUCTOR', () => { + + it('should initialize x and y to default values when no parameters are provided', () => { + + + const line1 = new Line({ point1: new Point({ x: 2, y: 5 }) }); + expect(line1.point1).toStrictEqual(new Point({ x: 2, y: 5 })); + expect(line1.point2).toStrictEqual(new Point()); + expect(line1.n).toBe(undefined); + expect(line1.slope).toBe(undefined); + + + const line2 = new Line({ point2: new Point({ x: 2, y: 5 }) }); + expect(line2.point1).toStrictEqual(new Point()); + expect(line2.point2).toStrictEqual(new Point({ x: 2, y: 5 })); + expect(line2.n).toBe(undefined); + expect(line2.slope).toBe(undefined); + + const line4 = new Line({ n: 3 }); + expect(line4.point1).toStrictEqual(new Point()); + expect(line4.point2).toStrictEqual(new Point()); + expect(line4.n).toBe(3); + expect(line4.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); + + }) +}); + +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 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 8, y: 5 })), point2: mockConstructorPoint(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 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 5, y: 2 })), point2: mockConstructorPoint(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 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 1, y: 5 })), point2: mockConstructorPoint(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 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 1, y: 2 })), point2: mockConstructorPoint(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 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 0 })), point2: mockConstructorPoint(new Point({ x: 2, y: 6 })) })); + line.calculateSlope(); + expect(line.slope).toBe(-3); + }); + +}) + +describe('CALCULATENOFLINEFUNCTION', () => { + + it('calculates the correct n value ', () => { + const line = mockConstructorLine(new Line({ point1: mockConstructorPoint(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 = mockConstructorLine(new Line({ slope: 2, n: -6 })); + expect(line.getPointOnXAsis()).toEqual(mockConstructorPoint(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 = mockConstructorLine(new Line({ slope: 5, n: 5 })); + expect(line.getPointOnYAsis()).toEqual(mockConstructorPoint(new Point({ x: 0, y: 5 }))); + + }) +}) + +describe('GET_POINT_BY_X', () => { + + + it('should throw error when x is undefined', () => { + const line = mockConstructorLine(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 = mockConstructorLine(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 = mockConstructorLine(new Line({ slope: 5, n: 7 })); + expect(line.getPointByX(4)).toEqual(mockConstructorPoint(new Point({ x: 4, y: 27 }))); + }) + +}) + + +describe('GET_POINT_BY_Y', () => { + + it('should throw error when y is undefined', () => { + const line = mockConstructorLine(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 = mockConstructorLine(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 = mockConstructorLine(new Line({ slope: 5, n: 5 })); + expect(line.getPointByY(20)).toEqual(mockConstructorPoint(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..46dc808 --- /dev/null +++ b/tests/modules/ecs6-class/point.test.js @@ -0,0 +1,91 @@ +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); + point = new Point({ x: 4 }) + expect(point.x).toBe(4); + expect(point.y).toBe(0); + 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 is 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); + + }) + +}) + + + + + From 144dce1462ee6696cca8cea6ee07b5d770f51152 Mon Sep 17 00:00:00 2001 From: sara raichbard Date: Mon, 22 Jul 2024 19:48:55 +0300 Subject: [PATCH 2/2] fix the mocks --- modules/ecs6-class/line.js | 13 +-- modules/ecs6-class/point.js | 7 +- modules/geometry-calculator.js | 37 +++++-- package-lock.json | 18 ++-- package.json | 6 +- tests/geometry-calculator.test.js | 128 ++++++++++++++++--------- tests/modules/ecs6-class/line.test.js | 85 +++++++++------- tests/modules/ecs6-class/point.test.js | 12 ++- 8 files changed, 194 insertions(+), 112 deletions(-) diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index 56a142e..2d1e80b 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 }) { @@ -77,12 +77,7 @@ 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 -}; +module.exports = Line + + diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index 4f03d95..6058b13 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,10 +1,11 @@ -class Point { +class Point { + constructor({ x = 0, y = 0 } = {}) { if (typeof (x) !== 'number' && typeof (y) !== 'number') - throw new Error('x and y is not 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') @@ -33,4 +34,4 @@ class Point { } } -module.exports = { Point, moveHorizontal: Point.prototype.moveHorizontal, moveVertical: Point.prototype.moveVertical }; +module.exports =Point diff --git a/modules/geometry-calculator.js b/modules/geometry-calculator.js index 8e4e628..160786d 100644 --- a/modules/geometry-calculator.js +++ b/modules/geometry-calculator.js @@ -1,5 +1,5 @@ -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) => { @@ -10,8 +10,6 @@ const calculateDistance = (point1, point2) => { throw new Error('point2 is not a Point'); } - - let distanceX = (point2.x - point1.x) ** 2; let distanceY = (point2.y - point1.y) ** 2; @@ -22,17 +20,32 @@ const calculateDistance = (point1, point2) => { const calculateJunctionPoint = (line1, line2) => { if (!(line1 instanceof Line)) { - throw new Error('line1 is not a Line'); + throw new Error('line1 is not a Line') } if (!(line2 instanceof Line)) { - throw new Error('line2 is not a 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 @@ -46,12 +59,22 @@ const calculateJunctionPoint = (line1, line2) => { } 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) { 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 cf19111..6f76dc2 100644 --- a/package.json +++ b/package.json @@ -7,9 +7,7 @@ "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" @@ -20,4 +18,4 @@ "homepage": "https://github.com/gemtechd/build-tests#readme", "author": "gemtechd", "license": "ISC" -} \ No newline at end of file +} diff --git a/tests/geometry-calculator.test.js b/tests/geometry-calculator.test.js index f93d609..0202652 100644 --- a/tests/geometry-calculator.test.js +++ b/tests/geometry-calculator.test.js @@ -1,9 +1,9 @@ const { calculateDistance, calculateJunctionPoint, isPointOnLine } = require('../modules/geometry-calculator') -const { Line, getPointByY, getPointByX } = 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); + Line.prototype.getPointByX = jest.fn().mockImplementation((x) => { return new Point({ x: 2, y: 12 }); @@ -18,82 +18,122 @@ Line.prototype.calculateNOfLineFunction = jest.fn().mockImplementation(() => { }); -let line1, line2 +let line1, line2, line let point1, point2 describe('CALACULATE_DISTANCE', () => { it(' should checks if the parameters are of the correct type', () => { - expect(() => calculateDistance('ab', mockConstructorPoint(new Point()))).toThrow('point1 is not a Point'); - expect(() => calculateDistance(mockConstructorPoint(new Point(), 'av'))).toThrow('point2 is not a Point'); + 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 = mockConstructorPoint(new Point({ x: 1, y: 2 })); - point2 = mockConstructorPoint(new Point({ x: 4, y: 6 })); + 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', mockConstructorLine(new Line({ n: 5 })))).toThrow('line1 is not a Line'); - expect(() => calculateJunctionPoint(mockConstructorLine(new Line({ n: 5 }), 'av'))).toThrow('line2 is not a Line'); + 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 = mockConstructorLine(new Line({ slope: 1, n: 1 })); - line2 = mockConstructorLine(new Line({ slope: 1, n: 1 })); + 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 = mockConstructorLine(new Line({ slope: 1, n: 1 })) - line2 = mockConstructorLine(new Line({ slope: 1, n: 2 })); + line1 = mockConstructor(new Line({ slope: 1, n: 1 })) + line2 = mockConstructor(new Line({ slope: 1, n: 2 })); expect(calculateJunctionPoint(line1, line2)).toBe(false); }); - - it(' should calculateJunctionPoint return the correct junction point for the given lines', () => { - - line1 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: 1, n: 10 })) - line2 = 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(line1, line2) - expect(result).toEqual(mockConstructorPoint(new Point({ x: 2, y: 12 }))); - }); - - }) describe('IS_POINTON_LINE', () => { it(' should checks if the parameters are of the correct type', () => { - line1 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: 1, n: 10 })) - point1 = mockConstructorPoint(new Point({ x: 4, y: 6 })) + 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 return false if the point does not lie on the given line', () => { - line1 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: 1, n: 10 })) - point1 = mockConstructorPoint(new Point({ x: 5, y: 4 })); - expect(isPointOnLine(line1, point1)).toBe(false); -}); + 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 lies on the line with the same slope but not the same value of n', () => { - point1 = mockConstructorPoint(new Point({ x: 5, y: 4 })) - line1 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(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 = mockConstructorPoint(new Point({ x: 5, y: 4 })) - line1 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 6 })), point2: mockConstructorPoint(new Point({ x: 8, y: 9 })), slope: -2, n: 14 })) - expect(isPointOnLine(line1, point1)).toBe(true) -}) + 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 index 1372a14..9c6f824 100644 --- a/tests/modules/ecs6-class/line.test.js +++ b/tests/modules/ecs6-class/line.test.js @@ -1,40 +1,55 @@ -const { Line, getPointByX, getPointByY } = require('../../../modules/ecs6-class/line'); -const { Point } = require('../../../modules/ecs6-class/point'); -const mockConstructorLine = jest.fn(constructor) -const mockConstructorPoint = jest.fn(constructor) +const Line = require('../../../modules/ecs6-class/line'); +const Point = require('../../../modules/ecs6-class/point'); +const mockConstructor = jest.fn(constructor) -describe('CONSTRUCTOR', () => { - it('should initialize x and y to default values when no parameters are provided', () => { +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).toStrictEqual(new Point({ x: 2, y: 5 })); - expect(line1.point2).toStrictEqual(new Point()); + 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).toStrictEqual(new Point()); - expect(line2.point2).toStrictEqual(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); + }) - const line4 = new Line({ n: 3 }); - expect(line4.point1).toStrictEqual(new Point()); - expect(line4.point2).toStrictEqual(new Point()); - expect(line4.n).toBe(3); - expect(line4.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).toStrictEqual(new Point()); - expect(line3.point2).toStrictEqual(new Point()); + 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 ', () => { @@ -66,18 +81,18 @@ describe('CALCULATE_SLOPE', () => { it('should result in NaN for the same points (division by zero)', () => { - const line = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 8, y: 5 })), point2: mockConstructorPoint(new Point({ x: 8, y: 5 })) })); + 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 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 5, y: 2 })), point2: mockConstructorPoint(new Point({ x: 5, y: 7 })) })); + 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 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 1, y: 5 })), point2: mockConstructorPoint(new Point({ x: 2, y: 5 })) })); + 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); }); @@ -85,13 +100,13 @@ describe('CALCULATE_SLOPE', () => { it('should calculate the slope correctly for different points with positive slope', () => { - const line = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 1, y: 2 })), point2: mockConstructorPoint(new Point({ x: 3, y: 8 })) })); + 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 = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 4, y: 0 })), point2: mockConstructorPoint(new Point({ x: 2, y: 6 })) })); + 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); }); @@ -101,7 +116,7 @@ describe('CALCULATE_SLOPE', () => { describe('CALCULATENOFLINEFUNCTION', () => { it('calculates the correct n value ', () => { - const line = mockConstructorLine(new Line({ point1: mockConstructorPoint(new Point({ x: 2, y: 5 })), slope: 3 })); + const line = mockConstructor(new Line({ point1: mockConstructor(new Point({ x: 2, y: 5 })), slope: 3 })); line.calculateNOfLineFunction(); expect(line.n).toEqual(-1); }); @@ -114,8 +129,8 @@ describe('GET_POINT_ON_X_ASIS', () => { it('returns the correct point on the x-axis for the given slope and n value', () => { - const line = mockConstructorLine(new Line({ slope: 2, n: -6 })); - expect(line.getPointOnXAsis()).toEqual(mockConstructorPoint(new Point({ x: 3, y: 0 }))); + const line = mockConstructor(new Line({ slope: 2, n: -6 })); + expect(line.getPointOnXAsis()).toEqual(mockConstructor(new Point({ x: 3, y: 0 }))); }) }) @@ -124,8 +139,8 @@ describe('GET_POINT_ON_Y_ASIS', () => { it('returns the correct point on the y-axis for the given slope and n value', () => { - const line = mockConstructorLine(new Line({ slope: 5, n: 5 })); - expect(line.getPointOnYAsis()).toEqual(mockConstructorPoint(new Point({ x: 0, y: 5 }))); + const line = mockConstructor(new Line({ slope: 5, n: 5 })); + expect(line.getPointOnYAsis()).toEqual(mockConstructor(new Point({ x: 0, y: 5 }))); }) }) @@ -134,18 +149,18 @@ describe('GET_POINT_BY_X', () => { it('should throw error when x is undefined', () => { - const line = mockConstructorLine(new Line({ slope: 5, n: 7 })); + 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 = mockConstructorLine(new Line({ slope: 5, n: 7 })); + 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 = mockConstructorLine(new Line({ slope: 5, n: 7 })); - expect(line.getPointByX(4)).toEqual(mockConstructorPoint(new Point({ x: 4, y: 27 }))); + const line = mockConstructor(new Line({ slope: 5, n: 7 })); + expect(line.getPointByX(4)).toEqual(mockConstructor(new Point({ x: 4, y: 27 }))); }) }) @@ -154,19 +169,19 @@ describe('GET_POINT_BY_X', () => { describe('GET_POINT_BY_Y', () => { it('should throw error when y is undefined', () => { - const line = mockConstructorLine(new Line({ slope: 5, n: 7 })); + 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 = mockConstructorLine(new Line({ slope: 5, n: 7 })); + 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 = mockConstructorLine(new Line({ slope: 5, n: 5 })); - expect(line.getPointByY(20)).toEqual(mockConstructorPoint(new Point({ x: 3, y: 20 }))); + 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 index 46dc808..afd282f 100644 --- a/tests/modules/ecs6-class/point.test.js +++ b/tests/modules/ecs6-class/point.test.js @@ -1,4 +1,4 @@ -const { Point } = require('../../../modules/ecs6-class/point'); +const Point = require('../../../modules/ecs6-class/point'); const mockConstructor = jest.fn(constructor); let point @@ -10,18 +10,24 @@ describe('CONSTRUCTOR', () => { 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 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');