Conversation
gemtechd
left a comment
There was a problem hiding this comment.
Add more checks in your code
then you'll have to write more tests and use better mocks
in the tests: give each test a description, and use the it test function and not test
modules/ecs6-class/line.js
Outdated
| throw new Error('the constructor should get two arguments of "Point"') | ||
| } | ||
| if ((typeof (n) !== "undefined" && typeof (n) !== "number") || (typeof (slope) !== 'number' && typeof (slope) !== "undefined")) { | ||
| throw new Error('the n and slope in constractor should get undefined or number') |
There was a problem hiding this comment.
Which one of the parameter threw an error??
modules/ecs6-class/line.js
Outdated
| } | ||
|
|
||
| calculateSlope() { | ||
| calculateSlope = () => {//שיפוע |
There was a problem hiding this comment.
change it back to calculateSlope() { } from calculateSlope = ()=>{}
|
|
||
| calculateSlope() { | ||
| calculateSlope = () => {//שיפוע | ||
| this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) |
There was a problem hiding this comment.
What happens if point1.x - point2.x === 0??
|
|
||
| calculateNOfLineFunction() { | ||
| calculateNOfLineFunction = () => {//מרחק | ||
| this.n = this.point1.y - this.slope * this.point1.x |
There was a problem hiding this comment.
was the slope already calculated?
modules/geometry-calculation.js
Outdated
| throw new Error('the function should get two arguments of "Point"') | ||
| } | ||
| if ((!(point1 instanceof Point)) || (!(point2 instanceof Point))) { | ||
| throw new Error('the function should get two arguments of "Point"') |
There was a problem hiding this comment.
Which parameter threw the error and why?
| return mPoint; | ||
| }); | ||
|
|
||
| const result2 = line1.getPointOnYAsis(); |
There was a problem hiding this comment.
this test is a bit weird: you description is not what you really do
There was a problem hiding this comment.
- give a good description for each test
- use the it test function instead of test
| const point1 = new Point({ x: 1, y: 1 }) | ||
| const point2 = new Point({ x: 2, y: 2 }) | ||
| const line1 = new Line({ point1, point2, n: 2, slope: 2 }) | ||
|
|
There was a problem hiding this comment.
don't declare parameters in the head of the module, declare each one inside the test
tests/geometry-calculation.test.js
Outdated
|
|
||
| describe('CALCULATE_DISTANCE', () => { | ||
| test('', () => { | ||
| expect(geometry.calculateDistance(point1, point2)).toBe(1.4142135623730951) |
There was a problem hiding this comment.
- write a description
- get a nice number
There was a problem hiding this comment.
change your code, and the tests will need more mocks
|
I whant a new task
בתאריך יום ב׳, 29 ביולי 2024 ב-1:16 מאת gemtechd <
***@***.***>:
… ***@***.**** commented on this pull request.
Add more checks in your code
then you'll have to write more tests and use better mocks
in the tests: give each test a description, and use the *it* test
function and not *test*
------------------------------
In modules/ecs6-class/line.js
<#34 (comment)>:
> @@ -2,17 +2,23 @@ const Point = require("./point");
class Line {
constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) {
+ if ((!(point1 instanceof Point)) || (!(point2 instanceof Point))) {
+ throw new Error('the constructor should get two arguments of "Point"')
+ }
+ if ((typeof (n) !== "undefined" && typeof (n) !== "number") || (typeof (slope) !== 'number' && typeof (slope) !== "undefined")) {
+ throw new Error('the n and slope in constractor should get undefined or number')
Which one of the parameter threw an error??
------------------------------
In modules/ecs6-class/line.js
<#34 (comment)>:
> this.point1 = point1;
this.point2 = point2;
this.slope = slope;
this.n = n;
}
- calculateSlope() {
+ calculateSlope = () => {//שיפוע
change it back to calculateSlope() { } from calculateSlope = ()=>{}
------------------------------
In modules/ecs6-class/line.js
<#34 (comment)>:
> this.point1 = point1;
this.point2 = point2;
this.slope = slope;
this.n = n;
}
- calculateSlope() {
+ calculateSlope = () => {//שיפוע
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
What happens if point1.x - point2.x === 0??
------------------------------
In modules/ecs6-class/line.js
<#34 (comment)>:
> this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
}
- calculateNOfLineFunction() {
+ calculateNOfLineFunction = () => {//מרחק
this.n = this.point1.y - this.slope * this.point1.x
was the slope already calculated?
------------------------------
In modules/geometry-calculation.js
<#34 (comment)>:
>
-const calculateDistance = (point1, point2) => {
+const calculateDistance = (point1, point2) => {//חישוב מרחק
+ if (point1 === undefined || point2 === undefined) {
+ throw new Error('the function should get two arguments of "Point"')
+ }
+ if ((!(point1 instanceof Point)) || (!(point2 instanceof Point))) {
+ throw new Error('the function should get two arguments of "Point"')
Which parameter threw the error and why?
------------------------------
In modules/geometry-calculation.js
<#34 (comment)>:
> const distance = Math.sqrt(distanceX + distanceY);
return distance;
}
-const calculateJunctionPoint = (line1, line2) => {
+const calculateJunctionPoint = (line1, line2) => {// נקודת אמצע
+ if (line1 === undefined || line2 === undefined) {
+ throw new Error('the function should get two arguments of "Line"')
+ }
+ if (!(line1 instanceof Line) || !(line2 instanceof Line)) {
+ throw new Error('the function should get two arguments of "Line"')
Which parameter threw the error and why?
------------------------------
In modules/geometry-calculation.js
<#34 (comment)>:
> const distance = Math.sqrt(distanceX + distanceY);
return distance;
}
-const calculateJunctionPoint = (line1, line2) => {
+const calculateJunctionPoint = (line1, line2) => {// נקודת אמצע
+ if (line1 === undefined || line2 === undefined) {
+ throw new Error('the function should get two arguments of "Line"')
+ }
+ if (!(line1 instanceof Line) || !(line2 instanceof Line)) {
+ throw new Error('the function should get two arguments of "Line"')
+ }
if (line1.slope === line2.slope) {
are both slopes calculated?
------------------------------
In modules/geometry-calculation.js
<#34 (comment)>:
> const distance = Math.sqrt(distanceX + distanceY);
return distance;
}
-const calculateJunctionPoint = (line1, line2) => {
+const calculateJunctionPoint = (line1, line2) => {// נקודת אמצע
+ if (line1 === undefined || line2 === undefined) {
+ throw new Error('the function should get two arguments of "Line"')
+ }
+ if (!(line1 instanceof Line) || !(line2 instanceof Line)) {
+ throw new Error('the function should get two arguments of "Line"')
+ }
if (line1.slope === line2.slope) {
if (line1.n === line2.n) {
return true
are both n calculated already?
------------------------------
In modules/geometry-calculation.js
<#34 (comment)>:
> @@ -24,6 +37,12 @@ const calculateJunctionPoint = (line1, line2) => {
}
const isPointOnLine = (line, point) => {
+ if (point === undefined || line === undefined) {
+ throw new Error('the function should get arg of "Point" and arg of "Line"')
+ }
+ if (!(line instanceof Line) || !(point instanceof Point)) {
+ throw new Error('the function should get arg of "Point" and arg of "Line"')
+ }
const proxyLine = new Line({ point1: line.point1, point2: point })
proxyLine.calculateSlope()
if (line.slope === proxyLine.slope) {
is the slope calculated?
------------------------------
In package.json
<#34 (comment)>:
> @@ -4,7 +4,9 @@
"description": "practice unit tests in javascript",
"main": "index.js",
"scripts": {
- "test": "jest"
+ "test": "jest",
+ "coverage": "npm run test -- --coverage"
+
},
"dependencies": {
"jest": "^29.7.0"
the jest module is not in the correct place
------------------------------
In tests/ecs6-class/line.test.js
<#34 (comment)>:
> @@ -0,0 +1,128 @@
+const Line = require('../../modules/ecs6-class/line')
+const line = require('../../modules/ecs6-class/line')
why do you require twice the same module?
------------------------------
In tests/ecs6-class/line.test.js
<#34 (comment)>:
> @@ -0,0 +1,128 @@
+const Line = require('../../modules/ecs6-class/line')
+const line = require('../../modules/ecs6-class/line')
+const point = require('../../modules/ecs6-class/point')
+
+const point1 = new point({ x: 1, y: 1 })
+const point2 = new point({ x: 2, y: 2 })
+const line1 = new line({ point1, point2, n: 2, slope: 2 })
+const TLine = new line({})
+
don't declare on top of the module a list of parameter, each test should
have its own. And if you need the same parameters for a group of tests,
declare them inside the describe block in the beforeAll(()=> { } )
function
------------------------------
In tests/ecs6-class/line.test.js
<#34 (comment)>:
> +});
+
+describe('GET_POINT_ON_X_ASIS', () => {
+ test('', () => {
+ const line1 = new Line({ n: 2, slope: 2 })
+ expect(line1.getPointOnXAsis()).toEqual({ x: -1, y: 0 })
+ });
+ test('mock on getPointOnXAsis', () => {
+ let mPoint = new point({ x: 2, y: 1 });
+ jest.spyOn(line1, 'getPointByX').mockImplementation((y) => {
+ const x = (y - line1.n) / line1.slope;
+ mPoint = new point({ x, y });
+ return mPoint;
+ });
+
+ const result2 = line1.getPointOnYAsis();
this test is a bit weird: you description is not what you really do
------------------------------
On tests/ecs6-class/line.test.js
<#34 (comment)>:
1. give a good description for each test
2. use the *it* test function instead of *test*
------------------------------
In tests/geometry-calculation.test.js
<#34 (comment)>:
> @@ -0,0 +1,120 @@
+const geometry = require('../modules/geometry-calculation')
+const Point = require('../modules/ecs6-class/point')
+const Line = require('../modules/ecs6-class/line')
+
+const point1 = new Point({ x: 1, y: 1 })
+const point2 = new Point({ x: 2, y: 2 })
+const line1 = new Line({ point1, point2, n: 2, slope: 2 })
+
don't declare parameters in the head of the module, declare each one
inside the test
------------------------------
In tests/geometry-calculation.test.js
<#34 (comment)>:
> @@ -0,0 +1,120 @@
+const geometry = require('../modules/geometry-calculation')
+const Point = require('../modules/ecs6-class/point')
+const Line = require('../modules/ecs6-class/line')
+
+const point1 = new Point({ x: 1, y: 1 })
+const point2 = new Point({ x: 2, y: 2 })
+const line1 = new Line({ point1, point2, n: 2, slope: 2 })
+
+
+
+describe('CALCULATE_DISTANCE', () => {
+ test('', () => {
+ expect(geometry.calculateDistance(point1, point2)).toBe(1.4142135623730951)
1. write a description
2. get a nice number
------------------------------
On tests/geometry-calculation.test.js
<#34 (comment)>:
change your code, and the tests will need more mocks
—
Reply to this email directly, view it on GitHub
<#34 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BJ4M46TTVY5VYRHK7S3GF7LZOVUVPAVCNFSM6AAAAABLS2AOEKVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDEMBTGU4TOMJUGI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
No description provided.