-
Notifications
You must be signed in to change notification settings - Fork 41
add tests #24
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 #24
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,13 +1,22 @@ | ||
| const Line = require('./ecs6-class/line') | ||
| const Line = require('./ecs6-class/line'); | ||
| const Point = require('./ecs6-class/point'); | ||
|
|
||
| const calculateDistance = (point1, point2) => { | ||
| if(point1 === undefined || point2 === undefined) | ||
| throw new Error('the function should get two arguments') | ||
| if(!(point1 instanceof(Point)) || !(point2 instanceof(Point))) | ||
| throw new Error('the argument should be point') | ||
| let distanceX = (point2.x - point1.x) ** 2; | ||
| let distanceY = (point2.y - point2.y) ** 2; | ||
| const distance = Math.sqrt(distanceX + distanceY); | ||
| return distance; | ||
| } | ||
|
|
||
| const calculateJunctionPoint = (line1, line2) => { | ||
| if(line1 === undefined || line2 === undefined) | ||
| throw new Error('the function should get two arguments') | ||
| if(!(line1 instanceof(Line)) || !(line2 instanceof(Line))) | ||
| throw new Error('the argument should be line') | ||
| if (line1.slope === line2.slope) { | ||
| if (line1.n === line2.n) { | ||
|
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 was not calculated or n was not calculated? |
||
| return true | ||
|
|
@@ -24,6 +33,14 @@ const calculateJunctionPoint = (line1, line2) => { | |
| } | ||
|
|
||
| const isPointOnLine = (line, point) => { | ||
| if(line === undefined || point === undefined) | ||
| throw new Error('the function should get two arguments') | ||
| if(!(line instanceof(Line))) | ||
| throw new Error('the argument should be line') | ||
| if(!(point instanceof(Point))) | ||
| throw new Error('the argument should be point') | ||
| if(line.slope === undefined) | ||
| line.calculateSlope() | ||
| const proxyLine = new Line({ point1: line.point1, point2: point }) | ||
| proxyLine.calculateSlope() | ||
| if (line.slope === proxyLine.slope) { | ||
gemtechd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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": "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. this is not the right place for the jest module |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| const Line = require('../../modules/ecs6-class/line'); | ||
| const Point = require('../../modules/ecs6-class/point'); | ||
| const mockGetPointByY = jest.fn(() => ({ x: 0, y: 0 })); | ||
| const mockGetPointByX = jest.fn(() => ({ x: 0, y: 5 })); | ||
|
|
||
| describe('CONSTRUCTOR', () => { | ||
| it('should build line when get point', () => { | ||
| const line = new Line({ point1: new Point({ x: 2, y: 2 }) }) | ||
| expect(line.point1).toEqual(new Point({ x: 2, y: 2 })); | ||
| }) | ||
|
|
||
| describe('ERRORS', () => { | ||
| it('should throw string error when argument are no point', () => { | ||
| expect(() => new Line({ point1: new Line({}) })).toThrow('point1 not valid') | ||
| expect(() => new Line({ point2: 'hello' })).toThrow('point2 not valid') | ||
| expect(() => new Line({ point1: ['x', 'y'] })).toThrow('point1 not valid') | ||
| expect(() => new Line({ point2: false })).toThrow('point2 not valid') | ||
| expect(() => new Line({ point1: () => true, point2: () => 'y' })).toThrow('point1 and point2 not valid') | ||
| }) | ||
|
|
||
| it('should throw string error when argumonts are no number string', () => { | ||
| expect(() => new Line({ slope: 'a' })).toThrow('slope should be a number') | ||
| expect(() => new Line({ slope: true })).toThrow('slope should be a number') | ||
| expect(() => new Line({ slope: ['a', 'b'] })).toThrow('slope should be a number') | ||
| expect(() => new Line({ slope: value => (value) })).toThrow('slope should be a number') | ||
| }) | ||
|
|
||
| it('should throw string error when argumonts are no number string', () => { | ||
| expect(() => new Line({ n: 'a' })).toThrow('n should be a number') | ||
| expect(() => new Line({ n: true })).toThrow('n should be a number') | ||
| expect(() => new Line({ n: ['a', 'b'] })).toThrow('n should be a number') | ||
| expect(() => new Line({ n: value => (value) })).toThrow('n should be a number') | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('CALCULATE_SLOPE', () => { | ||
| it('should calculate the slope', () => { | ||
| const point1 = new Point({ x: 4, y: 3 }); | ||
| const point2 = new Point({ x: 3, y: 2 }); | ||
| const line = new Line({ point1, point2 }); | ||
| line.calculateSlope(); | ||
| expect(line.slope).toBe(1) | ||
| }) | ||
|
|
||
| it('should throw string error when point1.x - point2.x equal 0', () => { | ||
| const line1 = new Line({}); | ||
| expect(() => line1.calculateSlope()).toThrow('can`t divide by 0') | ||
| }) | ||
| }) | ||
|
|
||
| describe('CALCULATE_N_OF_LINE_FUNCTION', () => { | ||
| it('should calculate n of line', () => { | ||
| const point1 = new Point({ x: 4, y: 3 }); | ||
| const point2 = new Point({ x: 3, y: 2 }); | ||
| const line = new Line({ point1, point2 }); | ||
| line.slope = 1; | ||
| line.calculateNOfLineFunction() | ||
| expect(line.n).toBe(-1) | ||
| }) | ||
| }) | ||
|
|
||
| describe('GET_POINT_ON_X_ASIS', () => { | ||
| it('mock getPointOnXAsis', () => { | ||
| const LineInstance = new Line({}); | ||
| LineInstance.getPointByY = mockGetPointByY; | ||
| LineInstance.getPointOnXAsis(); | ||
| expect(mockGetPointByY).toHaveBeenCalledWith(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 expect line is unnecessary |
||
| }); | ||
| }) | ||
|
|
||
| describe('GET_POINT_ON_Y_ASIS', () => { | ||
| it('mock getPointOnYAsis', () => { | ||
| const LineInstance = new Line(2, 3); | ||
| LineInstance.getPointByX = mockGetPointByX; | ||
| const result = LineInstance.getPointOnYAsis(); | ||
| expect(mockGetPointByX).toHaveBeenCalledWith(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. What is this test? |
||
| expect(result).toEqual({ x: 0, y: 5 }); | ||
| }); | ||
| }) | ||
|
|
||
| describe('GET_POINT_BY_X', () => { | ||
| it('should return point x', () => { | ||
| const point1 = new Point({ x: 4, y: 3 }); | ||
| const point2 = new Point({ x: 3, y: 2 }); | ||
| const line = new Line({ point1, point2 }); | ||
| expect(line.getPointByX(5)).toEqual({ x: 5, y: 4 }) | ||
| }) | ||
|
|
||
| it('should to send to function: calculateSlope if slope is undefined', () => { | ||
| const point1 = new Point({ x: 4, y: 3 }); | ||
| const point2 = new Point({ x: 3, y: 2 }); | ||
| const line2 = new Line({ point1, point2 }); | ||
| expect(line2.getPointByX(5)).toEqual({ x: 5, y: 4 }) | ||
| }) | ||
|
|
||
| it('should to send to function: calculateNOfLineFunction if n is undefined', () => { | ||
| const line1 = new Line({}); | ||
| line1.slope = 0; | ||
| expect(line1.getPointByX(5)).toEqual({ x: 5, y: 0 }) | ||
| }) | ||
|
|
||
| it('should throw string error when argumonts are no number string or undefined when argument is undefined', () => { | ||
| const point1 = new Point({ x: 4, y: 3 }); | ||
| const point2 = new Point({ x: 3, y: 2 }); | ||
| const line = new Line({ point1, point2 }); | ||
| expect(() => line.getPointByX('a')).toThrow('argument is not a number') | ||
| expect(() => line.getPointByX(true)).toThrow('argument is not a number') | ||
| expect(() => line.getPointByX(['x', 'y'])).toThrow('argument is not a number') | ||
| expect(() => line.getPointByX()).toThrow('value is undefined') | ||
| expect(() => line.getPointByX(value => (value))).toThrow('argument is not a number') | ||
| }) | ||
| }) | ||
|
|
||
| describe('GET_POINT_BY_Y', () => { | ||
| it('should return point y', () => { | ||
| const point1 = new Point({ x: 4, y: 3 }); | ||
| const point2 = new Point({ x: 3, y: 2 }); | ||
| const line = new Line({ point1, point2 }); | ||
| expect(line.getPointByY(2)).toEqual({ x: 3, y: 2 }) | ||
| }) | ||
|
|
||
| it('should to send to function: calculateSlope if slope is undefined', () => { | ||
| const line3 = new Line({}); | ||
| line3.slope = 2; | ||
| expect(line3.getPointByY(2)).toEqual({ x: 1, y: 2 }) | ||
| }) | ||
|
|
||
| it('should to send to function: calculateNOfLineFunction if n is undefined', () => { | ||
| const point1 = new Point({ x: 4, y: 3 }); | ||
| const point2 = new Point({ x: 3, y: 2 }); | ||
| const line4 = new Line({ point1, point2 }); | ||
| line4.n = 0; | ||
| expect(line4.getPointByY(2)).toEqual({ x: 2, y: 2 }) | ||
| }) | ||
|
|
||
| it('should throw string error when argumonts are no number string or undefined when argument is undefined', () => { | ||
| const point1 = new Point({ x: 4, y: 3 }); | ||
| const point2 = new Point({ x: 3, y: 2 }); | ||
| const line = new Line({ point1, point2 }); | ||
| const line1 = new Line({}); | ||
| line1.slope = 0; | ||
| expect(() => line.getPointByY('a')).toThrow('argument is not a number') | ||
| expect(() => line.getPointByY(true)).toThrow('argument is not a number') | ||
| expect(() => line.getPointByY(['x', 'y'])).toThrow('argument is not a number') | ||
| expect(() => line.getPointByY()).toThrow('value is undefined') | ||
| expect(() => line.getPointByY(value => (value))).toThrow('argument is not a number') | ||
| expect(() => line1.getPointByY(2)).toThrow('slope can`t be 0') | ||
| }) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| const Point = require('../../modules/ecs6-class/point'); | ||
| const point1 = new Point(); | ||
|
|
||
| describe('CONSTRUCTOR', () => { | ||
| it('should build point when get two arguments', () => { | ||
| expect(new Point({ x: 2, y: 2 })).toEqual({ x: 2, y: 2 }) | ||
| }) | ||
|
|
||
| it('should build point when get no arguments', () => { | ||
| expect(new Point()).toEqual({ x: 0, y: 0 }) | ||
| }) | ||
|
|
||
| describe('ERRORS', () => { | ||
| it('should throw string error when argumonts no type of point', () => { | ||
| expect(() => new Point({ x: 'x' })).toThrow('argument is not a number') | ||
| expect(() => new Point({ y: false })).toThrow('argument is not a number') | ||
| expect(() => new Point({ x: ['x', 'y'] })).toThrow('argument is not a number') | ||
| expect(() => new Point({ x: () => true })).toThrow('argument is not a number') | ||
| expect(() => new Point({ y: () => 'y' })).toThrow('argument is not a number') | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('MOVE_VERTICAL', () => { | ||
| it('should to add the value to y', () => { | ||
| const point = new Point({ x: 2, y: 2 }); | ||
| point.moveVertical(2); | ||
| expect(point).toEqual({ x: 2, y: 4 }); | ||
| }) | ||
|
|
||
| describe('ERRORS', () => { | ||
| it('should throw string error when argumonts are no number string or undefined when argument is undefined', () => { | ||
| expect(() => point1.moveVertical('a')).toThrow('argument is not a number') | ||
| expect(() => point1.moveVertical(true)).toThrow('argument is not a number') | ||
| expect(() => point1.moveVertical(['x', 'y'])).toThrow('argument is not a number') | ||
| expect(() => point1.moveVertical()).toThrow('value is undefined') | ||
| expect(() => point1.moveVertical(value => (value))).toThrow('argument is not a number') | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('MOVE_HORIZONTAL', () => { | ||
| it('should to add the value to x', () => { | ||
| const point = new Point(); | ||
| point.moveHorizontal(2); | ||
| expect(point).toEqual({ x: 2, y: 0 }); | ||
| }) | ||
| describe('ERRORS', () => { | ||
| it('should throw string error when argumonts are no number string or undefined when argument is undefined', () => { | ||
| expect(() => point1.moveHorizontal('a')).toThrow('argument is not a number') | ||
| expect(() => point1.moveHorizontal(true)).toThrow('argument is not a number') | ||
| expect(() => point1.moveHorizontal(['x', 'y'])).toThrow('argument is not a number') | ||
| expect(() => point1.moveHorizontal()).toThrow('value is undefined') | ||
| expect(() => point1.moveHorizontal(value => (value))).toThrow('argument is not a number') | ||
| }) | ||
| }) | ||
| }) |
Uh oh!
There was an error while loading. Please reload this page.