Skip to content

Conversation

@lev527
Copy link

@lev527 lev527 commented Jul 21, 2024

No description provided.

Copy link
Owner

@gemtechd gemtechd left a comment

Choose a reason for hiding this comment

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

See the comments

if(typeof(n)!='number'&& typeof(n)!='undefined'){
throw Error('The n should have a number')
}
if(typeof(slope)!='number'&& typeof(slope)!='undefined'){
Copy link
Owner

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


getPointOnXAsis() {
return this.getPointByY(0)
getPointOnXAsis(func=this.getPointByY(0)) {
Copy link
Owner

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

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

throw Error('The value is of an invalid type')
}
if(!point1.x||!point2.x){
throw Error('The value is of an invalid type')
Copy link
Owner

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

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

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

}

const isPointOnLine = (line, point) => {
const isPointOnLine = (line, point,func1,func2) => {
Copy link
Owner

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

Copy link
Owner

@gemtechd gemtechd left a 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

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

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?

this.n = this.point1.y - this.slope * this.point1.x
calculateNOfLineFunction() {

return 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.

What happens if the slope is not calculated yet?

Copy link
Owner

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

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?

Copy link
Owner

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

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?

Copy link
Owner

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', () => {
Copy link
Owner

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')

Copy link
Owner

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

Copy link
Owner

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', () => {
Copy link
Owner

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??

Copy link
Owner

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)

Copy link
Owner

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

Copy link
Author

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?

Copy link
Owner

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

Copy link
Owner

@gemtechd gemtechd left a 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

this.n = this.point1.y - this.slope * this.point1.x
calculateNOfLineFunction() {

return 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.

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

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

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

line1.calculateSlope()
line2.calculateSlope()
line1.calculateNOfLineFunction()
line2.calculateNOfLineFunction()
Copy link
Owner

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

Copy link
Author

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

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

proxyLine.slope=proxyLine.calculateSlope()
if (line.slope === proxyLine.slope) {
proxyLine.calculateNOfLineFunction()
proxyLine.n=proxyLine.calculateNOfLineFunction()
Copy link
Owner

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

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')

Copy link
Owner

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)

Copy link
Owner

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

gemtechd commented Jul 25, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants