-
Notifications
You must be signed in to change notification settings - Fork 41
Add unit test with mock function #33
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?
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 |
|---|---|---|
|
|
@@ -2,35 +2,72 @@ 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('InvalidPointError: point1 and point2 must be instances of Point'); | ||
| } | ||
| if (typeof(n)!=="number"&&n!=undefined) { | ||
| throw new Error('InvalidError: n must be a number'); | ||
| } | ||
| if (typeof(slope)!=="number"&&slope!=undefined) { | ||
| throw new Error('InvalidError: slope must be a number'); | ||
| } | ||
| this.point1 = point1; | ||
| this.point2 = point2; | ||
| this.slope = slope; | ||
| this.n = n; | ||
| } | ||
|
|
||
| calculateSlope() { | ||
| calculateSlope () { | ||
| if (this.point1.x === this.point2.x) { | ||
| throw new Error('UndefinedSlopeError: Slope is undefined for vertical lines'); | ||
| } | ||
| this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x) | ||
| } | ||
|
|
||
| calculateNOfLineFunction() { | ||
| calculateNOfLineFunction = () => { | ||
| if (this.slope === undefined) { | ||
| this.calculateSlope(); | ||
| } | ||
| this.n = this.point1.y - this.slope * this.point1.x | ||
| } | ||
|
|
||
| getPointOnXAsis() { | ||
|
|
||
| if (this.slope === undefined || this.n === undefined) { | ||
| this.calculateNOfLineFunction(); | ||
| } | ||
| return this.getPointByY(0) | ||
| } | ||
|
|
||
| getPointOnYAsis() { | ||
| if (this.slope === undefined || this.n === undefined) { | ||
| this.calculateNOfLineFunction(); | ||
| } | ||
| return this.getPointByX(0) | ||
| } | ||
|
|
||
|
|
||
| getPointByX(x) { | ||
| if (typeof x !== 'number') { | ||
| throw new Error('InvalidInputError: x must be a number'); | ||
| } | ||
| if (this.slope === undefined || this.n === undefined) { | ||
| this.calculateNOfLineFunction(); | ||
| } | ||
| let y = this.slope * x + this.n | ||
| return new Point({ x, y }) | ||
| } | ||
|
|
||
| getPointByY(y) { | ||
| if (typeof y !== 'number') { | ||
| throw new Error('InvalidInputError: y must be a number'); | ||
| } | ||
| if (this.slope === undefined || this.n === undefined) { | ||
| this.calculateNOfLineFunction(); | ||
| } | ||
| if (this.slope === 0) { | ||
| throw new Error('InvalidSlopeError: Slope cannot be zero for calculating X by 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. so what is the x? how do you calculate it? |
||
| } | ||
| let x = (y - this.n) / this.slope; | ||
| return new Point({ x, y }) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,41 @@ | ||
| const Line = require('./ecs6-class/line') | ||
|
|
||
| const Point = require('../modules/ecs6-class/point'); | ||
| const calculateDistance = (point1, point2) => { | ||
| if (!point1 || !point2 || typeof point1.x !== 'number' || typeof point1.y !== 'number' || typeof point2.x !== 'number' || typeof point2.y !== 'number') { | ||
| throw new Error('Invalid input points'); | ||
|
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 input is invalid? and why is it 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) => { | ||
| const calculateJunctionPoint = (line1, line2) => { | ||
|
|
||
| if (!(line1 instanceof Line)) { | ||
| throw new Error('Invalid input line1'); | ||
| } | ||
| if (!(line2 instanceof Line) ) { | ||
| throw new Error('Invalid input line2'); | ||
| } | ||
| if(line1.slope==undefined){ | ||
| line1.calculateSlope(); | ||
| } | ||
| if(line2.slope==undefined){ | ||
| line2.calculateSlope(); | ||
| } | ||
| if (line1.slope === line2.slope) { | ||
| if(line1.n===undefined){ | ||
| line1.calculateNOfLineFunction(); | ||
| } | ||
| if(line2.n===undefined){ | ||
| line2.calculateNOfLineFunction(); | ||
| } | ||
| if (line1.n === line2.n) { | ||
| return true | ||
| return true; | ||
| } | ||
| else { | ||
| return false | ||
| return false; | ||
| } | ||
| } | ||
| else { | ||
|
|
@@ -24,8 +46,17 @@ const calculateJunctionPoint = (line1, line2) => { | |
| } | ||
|
|
||
| const isPointOnLine = (line, point) => { | ||
| if (!(line instanceof Line) ) { | ||
| throw new Error('Invalid input line '); | ||
| } | ||
| if ( !(point instanceof Point) || typeof point.x !== 'number' || typeof point.y !== 'number') { | ||
| throw new Error('Invalid input point'); | ||
| } | ||
| const proxyLine = new Line({ point1: line.point1, point2: point }) | ||
| proxyLine.calculateSlope() | ||
| proxyLine.calculateSlope(); | ||
| if(line.slope==undefined){ | ||
| line.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. is the line.slope defined? |
||
| proxyLine.calculateNOfLineFunction() | ||
| if (line.n === proxyLine.n) { | ||
|
|
||
| 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": "jest --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 not the place for jest |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| const Line = require('../../modules/ecs6-class/line'); | ||
| const Point = require('../../modules/ecs6-class/point'); | ||
|
|
||
| describe('Line class', () => { | ||
| test('should create a line with default points and undefined slope and n', () => { | ||
| const line = new Line({}); | ||
|
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. give more examples for the good constructor |
||
| expect(line.point1).toBeInstanceOf(Point); | ||
| expect(line.point2).toBeInstanceOf(Point); | ||
| expect(line.slope).toBeUndefined(); | ||
| expect(line.n).toBeUndefined(); | ||
| }); | ||
| test('should create a line with selected values and undefined slope and n', () => { | ||
| const point1=new Point({x:5,y:3}); | ||
| const point2=new Point({x:4,y:6}); | ||
| const line = new Line({point1:point1,point2:point2}); | ||
| expect(line.point1).toEqual(point1); | ||
| expect(line.point2).toEqual(point2); | ||
| expect(line.slope).toBeUndefined(); | ||
| expect(line.n).toBeUndefined(); | ||
| }); | ||
| test('should create a line with selected values', () => { | ||
| const point1=new Point({x:5,y:3}); | ||
| const point2=new Point({x:4,y:6}); | ||
| const line = new Line({point1:point1,point2:point2,n:5,slope:3}); | ||
| expect(line.point1).toEqual(point1); | ||
| expect(line.point2).toEqual(point2); | ||
| expect(line.slope).toEqual(3); | ||
| expect(line.n).toEqual(5); | ||
| }); | ||
|
|
||
| test('should throw an error for invalid point1 or point2', () => { | ||
| expect(() => new Line({ point1: {}, point2: new Point() })).toThrow('InvalidPointError: point1 and point2 must be instances of Point'); | ||
| expect(() => new Line({ point1: new Point(), point2: {} })).toThrow('InvalidPointError: point1 and point2 must be instances of Point'); | ||
| }); | ||
|
|
||
| test('should throw an error for invalid n ', () => { | ||
| expect(() => new Line({ point1: new Point(), point2: new Point() ,n:'n',slope:5})).toThrow('InvalidError: n must be a number'); | ||
| expect(() => new Line({ point1: new Point(), point2: new Point() ,n:{},slope:5})).toThrow('InvalidError: n must be a number'); | ||
| expect(() => new Line({ point1: new Point(), point2: new Point() ,n:[],slope:5})).toThrow('InvalidError: n must be a number'); | ||
| expect(() => new Line({ point1: new Point(), point2: new Point() ,n:true,slope:5})).toThrow('InvalidError: n must be a number'); | ||
| }); | ||
| test('should throw an error for invalid slope ', () => { | ||
| expect(() => new Line({ point1: new Point(), point2: new Point() ,n:5,slope:{}})).toThrow('InvalidError: slope must be a number'); | ||
| expect(() => new Line({ point1: new Point(), point2: new Point() ,n:5,slope:'5'})).toThrow('InvalidError: slope must be a number'); | ||
| expect(() => new Line({ point1: new Point(), point2: new Point() ,n:5,slope:[]})).toThrow('InvalidError: slope must be a number'); | ||
| expect(() => new Line({ point1: new Point(), point2: new Point() ,n:5,slope:true})).toThrow('InvalidError: slope must be a number'); | ||
| }); | ||
| describe('calculateSlope', () => { | ||
| test('should calculate the slope of the line', () => { | ||
| const point1 = new Point({ x: 0, y: 0 }); | ||
| const point2 = new Point({ x: 4, y: 4 }); | ||
| const line = new Line({ point1, point2 }); | ||
| line.calculateSlope(); | ||
| expect(line.slope).toBe(1); | ||
| }); | ||
|
|
||
| test('should throw an error for undefined slope (vertical line)', () => { | ||
| const point1 = new Point({ x: 0, y: 0 }); | ||
| const point2 = new Point({ x: 0, y: 4 }); | ||
| const line = new Line({ point1, point2 }); | ||
| expect(() => line.calculateSlope()).toThrow('UndefinedSlopeError: Slope is undefined for vertical lines'); | ||
| }); | ||
| }) | ||
|
|
||
| describe('calculateNOfLineFunction', () => { | ||
| test('should calculate the n of the line function', () => { | ||
| const point1 = new Point({ x: 0, y: 0 }); | ||
| const point2 = new Point({ x: 4, y: 4 }); | ||
| const line = new Line({ point1, point2 }); | ||
| line.calculateNOfLineFunction(); | ||
| expect(line.n).toBe(0); | ||
| }); | ||
| test('should throw an error for undefined slope (vertical line)', () => { | ||
| const point1 = new Point({ x: 4, y: 0 }); | ||
| const point2 = new Point({ x: 4, y: 4 }); | ||
| const line = new Line({ point1, point2 }); | ||
| expect(() => line.calculateNOfLineFunction()).toThrow('UndefinedSlopeError: Slope is undefined for vertical lines'); | ||
| }); | ||
| }); | ||
| describe('getPointOnXAsis', () => { | ||
| test('should get point on X axis', () => { | ||
| const point1 = new Point({ x: 0, y: 1 }); | ||
| const point2 = new Point({ x: 1, y: 0 }); | ||
| const line = new Line({ point1, point2 }); | ||
| const pointOnXAxis = line.getPointOnXAsis(); | ||
| expect(pointOnXAxis.x).toBe(1); | ||
| expect(pointOnXAxis.y).toBe(0); | ||
| }); | ||
|
|
||
|
|
||
| test('should handle horizontal lines correctly by throwing an error', () => { | ||
| const point1 = new Point({ x: 1, y: 0 }); | ||
| const point2 = new Point({ x: 3, y: 0 }); | ||
|
|
||
| const line = new Line({ | ||
| point1: point1, | ||
| point2: point2, | ||
| slope: 0, | ||
| n: 0 | ||
| }); | ||
|
|
||
| expect(() => line.getPointOnXAsis()).toThrow('InvalidSlopeError: Slope cannot be zero for calculating X by Y'); | ||
| }); | ||
| test('should throw an error for undefined slope (vertical line)', () => { | ||
| const point1 = new Point({ x: 4, y: 0 }); | ||
| const point2 = new Point({ x: 4, y: 4 }); | ||
| const line = new Line({ point1, point2 }); | ||
| expect(() => line.getPointOnXAsis()).toThrow('UndefinedSlopeError: Slope is undefined for vertical lines'); | ||
| }); | ||
| }) | ||
| describe('getPointOnYAsis',()=>{ | ||
| test('should get point on Y axis', () => { | ||
| const point1 = new Point({ x: 1, y: 0 }); | ||
| const point2 = new Point({ x: 2, y: 1 }); | ||
| const line = new Line({ point1, point2 }); | ||
| const pointOnYAxis = line.getPointOnYAsis(); | ||
| expect(pointOnYAxis.x).toBe(0); | ||
| expect(pointOnYAxis.y).toBe(-1); | ||
| }); | ||
|
|
||
| test('should return the Y-intercept for horizontal lines', () => { | ||
| const point1 = new Point({ x: 1, y: 5 }); | ||
| const point2 = new Point({ x: 3, y: 5 }); | ||
| const line = new Line({ point1: point1, point2: point2, slope: 0, n: 5 }); | ||
| const expectedPoint = new Point({ x: 0, y: 5 }); | ||
| const result = line.getPointOnYAsis(); | ||
|
|
||
| expect(result).toEqual(expectedPoint); | ||
| }); | ||
| test('should throw an error for undefined slope (vertical line)', () => { | ||
| const point1 = new Point({ x: 4, y: 0 }); | ||
| const point2 = new Point({ x: 4, y: 4 }); | ||
| const line = new Line({ point1, point2 }); | ||
| expect(() => line.getPointOnYAsis()).toThrow('UndefinedSlopeError: Slope is undefined for vertical lines'); | ||
| }); | ||
| }) | ||
|
|
||
| describe('getPointByX',()=>{ | ||
| test('should get point by X', () => { | ||
| const point1 = new Point({ x: 0, y: 0 }); | ||
| const point2 = new Point({ x: 4, y: 4 }); | ||
| const line = new Line({ point1, point2 }); | ||
| const point = line.getPointByX(2); | ||
| expect(point.x).toBe(2); | ||
| expect(point.y).toBe(2); | ||
| }); | ||
| test('should throw an error for invalid x input in getPointByX', () => { | ||
| const line = new Line({}); | ||
| expect(() => line.getPointByX('a')).toThrow('InvalidInputError: x must be a number'); | ||
| expect(() => line.getPointByX()).toThrow('InvalidInputError: x must be a number'); | ||
| expect(() => line.getPointByX({})).toThrow('InvalidInputError: x must be a number'); | ||
| expect(() => line.getPointByX([])).toThrow('InvalidInputError: x must be a number'); | ||
| }); | ||
| }) | ||
| describe('getPointByY',()=>{ | ||
| test('should get point by Y', () => { | ||
| const point1 = new Point({ x: 0, y: 0 }); | ||
| const point2 = new Point({ x: 4, y: 4 }); | ||
| const line = new Line({ point1, point2 }); | ||
| const point = line.getPointByY(2); | ||
| expect(point.x).toBe(2); | ||
| expect(point.y).toBe(2); | ||
| }); | ||
| test('should throw an error for invalid slope (slope is zero) in getPointByY', () => { | ||
| const point1 = new Point({ x: 0, y: 0 }); | ||
| const point2 = new Point({ x: 4, y: 0 }); | ||
| const line = new Line({ point1, point2 }); | ||
| expect(() => line.getPointByY(2)).toThrow('InvalidSlopeError: Slope cannot be zero for calculating X by Y'); | ||
| }); | ||
|
|
||
| test('should throw an error for invalid y input in getPointByY', () => { | ||
| const line = new Line({}); | ||
| expect(() => line.getPointByY('a')).toThrow('InvalidInputError: y must be a number'); | ||
| expect(() => line.getPointByY()).toThrow('InvalidInputError: y must be a number'); | ||
| expect(() => line.getPointByY({})).toThrow('InvalidInputError: y must be a number'); | ||
| expect(() => line.getPointByY([])).toThrow('InvalidInputError: y must be a number'); | ||
| }); | ||
| }) | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| }); | ||
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 n is a string, or if slope is an object (for example)