{
"server": [
"src",
"node_modules/bootstrap/dist/css",
"node_modules/bootstrap/dist/js"
],
"port": 3000
}
{
"name": "tombsug",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jasmine-browser-runner serve",
"start": "lite-server"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"jasmine-browser-runner": "^2.1.0",
"jasmine-core": "^5.1.0",
"lite-server": "^2.6.1"
},
"dependencies": {
"bootstrap": "^5.3.1"
}
}
===== Alkalmazás =====
Document
Rhombus
Sugár
A rombuszba írható kör sugara
const doc = {
sideInput: document.querySelector('#side'),
alphaInput: document.querySelector('#alpha'),
radiusInput: document.querySelector('#radius'),
calcButton: document.querySelector('#calcButton')
}
const state = {
radius: null
}
window.addEventListener('load', () => {
init()
})
function init() {
handleEvent()
}
function handleEvent() {
if(doc.calcButton) {
doc.calcButton.addEventListener('click', () => {
startCalc()
})
}
}
function startCalc() {
try {
tryStartCalc()
}catch(err) {
console.log(err.message)
alert(err.message)
}
}
function tryStartCalc() {
let side = doc.sideInput.value;
let alpha = doc.alphaInput.value;
state.radius = calcRadius(side, alpha)
doc.radiusInput.value = state.radius
}
function calcRadius(side, alpha) {
if(!isValidInput(side)) {
throw new Error('Hiba! Az oldal érték hibás!')
}
if(!isValidInput(alpha)) {
throw new Error('Hiba! Az szög érték hibás!')
}
let rad = alpha * Math.PI / 180
return 1.0/2.0 * side * Math.sin(rad)
}
function isValidInput(input) {
inputStr = String(input)
if(!inputStr.match(/^[0-9]+$/)) {
return false
}
if(input <= 0) {
return false
}
return true
}
===== Teszt =====
Beállítások:
{
"srcDir": "src",
"srcFiles": [
"**/*.js"
],
"specDir": "test",
"specFiles": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
],
"env": {
"stopSpecOnExpectationFailure": false,
"stopOnSpecFailure": false,
"random": false
},
"browser": {
"name": "firefox"
}
}
Az eredetihez képest, két tulajdonságot változtattunk meg:
* specDir
* env:random
A teszt:
describe('A számítás tesztelése', () => {
it('A 30, 35 bementre 8.60364654526569', () => {
const expected = 8.60364654526569
const actual = calcRadius(30, 35)
expect(actual).toEqual(expected)
})
it('A 130, 140 bementre 41.78119462962506', () => {
const expected = 41.78119462962506
const actual = calcRadius(130, 140)
expect(actual).toEqual(expected)
})
it('A 0, 35 bementre Hiba', () => {
expect(() => {
calcRadius(0, 35)
}).toThrow();
})
it('A 30, 0 bementre Hiba', () => {
expect(() => {
calcRadius(30, 0)
}).toThrow();
})
})
describe('A bemenet ellenőrző függvény', () => {
it('A 30 bementre true', () => {
const expected = true
const actual = isValidInput(30)
expect(actual).toBe(expected)
})
it('Az 1 bementre true', () => {
const expected = true
const actual = isValidInput(1)
expect(actual).toBe(expected)
})
it('A abc bementre false', () => {
const expected = false
const actual = isValidInput('abc')
expect(actual).toBe(expected)
})
it('A 0 bementre false', () => {
const expected = false
const actual = isValidInput(0)
expect(actual).toBe(expected)
})
it('A -4 bementre false', () => {
const expected = false
const actual = isValidInput(-4)
expect(actual).toBe(expected)
})
it('A 35a bementre false', () => {
const expected = false
const actual = isValidInput('35a')
expect(actual).toBe(expected)
})
})