Commit 17cb3bed authored by Petr D. Svoboda's avatar Petr D. Svoboda

Add testing

parent cc11b302
......@@ -23,10 +23,12 @@
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"test:coverage": "react-scripts test --coverage",
"eject": "react-scripts eject"
},
"devDependencies": {
"@types/file-saver": "^2.0.0",
"@types/jest": "^24.0.11",
"@types/node": "^11.13.0",
"@types/ramda": "^0.26.6",
"@types/react": "^16.8.13",
......
......@@ -71,7 +71,7 @@ export const max: Operator = (a, b) => ({
export const distance: MonoNumOperator = a =>
Math.sqrt(Math.pow(a.x, 2) + Math.pow(a.y, 2))
export const round: MonoOperator = (a, prec: number = 2) => ({
export const round = (a: Position, prec: number = 2): Position => ({
x: roundNum(a.x, prec),
y: roundNum(a.y, prec)
})
......@@ -86,7 +86,7 @@ export const random: MonoOperator = bounds =>
export const toBounds: Operator = (bounds, a) =>
min(
substract(bounds, multiplyByX(bounds, 0.1)),
max(add(multiplyByX(bounds, 0.1), { x: 0, y: 0 }), a)
max(add(multiplyByX(bounds, 0.1), toPosition(0, 0)), a)
)
export const zoom = (
......
import Position, * as Op from '../Position'
// Mock random
// https://stackoverflow.com/a/43532567/7152861
const mockMath = Object.create(global.Math)
mockMath.random = () => 0.5
global.Math = mockMath
const a = Op.toPosition(1, 2)
const b = Op.toPosition(2, 3)
const c = Op.toPosition(1, -2)
const d = Op.toPosition(-3, 4)
const e = Op.toPosition(1.43231, 1.89984)
const bounds = Op.toPosition(800, 400)
test('(1, 2) + (2, 3) equals (3, 5)', () => {
expect(Op.add(a, b)).toEqual(Op.toPosition(3, 5))
})
test('(1, 2) + 3 equals (4, 5)', () => {
expect(Op.addX(a, 3)).toEqual(Op.toPosition(4, 5))
})
test('(1, 2) - (2, 3) equals (-1, -1)', () => {
expect(Op.substract(a, b)).toEqual(Op.toPosition(-1, -1))
})
test('(1, 2) * (2, 3) equals (2, 6)', () => {
expect(Op.multiply(a, b)).toEqual(Op.toPosition(2, 6))
})
test('(1, 2) * 3 equals (3, 6)', () => {
expect(Op.multiplyByX(a, 3)).toEqual(Op.toPosition(3, 6))
})
test('(1, 2) / (2, 3) equals (1/2, 2/3)', () => {
expect(Op.divide(a, b)).toEqual(Op.toPosition(1 / 2, 2 / 3))
})
test('(1, 2) / 3 equals (1/3, 2/3)', () => {
expect(Op.divideByX(a, 3)).toEqual(Op.toPosition(1 / 3, 2 / 3))
})
test('(1, 2) / (0, 0) equals (0,0)', () => {
expect(Op.divide(a, Op.toPosition(0, 0))).toEqual(Op.toPosition(0, 0))
})
test('(1, 2) / 0 equals (0, 0)', () => {
expect(Op.divideByX(a, 0)).toEqual(Op.toPosition(0, 0))
})
test('(1, 2)^2 equals (1, 4)', () => {
expect(Op.toPower(a, 2)).toEqual(Op.toPosition(1, 4))
})
test('(1, 2)^1/2 equals (1, 4)', () => {
expect(Op.squareRoot(a)).toEqual(Op.toPosition(1, Math.sqrt(2)))
})
test('|(1, -2)| equals (1, 2)', () => {
expect(Op.absolute(c)).toEqual(a)
})
test('min of (1, 2) and (-3, 4) equals (-3, 2)', () => {
expect(Op.min(a, d)).toEqual(Op.toPosition(-3, 2))
})
test('min of (-3, 4) and 2 equals (2, 2)', () => {
expect(Op.minX(d, 2)).toEqual(Op.toPosition(-3, 2))
})
test('max of (1, 2) and (-3, 4) equals (-3, 2)', () => {
expect(Op.max(a, d)).toEqual(Op.toPosition(1, 4))
})
test('max of (-3, 4) and 2 equals (2, 2)', () => {
expect(Op.maxX(d, 2)).toEqual(Op.toPosition(2, 4))
})
test('distance of (-3, 4) equals 5', () => {
expect(Op.distance(d)).toEqual(5)
})
test('round to 3 dec of (1.43231, 1.89984) equals (1.43, 1.9)', () => {
expect(Op.round(e, 3)).toEqual(Op.toPosition(1.432, 1.9))
})
test('creates Position object', () => {
expect(Op.toPosition(1, 2)).toEqual(<Position>{ x: 1, y: 2 })
})
test('random(0.5) within bounds (800, 400) centering equals (600, 300)', () => {
expect(Op.random(bounds)).toEqual(Op.toPosition(600, 300))
})
test('contain (234, -234) within bounds (800, 400) equals (234, 40)', () => {
expect(Op.toBounds(bounds, Op.toPosition(234, -234))).toEqual(
Op.toPosition(234, 40)
)
})
test('zoom (100, -200) with offset (-10, 20) and scale 0.75 by factor -0.25 equals (-35, 70)', () => {
expect(
Op.zoom(Op.toPosition(100, -200), Op.toPosition(-10, 20), 0.75, 0.25)
).toEqual(Op.toPosition(-35, 70))
})
test('normalize (100, -200) with offset (-10, 20) and scale 0.5 equals (220, -440)', () => {
expect(
Op.normalize(Op.toPosition(100, -200), Op.toPosition(-10, 20), 0.5)
).toEqual(Op.toPosition(220, -440))
})
......@@ -13,7 +13,8 @@
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve"
"jsx": "preserve",
"types": ["jest"]
},
"include": ["src"]
}
......@@ -979,6 +979,18 @@
"@types/react" "*"
hoist-non-react-statics "^3.3.0"
"@types/jest-diff@*":
version "20.0.1"
resolved "https://registry.yarnpkg.com/@types/jest-diff/-/jest-diff-20.0.1.tgz#35cc15b9c4f30a18ef21852e255fdb02f6d59b89"
integrity sha512-yALhelO3i0hqZwhjtcr6dYyaLoCHbAMshwtj6cGxTvHZAKXHsYGdff6E8EPw3xLKY0ELUTQ69Q1rQiJENnccMA==
"@types/jest@^24.0.11":
version "24.0.11"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-24.0.11.tgz#1f099bea332c228ea6505a88159bfa86a5858340"
integrity sha512-2kLuPC5FDnWIDvaJBzsGTBQaBbnDweznicvK7UGYzlIJP4RJR2a4A/ByLUXEyEgag6jz8eHdlWExGDtH3EYUXQ==
dependencies:
"@types/jest-diff" "*"
"@types/node@*", "@types/node@^11.13.0":
version "11.13.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-11.13.0.tgz#b0df8d6ef9b5001b2be3a94d909ce3c29a80f9e1"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment