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
72 changes: 72 additions & 0 deletions modules/ecs6-class/__mocks__/line.js
Original file line number Diff line number Diff line change
@@ -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!!!!!");
// })
// };
// });

Copy link
Owner

Choose a reason for hiding this comment

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

Why do you push remarks?


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
20 changes: 20 additions & 0 deletions modules/ecs6-class/__mocks__/point.js
Original file line number Diff line number Diff line change
@@ -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;
22 changes: 16 additions & 6 deletions modules/ecs6-class/line.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
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 }={}) {
if(!(point1 instanceof Point) || !(point2 instanceof Point)){
throw new Error('points must be instances of point')
Copy link
Owner

Choose a reason for hiding this comment

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

Which point throws the error?

}

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)
Copy link
Owner

Choose a reason for hiding this comment

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

did you check that the x for both points are not identical? =>should divide by zero

}

calculateNOfLineFunction = () => {
calculateNOfLineFunction(){
this.n = this.point1.y - this.slope * this.point1.x
Copy link
Owner

Choose a reason for hiding this comment

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

is the slope calculated??

}

Expand All @@ -25,12 +29,18 @@ class Line {
}


getPointByX(x) {
getPointByX(x=0) {
if(typeof(x)!='number'){
throw new Error('value must be a number')
Copy link
Owner

Choose a reason for hiding this comment

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

use always !== or ===

}
let y = this.slope * x + this.n
Copy link
Owner

Choose a reason for hiding this comment

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

are the n and slope calculated?

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;
Copy link
Owner

Choose a reason for hiding this comment

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

are the n and the slope calculated?

return new Point({ x, y })
}
Expand Down
22 changes: 17 additions & 5 deletions modules/ecs6-class/point.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
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) {
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;
}
}

module.exports = Point
module.exports = Point;

22 changes: 20 additions & 2 deletions modules/geometry-calculation.js
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Owner

Choose a reason for hiding this comment

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

Are the slopes calculated?

if (line1.n === line2.n) {
return true
Expand All @@ -24,15 +34,23 @@ 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()
Copy link
Owner

Choose a reason for hiding this comment

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

Is the slope calculated?

if (line.slope === proxyLine.slope) {
proxyLine.calculateNOfLineFunction()

if (line.n === proxyLine.n) {
return true
}
}
return false

}

module.exports = {
Expand Down
3 changes: 2 additions & 1 deletion 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",
"coverage":"npm test -- --coverage"
},
"dependencies": {
"jest": "^29.7.0"
Copy link
Owner

Choose a reason for hiding this comment

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

this is really not the good place

Expand Down
Loading