From bd52414751955a9d4edf18dfd7313dc2dbeea7d0 Mon Sep 17 00:00:00 2001 From: M-Kroizer Date: Sun, 21 Jul 2024 13:03:52 +0300 Subject: [PATCH 1/2] add tests for all pages still without mocks --- modules/ecs6-class/line.js | 12 ++- modules/ecs6-class/point.js | 10 ++- modules/geometry-calculation.js | 21 ++++- package.json | 3 +- tests/geometry-calculation.test.js | 138 +++++++++++++++++++++++++++++ tests/line.test.js | 137 ++++++++++++++++++++++++++++ tests/point.test.js | 105 ++++++++++++++++++++++ 7 files changed, 418 insertions(+), 8 deletions(-) create mode 100644 tests/geometry-calculation.test.js create mode 100644 tests/line.test.js create mode 100644 tests/point.test.js diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index 826c675..b8b61af 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -1,7 +1,7 @@ const Point = require("./point"); class Line { - constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) { + constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }={}) { this.point1 = point1; this.point2 = point2; this.slope = slope; @@ -25,12 +25,18 @@ class Line { } - getPointByX(x) { + getPointByX(x=0) { + if(typeof(x)!='number'){ + throw new Error('value must be a number') + } let y = this.slope * x + this.n return new Point({ x, y }) } - getPointByY(y) { + getPointByY(y=0) { + if(typeof(y)!='number'){ + throw new Error('value must be a number') + } let x = (y - this.n) / this.slope; return new Point({ x, y }) } diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index e81b4a4..e829a76 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -3,10 +3,16 @@ class Point { this.x = x; this.y = y; } - moveVertical(value) { + moveVertical(value=0 ) { + if(typeof(value)!='number'){ + throw new Error('value must be a number') + } this.y += value; } - moveHorizontal(value) { + moveHorizontal(value=0) { + if(typeof(value)!='number'){ + throw new Error('value must be a number') + } this.x += value; } } diff --git a/modules/geometry-calculation.js b/modules/geometry-calculation.js index 6e11643..1c7cf27 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -1,13 +1,23 @@ -const Line = require('./ecs6-class/line') +const Line = require('./ecs6-class/line'); +const Point = require('./ecs6-class/point'); const calculateDistance = (point1, point2) => { + + if(!point2) + throw new Error('must enter 2 points') + if(!(point1 instanceof Point)||!(point2 instanceof Point)) + throw new Error('points must be instances of 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(!line2) + throw new Error('must enter 2 lines') + if(!(line2 instanceof Line)||!(line1 instanceof Line)) + throw new Error('lines must be instances of Line') if (line1.slope === line2.slope) { if (line1.n === line2.n) { return true @@ -24,6 +34,12 @@ const calculateJunctionPoint = (line1, line2) => { } const isPointOnLine = (line, point) => { + if(!line||!point) + throw new Error('must get both line and point') + if( !( point instanceof Point)) + throw new Error('point must be instance of Point') + if(! (line instanceof Line)) + throw new Error('line must be instance of Line') const proxyLine = new Line({ point1: line.point1, point2: point }) proxyLine.calculateSlope() if (line.slope === proxyLine.slope) { @@ -33,6 +49,7 @@ const isPointOnLine = (line, point) => { } } return false + } module.exports = { diff --git a/package.json b/package.json index 56bf17b..a1390e3 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "practice unit tests in javascript", "main": "index.js", "scripts": { - "test": "jest" + "test": "jest", + "coverage":"npm test -- --coverage" }, "dependencies": { "jest": "^29.7.0" diff --git a/tests/geometry-calculation.test.js b/tests/geometry-calculation.test.js new file mode 100644 index 0000000..0bdfefc --- /dev/null +++ b/tests/geometry-calculation.test.js @@ -0,0 +1,138 @@ +const Line = require("../modules/ecs6-class/line") +const Point = require("../modules/ecs6-class/point") +const { calculateDistance, calculateJunctionPoint, isPointOnLine } = require("../modules/geometry-calculation") + + +describe('calculateDistance', () => { + it('should return the distance', () => { + const p1 = new Point({ x: 1, y: 5 }) + const p2 = new Point({ x: 12, y: 5 }) + const result = calculateDistance(p1,p2) + expect(result).toBeDefined() + expect(typeof(result)).toBe('number') + }) + it('should throw an error if no arguments where given',()=>{ + const p1 = new Point({ x: 1, y: 5 }) + expect(()=>calculateDistance(p1)).toThrow('must enter 2 points') + expect(()=>calculateDistance()).toThrow('must enter 2 points') + + }) + it('should throw an error if points are not instances of Point',()=>{ + const p1 = { x: 1, y: 5 } + const p2 = { x: 12, y: 5 } + + expect(()=>calculateDistance(p1,p2)).toThrow('points must be instances of Point') + expect(()=>calculateDistance([new Point({ x: 1, y: 5 })],new Point({ x: 1, y: 5 }))).toThrow('points must be instances of Point') + expect(()=>calculateDistance('p1',p2)).toThrow('points must be instances of Point') + expect(()=>calculateDistance({point1:new Point({ x: 1, y: 5 })},{point2:new Point({ x: 1, y: 5 })})).toThrow('points must be instances of Point') + }) +}) + +describe('cacalculateJunctionPoint', () => { + it('should return the Junction Point', () => { + const p1 = new Point({ x: 1, y: 5 }) + const p2 = new Point({ x: 12, y: 5 }) + line1 = new Line({p1,p2,n:5,slope:10}) + line2 = new Line({p1,p2,n:2,slope:12}) + + const result = calculateJunctionPoint(line1,line2) + expect(result).toBeDefined() + expect(typeof(result)).toBe('object') + expect(result instanceof Point).toBe(true) + }) + it('should return false if slopes equales but not the ns',(()=>{ + const p1 = new Point({ x: 1, y: 5 }) + const p2 = new Point({ x: 12, y: 5 }) + line1 = new Line({p1,p2,n:5,slope:10}) + line2 = new Line({p1,p2,n:2,slope:10}) + const result = calculateJunctionPoint(line1,line2) + expect(result).toBeDefined() + expect(typeof(result)).toBe('boolean') + expect(result).toBe(false) + + })) + it('should return true if both slopes and ns equals',(()=>{ + const p1 = new Point({ x: 1, y: 5 }) + const p2 = new Point({ x: 12, y: 5 }) + line1 = new Line({p1,p2,n:5,slope:10}) + line2 = new Line({p1,p2,n:5,slope:10}) + const result = calculateJunctionPoint(line1,line2) + expect(result).toBeDefined() + expect(typeof(result)).toBe('boolean') + expect(result).toBe(true) + + })) + it('should throw an error if no arguments where given',()=>{ + const p1 = new Point({ x: 1, y: 5 }) + const p2 = new Point({ x: 12, y: 5 }) + + line2 = new Line({p1,p2,n:2,slope:10}) + + expect(()=>calculateJunctionPoint(line2)).toThrow('must enter 2 lines') + expect(()=>calculateJunctionPoint()).toThrow('must enter 2 lines') + + }) + it('should throw an error if lines are not instances of Line',()=>{ + + const p1 = new Point({ x: 1, y: 5 }) + const p2 = new Point({ x: 12, y: 5 }) + + expect(()=>calculateJunctionPoint(p1,p2)).toThrow('lines must be instances of Line') + expect(()=>calculateJunctionPoint([new Point({ x: 1, y: 5 })],new Point({ x: 1, y: 5 }))).toThrow('lines must be instances of Line') + expect(()=>calculateJunctionPoint('p1',p2)).toThrow('lines must be instances of Line') + expect(()=>calculateJunctionPoint({point1:new Point({ x: 1, y: 5 })},{point2:new Point({ x: 1, y: 5 })})).toThrow('lines must be instances of Line') + }) +}) +describe('isPointOnLine',()=>{ + it('should return true if slope and n of proxy line equals to the given line',()=>{ + const p1 = new Point({ x: 1, y: 5 }) + const p2 = new Point({ x: 12, y: 5 }) + + line2 = new Line({p1,p2,n:0,slope:5}) + const result =isPointOnLine(line2,p1) + + expect(result).toBeDefined() + expect(result).toBe(true) + + }) + it('should return false when n of line and proxyline are not equales',()=>{ + const p1 = new Point({ x: 1, y: 5 }) + const p2 = new Point({ x: 12, y: 5 }) + + line2 = new Line({p1,p2,n:2,slope:5}) + const result =isPointOnLine(line2,p1) + console.log(result); + expect(result).toBeDefined() + expect(result).toBe(false) + line1 = new Line({p1,p2,n:2,slope:10}) + const result2 =isPointOnLine(line1,p1) + console.log(result2); + expect(result2).toBeDefined() + expect(result2).toBe(false) + + }) + it('should throw an error if type doesnt match the exepted',()=>{ + const p1 = new Point({ x: 1, y: 5 }) + const p2 = new Point({ x: 12, y: 5 }) + + line2 = new Line({p1,p2,n:2,slope:10}) +expect(()=>isPointOnLine(line2,'ll')).toThrow('point must be instance of Point') +expect(()=>isPointOnLine(line2,{point:p1})).toThrow('point must be instance of Point') +expect(()=>isPointOnLine(line2,[p1])).toThrow('point must be instance of Point') + + +expect(()=>isPointOnLine('line2',p1)).toThrow('line must be instance of Line') +expect(()=>isPointOnLine([line1],p1)).toThrow('line must be instance of Line') +expect(()=>isPointOnLine({line2},p1)).toThrow('line must be instance of Line') + }) + it('should throw an error if arguments werent given',()=>{ + const p1 = new Point({ x: 1, y: 5 }) + const p2 = new Point({ x: 12, y: 5 }) + + line2 = new Line({p1,p2,n:2,slope:10}) + expect(()=>isPointOnLine()).toThrow('must get both line and point') + expect(()=>isPointOnLine(line2)).toThrow('must get both line and point') + expect(()=>isPointOnLine(p1)).toThrow('must get both line and point') + + }) +}) \ No newline at end of file diff --git a/tests/line.test.js b/tests/line.test.js new file mode 100644 index 0000000..4542eaa --- /dev/null +++ b/tests/line.test.js @@ -0,0 +1,137 @@ +const Line = require('../modules/ecs6-class/line') +const Point = require('../modules/ecs6-class/point') + +describe('LINE_CLASS', () => { + describe('ctor', () => { + it('should build a new obj', () => { + l = new Line() + expect(l).toBeDefined() + expect(typeof (l)).toBe('object') + expect(l instanceof Line).toBeTruthy() + }) + it('point1 should be type of point ', () => { + point1 = new Point({ x: 1, y: 2 }) + l = new Line({ point1 }) + l2 = new Line() + expect(l).toBeDefined() + expect(typeof (l)).toBe('object') + expect(l.point1 instanceof Point).toBe(true) + expect(l2).toBeDefined() + expect(typeof (l2)).toBe('object') + expect(l2.point2 instanceof Point).toBe(true) + }) + it('point2 should be type of point ', () => { + point2 = new Point({ x: 1, y: 2 }) + + l = new Line({ point2 }) + l2 = new Line() + expect(l).toBeDefined() + expect(typeof (l)).toBe('object') + expect(l.point2 instanceof Point).toBe(true) + expect(l2).toBeDefined() + expect(typeof (l2)).toBe('object') + expect(l2.point2 instanceof Point).toBe(true) + }) + it('should build an empty obj if no parameters were given', () => { + + }) + + }) + describe('calculateSlope', () => { + it('should put the calculated slope into slope', () => { + const l = new Line() + l.calculateSlope() + expect(typeof (l.slope)).toBe('number') + }) + + + }) + describe('calculateNOfLineFunction', () => { + it('should put the calculate N Of Line into n',()=>{ + const l = new Line() + l.calculateNOfLineFunction() + expect(typeof (l.n)).toBe('number') + }) + + }) + describe('getPointByX', () => { + it('should return a point',()=>{ + const l = new Line() + const point = l.getPointByX(1) + expect(point).toBeDefined() + expect(typeof (point)).toBe('object') + expect(point instanceof Point).toBeTruthy() + }) + + }) + it('should calculate x as 0 if no number was given', () => { + const l = new Line() + const point = l.getPointByX() + expect(point).toBeDefined() + expect(typeof (point)).toBe('object') + expect(point instanceof Point).toBeTruthy() + expect(point.x).toBe(0) + }) + it('should throw an error if x is not type of number', () => { + const l = new Line() + + expect(() => l.getPointByX('12')).toThrow('value must be a number') + expect(() => l.getPointByX({ x: 12 })).toThrow('value must be a number') + expect(() => l.getPointByX([12])).toThrow('value must be a number') + + }) + + describe('getPointByY', () => { + it('should return a point',()=>{ + const l = new Line() + const point = l.getPointByX(1) + expect(point).toBeDefined() + expect(typeof (point)).toBe('object') + expect(point instanceof Point).toBeTruthy() + }) + + }) + it('should calculate y as 0 if no number was given', () => { + const l = new Line() + const point = l.getPointByY() + expect(point).toBeDefined() + expect(typeof (point)).toBe('object') + expect(point instanceof Point).toBeTruthy() + expect(point.y).toBe(0) + }) + it('should throw an error if y is not type of number', () => { + const l = new Line() + + expect(() => l.getPointByY('12')).toThrow('value must be a number') + expect(() => l.getPointByY({ x: 12 })).toThrow('value must be a number') + expect(() => l.getPointByY([12])).toThrow('value must be a number') + + }) + + describe('getPointOnXAsis', () => { + it('should call getPointByX',()=>{ + const l = new Line() + const result = l.getPointOnXAsis() + expect(result).toBeDefined() + expect(typeof (result)).toBe('object') + expect(result instanceof Point).toBeTruthy() + + expect(result).toEqual(l.getPointByY(0)) + + }) + + }) + describe('getPointOnYAsis', () => { + it('should call getPointByY',()=>{ + const l = new Line() + const result = l.getPointOnYAsis() + expect(result).toBeDefined() + expect(typeof(result)).toBe('object') + expect(result instanceof Point).toBeTruthy() + console.log({result}); + expect(result).toEqual(l.getPointByX(0)) + + }) + }) + +}) \ No newline at end of file diff --git a/tests/point.test.js b/tests/point.test.js new file mode 100644 index 0000000..1bdd4be --- /dev/null +++ b/tests/point.test.js @@ -0,0 +1,105 @@ + +const Point = require("../modules/ecs6-class/point") + + +describe('POINT_CLASS', () => { + describe('ctor', () => { + + it('should build a new obj', () => { + + p1 = new Point() + + expect(p1).toBeDefined() + expect(typeof (p1)).toBe('object') + expect(p1 instanceof Point).toBe(true) + }) + it('if x was given x should be defined else 0', () => { + p1 = new Point({ x: 1 }) + p2 = new Point() + expect(p1.x).toBeDefined() + expect(p2.x).toBe(0) + + }) + it('if y was given y should be defined else 0', () => { + p1 = new Point({ y: 1 }) + p2 = new Point() + expect(p1.y).toBeDefined() + expect(p2.y).toBe(0) + }) + it('should start x and y with if no object argument was given',()=>{ + p1 = new Point(1,0) + + expect(p1).toBeDefined() + expect(typeof(p1)).toBe('object') + expect(p1 instanceof Point).toBe(true) + expect(p1.x).toBe(0) + expect(p1.y).toBe(0) + + }) + }) + + describe('moveVertical', () => { + it('should move the y with the given value and not change the x', () => { + x = 1 + y = 1 + value = 5 + p1 = new Point({ x, y }) + p1.moveVertical(value) + expect(p1.y).toBeDefined() + expect(p1.y).toBe(y + value) + expect(p1.x).toBe(x) + + }) + it('should do anything if value was not given', () => { + x = 1 + y = 1 + + p1 = new Point({ x, y }) + p1.moveVertical() + expect(p1.y).toBeDefined() + expect(p1.y).toBe(y) + expect(p1.x).toBe(x) + }) + + it('should throw an error if value is not a number', () => { + p1 = new Point({ w: 1, y: 0 }) + expect(()=>p1.moveVertical('1')).toThrow('value must be a number') + expect(()=>p1.moveVertical({ value: 1 })).toThrow('value must be a number') + expect(()=>p1.moveVertical([1])).toThrow('value must be a number') + + }) + }) + describe('moveHorizontal', () => { + it('should move the x with the given value and not change the y', () => { + x = 1 + y = 1 + value = 5 + p1 = new Point({ x, y }) + p1.moveHorizontal(value) + expect(p1.x).toBeDefined() + expect(p1.x).toBe(x + value) + expect(p1.y).toBe(y) + + }) + it('should do anything if value was not given', () => { + x = 1 + y = 1 + p1 = new Point({ x, y }) + p1.moveHorizontal() + expect(p1.y).toBeDefined() + expect(p1.y).toBe(y) + expect(p1.x).toBe(x) + }) + + it('should throw an error if value is not a number', () => { + p1 = new Point({ w: 1, y: 0 }) + expect(()=>p1.moveHorizontal('1')).toThrow('value must be a number') + expect(()=>p1.moveHorizontal({ value: 1 })).toThrow('value must be a number') + expect(()=>p1.moveHorizontal([1])).toThrow('value must be a number') + + }) + }) + + + +}) \ No newline at end of file From a7103139818da92e5e106e4ad3e26f9c04e76807 Mon Sep 17 00:00:00 2001 From: M-Kroizer Date: Sun, 28 Jul 2024 17:28:24 +0300 Subject: [PATCH 2/2] add __mocks__ folder but not everything works well --- modules/ecs6-class/__mocks__/line.js | 72 ++++++ modules/ecs6-class/__mocks__/point.js | 20 ++ modules/ecs6-class/line.js | 10 +- modules/ecs6-class/point.js | 12 +- modules/geometry-calculation.js | 1 + tests/geometry-calculation.test.js | 305 +++++++++++++++++++------- tests/line.test.js | 62 ++++-- tests/point.test.js | 9 + 8 files changed, 391 insertions(+), 100 deletions(-) create mode 100644 modules/ecs6-class/__mocks__/line.js create mode 100644 modules/ecs6-class/__mocks__/point.js diff --git a/modules/ecs6-class/__mocks__/line.js b/modules/ecs6-class/__mocks__/line.js new file mode 100644 index 0000000..3136e23 --- /dev/null +++ b/modules/ecs6-class/__mocks__/line.js @@ -0,0 +1,72 @@ +// // Import this named export into your test file: +// const mockcalculateSlope= jest.fn().mockImplementation(()=>{ +// this.slope = 5 +// }); +// const mockcalculateNOfLineFunction= jest.fn().mockImplementation(()=>{ +// this.n = 0 +// }); + +// const mock = jest.fn().mockImplementation(() => { +// return {constructor:jest.fn().mockImplementation(()=>{console.log('in mocked constructor of line');}), +// calculateSlope: mockcalculateSlope, +// calculateNOfLineFunction:mockcalculateNOfLineFunction, +// getPointByX:jest.fn().mockImplementation(()=>{ +// console.log("mocked!!!!!"); +// }) +// }; +// }); + + +const Point = require("./point"); + +class MockLine { + constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }={}) { + this.point1 = point1; + this.point2 = point2; + this.slope = slope; + this.n = n; + } + + calculateSlope(){ + this.slope=2 + console.log('mocked:)))))))))))'); + console.log(this.slope); + // this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) + return + } + + calculateNOfLineFunction (){ + this.n=10 + // this.n = this.point1.y - this.slope * this.point1.x + return + } + + getPointOnXAsis() { + return this.getPointByY(0) + } + + getPointOnYAsis() { + return this.getPointByX(0) + } + + + getPointByX(x=0) { + if(typeof(x)!='number'){ + throw new Error('value must be a number') + } + let y = this.slope * x + this.n + return new Point({ x, y }) + } + + getPointByY(y=0) { + if(typeof(y)!='number'){ + throw new Error('value must be a number') + } + let x = (y - this.n) / this.slope; + return new Point({ x, y }) + } +} + + + +module.exports = MockLine \ No newline at end of file diff --git a/modules/ecs6-class/__mocks__/point.js b/modules/ecs6-class/__mocks__/point.js new file mode 100644 index 0000000..dcc3536 --- /dev/null +++ b/modules/ecs6-class/__mocks__/point.js @@ -0,0 +1,20 @@ +class MockPoint { + constructor() { + this.x = 0; + this.y = 0; + } + moveVertical(value=0 ) { + if(typeof(value)!='number'){ + throw new Error('value must be a number') + } + this.y += value; + } + moveHorizontal(value=0) { + if(typeof(value)!='number'){ + throw new Error('value must be a number') + } + this.x += value; + } +} + +module.exports = MockPoint; diff --git a/modules/ecs6-class/line.js b/modules/ecs6-class/line.js index b8b61af..f0cee44 100644 --- a/modules/ecs6-class/line.js +++ b/modules/ecs6-class/line.js @@ -2,17 +2,21 @@ const Point = require("./point"); class Line { constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }={}) { + if(!(point1 instanceof Point) || !(point2 instanceof Point)){ + throw new Error('points must be instances of point') + } + this.point1 = point1; this.point2 = point2; this.slope = slope; - this.n = n; + this.n = n } - calculateSlope = () => { + calculateSlope () { this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) } - calculateNOfLineFunction = () => { + calculateNOfLineFunction(){ this.n = this.point1.y - this.slope * this.point1.x } diff --git a/modules/ecs6-class/point.js b/modules/ecs6-class/point.js index e829a76..0bfdc19 100644 --- a/modules/ecs6-class/point.js +++ b/modules/ecs6-class/point.js @@ -1,7 +1,12 @@ class Point { constructor({x=0, y=0}={}) { - this.x = x; - this.y = y; + if(typeof(x)!='number'||typeof(y)!=='number'){ + throw new Error('x and y must be numbers') + } + this.x =x; + this.y = y; + + } moveVertical(value=0 ) { if(typeof(value)!='number'){ @@ -17,4 +22,5 @@ class Point { } } -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 1c7cf27..1312939 100644 --- a/modules/geometry-calculation.js +++ b/modules/geometry-calculation.js @@ -44,6 +44,7 @@ const isPointOnLine = (line, point) => { proxyLine.calculateSlope() if (line.slope === proxyLine.slope) { proxyLine.calculateNOfLineFunction() + if (line.n === proxyLine.n) { return true } diff --git a/tests/geometry-calculation.test.js b/tests/geometry-calculation.test.js index 0bdfefc..466e557 100644 --- a/tests/geometry-calculation.test.js +++ b/tests/geometry-calculation.test.js @@ -1,138 +1,295 @@ + +const { calculateDistance, calculateJunctionPoint, isPointOnLine } = require("../modules/geometry-calculation") +jest.mock('../modules/ecs6-class/point') +jest.unmock('../modules/ecs6-class/line') const Line = require("../modules/ecs6-class/line") const Point = require("../modules/ecs6-class/point") -const { calculateDistance, calculateJunctionPoint, isPointOnLine } = require("../modules/geometry-calculation") - - describe('calculateDistance', () => { +beforeEach(()=>{ + jest.mock('../modules/ecs6-class/line', () => ({ + __esModule: true, + default: class { + constructor() { + console.log('mocked:)'); + + this.n = 5; // Change the value of n + this.slope = 10; // Change the value of slope + } + }, + })); +}) it('should return the distance', () => { const p1 = new Point({ x: 1, y: 5 }) const p2 = new Point({ x: 12, y: 5 }) - const result = calculateDistance(p1,p2) + const result = calculateDistance(p1, p2) expect(result).toBeDefined() - expect(typeof(result)).toBe('number') + expect(typeof (result)).toBe('number') }) - it('should throw an error if no arguments where given',()=>{ + it('should throw an error if no arguments where given', () => { const p1 = new Point({ x: 1, y: 5 }) - expect(()=>calculateDistance(p1)).toThrow('must enter 2 points') - expect(()=>calculateDistance()).toThrow('must enter 2 points') + expect(() => calculateDistance(p1)).toThrow('must enter 2 points') + expect(() => calculateDistance()).toThrow('must enter 2 points') }) - it('should throw an error if points are not instances of Point',()=>{ + it('should throw an error if points are not instances of Point', () => { const p1 = { x: 1, y: 5 } const p2 = { x: 12, y: 5 } - expect(()=>calculateDistance(p1,p2)).toThrow('points must be instances of Point') - expect(()=>calculateDistance([new Point({ x: 1, y: 5 })],new Point({ x: 1, y: 5 }))).toThrow('points must be instances of Point') - expect(()=>calculateDistance('p1',p2)).toThrow('points must be instances of Point') - expect(()=>calculateDistance({point1:new Point({ x: 1, y: 5 })},{point2:new Point({ x: 1, y: 5 })})).toThrow('points must be instances of Point') + expect(() => calculateDistance(p1, p2)).toThrow('points must be instances of Point') + expect(() => calculateDistance([new Point({ x: 1, y: 5 })], new Point({ x: 1, y: 5 }))).toThrow('points must be instances of Point') + expect(() => calculateDistance('p1', p2)).toThrow('points must be instances of Point') + expect(() => calculateDistance({ point1: new Point({ x: 1, y: 5 }) }, { point2: new Point({ x: 1, y: 5 }) })).toThrow('points must be instances of Point') }) }) describe('cacalculateJunctionPoint', () => { it('should return the Junction Point', () => { + const p1 = new Point({ x: 1, y: 5 }) - const p2 = new Point({ x: 12, y: 5 }) - line1 = new Line({p1,p2,n:5,slope:10}) - line2 = new Line({p1,p2,n:2,slope:12}) + const p2 = new Point({ x: 12, y: 5 }) + jest.unmock('../modules/ecs6-class/line'); // Unmock the manual mock + + // Modify the manual mock directly for this test case + jest.mock('../modules/ecs6-class/line', () => ({ + __esModule: true, + default: class { + constructor() { + console.log('mocked:)'); + + this.n = 5; // Change the value of n + this.slope = 10; // Change the value of slope + } + }, + })); + + + jest.mock('../modules/ecs6-class/line', () => require('../__mocks__/line.custom.mock')) - const result = calculateJunctionPoint(line1,line2) + const line1 = new Line({ p1, p2, n: 5, slope: 10 }) + jest.unmock('../modules/ecs6-class/line'); // Unmock the manual mock + + // // Modify the manual mock directly for this test case + jest.mock('../modules/ecs6-class/line', () => ({ + __esModule: true, + default: class { + constructor() { + console.log('mocked:)'); + + this.n = 2; // Change the value of n + this.slope = 12; // Change the value of slope + } + }, + })); + + + const line2 = new Line({ p1, p2, n: 2, slope: 12 }) + + const result = calculateJunctionPoint(line1, line2) expect(result).toBeDefined() - expect(typeof(result)).toBe('object') - expect(result instanceof Point).toBe(true) + expect(typeof (result)).toBe('object') + // expect(result instanceof Point).toBe(true) + // expect(mockgetPointByX).toHaveBeenCalled() }) - it('should return false if slopes equales but not the ns',(()=>{ + it('should return false if slopes equales but not the ns', (() => { const p1 = new Point({ x: 1, y: 5 }) - const p2 = new Point({ x: 12, y: 5 }) - line1 = new Line({p1,p2,n:5,slope:10}) - line2 = new Line({p1,p2,n:2,slope:10}) - const result = calculateJunctionPoint(line1,line2) + const p2 = new Point({ x: 12, y: 5 }) + + // jest.unmock('../modules/ecs6-class/line'); // Unmock the manual mock + + // Modify the manual mock directly for this test case + jest.mock('../modules/ecs6-class/line', () => ({ + __esModule: true, + default: class { + constructor() { + console.log('mocked:)'); + + this.n = 5; // Change the value of n + this.slope = 10; // Change the value of slope + } + }, + })); + + // jest.mock('../modules/ecs6-class/line', () => require('../__mocks__/line.custom.mock')) + + const line1 = new Line({ p1, p2, n: 5, slope: 10 }) + jest.unmock('../modules/ecs6-class/line'); // Unmock the manual mock + + // Modify the manual mock directly for this test case + jest.mock('../modules/ecs6-class/line', () => ({ + __esModule: true, + default: class { + constructor() { + console.log('mocked:)'); + + this.n =2; // Change the value of n + this.slope = 10; // Change the value of slope + } + }, + })); + + // jest.mock('../modules/ecs6-class/line') + + const line2 = new Line({ p1, p2, n: 2, slope: 10 }) + const result = calculateJunctionPoint(line1, line2) expect(result).toBeDefined() - expect(typeof(result)).toBe('boolean') + expect(typeof (result)).toBe('boolean') expect(result).toBe(false) })) - it('should return true if both slopes and ns equals',(()=>{ + it('should return true if both slopes and ns equals', (() => { const p1 = new Point({ x: 1, y: 5 }) - const p2 = new Point({ x: 12, y: 5 }) - line1 = new Line({p1,p2,n:5,slope:10}) - line2 = new Line({p1,p2,n:5,slope:10}) - const result = calculateJunctionPoint(line1,line2) + const p2 = new Point({ x: 12, y: 5 }) + + const line1 = new Line({ p1, p2, n: 5, slope: 10 }) + const line2 = new Line({ p1, p2, n: 5, slope: 10 }) + const result = calculateJunctionPoint(line1, line2) expect(result).toBeDefined() - expect(typeof(result)).toBe('boolean') + expect(typeof (result)).toBe('boolean') expect(result).toBe(true) - + })) - it('should throw an error if no arguments where given',()=>{ + it('should throw an error if no arguments where given', () => { const p1 = new Point({ x: 1, y: 5 }) - const p2 = new Point({ x: 12, y: 5 }) - - line2 = new Line({p1,p2,n:2,slope:10}) + const p2 = new Point({ x: 12, y: 5 }) + + const line2 = new Line({ p1, p2, n: 2, slope: 10 }) - expect(()=>calculateJunctionPoint(line2)).toThrow('must enter 2 lines') - expect(()=>calculateJunctionPoint()).toThrow('must enter 2 lines') + expect(() => calculateJunctionPoint(line2)).toThrow('must enter 2 lines') + expect(() => calculateJunctionPoint()).toThrow('must enter 2 lines') }) - it('should throw an error if lines are not instances of Line',()=>{ + it('should throw an error if lines are not instances of Line', () => { const p1 = new Point({ x: 1, y: 5 }) - const p2 = new Point({ x: 12, y: 5 }) + const p2 = new Point({ x: 12, y: 5 }) - expect(()=>calculateJunctionPoint(p1,p2)).toThrow('lines must be instances of Line') - expect(()=>calculateJunctionPoint([new Point({ x: 1, y: 5 })],new Point({ x: 1, y: 5 }))).toThrow('lines must be instances of Line') - expect(()=>calculateJunctionPoint('p1',p2)).toThrow('lines must be instances of Line') - expect(()=>calculateJunctionPoint({point1:new Point({ x: 1, y: 5 })},{point2:new Point({ x: 1, y: 5 })})).toThrow('lines must be instances of Line') + expect(() => calculateJunctionPoint(p1, p2)).toThrow('lines must be instances of Line') + expect(() => calculateJunctionPoint([new Point({ x: 1, y: 5 })], new Point({ x: 1, y: 5 }))).toThrow('lines must be instances of Line') + expect(() => calculateJunctionPoint('p1', p2)).toThrow('lines must be instances of Line') + expect(() => calculateJunctionPoint({ point1: new Point({ x: 1, y: 5 }) }, { point2: new Point({ x: 1, y: 5 }) })).toThrow('lines must be instances of Line') }) }) -describe('isPointOnLine',()=>{ - it('should return true if slope and n of proxy line equals to the given line',()=>{ +describe('isPointOnLine', () => { + it('should return true if slope and n of proxy line equals to the given line', () => { const p1 = new Point({ x: 1, y: 5 }) - const p2 = new Point({ x: 12, y: 5 }) + const p2 = new Point({ x: 12, y: 5 }) - line2 = new Line({p1,p2,n:0,slope:5}) - const result =isPointOnLine(line2,p1) + const line2 = new Line({ p1, p2, n: 0, slope: 5 }) + const result = isPointOnLine(line2, p1) + // this.n = 5 - 5* 1=0 + // this.slope = (5 - 5) / (1- 12)=5 + expect(result).toBeDefined() expect(result).toBe(true) + // expect(mockcalculateNOfLineFunction).toHaveBeenCalled() + // expect(mockcalcalculateSlope).toHaveBeenCalled() }) - it('should return false when n of line and proxyline are not equales',()=>{ + it('should return false when n of line and proxyline are not equales', () => { const p1 = new Point({ x: 1, y: 5 }) - const p2 = new Point({ x: 12, y: 5 }) + const p2 = new Point({ x: 12, y: 5 }) + // this.slope = (5 - 5) / (1 - 12)=1/-11 + jest.unmock('../modules/ecs6-class/line'); // Unmock the manual mock + + // Modify the manual mock directly for this test case + jest.mock('../modules/ecs6-class/line', () => ({ + __esModule: true, + default: class { + constructor() { + console.log('mocked:)'); + + this.n =2; // Change the value of n + this.slope = 5; // Change the value of slope + } + }, + })); + + + + const line2 = new Line({ p1, p2, n: 2, slope: 5 }) + jest.unmock('../modules/ecs6-class/line'); // Unmock the manual mock + + // Modify the manual mock directly for this test case + jest.mock('../modules/ecs6-class/line', () => ({ + __esModule: true, + default: class { + constructor() { + console.log('mocked:)'); + + this.n =0; // Change the value of n + this.slope = 5; // Change the value of slope + } + }, + })); - line2 = new Line({p1,p2,n:2,slope:5}) - const result =isPointOnLine(line2,p1) - console.log(result); + const result = isPointOnLine(line2, p1) expect(result).toBeDefined() expect(result).toBe(false) - line1 = new Line({p1,p2,n:2,slope:10}) - const result2 =isPointOnLine(line1,p1) - console.log(result2); + // expect(mockcalcalculateSlope).toHaveBeenCalled() + + jest.unmock('../modules/ecs6-class/line'); // Unmock the manual mock + + // Modify the manual mock directly for this test case + jest.mock('../modules/ecs6-class/line', () => ({ + __esModule: true, + default: class { + constructor() { + console.log('mocked:)'); + + this.n = 2; // Change the value of n + this.slope = 10; // Change the value of slope + } + }, + })); + + + const line1 = new Line({ p1, p2, n: 2, slope: 10 }) + + jest.unmock('../modules/ecs6-class/line'); // Unmock the manual mock + + // Modify the manual mock directly for this test case + jest.mock('../modules/ecs6-class/line', () => ({ + __esModule: true, + default: class { + constructor() { + console.log('mocked:)'); + this.n = 0; // Change the value of n + this.slope =5; // Change the value of slope + } + }, + })); + + + const result2 = isPointOnLine(line1, p1) expect(result2).toBeDefined() expect(result2).toBe(false) + // expect(mockcalcalculateSlope).toHaveBeenCalled() + }) - it('should throw an error if type doesnt match the exepted',()=>{ + it('should throw an error if type doesnt match the exepted', () => { const p1 = new Point({ x: 1, y: 5 }) - const p2 = new Point({ x: 12, y: 5 }) - - line2 = new Line({p1,p2,n:2,slope:10}) -expect(()=>isPointOnLine(line2,'ll')).toThrow('point must be instance of Point') -expect(()=>isPointOnLine(line2,{point:p1})).toThrow('point must be instance of Point') -expect(()=>isPointOnLine(line2,[p1])).toThrow('point must be instance of Point') + const p2 = new Point({ x: 12, y: 5 }) + + const line2 = new Line({ p1, p2, n: 2, slope: 10 }) + expect(() => isPointOnLine(line2, 'll')).toThrow('point must be instance of Point') + expect(() => isPointOnLine(line2, { point: p1 })).toThrow('point must be instance of Point') + expect(() => isPointOnLine(line2, [p1])).toThrow('point must be instance of Point') -expect(()=>isPointOnLine('line2',p1)).toThrow('line must be instance of Line') -expect(()=>isPointOnLine([line1],p1)).toThrow('line must be instance of Line') -expect(()=>isPointOnLine({line2},p1)).toThrow('line must be instance of Line') + expect(() => isPointOnLine('line2', p1)).toThrow('line must be instance of Line') + expect(() => isPointOnLine([line2], p1)).toThrow('line must be instance of Line') + expect(() => isPointOnLine({ line2 }, p1)).toThrow('line must be instance of Line') }) - it('should throw an error if arguments werent given',()=>{ + it('should throw an error if arguments werent given', () => { const p1 = new Point({ x: 1, y: 5 }) - const p2 = new Point({ x: 12, y: 5 }) - - line2 = new Line({p1,p2,n:2,slope:10}) - expect(()=>isPointOnLine()).toThrow('must get both line and point') - expect(()=>isPointOnLine(line2)).toThrow('must get both line and point') - expect(()=>isPointOnLine(p1)).toThrow('must get both line and point') + const p2 = new Point({ x: 12, y: 5 }) + + const line2 = new Line({ p1, p2, n: 2, slope: 10 }) + expect(() => isPointOnLine()).toThrow('must get both line and point') + expect(() => isPointOnLine(line2)).toThrow('must get both line and point') + expect(() => isPointOnLine(p1)).toThrow('must get both line and point') }) }) \ No newline at end of file diff --git a/tests/line.test.js b/tests/line.test.js index 4542eaa..5b958f7 100644 --- a/tests/line.test.js +++ b/tests/line.test.js @@ -1,7 +1,21 @@ const Line = require('../modules/ecs6-class/line') + const Point = require('../modules/ecs6-class/point') +jest.mock('../modules/ecs6-class/point') +jest.unmock('../modules/ecs6-class/line') +const mockgetPointByX = jest + .spyOn(Line.prototype, 'getPointByX') + +const mockgetPointByY = jest + .spyOn(Line.prototype, 'getPointByY') + +// TODO to mock point class + describe('LINE_CLASS', () => { + // beforeEach(() => { + + // }) describe('ctor', () => { it('should build a new obj', () => { l = new Line() @@ -35,6 +49,14 @@ describe('LINE_CLASS', () => { it('should build an empty obj if no parameters were given', () => { }) + it('should throw an error if points are not points',()=>{ + + + expect(()=>new Line({point1:(1,2),point2:2})).toThrow('points must be instances of point') + expect(()=>new Line({point1:[1,2]})).toThrow('points must be instances of point') + + + }) }) describe('calculateSlope', () => { @@ -47,22 +69,22 @@ describe('LINE_CLASS', () => { }) describe('calculateNOfLineFunction', () => { - it('should put the calculate N Of Line into n',()=>{ + it('should put the calculate N Of Line into n', () => { const l = new Line() l.calculateNOfLineFunction() expect(typeof (l.n)).toBe('number') }) - + }) describe('getPointByX', () => { - it('should return a point',()=>{ + it('should return a point', () => { const l = new Line() const point = l.getPointByX(1) expect(point).toBeDefined() expect(typeof (point)).toBe('object') expect(point instanceof Point).toBeTruthy() }) - + }) it('should calculate x as 0 if no number was given', () => { const l = new Line() @@ -82,14 +104,14 @@ describe('LINE_CLASS', () => { }) describe('getPointByY', () => { - it('should return a point',()=>{ - const l = new Line() - const point = l.getPointByX(1) - expect(point).toBeDefined() - expect(typeof (point)).toBe('object') - expect(point instanceof Point).toBeTruthy() + it('should return a point', () => { + const l = new Line() + const point = l.getPointByX(1) + expect(point).toBeDefined() + expect(typeof (point)).toBe('object') + expect(point instanceof Point).toBeTruthy() }) - + }) it('should calculate y as 0 if no number was given', () => { const l = new Line() @@ -109,29 +131,29 @@ describe('LINE_CLASS', () => { }) describe('getPointOnXAsis', () => { - it('should call getPointByX',()=>{ + it('should call getPointByX', () => { const l = new Line() const result = l.getPointOnXAsis() expect(result).toBeDefined() expect(typeof (result)).toBe('object') expect(result instanceof Point).toBeTruthy() - - expect(result).toEqual(l.getPointByY(0)) + expect(mockgetPointByY).toHaveBeenCalled() + // expect(result).toEqual(l.getPointByY(0)) }) - + }) describe('getPointOnYAsis', () => { - it('should call getPointByY',()=>{ + it('should call getPointByY', () => { const l = new Line() const result = l.getPointOnYAsis() expect(result).toBeDefined() - expect(typeof(result)).toBe('object') + expect(typeof (result)).toBe('object') expect(result instanceof Point).toBeTruthy() - console.log({result}); - expect(result).toEqual(l.getPointByX(0)) + // expect(result).toEqual(l.getPointByX(0)) + expect(mockgetPointByX).toHaveBeenCalled() }) }) - + }) \ No newline at end of file diff --git a/tests/point.test.js b/tests/point.test.js index 1bdd4be..bcb3d5d 100644 --- a/tests/point.test.js +++ b/tests/point.test.js @@ -35,6 +35,15 @@ describe('POINT_CLASS', () => { expect(p1.x).toBe(0) expect(p1.y).toBe(0) + }) + it('should throw an error if x or y are not numbers',()=>{ + + + expect(()=>new Point({x:{},y:{}})).toThrow('x and y must be numbers') + expect(()=>new Point({x:[1],y:[2]})).toThrow('x and y must be numbers') + expect(()=>new Point({x:'1',y:'5'})).toThrow('x and y must be numbers') + + }) })