-
Notifications
You must be signed in to change notification settings - Fork 41
add tests #13
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?
add tests #13
Conversation
gemtechd
left a comment
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.
See the comments
modules/ecs6-class/line.js
Outdated
| if(typeof(n)!='number'&& typeof(n)!='undefined'){ | ||
| throw Error('The n should have a number') | ||
| } | ||
| if(typeof(slope)!='number'&& typeof(slope)!='undefined'){ |
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.
use the three equals operator
modules/ecs6-class/line.js
Outdated
|
|
||
| getPointOnXAsis() { | ||
| return this.getPointByY(0) | ||
| getPointOnXAsis(func=this.getPointByY(0)) { |
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 did you add this code, no need
|
|
||
| const calculateDistance = (point1, point2) => { | ||
| if(typeof(point1)!='object'||typeof(point1)!='object'){ | ||
| throw Error('The value is of an invalid type') |
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.
Which of the point is invalid?
You can do a better check
modules/geometry-calculation.js
Outdated
| throw Error('The value is of an invalid type') | ||
| } | ||
| if(!point1.x||!point2.x){ | ||
| throw Error('The value is of an invalid type') |
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.
And what with the y value?
check in your first if not only the object but deeper
modules/geometry-calculation.js
Outdated
| const calculateJunctionPoint = (line1, line2) => { | ||
| const calculateJunctionPoint = (line1, line2,func) => { | ||
| if(typeof(line1)!='object'||typeof(line2)!='object'){ | ||
| throw Error('The value is of an invalid type') |
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.
Check deeper and not only object
modules/geometry-calculation.js
Outdated
| } | ||
|
|
||
| const isPointOnLine = (line, point) => { | ||
| const isPointOnLine = (line, point,func1,func2) => { |
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.
don't send this funcs, in the tests use real mocks
gemtechd
left a comment
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.
Update your code, and see if you hve 100 coverage
modules/ecs6-class/line.js
Outdated
| if(typeof(point1.x)!='number'||typeof(point2.x)!='number'){ | ||
| constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined } = {}) { | ||
| if (!(point1 instanceof Point) || !(point2 instanceof Point)) { | ||
| throw Error('The points should have Point type') |
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.
Which one threw the error, point1 or point2?
modules/ecs6-class/line.js
Outdated
| this.n = this.point1.y - this.slope * this.point1.x | ||
| calculateNOfLineFunction() { | ||
|
|
||
| return this.n = this.point1.y - this.slope * this.point1.x |
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.
What happens if the slope is not calculated yet?
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.
you really don't need to do it each time, only in case that it is not calculated
| throw Error('The value is of an invalid type') | ||
| } | ||
| let y = this.slope * x + this.n | ||
| return new Point({ x, y }) |
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.
what happens if the slope or the line are not calculated yet?
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.
first check what is the n value
you can call only the n, it will calculate the slope if the slope is undefined
| if (typeof (y) !== 'number') { | ||
| throw Error('The value is of an invalid type') | ||
| } | ||
| let x = (y - this.n) / this.slope; |
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.
what happens when the slope is not calculated or is zero?
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.
The same as the previous remark
| let point2 = new Point({ x: 3, y: 8 }); | ||
| let line1 = new Line({ point1, point2, n: 9.5, slope: -0.5 }); | ||
| expect(line1.getPointOnXAsis(mock)).toEqual({ x: 19, y: 0 }) | ||
| test('It should be returned if in the call to the function the function getPointByY was called', () => { |
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.
use the it function
|
|
||
| const getPointByXMock = jest | ||
| .spyOn(Line.prototype, 'getPointByX') | ||
|
|
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.
where do you restore the mocks?
I prefer using the jest.mock and not the spyOn
but if you use it, restore it
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.
use this mocks inside the test where they are needed
| @@ -39,46 +45,70 @@ describe('Check calculateNOfLineFunction function', () => { | |||
| }) | |||
|
|
|||
| describe('Check getPointOnXAsis function', () => { | |||
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.
You are testing only if the mock was called, but where are the tests about the values of the function??
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.
Call the test functions with the it expression
| const calculateNOfLineFunctionMock = jest | ||
| .spyOn(Line.prototype, 'calculateNOfLineFunction').mockImplementation() | ||
| .mockReturnValue(9) | ||
|
|
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.
I think it's better to mock them each time again and restore
you cannot hold how many times you'll use them
and each time you can expect a different value
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.
Do I need to write the mock function many times?
And each time give a different value?
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.
mock the class once with jest.mock, and each time mocktheReturnValue for each function
gemtechd
left a comment
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.
See the comments
the changed is the code are important
modules/ecs6-class/line.js
Outdated
| this.n = this.point1.y - this.slope * this.point1.x | ||
| calculateNOfLineFunction() { | ||
|
|
||
| return this.n = this.point1.y - this.slope * this.point1.x |
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.
you really don't need to do it each time, only in case that it is not calculated
| throw Error('The value is of an invalid type') | ||
| } | ||
| let y = this.slope * x + this.n | ||
| return new Point({ x, y }) |
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.
first check what is the n value
you can call only the n, it will calculate the slope if the slope is undefined
| if (typeof (y) !== 'number') { | ||
| throw Error('The value is of an invalid type') | ||
| } | ||
| let x = (y - this.n) / this.slope; |
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.
The same as the previous remark
modules/geometry-calculation.js
Outdated
| line1.calculateSlope() | ||
| line2.calculateSlope() | ||
| line1.calculateNOfLineFunction() | ||
| line2.calculateNOfLineFunction() |
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.
the same here, we don't call a function if it is not necessary
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.
But the teacher wrote that you should check if the slope and n are correct.
If the user entered wrong values?
and also
If the user entered only n and no slope - the slope will not be calculated.
Therefore, it should be checked separately.
| } | ||
|
|
||
| proxyLine.slope=proxyLine.calculateSlope() | ||
| if (line.slope === proxyLine.slope) { |
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.
the calculateSlope returns a value???
you are updating the slope into undefined
modules/geometry-calculation.js
Outdated
| proxyLine.slope=proxyLine.calculateSlope() | ||
| if (line.slope === proxyLine.slope) { | ||
| proxyLine.calculateNOfLineFunction() | ||
| proxyLine.n=proxyLine.calculateNOfLineFunction() |
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.
the same here, the n will be updated into undefined
if you did it for the mocks, in the mocks you can do whatever you want, but here you need to use the function as it has to be
| "test:coverage": "jest --coverage" | ||
| }, | ||
| "dependencies": { | ||
| "jest": "^29.7.0" |
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.
????
|
|
||
| const getPointByXMock = jest | ||
| .spyOn(Line.prototype, 'getPointByX') | ||
|
|
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.
use this mocks inside the test where they are needed
| const calculateNOfLineFunctionMock = jest | ||
| .spyOn(Line.prototype, 'calculateNOfLineFunction').mockImplementation() | ||
| .mockReturnValue(9) | ||
|
|
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.
mock the class once with jest.mock, and each time mocktheReturnValue for each function
|
good, leave it like this
and continue
בתאריך יום ה׳, 25 ביולי 2024 ב-10:24 מאת lev527 <
***@***.***>:
… ***@***.**** commented on this pull request.
------------------------------
In modules/geometry-calculation.js
<#13 (comment)>:
> const distance = Math.sqrt(distanceX + distanceY);
return distance;
}
const calculateJunctionPoint = (line1, line2) => {
+ if (!(line1 instanceof Line) && !(line2 instanceof Line)) {
+ throw Error('The values are of an invalid type')
+ }
+ if (!(line1 instanceof Line)) {
+ throw Error('The first value is of an invalid type')
+ }
+ if (!(line2 instanceof Line)) {
+ throw Error('The second value is of an invalid type')
+ }
+ line1.calculateSlope()
+ line2.calculateSlope()
+ line1.calculateNOfLineFunction()
+ line2.calculateNOfLineFunction()
But the teacher wrote that you should check if the slope and n are correct.
If the user entered wrong values?
and also
If the user entered only n and no slope - the slope will not be calculated.
Therefore, it should be checked separately.
—
Reply to this email directly, view it on GitHub
<#13 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BHKPZ653IUSXTGTUARUWFZTZOCR3DAVCNFSM6AAAAABLGVDTQOVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDCOJYGUZDKOJYHA>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
No description provided.