Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions modules/ecs6-class/line.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Point = require("./point");

class Line {

constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) {
this.point1 = point1;
this.point2 = point2;
Expand All @@ -9,6 +10,8 @@ class Line {
}

calculateSlope = () => {
if (this.point1.x === this.point2.x)
throw new Error("don't divide by 0")
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
}

Expand All @@ -31,9 +34,11 @@ class Line {
}

getPointByY(y) {
let x = (y - this.slope) / this.n;
if (this.slope === 0)
throw new Error("don't divide by 0")
let x = (y - this.n) / this.slope;
return new Point({ x, y })
}
}

module.exports = Line
module.exports = Line
10 changes: 8 additions & 2 deletions modules/ecs6-class/point.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
class Point {
constructor({x=0, y=0}={}) {
constructor({ x = 0, y = 0 } = {}) {
if (!(typeof (x) === "number") || !(typeof (y) === "number"))
throw new Error('x and y must be of the number type')
this.x = x;
this.y = y;
}
moveVertical(value) {
if (!(typeof (value) === "number"))
throw new Error('value must be of the number type')
this.y += value;
}
moveHorizontal(value) {
if (!(typeof (value) === "number"))
throw new Error('value must be of the number type')
this.x += value;
}
}

module.exports = Point
module.exports = Point
15 changes: 13 additions & 2 deletions modules/geometry-calculation.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
const Line = require('./ecs6-class/line')
const Line = require('./ecs6-class/line');
const Point = require('./ecs6-class/point');

const calculateDistance = (point1, point2) => {
if (!(point1 instanceof Point) || !(point2 instanceof Point)) {
throw new Error('Invalid input. Expected objects of type point.');
}
let distanceX = (point2.x - point1.x) ** 2;
let distanceY = (point2.y - point2.y) ** 2;
let distanceY = (point2.y - point1.y) ** 2;
const distance = Math.sqrt(distanceX + distanceY);
return distance;
}

const calculateJunctionPoint = (line1, line2) => {
if (!(line1 instanceof Line) || !(line2 instanceof Line))
throw new Error('Must be of the Line type')
if (line1.slope === line2.slope) {
if (line1.n === line2.n) {
return true
Expand All @@ -24,6 +30,11 @@ const calculateJunctionPoint = (line1, line2) => {
}

const isPointOnLine = (line, point) => {

if (!(line instanceof Line) || !(point instanceof Point)) {
throw new Error("The objects should be of the Line and Point types")
}

const proxyLine = new Line({ point1: line.point1, point2: point })
proxyLine.calculateSlope()
if (line.slope === proxyLine.slope) {
Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -18,6 +19,9 @@
},
"homepage": "https://github.com/gemtechd/build-tests#readme",
"author": "gemtechd",
"license": "ISC"

"license": "ISC",
"directories": {
"test": "tests"
},
"devDependencies": {}
}
82 changes: 82 additions & 0 deletions tests/modules/ecs6-class/line.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const Line = require('../../../modules/ecs6-class/line')
const Point = require('../../../modules/ecs6-class/point')

describe('LINE', () => {
describe('CONSTRUCTOR', () => {
it('should instantiate the class with the correct values', () => {
const l = new Line({ slope: 3, n: 7 });
expect(l.slope).toBe(3);
expect(l.n).toBe(7);
});
}),
describe('CALCULATE_N_Of_LINE_FUNCTION', () => {

it('calculateNOfLineFunction calculates the correct value of n', () => {
const line = new Line({ point1: { x: 0, y: 0 }, point2: { x: 2, y: 2 } });
line.calculateSlope();
line.calculateNOfLineFunction();
expect(line.n).toBe(0);
});
}),
describe('CALCULATE_SLOPE', () => {
it('should return error if the point1.x - point2.x =0', () => {
const point1 = new Point({ x: 5, y: 9 })
const point2 = new Point({ x: 5, y: 2 })
const line = new Line({ point1: point1, point2: point2 })
expect(() => line.calculateSlope()).toThrowError("don't divide by 0")
})
it('should return if the function calculate slope between two points', () => {
const point1 = new Point({ x: 1, y: 2 });
const point2 = new Point({ x: 3, y: 4 });
const line = new Line({ point1, point2 });
line.calculateSlope();
expect(line.slope).toBe(1);
});
}),
describe('GET_POINT_ON_Y_AXIS', () => {
it('Get Point on Y axis', () => {
const line = new Line({ slope: 1, n: 0 });
const point = line.getPointOnYAsis();
expect(point.x).toBe(0);
expect(point.y).toBe(0);
});
}),
describe('GET_POINT_BY_Y', () => {
it('Get Point by Y', () => {
const line = new Line({ slope: 2, n: 3 });
const expectedPoint = new Point({ x: 1, y: 5 });
const point = line.getPointByY(5);
expect(point.x).toBe(expectedPoint.x);
expect(point.y).toBe(expectedPoint.y);
});
it('should return error.message if the slope = 0', () => {
const line = new Line({ n: 1, slope: 0 })
expect(() => line.getPointByY(5)).toThrowError("don't divide by 0")
});
}),
describe('GET_POINT_BY_X', () => {
it('get point by x', () => {
const line = new Line({ slope: 1, n: 1 })
const point = line.getPointByX(2)
expect(point.x).toBe(2)
expect(point.y).toBe(3)
});
}),

describe('GET_POINT_ON_X_AXIS', () => {
it('Get Point on X Axis', () => {
const line = new Line({ slope: 2, n: 3 });
const expectedPoint = line.getPointByY(0);
const point = line.getPointOnXAsis();
expect(point.x).toBe(expectedPoint.x);
expect(point.y).toBe(expectedPoint.y);
});
})

})






56 changes: 56 additions & 0 deletions tests/modules/ecs6-class/point.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const Point = require('../../../modules/ecs6-class/point')

describe('POINT', () => {
describe('CONSTRUCTOR', () => {
it('Check if a point is created with default values', () => {
const point = new Point();
expect(point.x).toBe(0);
expect(point.y).toBe(0);
});
it('should return x=5 and y=6', () => {
const point = new Point({ x: 5, y: 6 })
expect(point.x).toBe(5)
expect(point.y).toBe(6)
})
it('should return error.message if the values is not number type', () => {
const invalidX = 'hello';
const invalidY = true;
expect(() => new Point({ x: invalidX, y: invalidY })).toThrowError('x and y must be of the number type');
})
it('should return error.message if the values is not number type', () => {
const invalidX = 'hello';
const invalidY = 5;
expect(() => new Point({ x: invalidX, y: invalidY })).toThrowError('x and y must be of the number type');
})
it('should return error.message if the values is not number type', () => {
const invalidX = 5;
const invalidY = 'hello';
expect(() => new Point({ x: invalidX, y: invalidY })).toThrowError('x and y must be of the number type');
})
}),
describe('MOVE_VETRCAL', () => {
it('should return error.meaasge if the value is not number type', () => {
const point = new Point({ x: 1, y: 2 });
const value = "3";
expect(() => point.moveVertical(value)).toThrowError('value must be of the number type');
})
it('Move Point Vertically', () => {
const point = new Point({ x: 1, y: 2 });
point.moveVertical(3);
expect(point.y).toBe(5);
});
}),
describe('HORIZONTA', () => {
it('should return error.meaasge if the value is not number', () => {
const point = new Point({ x: 1, y: 2 });
const value = "3";
expect(() => point.moveHorizontal(value)).toThrowError('value must be of the number type');
})
it('Move Point Horizontally', () => {
const point = new Point({ x: 2, y: 3 });
point.moveHorizontal(-1);
expect(point.x).toBe(1);
});

})
})
75 changes: 75 additions & 0 deletions tests/modules/geometry-calucation.test.js
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are the mocks for the inner function?

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const Point = require('../../modules/ecs6-class/point')
const Line = require('../../modules/ecs6-class/line')
const { calculateDistance, isPointOnLine, calculateJunctionPoint } = require('../../modules/geometry-calculation')
describe('GEOMETRY_CALCULATION', () => {
describe('CALCULATE_DISTANCE', () => {
it('should return error.message', () => {
const point1 = new Point({ x: 2, y: 3 });
const point2 = new Line({ x: 5, y: 7 });
expect(() => calculateDistance(point1, point2)).toThrowError('Invalid input. Expected objects of type point.');
});
it('should calculate the correct distance between two points', () => {
const result = calculateDistance(point1 = new Point({ x: 6, y: 8 }), point2 = new Point({ x: 2, y: 5 }));
expect(result).toBe(5)
});
}),
describe('IS_POINT_ON_LINE', () => {
it('should return error if the objects are not Line or Point types', function () {
const line = new Line({ point1: new Point({ x: 2, y: 3 }), point2: new Point({ x: 4, y: 6 }) });
const point = new Line({ point1: new Point({ x: 0, y: 0 }) });
expect(() => isPointOnLine(line, point)).toThrowError("The objects should be of the Line and Point types");
});
it('should return true if the point lies on the line', () => {
const line = new Line({ point1: { x: 2, y: 5 }, slope: 2, n: 1 });
const point = new Point({ x: 3, y: 7 });
const result = isPointOnLine(line, point);
expect(result).toBe(true);
});
it('should return false if the point does not lie on the line', () => {
const line = new Line({ slope: 2, n: 1 });
const point = new Point({ x: 1, y: 3 });
const result = isPointOnLine(line, point);
expect(result).toBe(false);
});
it('should return false if the point is parallels', () => {
const line = new Line({ point1: { x: 6, y: 13 }, slope: 2, n: 1 });
const point = new Point({ x: 1, y: 5 });
const result = isPointOnLine(line, point);
expect(result).toBe(false);
})
it('should return false if the point does not lie on the line', () => {
const line = new Line({ slope: 4, n: -1 });
const point = new Point({ x: 1, y: 4 });
const result = isPointOnLine(line, point);
expect(result).toBe(false);
});
}),
describe('CALCULATE_JUNCTION_POINT', () => {
it('should return error if it is not of the Line type', () => {
const line1 = new Line({ slope: 2, n: 4 });
const line2 = new Point({ x: 3, y: 5 });
expect(() => calculateJunctionPoint(line1, line2)).toThrowError('Must be of the Line type');

});
it('should return true if slopes and intercepts are equal', () => {
const line1 = new Line({ slope: 2, n: 1 });
const line2 = new Line({ slope: 2, n: 1 });
const result = calculateJunctionPoint(line1, line2);
expect(result).toBe(true);
});
it('should return false if slopes are equal but intercepts are different', () => {
const line1 = new Line({ slope: 2, n: 1 });
const line2 = new Line({ slope: 2, n: 5 });
const result = calculateJunctionPoint(line1, line2);
expect(result).toBe(false);
});
it('should calculate the junction point correctly for two lines with different slopes', () => {
const line1 = new Line({ slope: 2, n: 1, getPointByX: function (x) { return { x: x, y: 2 * x + 1 } } });
const line2 = new Line({ slope: -1, n: 7, getPointByX: function (x) { return { x: x, y: -x + 7 } } });
const result = calculateJunctionPoint(line1, line2);
expect(result).toEqual({ x: 2, y: 5 });
});
});
})