-
Notifications
You must be signed in to change notification settings - Fork 41
my tests #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
my tests #35
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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!!!!!"); | ||
| // }) | ||
| // }; | ||
| // }); | ||
|
|
||
|
|
||
| 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 | ||
| 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; |
| 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') | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did you check that the |
||
| } | ||
|
|
||
| calculateNOfLineFunction = () => { | ||
| calculateNOfLineFunction(){ | ||
| this.n = this.point1.y - this.slope * this.point1.x | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is the slope calculated?? |
||
| } | ||
|
|
||
|
|
@@ -25,12 +29,18 @@ class Line { | |
| } | ||
|
|
||
|
|
||
| getPointByX(x) { | ||
| getPointByX(x=0) { | ||
| if(typeof(x)!='number'){ | ||
| throw new Error('value must be a number') | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use always !== or === |
||
| } | ||
| let y = this.slope * x + this.n | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are the |
||
| 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; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are the |
||
| return new Point({ x, y }) | ||
| } | ||
|
|
||
| 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; | ||
|
|
| 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) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are the slopes calculated? |
||
| if (line1.n === line2.n) { | ||
| return true | ||
|
|
@@ -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() | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is really not the good place |
||
|
|
||
There was a problem hiding this comment.
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?