-
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
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 |
|---|---|---|
| @@ -1,19 +1,44 @@ | ||
| 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)) { | ||
| throw Error('The point1 should be a Point type') | ||
| } | ||
| if (!(point2 instanceof Point)) { | ||
| throw Error('The point2 should be a Point type') | ||
| } | ||
| if (typeof (n) !== 'number' && typeof (n) !== 'undefined') { | ||
| throw Error('The n should have a number') | ||
| } | ||
| if (typeof (slope) !== 'number' && typeof (slope) !== 'undefined') { | ||
| throw Error('The slope should have a number') | ||
| } | ||
| this.point1 = point1; | ||
| this.point2 = point2; | ||
| this.slope = slope; | ||
| this.n = n; | ||
| } | ||
|
|
||
| calculateSlope = () => { | ||
| this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) | ||
| calculateSlope() { | ||
| if (this.point1.x === this.point2.x) { | ||
| throw Error("it isn't a real geometry line") | ||
| } | ||
| else { | ||
| this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| calculateNOfLineFunction = () => { | ||
| calculateNOfLineFunction() { | ||
| if (!this.slope) { | ||
| this.calculateSlope() | ||
|
|
||
| } | ||
| this.n = this.point1.y - this.slope * this.point1.x | ||
|
|
||
|
|
||
| } | ||
|
|
||
| getPointOnXAsis() { | ||
|
|
@@ -26,11 +51,35 @@ class Line { | |
|
|
||
|
|
||
| getPointByX(x) { | ||
| if (typeof (x) !== 'number') { | ||
| throw Error('The value is of an invalid type') | ||
| } | ||
| if (!this.slope) { | ||
| this.calculateSlope() | ||
|
|
||
| } | ||
| if (!this.n) { | ||
| this.calculateNOfLineFunction() | ||
| } | ||
|
|
||
| let y = this.slope * x + this.n | ||
| return new Point({ x, y }) | ||
|
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. what happens if the slope or the line are not calculated yet?
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. first check what is the n value |
||
| } | ||
|
|
||
| getPointByY(y) { | ||
| if (typeof (y) !== 'number') { | ||
| throw Error('The value is of an invalid type') | ||
| } | ||
| if (!this.slope) { | ||
| this.calculateSlope() | ||
| } | ||
| if (!this.n) { | ||
| this.calculateNOfLineFunction() | ||
| } | ||
|
|
||
| if (this.slope === 0) { | ||
| throw Error('The slope cannot be 0') | ||
| } | ||
| 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. what happens when the slope is not calculated or is zero?
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. The same as the previous remark |
||
| return new Point({ x, y }) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,43 @@ | ||
| const Line = require('./ecs6-class/line') | ||
| const Line = require('./ecs6-class/line'); | ||
| const Point = require('./ecs6-class/point'); | ||
|
|
||
|
|
||
|
|
||
|
|
||
| const calculateDistance = (point1, point2) => { | ||
| if (!(point1 instanceof Point) || !(point1 instanceof Point)) { | ||
| throw Error('The value is of an invalid type') | ||
|
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 of the point is invalid? |
||
| } | ||
| 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 (!(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') | ||
| } | ||
| if (!line1.slope) { | ||
| line1.calculateSlope() | ||
|
|
||
| } | ||
| if (!line2.slope) { | ||
| line2.calculateSlope() | ||
| } | ||
| if (!line1.n) { | ||
| line1.calculateNOfLineFunction() | ||
| } | ||
| if (!line2.n) { | ||
| line2.calculateNOfLineFunction() | ||
| } | ||
|
|
||
| if (line1.slope === line2.slope) { | ||
| if (line1.n === line2.n) { | ||
| return true | ||
|
|
@@ -24,6 +54,10 @@ const calculateJunctionPoint = (line1, line2) => { | |
| } | ||
|
|
||
| const isPointOnLine = (line, point) => { | ||
| if (!(line instanceof Line) || !(point instanceof Point)) { | ||
| throw Error('The value is of an invalid type') | ||
gemtechd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| const proxyLine = new Line({ point1: line.point1, point2: point }) | ||
| proxyLine.calculateSlope() | ||
| if (line.slope === proxyLine.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. line has a calculated slope already?
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. the calculateSlope returns a value??? |
||
|
|
||
| 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", | ||
| "test:coverage": "npm run 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. jest is not in the correct section
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. ???? |
||
|
|
||
|
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. Call the test functions with the it expression |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| const Line = require('../../../modules/ecs6-class/line'); | ||
| const Point = require('../../../modules/ecs6-class/point'); | ||
|
|
||
| const getPointByYMock = jest | ||
| .spyOn(Line.prototype, 'getPointByY') | ||
|
|
||
| const getPointByXMock = jest | ||
| .spyOn(Line.prototype, 'getPointByX') | ||
|
|
||
|
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. where do you restore the mocks?
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 this mocks inside the test where they are needed |
||
|
|
||
|
|
||
| describe('Check calculateSlope function', () => { | ||
| it(' Should return the slope of the line', () => { | ||
| let point1 = new Point({ x: 5, y: 7 }); | ||
| let point2 = new Point({ x: 3, y: 8 }); | ||
| let line1 = new Line({ point1, point2 }); | ||
| line1.calculateSlope(); | ||
| expect(line1.slope).toBe(-0.5) | ||
| }) | ||
| it(' Should return 0 if the points are in the same position on the x-axis', () => { | ||
| let point1 = new Point({ x: 5, y: 7 }); | ||
| let point2 = new Point({ x: 3, y: 7 }); | ||
| let line1 = new Line({ point1, point2 }); | ||
| line1.calculateSlope(); | ||
| expect(line1.slope).toBe(0) | ||
| }) | ||
| describe('ERRORS', () => { | ||
| it('should throw error when the type is not valid', () => { | ||
| let point1 = new Point({ x: 3, y: 7 }); | ||
| let point2 = new Point({ x: 3, y: 8 }); | ||
| let line1 = new Line({ point1, point2 }); | ||
| expect(() => line1.calculateSlope()).toThrow("it isn't a real geometry line") | ||
|
|
||
|
|
||
| }) | ||
| }) | ||
|
|
||
| }) | ||
|
|
||
| describe('Check calculateNOfLineFunction function', () => { | ||
| it('Should return n of the line', () => { | ||
| let point1 = new Point({ x: 5, y: 7 }); | ||
| let point2 = new Point({ x: 3, y: 8 }); | ||
| let line1 = new Line({ point1, point2, slope: -0.5 }); | ||
| line1.calculateNOfLineFunction(); | ||
| expect(line1.n).toBe(9.5) | ||
| }) | ||
| it('Should return n of the line and computer the slpoe if it is not sent', () => { | ||
| let point1 = new Point({ x: 5, y: 7 }); | ||
| let point2 = new Point({ x: 3, y: 8 }); | ||
| let line1 = new Line({ point1, point2 }); | ||
| line1.calculateNOfLineFunction(); | ||
| expect(line1.slope).toBe(-0.5) | ||
| expect(line1.n).toBe(9.5) | ||
| }) | ||
| }) | ||
|
|
||
| describe('Check getPointOnXAsis function', () => { | ||
|
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. You are testing only if the mock was called, but where are the tests about the values of the function?? |
||
| it('It should be returned if in the call to the function the function getPointByY was called', () => { | ||
| let point1 = new Point({ x: 5, y: 7 }); | ||
| let point2 = new Point({ x: 3, y: 8 }); | ||
| const line1 = new Line({point1,point2,n:7,slope:6}); | ||
| line1.getPointOnXAsis(); | ||
| expect(getPointByYMock).toHaveBeenCalled(); | ||
| }); | ||
| }) | ||
|
|
||
| describe('Check getPointOnYAsis function', () => { | ||
| it('It should be returned if in the call to the function the function getPointByX was called', () => { | ||
| let point1 = new Point({ x: 5, y: 7 }); | ||
| let point2 = new Point({ x: 3, y: 8 }); | ||
| const line1 = new Line({point1,point2,n:7,slope:6}); | ||
| line1.getPointOnYAsis(); | ||
| expect(getPointByXMock).toHaveBeenCalled(); | ||
| }); | ||
| }) | ||
|
|
||
|
|
||
| describe('Check getPointByX function', () => { | ||
| it(' Should return point', () => { | ||
| let point1 = new Point({ x: 5, y: 7 }); | ||
| let point2 = new Point({ x: 3, y: 8 }); | ||
| let line1 = new Line({ point1, point2, slope: 2, n: 7 }); | ||
| expect(line1.getPointByX(6)).toEqual({ x: 6, y: 19 }) | ||
|
|
||
| }) | ||
| it('Should return point and computer the slpoe if it is not sent', () => { | ||
| let point1 = new Point({ x: 5, y: 7 }); | ||
| let point2 = new Point({ x: 3, y: 8 }); | ||
| let line1 = new Line({ point1, point2, n: 7 }); | ||
| expect(line1.getPointByX(6)).toEqual({ x: 6, y: 4 }) | ||
| expect(line1.slope).toBe(-0.5) | ||
| }) | ||
| it('Should return point and computer the n if it is not sent', () => { | ||
| let point1 = new Point({ x: 5, y: 7 }); | ||
| let point2 = new Point({ x: 3, y: 8 }); | ||
| let line1 = new Line({ point1, point2, slope:-0.5 }); | ||
| expect(line1.getPointByX(6)).toEqual({ x: 6, y: 6.5 }) | ||
| expect(line1.n).toBe(9.5) | ||
| }) | ||
| it('Should return a point and calculate n and the slope if they are not sent', () => { | ||
| let point1 = new Point({ x: 5, y: 7 }); | ||
| let point2 = new Point({ x: 3, y: 8 }); | ||
| let line1 = new Line({ point1, point2}); | ||
| expect(line1.getPointByX(6)).toEqual({ x: 6, y: 6.5 }) | ||
| expect(line1.slope).toBe(-0.5) | ||
| expect(line1.n).toBe(9.5) | ||
| }) | ||
| describe('ERRORS', () => { | ||
| it('should throw error when the type is not valid', () => { | ||
| let point1 = new Point({ x: 5, y: 7 }); | ||
| let point2 = new Point({ x: 3, y: 8 }); | ||
| let line1 = new Line({ point1, point2 }); | ||
| expect(() => line1.getPointByX({ x: 1 })).toThrow('The value is of an invalid type') | ||
| expect(() => line1.getPointByX('o')).toThrow('The value is of an invalid type') | ||
| expect(() => line1.getPointByX(false)).toThrow('The value is of an invalid type') | ||
| expect(() => line1.getPointByX([1, 2])).toThrow('The value is of an invalid type') | ||
| expect(() => line1.getPointByX()).toThrow("The value is of an invalid type") | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('Check getPointByY function', () => { | ||
| it('Should return point ', () => { | ||
| let point1 = new Point({ x: 5, y: 7 }); | ||
| let point2 = new Point({ x: 3, y: 8 }); | ||
| let line1 = new Line({ point1,point2,slope: 2, n: 7 }); | ||
| expect(line1.getPointByY(6)).toEqual({ x: -0.5, y: 6 }) | ||
| }) | ||
| it('Should return point and computer the slpoe if it is not sent', () => { | ||
| let point1 = new Point({ x: 5, y: 7 }); | ||
| let point2 = new Point({ x: 3, y: 8 }); | ||
| let line1 = new Line({ point1, point2, n: 9.5 }); | ||
| expect(line1.getPointByY(6)).toEqual({ x: 7, y: 6 }) | ||
| expect(line1.slope).toBe(-0.5) | ||
| }) | ||
| it('Should return point and computer the n if it is not sent', () => { | ||
| let point1 = new Point({ x: 5, y: 7 }); | ||
| let point2 = new Point({ x: 3, y: 8 }); | ||
| let line1 = new Line({ point1, point2, slope:-0.5 }); | ||
| expect(line1.getPointByY(6)).toEqual({ x: 7, y: 6 }) | ||
| expect(line1.n).toBe(9.5) | ||
| }) | ||
| it('Should return a point and calculate n and the slope if they are not sent', () => { | ||
| let point1 = new Point({ x: 5, y: 7 }); | ||
| let point2 = new Point({ x: 3, y: 8 }); | ||
| let line1 = new Line({ point1, point2}); | ||
| expect(line1.getPointByY(6)).toEqual({ x: 7, y: 6 }) | ||
| expect(line1.slope).toBe(-0.5) | ||
| expect(line1.n).toBe(9.5) | ||
| }) | ||
| describe('ERRORS', () => { | ||
| it('should throw error when the type is not valid', () => { | ||
| let point1 = new Point({ x: 5, y: 7 }); | ||
| let point2 = new Point({ x: 3, y: 8 }); | ||
| let line1 = new Line({ point1, point2 }); | ||
| expect(() => line1.getPointByY({ x: 2 })).toThrow('The value is of an invalid type') | ||
| expect(() => line1.getPointByY('o')).toThrow('The value is of an invalid type') | ||
| expect(() => line1.getPointByY(false)).toThrow('The value is of an invalid type') | ||
| expect(() => line1.getPointByY([1, 2])).toThrow('The value is of an invalid type') | ||
| expect(() => line1.getPointByY()).toThrow("The value is of an invalid type") | ||
| }) | ||
| it('should throw error when The slope is equal to 0',()=>{ | ||
| let point1 = new Point({ x: 5, y: 8 }); | ||
| let point2 = new Point({ x: 3, y: 8 }); | ||
| let line1= new Line({ point1, point2 ,slope:0}); | ||
| expect(() => line1.getPointByY(21)).toThrow("The slope cannot be 0") | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('ERRORS', () => { | ||
| it('should throw error when the values of the line is not valid', () => { | ||
| let point1 = new Point({ x: 2, y: 3 }) | ||
| let point2 = new Point({ x: 4, y: 5 }) | ||
| let line1; | ||
| expect(() => line1 = new Line({ point1: 9, point2, n: 4, slope: 6 })).toThrow('The point1 should be a Point type') | ||
| expect(() => line1 = new Line({ point1, point2: [1, 2], n: 4, slope: 6 })).toThrow('The point2 should be a Point type') | ||
| expect(() => line1 = new Line({ point1, point2, n: 'c', slope: 6 })).toThrow('The n should have a number') | ||
| expect(() => line1 = new Line({ point1, point2, n: 4, slope: false })).toThrow('The slope should have a number') | ||
|
|
||
| }) | ||
| }) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.